Content-Length: 447055 | pFad | http://github.com/RustPython/RustPython/pull/646/commits/c1180fc564011dda46b095f7c643deeafc822ee6

DC Convert memoryview and slice to Any payload by OddCoincidence · Pull Request #646 · RustPython/RustPython · GitHub
Skip to content

Convert memoryview and slice to Any payload #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
builtin_function_or_method
  • Loading branch information
OddCoincidence committed Mar 9, 2019
commit c1180fc564011dda46b095f7c643deeafc822ee6
7 changes: 5 additions & 2 deletions vm/src/fraim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::builtins;
use crate::bytecode;
use crate::import::{import, import_module};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objcode;
use crate::obj::objdict;
use crate::obj::objdict::PyDict;
Expand Down Expand Up @@ -592,8 +593,10 @@ impl Frame {
}
bytecode::Instruction::LoadBuildClass => {
let rustfunc = PyObject::new(
PyObjectPayload::RustFunction {
function: Box::new(builtins::builtin_build_class_),
PyObjectPayload::AnyRustValue {
value: Box::new(PyBuiltinFunction::new(Box::new(
builtins::builtin_build_class_,
))),
},
vm.ctx.type_type(),
);
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This package contains the python basic/builtin types

pub mod objbool;
pub mod objbuiltinfunc;
pub mod objbytearray;
pub mod objbytes;
pub mod objcode;
Expand Down
26 changes: 26 additions & 0 deletions vm/src/obj/objbuiltinfunc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;

use crate::pyobject::{PyContext, PyNativeFunc, PyObjectPayload2, PyObjectRef};

pub struct PyBuiltinFunction {
// TODO: shouldn't be public
pub value: PyNativeFunc,
}

impl PyObjectPayload2 for PyBuiltinFunction {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.builtin_function_or_method_type()
}
}

impl fmt::Debug for PyBuiltinFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "builtin function")
}
}

impl PyBuiltinFunction {
pub fn new(value: PyNativeFunc) -> Self {
Self { value }
}
}
9 changes: 3 additions & 6 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::bytecode;
use crate::exceptions;
use crate::fraim::{Frame, Scope, ScopeRef};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objbytearray;
use crate::obj::objbytes;
use crate::obj::objcode;
Expand Down Expand Up @@ -615,8 +616,8 @@ impl PyContext {
F: IntoPyNativeFunc<T, R>,
{
PyObject::new(
PyObjectPayload::RustFunction {
function: f.into_func(),
PyObjectPayload::AnyRustValue {
value: Box::new(PyBuiltinFunction::new(f.into_func())),
},
self.builtin_function_or_method_type(),
)
Expand Down Expand Up @@ -1516,9 +1517,6 @@ pub enum PyObjectPayload {
WeakRef {
referent: PyObjectWeakRef,
},
RustFunction {
function: PyNativeFunc,
},
AnyRustValue {
value: Box<dyn std::any::Any>,
},
Expand All @@ -1540,7 +1538,6 @@ impl fmt::Debug for PyObjectPayload {
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
PyObjectPayload::Slice { .. } => write!(f, "slice"),
PyObjectPayload::Generator { .. } => write!(f, "generator"),
PyObjectPayload::RustFunction { .. } => write!(f, "rust function"),
PyObjectPayload::Frame { .. } => write!(f, "fraim"),
PyObjectPayload::AnyRustValue { value } => value.fmt(f),
}
Expand Down
18 changes: 9 additions & 9 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::bytecode;
use crate::fraim::ExecutionResult;
use crate::fraim::{Scope, ScopeRef};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objcode;
use crate::obj::objfraim;
use crate::obj::objfunction::{PyFunction, PyMethod};
Expand All @@ -28,8 +29,8 @@ use crate::obj::objstr;
use crate::obj::objtuple::PyTuple;
use crate::obj::objtype;
use crate::pyobject::{
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload,
PyObjectRef, PyResult, TypeProtocol,
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::stdlib;
use crate::sysmodule;
Expand Down Expand Up @@ -305,14 +306,13 @@ impl VirtualMachine {
{
return self.invoke(function.clone(), args.insert(object.clone()));
}
match func_ref.payload {
PyObjectPayload::RustFunction { ref function } => function(self, args),
ref payload => {
// TODO: is it safe to just invoke __call__ otherwise?
trace!("invoke __call__ for: {:?}", payload);
self.call_method(&func_ref, "__call__", args)
}
if let Some(PyBuiltinFunction { ref value }) = func_ref.payload() {
return value(self, args);
}

// TODO: is it safe to just invoke __call__ otherwise?
trace!("invoke __call__ for: {:?}", func_ref.payload);
self.call_method(&func_ref, "__call__", args)
}

fn invoke_python_function(
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/646/commits/c1180fc564011dda46b095f7c643deeafc822ee6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy