FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement PEP 814 frozendict by youknowdot · Pull Request #8951 · RustPython/RustPython · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (7) .rs  (25) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions Lib/_collections_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ def __eq__(self, other):

__reversed__ = None

Mapping.register(frozendict)
Mapping.register(mappingproxy)
Mapping.register(framelocalsproxy)

Expand Down
17 changes: 16 additions & 1 deletion Lib/copy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def copy(x):


_copy_atomic_types = {types.NoneType, int, float, bool, complex, str, tuple,
bytes, frozenset, type, range, slice, property,
bytes, frozendict, frozenset, type, range, slice, property,
types.BuiltinFunctionType, types.EllipsisType,
types.NotImplementedType, types.FunctionType, types.CodeType,
weakref.ref, super}
Expand Down Expand Up @@ -203,6 +203,21 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
return y
d[dict] = _deepcopy_dict

def _deepcopy_frozendict(x, memo, deepcopy=deepcopy):
y = {}
for key, value in x.items():
y[deepcopy(key, memo)] = deepcopy(value, memo)

# We're not going to put the frozendict in the memo, but it's still
# important we check for it, in case the frozendict contains recursive
# mutable structures.
try:
return memo[id(x)]
except KeyError:
pass
return frozendict(y)
d[frozendict] = _deepcopy_frozendict

def _deepcopy_method(x, memo): # Copy instance methods
return type(x)(x.__func__, deepcopy(x.__self__, memo))
d[types.MethodType] = _deepcopy_method
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_dict.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@ def test_empty_presized_dict_in_freelist(self):
'f': None, 'g': None, 'h': None}
d = {}

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_container_iterator(self):
# Bug #3680: tp_traverse was not implemented for dictiter and
# dictview objects.
Expand Down Expand Up @@ -1264,7 +1263,6 @@ def test_free_after_iterating(self):
support.check_free_after_iterating(self, lambda d: iter(d.values()), dict)
support.check_free_after_iterating(self, lambda d: iter(d.items()), dict)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_equal_operator_modifying_operand(self):
# test fix for seg fault reported in bpo-27945 part 3.
class X():
Expand Down
127 changes: 124 additions & 3 deletions crates/capi/src/ceval.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::pystate::with_vm;
use crate::unicodeobject::decode_fsdefault_and_size;
use crate::util::FfiPtrExt;
use core::ffi::{CStr, c_char, c_int};
use rustpython_vm::builtins::{PyCode, PyDict};
use rustpython_vm::builtins::{PyAnyDictRef, PyCode};
use rustpython_vm::function::ArgMapping;
use rustpython_vm::scope::Scope;
use rustpython_vm::{AsObject, PyObject, TryFromObject};
Expand Down Expand Up @@ -32,12 +32,15 @@ pub unsafe extern "C" fn PyEval_EvalCode(
) -> *mut PyObject {
with_vm(|vm| {
let code = unsafe { co.assume_borrowed_and_cast::<PyCode>(vm) }?;
let globals = unsafe { globals.assume_borrowed_and_cast::<PyDict>(vm) }?;
let globals =
PyAnyDictRef::try_from_object(vm, unsafe { globals.assume_borrowed() }.to_owned())?;
let locals = unsafe { locals.assume_borrowed_or_opt() }
.map(|obj| ArgMapping::try_from_object(vm, obj.to_owned()))
.transpose()?;

let scope = Scope::with_builtins(locals, globals.to_owned(), vm);
let locals = locals.or_else(|| Some(ArgMapping::from_anydict_exact(globals.clone())));
// Unlike Python's eval/exec, this API does not insert __builtins__.
let scope = Scope::new(locals, globals);

vm.run_code_obj(code.to_owned(), scope)
})
Expand Down Expand Up @@ -268,4 +271,122 @@ assert not hidden_leaked
.unwrap();
})
}

#[pyfunction]
fn frame_global_views(py: Python<'_>) -> PyResult<Vec<Bound<'_, PyAny>>> {
unsafe {
let frame = super::PyEval_GetFrame();
Ok(vec![
Bound::from_borrowed_ptr(py, super::PyEval_GetGlobals().cast()),
Bound::from_owned_ptr_or_err(py, super::PyEval_GetFrameGlobals().cast())?,
Bound::from_owned_ptr_or_err(py, crate::pyframe::PyFrame_GetGlobals(frame).cast())?,
])
}
}

fn eval_with_globals<'py>(
globals: &Bound<'py, PyAny>,
source: &str,
mode: &str,
locals: Option<&Bound<'py, PyAny>>,
) -> PyResult<Bound<'py, PyAny>> {
let py = globals.py();
let code = py.import("builtins")?.getattr("compile")?.call1((
source,
"<capi-frozendict>",
mode,
))?;
unsafe {
Bound::from_owned_ptr_or_err(
py,
pyo3::ffi::PyEval_EvalCode(
code.as_ptr(),
globals.as_ptr(),
locals.map_or(core::ptr::null_mut(), Bound::as_ptr),
),
)
}
}

#[test]
fn code_eval_frozen_globals() {
Python::attach(|py| {
let globals = py.eval(c"frozendict(answer=40)", None, None).unwrap();
let result = eval_with_globals(&globals, "answer + len((1, 2))", "eval", None).unwrap();
assert_eq!(result.extract::<i32>().unwrap(), 42);
let identity = eval_with_globals(&globals, "globals()", "eval", None).unwrap();
assert!(identity.is(&globals));
assert!(!globals.contains("__builtins__").unwrap());

let namespace = pyo3::types::PyDict::new(py);
namespace
.set_item(
"frame_global_views",
wrap_pyfunction!(frame_global_views, py).unwrap(),
)
.unwrap();
let globals = py
.import("builtins")
.unwrap()
.getattr("frozendict")
.unwrap()
.call1((namespace,))
.unwrap();
let views = eval_with_globals(&globals, "frame_global_views()", "eval", None).unwrap();
for view in views.try_iter().unwrap() {
assert!(view.unwrap().is(&globals));
}

let explicit = py
.eval(
c"frozendict(answer=40, __builtins__=frozendict(len=lambda x: 9))",
None,
None,
)
.unwrap();
let result = eval_with_globals(&explicit, "answer + len(())", "eval", None).unwrap();
assert_eq!(result.extract::<i32>().unwrap(), 49);
})
}

#[test]
fn code_eval_frozen_globals_mutation() {
Python::attach(|py| {
let globals = py.eval(c"frozendict(answer=40)", None, None).unwrap();
for source in [
"answer = 1",
"global answer; answer = 1",
"global answer; del answer",
] {
let error = eval_with_globals(&globals, source, "exec", None).unwrap_err();
assert!(error.is_instance_of::<pyo3::exceptions::PyTypeError>(py));
}
let locals = pyo3::types::PyDict::new(py);
eval_with_globals(
&globals,
"local_answer = answer + 2",
"exec",
Some(locals.as_any()),
)
.unwrap();
assert_eq!(
locals
.get_item("local_answer")
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap(),
42
);
assert_eq!(
globals
.get_item("answer")
.unwrap()
.extract::<i32>()
.unwrap(),
40
);
assert!(!globals.contains("__builtins__").unwrap());
})
}
}
Loading
Loading

Back | FazBrowse Home | New Git URL