Make more builtin functions be named

This commit is contained in:
Noah committed 2020-09-21 10:32:25 -05:00
1 parent 6c3e4819ed
commit ca5f2da9e9
11 files changed
+413 -415

No files matched your search

+2 -2
View File
@@ -186,7 +186,7 @@ impl Reader {
}
}
fn csv_reader(fp: PyObjectRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
fn _csv_reader(fp: PyObjectRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
if let Ok(iterable) = PyIterable::<PyObjectRef>::try_from_object(vm, fp) {
build_reader(iterable, args, vm)
} else {
@@ -206,7 +206,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
);
py_module!(vm, "_csv", {
"reader" => ctx.new_function(csv_reader),
"reader" => named_function!(ctx, _csv, reader),
"Reader" => reader_type,
"Error" => error,
// constants
+263 -267
View File
@@ -1,290 +1,286 @@
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::function::{OptionalArg, PyFuncArgs};
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{BorrowValue, PyClassImpl, PyObjectRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
use std::fmt;
pub(crate) use hashlib::make_module;
use blake2::{Blake2b, Blake2s};
use digest::DynDigest;
use md5::Md5;
use sha1::Sha1;
use sha2::{Sha224, Sha256, Sha384, Sha512};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; // TODO: , Shake128, Shake256};
#[pymodule]
mod hashlib {
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::function::{OptionalArg, PyFuncArgs};
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{BorrowValue, PyClassImpl, PyResult, PyValue};
use crate::vm::VirtualMachine;
use std::fmt;
#[pyclass(module = "hashlib", name = "hasher")]
struct PyHasher {
name: String,
buffer: PyRwLock<HashWrapper>,
}
use blake2::{Blake2b, Blake2s};
use digest::DynDigest;
use md5::Md5;
use sha1::Sha1;
use sha2::{Sha224, Sha256, Sha384, Sha512};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; // TODO: , Shake128, Shake256};
impl fmt::Debug for PyHasher {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hasher {}", self.name)
#[pyattr]
#[pyclass(module = "hashlib", name = "hasher")]
struct PyHasher {
name: String,
buffer: PyRwLock<HashWrapper>,
}
}
impl PyValue for PyHasher {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("hashlib", "hasher")
}
}
#[pyimpl]
impl PyHasher {
fn new(name: &str, d: HashWrapper) -> Self {
PyHasher {
name: name.to_owned(),
buffer: PyRwLock::new(d),
impl fmt::Debug for PyHasher {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hasher {}", self.name)
}
}
fn borrow_value(&self) -> PyRwLockReadGuard<'_, HashWrapper> {
self.buffer.read()
impl PyValue for PyHasher {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("hashlib", "hasher")
}
}
fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, HashWrapper> {
self.buffer.write()
}
#[pyslot]
fn tp_new(_cls: PyClassRef, _args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
Ok(PyHasher::new("md5", HashWrapper::md5())
.into_ref(vm)
.into_object())
}
#[pyproperty(name = "name")]
fn name(&self) -> String {
self.name.clone()
}
#[pyproperty(name = "digest_size")]
fn digest_size(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_int(self.borrow_value().digest_size()))
}
#[pymethod(name = "update")]
fn update(&self, data: PyBytesRef) {
self.borrow_value_mut().input(data.borrow_value());
}
#[pymethod(name = "digest")]
fn digest(&self) -> PyBytes {
self.get_digest().into()
}
#[pymethod(name = "hexdigest")]
fn hexdigest(&self) -> String {
let result = self.get_digest();
hex::encode(result)
}
fn get_digest(&self) -> Vec<u8> {
self.borrow_value().get_digest()
}
}
fn hashlib_new(
name: PyStringRef,
data: OptionalArg<PyBytesRef>,
vm: &VirtualMachine,
) -> PyResult<PyHasher> {
match name.borrow_value() {
"md5" => md5(data),
"sha1" => sha1(data),
"sha224" => sha224(data),
"sha256" => sha256(data),
"sha384" => sha384(data),
"sha512" => sha512(data),
"sha3_224" => sha3_224(data),
"sha3_256" => sha3_256(data),
"sha3_384" => sha3_384(data),
"sha3_512" => sha3_512(data),
// TODO: "shake128" => shake128(data, ),
// TODO: "shake256" => shake256(data, ),
"blake2b" => blake2b(data),
"blake2s" => blake2s(data),
other => Err(vm.new_value_error(format!("Unknown hashing algorithm: {}", other))),
}
}
fn init(hasher: PyHasher, data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
if let OptionalArg::Present(data) = data {
hasher.update(data);
}
Ok(hasher)
}
fn md5(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("md5", HashWrapper::md5()), data)
}
fn sha1(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha1", HashWrapper::sha1()), data)
}
fn sha224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha224", HashWrapper::sha224()), data)
}
fn sha256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha256", HashWrapper::sha256()), data)
}
fn sha384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha384", HashWrapper::sha384()), data)
}
fn sha512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha512", HashWrapper::sha512()), data)
}
fn sha3_224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_224", HashWrapper::sha3_224()), data)
}
fn sha3_256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_256", HashWrapper::sha3_256()), data)
}
fn sha3_384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_384", HashWrapper::sha3_384()), data)
}
fn sha3_512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_512", HashWrapper::sha3_512()), data)
}
fn shake128(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
Err(vm.new_not_implemented_error("shake256".to_owned()))
}
fn shake256(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
Err(vm.new_not_implemented_error("shake256".to_owned()))
}
fn blake2b(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
// TODO: handle parameters
init(PyHasher::new("blake2b", HashWrapper::blake2b()), data)
}
fn blake2s(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
// TODO: handle parameters
init(PyHasher::new("blake2s", HashWrapper::blake2s()), data)
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
let hasher_type = PyHasher::make_class(ctx);
py_module!(vm, "hashlib", {
"new" => ctx.new_function(hashlib_new),
"md5" => ctx.new_function(md5),
"sha1" => ctx.new_function(sha1),
"sha224" => ctx.new_function(sha224),
"sha256" => ctx.new_function(sha256),
"sha384" => ctx.new_function(sha384),
"sha512" => ctx.new_function(sha512),
"sha3_224" => ctx.new_function(sha3_224),
"sha3_256" => ctx.new_function(sha3_256),
"sha3_384" => ctx.new_function(sha3_384),
"sha3_512" => ctx.new_function(sha3_512),
"shake128" => ctx.new_function(shake128),
"shake256" => ctx.new_function(shake256),
"blake2b" => ctx.new_function(blake2b),
"blake2s" => ctx.new_function(blake2s),
"hasher" => hasher_type,
})
}
trait ThreadSafeDynDigest: DynDigest + Sync + Send {}
impl<T> ThreadSafeDynDigest for T where T: DynDigest + Sync + Send {}
/// Generic wrapper patching around the hashing libraries.
struct HashWrapper {
inner: Box<dyn ThreadSafeDynDigest>,
}
impl HashWrapper {
fn new<D: 'static>(d: D) -> Self
where
D: ThreadSafeDynDigest,
{
HashWrapper { inner: Box::new(d) }
}
fn md5() -> Self {
Self::new(Md5::default())
}
fn sha1() -> Self {
Self::new(Sha1::default())
}
fn sha224() -> Self {
Self::new(Sha224::default())
}
fn sha256() -> Self {
Self::new(Sha256::default())
}
fn sha384() -> Self {
Self::new(Sha384::default())
}
fn sha512() -> Self {
Self::new(Sha512::default())
}
fn sha3_224() -> Self {
Self::new(Sha3_224::default())
}
fn sha3_256() -> Self {
Self::new(Sha3_256::default())
}
fn sha3_384() -> Self {
Self::new(Sha3_384::default())
}
fn sha3_512() -> Self {
Self::new(Sha3_512::default())
}
/* TODO:
fn shake128() -> Self {
Self::new(Shake128::default())
#[pyimpl]
impl PyHasher {
fn new(name: &str, d: HashWrapper) -> Self {
PyHasher {
name: name.to_owned(),
buffer: PyRwLock::new(d),
}
}
fn shake256() -> Self {
Self::new(Shake256::default())
fn borrow_value(&self) -> PyRwLockReadGuard<'_, HashWrapper> {
self.buffer.read()
}
fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, HashWrapper> {
self.buffer.write()
}
#[pyslot]
fn tp_new(_cls: PyClassRef, _args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
Ok(PyHasher::new("md5", HashWrapper::md5())
.into_ref(vm)
.into_object())
}
#[pyproperty(name = "name")]
fn name(&self) -> String {
self.name.clone()
}
#[pyproperty(name = "digest_size")]
fn digest_size(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_int(self.borrow_value().digest_size()))
}
#[pymethod(name = "update")]
fn update(&self, data: PyBytesRef) {
self.borrow_value_mut().input(data.borrow_value());
}
#[pymethod(name = "digest")]
fn digest(&self) -> PyBytes {
self.get_digest().into()
}
#[pymethod(name = "hexdigest")]
fn hexdigest(&self) -> String {
let result = self.get_digest();
hex::encode(result)
}
fn get_digest(&self) -> Vec<u8> {
self.borrow_value().get_digest()
}
*/
fn blake2b() -> Self {
Self::new(Blake2b::default())
}
fn blake2s() -> Self {
Self::new(Blake2s::default())
#[pyfunction(name = "new")]
fn hashlib_new(
name: PyStringRef,
data: OptionalArg<PyBytesRef>,
vm: &VirtualMachine,
) -> PyResult<PyHasher> {
match name.borrow_value() {
"md5" => md5(data),
"sha1" => sha1(data),
"sha224" => sha224(data),
"sha256" => sha256(data),
"sha384" => sha384(data),
"sha512" => sha512(data),
"sha3_224" => sha3_224(data),
"sha3_256" => sha3_256(data),
"sha3_384" => sha3_384(data),
"sha3_512" => sha3_512(data),
// TODO: "shake128" => shake128(data, ),
// TODO: "shake256" => shake256(data, ),
"blake2b" => blake2b(data),
"blake2s" => blake2s(data),
other => Err(vm.new_value_error(format!("Unknown hashing algorithm: {}", other))),
}
}
fn input(&mut self, data: &[u8]) {
self.inner.input(data);
fn init(hasher: PyHasher, data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
if let OptionalArg::Present(data) = data {
hasher.update(data);
}
Ok(hasher)
}
fn digest_size(&self) -> usize {
self.inner.output_size()
#[pyfunction]
fn md5(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("md5", HashWrapper::md5()), data)
}
fn get_digest(&self) -> Vec<u8> {
let cloned = self.inner.box_clone();
cloned.result().to_vec()
#[pyfunction]
fn sha1(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha1", HashWrapper::sha1()), data)
}
#[pyfunction]
fn sha224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha224", HashWrapper::sha224()), data)
}
#[pyfunction]
fn sha256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha256", HashWrapper::sha256()), data)
}
#[pyfunction]
fn sha384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha384", HashWrapper::sha384()), data)
}
#[pyfunction]
fn sha512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha512", HashWrapper::sha512()), data)
}
#[pyfunction]
fn sha3_224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_224", HashWrapper::sha3_224()), data)
}
#[pyfunction]
fn sha3_256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_256", HashWrapper::sha3_256()), data)
}
#[pyfunction]
fn sha3_384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_384", HashWrapper::sha3_384()), data)
}
#[pyfunction]
fn sha3_512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
init(PyHasher::new("sha3_512", HashWrapper::sha3_512()), data)
}
#[pyfunction]
fn shake128(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
Err(vm.new_not_implemented_error("shake256".to_owned()))
}
#[pyfunction]
fn shake256(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
Err(vm.new_not_implemented_error("shake256".to_owned()))
}
#[pyfunction]
fn blake2b(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
// TODO: handle parameters
init(PyHasher::new("blake2b", HashWrapper::blake2b()), data)
}
#[pyfunction]
fn blake2s(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
// TODO: handle parameters
init(PyHasher::new("blake2s", HashWrapper::blake2s()), data)
}
trait ThreadSafeDynDigest: DynDigest + Sync + Send {}
impl<T> ThreadSafeDynDigest for T where T: DynDigest + Sync + Send {}
/// Generic wrapper patching around the hashing libraries.
struct HashWrapper {
inner: Box<dyn ThreadSafeDynDigest>,
}
impl HashWrapper {
fn new<D: 'static>(d: D) -> Self
where
D: ThreadSafeDynDigest,
{
HashWrapper { inner: Box::new(d) }
}
fn md5() -> Self {
Self::new(Md5::default())
}
fn sha1() -> Self {
Self::new(Sha1::default())
}
fn sha224() -> Self {
Self::new(Sha224::default())
}
fn sha256() -> Self {
Self::new(Sha256::default())
}
fn sha384() -> Self {
Self::new(Sha384::default())
}
fn sha512() -> Self {
Self::new(Sha512::default())
}
fn sha3_224() -> Self {
Self::new(Sha3_224::default())
}
fn sha3_256() -> Self {
Self::new(Sha3_256::default())
}
fn sha3_384() -> Self {
Self::new(Sha3_384::default())
}
fn sha3_512() -> Self {
Self::new(Sha3_512::default())
}
/* TODO:
fn shake128() -> Self {
Self::new(Shake128::default())
}
fn shake256() -> Self {
Self::new(Shake256::default())
}
*/
fn blake2b() -> Self {
Self::new(Blake2b::default())
}
fn blake2s() -> Self {
Self::new(Blake2s::default())
}
fn input(&mut self, data: &[u8]) {
self.inner.input(data);
}
fn digest_size(&self) -> usize {
self.inner.output_size()
}
fn get_digest(&self) -> Vec<u8> {
let cloned = self.inner.box_clone();
cloned.result().to_vec()
}
}
}
+30 -30
View File
@@ -15,11 +15,11 @@ mod lock {
pub(super) static IMP_LOCK: RawRMutex = RawRMutex::INIT;
pub(super) fn imp_acquire_lock(_vm: &VirtualMachine) {
pub(super) fn _imp_acquire_lock(_vm: &VirtualMachine) {
IMP_LOCK.lock()
}
pub(super) fn imp_release_lock(vm: &VirtualMachine) -> PyResult<()> {
pub(super) fn _imp_release_lock(vm: &VirtualMachine) -> PyResult<()> {
if !IMP_LOCK.is_locked() {
Err(vm.new_runtime_error("Global import lock not held".to_owned()))
} else {
@@ -28,7 +28,7 @@ mod lock {
}
}
pub(super) fn imp_lock_held(_vm: &VirtualMachine) -> bool {
pub(super) fn _imp_lock_held(_vm: &VirtualMachine) -> bool {
IMP_LOCK.is_locked()
}
}
@@ -36,28 +36,28 @@ mod lock {
#[cfg(not(feature = "threading"))]
mod lock {
use crate::vm::VirtualMachine;
pub(super) fn imp_acquire_lock(_vm: &VirtualMachine) {}
pub(super) fn imp_release_lock(_vm: &VirtualMachine) {}
pub(super) fn imp_lock_held(_vm: &VirtualMachine) -> bool {
pub(super) fn _imp_acquire_lock(_vm: &VirtualMachine) {}
pub(super) fn _imp_release_lock(_vm: &VirtualMachine) {}
pub(super) fn _imp_lock_held(_vm: &VirtualMachine) -> bool {
false
}
}
use lock::{imp_acquire_lock, imp_lock_held, imp_release_lock};
use lock::{_imp_acquire_lock, _imp_lock_held, _imp_release_lock};
fn imp_extension_suffixes(vm: &VirtualMachine) -> PyResult {
fn _imp_extension_suffixes(vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_list(vec![]))
}
fn imp_is_builtin(name: PyStringRef, vm: &VirtualMachine) -> bool {
fn _imp_is_builtin(name: PyStringRef, vm: &VirtualMachine) -> bool {
vm.state.stdlib_inits.contains_key(name.borrow_value())
}
fn imp_is_frozen(name: PyStringRef, vm: &VirtualMachine) -> bool {
fn _imp_is_frozen(name: PyStringRef, vm: &VirtualMachine) -> bool {
vm.state.frozen.contains_key(name.borrow_value())
}
fn imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn _imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
let spec = vm.get_attribute(spec, "name")?;
let name = objstr::borrow_value(&spec);
@@ -71,12 +71,12 @@ fn imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
}
}
fn imp_exec_builtin(_mod: PyModuleRef) -> i32 {
fn _imp_exec_builtin(_mod: PyModuleRef) -> i32 {
// TOOD: Should we do something here?
0
}
fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyCode> {
fn _imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyCode> {
let name = name.borrow_value();
vm.state
.frozen
@@ -89,11 +89,11 @@ fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyC
.ok_or_else(|| vm.new_import_error(format!("No such frozen object named {}", name), name))
}
fn imp_init_frozen(name: PyStringRef, vm: &VirtualMachine) -> PyResult {
fn _imp_init_frozen(name: PyStringRef, vm: &VirtualMachine) -> PyResult {
import::import_frozen(vm, name.borrow_value())
}
fn imp_is_frozen_package(name: PyStringRef, vm: &VirtualMachine) -> PyResult<bool> {
fn _imp_is_frozen_package(name: PyStringRef, vm: &VirtualMachine) -> PyResult<bool> {
let name = name.borrow_value();
vm.state
.frozen
@@ -102,11 +102,11 @@ fn imp_is_frozen_package(name: PyStringRef, vm: &VirtualMachine) -> PyResult<boo
.ok_or_else(|| vm.new_import_error(format!("No such frozen object named {}", name), name))
}
fn imp_fix_co_filename(_code: PyObjectRef, _path: PyStringRef) {
fn _imp_fix_co_filename(_code: PyObjectRef, _path: PyStringRef) {
// TODO:
}
fn imp_source_hash(_key: u64, _source: PyBytesRef, vm: &VirtualMachine) -> PyResult {
fn _imp_source_hash(_key: u64, _source: PyBytesRef, vm: &VirtualMachine) -> PyResult {
// TODO:
Ok(vm.ctx.none())
}
@@ -114,18 +114,18 @@ fn imp_source_hash(_key: u64, _source: PyBytesRef, vm: &VirtualMachine) -> PyRes
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
py_module!(vm, "_imp", {
"extension_suffixes" => ctx.new_function(imp_extension_suffixes),
"acquire_lock" => ctx.new_function(imp_acquire_lock),
"release_lock" => ctx.new_function(imp_release_lock),
"lock_held" => ctx.new_function(imp_lock_held),
"is_builtin" => ctx.new_function(imp_is_builtin),
"is_frozen" => ctx.new_function(imp_is_frozen),
"create_builtin" => ctx.new_function(imp_create_builtin),
"exec_builtin" => ctx.new_function(imp_exec_builtin),
"get_frozen_object" => ctx.new_function(imp_get_frozen_object),
"init_frozen" => ctx.new_function(imp_init_frozen),
"is_frozen_package" => ctx.new_function(imp_is_frozen_package),
"_fix_co_filename" => ctx.new_function(imp_fix_co_filename),
"source_hash" => ctx.new_function(imp_source_hash)
"extension_suffixes" => named_function!(ctx, _imp, extension_suffixes),
"acquire_lock" => named_function!(ctx, _imp, acquire_lock),
"release_lock" => named_function!(ctx, _imp, release_lock),
"lock_held" => named_function!(ctx, _imp, lock_held),
"is_builtin" => named_function!(ctx, _imp, is_builtin),
"is_frozen" => named_function!(ctx, _imp, is_frozen),
"create_builtin" => named_function!(ctx, _imp, create_builtin),
"exec_builtin" => named_function!(ctx, _imp, exec_builtin),
"get_frozen_object" => named_function!(ctx, _imp, get_frozen_object),
"init_frozen" => named_function!(ctx, _imp, init_frozen),
"is_frozen_package" => named_function!(ctx, _imp, is_frozen_package),
"_fix_co_filename" => named_function!(ctx, _imp, fix_co_filename),
"source_hash" => named_function!(ctx, _imp, source_hash),
})
}
+5 -5
View File
@@ -1139,7 +1139,7 @@ fn split_mode_string(mode_string: &str) -> Result<(String, String), String> {
Ok((mode, typ.to_string()))
}
fn io_open_wrapper(
fn _io_open(
file: PyObjectRef,
mode: OptionalArg<PyStringRef>,
opts: OpenArgs,
@@ -1152,9 +1152,9 @@ fn io_open_wrapper(
vm,
)
}
fn io_open_code(file: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn _io_open_code(file: PyObjectRef, vm: &VirtualMachine) -> PyResult {
// TODO: lifecycle hooks or something?
io_open(file, Some("rb"), Default::default(), vm)
io_open(file, Some("rb"), OpenArgs::default(), vm)
}
#[derive(FromArgs)]
@@ -1368,8 +1368,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
});
let module = py_module!(vm, "_io", {
"open" => ctx.new_function(io_open_wrapper),
"open_code" => ctx.new_function(io_open_code),
"open" => named_function!(ctx, _io, open),
"open_code" => named_function!(ctx, _io, open_code),
"_IOBase" => io_base,
"_RawIOBase" => raw_io_base.clone(),
"_BufferedIOBase" => buffered_io_base,
+46 -46
View File
@@ -404,69 +404,69 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
py_module!(vm, "math", {
// Number theory functions:
"fabs" => ctx.new_function(math_fabs),
"isfinite" => ctx.new_function(math_isfinite),
"isinf" => ctx.new_function(math_isinf),
"isnan" => ctx.new_function(math_isnan),
"isclose" => ctx.new_function(math_isclose),
"copysign" => ctx.new_function(math_copysign),
"fabs" => named_function!(ctx, math, fabs),
"isfinite" => named_function!(ctx, math, isfinite),
"isinf" => named_function!(ctx, math, isinf),
"isnan" => named_function!(ctx, math, isnan),
"isclose" => named_function!(ctx, math, isclose),
"copysign" => named_function!(ctx, math, copysign),
// Power and logarithmic functions:
"exp" => ctx.new_function(math_exp),
"expm1" => ctx.new_function(math_expm1),
"log" => ctx.new_function(math_log),
"log1p" => ctx.new_function(math_log1p),
"log2" => ctx.new_function(math_log2),
"log10" => ctx.new_function(math_log10),
"pow" => ctx.new_function(math_pow),
"sqrt" => ctx.new_function(math_sqrt),
"exp" => named_function!(ctx, math, exp),
"expm1" => named_function!(ctx, math, expm1),
"log" => named_function!(ctx, math, log),
"log1p" => named_function!(ctx, math, log1p),
"log2" => named_function!(ctx, math, log2),
"log10" => named_function!(ctx, math, log10),
"pow" => named_function!(ctx, math, pow),
"sqrt" => named_function!(ctx, math, sqrt),
// Trigonometric functions:
"acos" => ctx.new_function(math_acos),
"asin" => ctx.new_function(math_asin),
"atan" => ctx.new_function(math_atan),
"atan2" => ctx.new_function(math_atan2),
"cos" => ctx.new_function(math_cos),
"hypot" => ctx.new_function(math_hypot),
"sin" => ctx.new_function(math_sin),
"tan" => ctx.new_function(math_tan),
"acos" => named_function!(ctx, math, acos),
"asin" => named_function!(ctx, math, asin),
"atan" => named_function!(ctx, math, atan),
"atan2" => named_function!(ctx, math, atan2),
"cos" => named_function!(ctx, math, cos),
"hypot" => named_function!(ctx, math, hypot),
"sin" => named_function!(ctx, math, sin),
"tan" => named_function!(ctx, math, tan),
"degrees" => ctx.new_function(math_degrees),
"radians" => ctx.new_function(math_radians),
"degrees" => named_function!(ctx, math, degrees),
"radians" => named_function!(ctx, math, radians),
// Hyperbolic functions:
"acosh" => ctx.new_function(math_acosh),
"asinh" => ctx.new_function(math_asinh),
"atanh" => ctx.new_function(math_atanh),
"cosh" => ctx.new_function(math_cosh),
"sinh" => ctx.new_function(math_sinh),
"tanh" => ctx.new_function(math_tanh),
"acosh" => named_function!(ctx, math, acosh),
"asinh" => named_function!(ctx, math, asinh),
"atanh" => named_function!(ctx, math, atanh),
"cosh" => named_function!(ctx, math, cosh),
"sinh" => named_function!(ctx, math, sinh),
"tanh" => named_function!(ctx, math, tanh),
// Special functions:
"erf" => ctx.new_function(math_erf),
"erfc" => ctx.new_function(math_erfc),
"gamma" => ctx.new_function(math_gamma),
"lgamma" => ctx.new_function(math_lgamma),
"erf" => named_function!(ctx, math, erf),
"erfc" => named_function!(ctx, math, erfc),
"gamma" => named_function!(ctx, math, gamma),
"lgamma" => named_function!(ctx, math, lgamma),
"frexp" => ctx.new_function(math_frexp),
"ldexp" => ctx.new_function(math_ldexp),
"modf" => ctx.new_function(math_modf),
"fmod" => ctx.new_function(math_fmod),
"remainder" => ctx.new_function(math_remainder),
"frexp" => named_function!(ctx, math, frexp),
"ldexp" => named_function!(ctx, math, ldexp),
"modf" => named_function!(ctx, math, modf),
"fmod" => named_function!(ctx, math, fmod),
"remainder" => named_function!(ctx, math, remainder),
// Rounding functions:
"trunc" => ctx.new_function(math_trunc),
"ceil" => ctx.new_function(math_ceil),
"floor" => ctx.new_function(math_floor),
"trunc" => named_function!(ctx, math, trunc),
"ceil" => named_function!(ctx, math, ceil),
"floor" => named_function!(ctx, math, floor),
// Gcd function
"gcd" => ctx.new_function(math_gcd),
"lcm" => ctx.new_function(math_lcm),
"gcd" => named_function!(ctx, math, gcd),
"lcm" => named_function!(ctx, math, lcm),
// Factorial function
"factorial" => ctx.new_function(math_factorial),
"factorial" => named_function!(ctx, math, factorial),
"nextafter" => ctx.new_function(math_nextafter),
"nextafter" => named_function!(ctx, math, nextafter),
// Constants:
"pi" => ctx.new_float(std::f64::consts::PI), // 3.14159...
+9 -9
View File
@@ -86,15 +86,15 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
py_module!(vm, "msvcrt", {
"getch" => ctx.new_function(msvcrt_getch),
"getwch" => ctx.new_function(msvcrt_getwch),
"getche" => ctx.new_function(msvcrt_getche),
"getwche" => ctx.new_function(msvcrt_getwche),
"putch" => ctx.new_function(msvcrt_putch),
"putwch" => ctx.new_function(msvcrt_putwch),
"setmode" => ctx.new_function(msvcrt_setmode),
"open_osfhandle" => ctx.new_function(msvcrt_open_osfhandle),
"SetErrorMode" => ctx.new_function(msvcrt_seterrormode),
"getch" => named_function!(ctx, msvcrt, getch),
"getwch" => named_function!(ctx, msvcrt, getwch),
"getche" => named_function!(ctx, msvcrt, getche),
"getwche" => named_function!(ctx, msvcrt, getwche),
"putch" => named_function!(ctx, msvcrt, putch),
"putwch" => named_function!(ctx, msvcrt, putwch),
"setmode" => named_function!(ctx, msvcrt, setmode),
"open_osfhandle" => named_function!(ctx, msvcrt, open_osfhandle),
"SetErrorMode" => named_function!(ctx, msvcrt, seterrormode),
"SEM_FAILCRITICALERRORS" => ctx.new_int(SEM_FAILCRITICALERRORS),
"SEM_NOALIGNMENTFAULTEXCEPT" => ctx.new_int(SEM_NOALIGNMENTFAULTEXCEPT),
"SEM_NOGPFAULTERRORBOX" => ctx.new_int(SEM_NOGPFAULTERRORBOX),
+5 -4
View File
@@ -6,7 +6,7 @@ use crate::pyobject::{BorrowValue, Either, PyObjectRef, PyResult, TypeProtocol};
use crate::VirtualMachine;
use volatile::Volatile;
fn operator_length_hint(obj: PyObjectRef, default: OptionalArg, vm: &VirtualMachine) -> PyResult {
fn _operator_length_hint(obj: PyObjectRef, default: OptionalArg, vm: &VirtualMachine) -> PyResult {
let default = default.unwrap_or_else(|| vm.ctx.new_int(0));
if !objtype::isinstance(&default, &vm.ctx.types.int_type) {
return Err(vm.new_type_error(format!(
@@ -69,7 +69,7 @@ fn timing_safe_cmp(a: &[u8], b: &[u8]) -> bool {
result == 0
}
fn operator_compare_digest(
fn _operator_compare_digest(
a: Either<PyStringRef, PyBytesLike>,
b: Either<PyStringRef, PyBytesLike>,
vm: &VirtualMachine,
@@ -93,8 +93,9 @@ fn operator_compare_digest(
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
py_module!(vm, "_operator", {
"length_hint" => vm.ctx.new_function(operator_length_hint),
"_compare_digest" => vm.ctx.new_function(operator_compare_digest),
"length_hint" => named_function!(ctx, _operator, length_hint),
"_compare_digest" => named_function!(ctx, _operator, compare_digest),
})
}
+12 -12
View File
@@ -201,7 +201,7 @@ impl PyRLock {
}
}
fn thread_get_ident() -> u64 {
fn _thread_get_ident() -> u64 {
thread_to_id(&thread::current())
}
@@ -211,11 +211,11 @@ fn thread_to_id(t: &thread::Thread) -> u64 {
unsafe { std::mem::transmute(t.id()) }
}
fn thread_allocate_lock() -> PyLock {
fn _thread_allocate_lock() -> PyLock {
PyLock { mu: RawMutex::INIT }
}
fn thread_start_new_thread(
fn _thread_start_new_thread(
func: PyCallable,
args: PyTupleRef,
kwargs: OptionalArg<PyDictRef>,
@@ -268,19 +268,19 @@ fn run_thread(func: PyCallable, args: PyFuncArgs, vm: &VirtualMachine) {
thread_local!(static SENTINELS: RefCell<Vec<PyLockRef>> = RefCell::default());
fn thread_set_sentinel(vm: &VirtualMachine) -> PyLockRef {
fn _thread_set_sentinel(vm: &VirtualMachine) -> PyLockRef {
let lock = PyLock { mu: RawMutex::INIT }.into_ref(vm);
SENTINELS.with(|sents| sents.borrow_mut().push(lock.clone()));
lock
}
fn thread_stack_size(size: OptionalArg<usize>, vm: &VirtualMachine) -> usize {
fn _thread_stack_size(size: OptionalArg<usize>, vm: &VirtualMachine) -> usize {
let size = size.unwrap_or(0);
// TODO: do validation on this to make sure it's not too small
vm.state.stacksize.swap(size)
}
fn thread_count(vm: &VirtualMachine) -> usize {
fn _thread_count(vm: &VirtualMachine) -> usize {
vm.state.thread_count.load()
}
@@ -363,12 +363,12 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"RLock" => PyRLock::make_class(ctx),
"LockType" => PyLock::make_class(ctx),
"_local" => PyLocal::make_class(ctx),
"get_ident" => ctx.new_function(thread_get_ident),
"allocate_lock" => ctx.new_function(thread_allocate_lock),
"start_new_thread" => ctx.new_function(thread_start_new_thread),
"_set_sentinel" => ctx.new_function(thread_set_sentinel),
"stack_size" => ctx.new_function(thread_stack_size),
"_count" => ctx.new_function(thread_count),
"get_ident" => named_function!(ctx, _thread, get_ident),
"allocate_lock" => named_function!(ctx, _thread, allocate_lock),
"start_new_thread" => named_function!(ctx, _thread, start_new_thread),
"_set_sentinel" => named_function!(ctx, _thread, set_sentinel),
"stack_size" => named_function!(ctx, _thread, stack_size),
"_count" => named_function!(ctx, _thread, count),
"error" => ctx.exceptions.runtime_error.clone(),
"TIMEOUT_MAX" => ctx.new_float(TIMEOUT_MAX),
})
+11 -11
View File
@@ -266,17 +266,17 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let struct_time_type = PyStructTime::make_class(ctx);
py_module!(vm, "time", {
"asctime" => ctx.new_function(time_asctime),
"ctime" => ctx.new_function(time_ctime),
"gmtime" => ctx.new_function(time_gmtime),
"mktime" => ctx.new_function(time_mktime),
"localtime" => ctx.new_function(time_localtime),
"monotonic" => ctx.new_function(time_monotonic),
"strftime" => ctx.new_function(time_strftime),
"strptime" => ctx.new_function(time_strptime),
"sleep" => ctx.new_function(time_sleep),
"asctime" => named_function!(ctx, time, asctime),
"ctime" => named_function!(ctx, time, ctime),
"gmtime" => named_function!(ctx, time, gmtime),
"mktime" => named_function!(ctx, time, mktime),
"localtime" => named_function!(ctx, time, localtime),
"monotonic" => named_function!(ctx, time, monotonic),
"strftime" => named_function!(ctx, time, strftime),
"strptime" => named_function!(ctx, time, strptime),
"sleep" => named_function!(ctx, time, sleep),
"struct_time" => struct_time_type,
"time" => ctx.new_function(time_time),
"perf_counter" => ctx.new_function(time_time), // TODO: fix
"time" => named_function!(ctx, time, time),
"perf_counter" => named_function!(ctx, time, time), // TODO: fix
})
}
+7 -7
View File
@@ -252,13 +252,13 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let hkey_type = PyHKEY::make_class(ctx);
let module = py_module!(vm, "winreg", {
"HKEYType" => hkey_type,
"OpenKey" => ctx.new_function(winreg_OpenKey),
"OpenKeyEx" => ctx.new_function(winreg_OpenKey),
"QueryValue" => ctx.new_function(winreg_QueryValue),
"QueryValueEx" => ctx.new_function(winreg_QueryValueEx),
"EnumKey" => ctx.new_function(winreg_EnumKey),
"EnumValue" => ctx.new_function(winreg_EnumValue),
"CloseKey" => ctx.new_function(winreg_CloseKey),
"OpenKey" => named_function!(ctx, winreg, OpenKey),
"OpenKeyEx" => named_function!(ctx, winreg, OpenKey),
"QueryValue" => named_function!(ctx, winreg, QueryValue),
"QueryValueEx" => named_function!(ctx, winreg, QueryValueEx),
"EnumKey" => named_function!(ctx, winreg, EnumKey),
"EnumValue" => named_function!(ctx, winreg, EnumValue),
"CloseKey" => named_function!(ctx, winreg, CloseKey),
});
macro_rules! add_constants {
+23 -22
View File
@@ -58,7 +58,8 @@ fn _base_executable(ctx: &PyContext) -> PyObjectRef {
}
}
fn getframe(offset: OptionalArg<usize>, vm: &VirtualMachine) -> PyResult<FrameRef> {
#[allow(non_snake_case)] // it's the function sys._getframe -> sys__getframe
fn sys__getframe(offset: OptionalArg<usize>, vm: &VirtualMachine) -> PyResult<FrameRef> {
let offset = offset.into_option().unwrap_or(0);
if offset > vm.frames.borrow().len() - 1 {
return Err(vm.new_value_error("call stack is not deep enough".to_owned()));
@@ -650,24 +651,24 @@ settrace() -- set the global debug tracing function
"_base_executable" => _base_executable(ctx),
"executable" => executable(ctx),
"flags" => flags,
"getrefcount" => ctx.new_function(sys_getrefcount),
"getrecursionlimit" => ctx.new_function(sys_getrecursionlimit),
"getsizeof" => ctx.new_function(sys_getsizeof),
"getrefcount" => named_function!(ctx, sys, getrefcount),
"getrecursionlimit" => named_function!(ctx, sys, getrecursionlimit),
"getsizeof" => named_function!(ctx, sys, getsizeof),
"implementation" => implementation,
"getfilesystemencoding" => ctx.new_function(sys_getfilesystemencoding),
"getfilesystemencodeerrors" => ctx.new_function(sys_getfilesystemencodeerrors),
"getdefaultencoding" => ctx.new_function(sys_getdefaultencoding),
"getprofile" => ctx.new_function(sys_getprofile),
"gettrace" => ctx.new_function(sys_gettrace),
"getfilesystemencoding" => named_function!(ctx, sys, getfilesystemencoding),
"getfilesystemencodeerrors" => named_function!(ctx, sys, getfilesystemencodeerrors),
"getdefaultencoding" => named_function!(ctx, sys, getdefaultencoding),
"getprofile" => named_function!(ctx, sys, getprofile),
"gettrace" => named_function!(ctx, sys, gettrace),
"hash_info" => hash_info,
"intern" => ctx.new_function(sys_intern),
"intern" => named_function!(ctx, sys, intern),
"maxunicode" => ctx.new_int(std::char::MAX as u32),
"maxsize" => ctx.new_int(std::isize::MAX),
"path" => path,
"ps1" => ctx.new_str(">>>>> "),
"ps2" => ctx.new_str("..... "),
"__doc__" => ctx.new_str(sys_doc),
"_getframe" => ctx.new_function(getframe),
"_getframe" => named_function!(ctx, sys, _getframe),
"modules" => modules.clone(),
"warnoptions" => ctx.new_list(vec![]),
"platform" => ctx.new_str(PLATFORM.to_owned()),
@@ -677,24 +678,24 @@ settrace() -- set the global debug tracing function
"path_importer_cache" => ctx.new_dict(),
"pycache_prefix" => vm.ctx.none(),
"dont_write_bytecode" => vm.ctx.new_bool(vm.state.settings.dont_write_bytecode),
"setprofile" => ctx.new_function(sys_setprofile),
"setrecursionlimit" => ctx.new_function(sys_setrecursionlimit),
"settrace" => ctx.new_function(sys_settrace),
"setprofile" => named_function!(ctx, sys, setprofile),
"setrecursionlimit" => named_function!(ctx, sys, setrecursionlimit),
"settrace" => named_function!(ctx, sys, settrace),
"version" => vm.ctx.new_str(version::get_version()),
"version_info" => version_info,
"_git" => sys_git_info(vm),
"exc_info" => ctx.new_function(sys_exc_info),
"exc_info" => named_function!(ctx, sys, exc_info),
"prefix" => ctx.new_str(prefix),
"base_prefix" => ctx.new_str(base_prefix),
"exec_prefix" => ctx.new_str(exec_prefix),
"base_exec_prefix" => ctx.new_str(base_exec_prefix),
"exit" => ctx.new_function(sys_exit),
"exit" => named_function!(ctx, sys, exit),
"abiflags" => ctx.new_str(ABIFLAGS.to_owned()),
"audit" => ctx.new_function(sys_audit),
"displayhook" => ctx.new_function(sys_displayhook),
"__displayhook__" => ctx.new_function(sys_displayhook),
"excepthook" => ctx.new_function(sys_excepthook),
"__excepthook__" => ctx.new_function(sys_excepthook),
"audit" => named_function!(ctx, sys, audit),
"displayhook" => named_function!(ctx, sys, displayhook),
"__displayhook__" => named_function!(ctx, sys, displayhook),
"excepthook" => named_function!(ctx, sys, excepthook),
"__excepthook__" => named_function!(ctx, sys, excepthook),
"hexversion" => ctx.new_int(version::VERSION_HEX),
"api_version" => ctx.new_int(0x0), // what C api?
"float_info" => float_info,
@@ -708,7 +709,7 @@ settrace() -- set the global debug tracing function
{
let getwindowsversion = WindowsVersion::make_class(ctx);
extend_module!(vm, module, {
"getwindowsversion" => ctx.new_function(sys_getwindowsversion),
"getwindowsversion" => named_function!(ctx, sys, getwindowsversion),
"_getwindowsversion_type" => getwindowsversion, // XXX: This is not a python spec but required by current RustPython implementation
})
}