-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add getset_descriptor #1738
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
Add getset_descriptor #1738
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f5cd17
Add getset_descriptor
youknowone c3d5f6c
IntoPyGetterFunc, IntoPySetterFunc
youknowone ca55778
`&self` support for getter/setter
youknowone d1f9cb4
PySetResult and IntoPySetResult
youknowone 226a2a6
VM polymorphism for getter and setter
youknowone 23381b9
compatiibility for CPytthon descr_check
youknowone 0aee78d
pyproperty generates PyGetSet instead of PyProperty
youknowone facabfe
Remove PropertyBuilder and add new_getset
youknowone 58744df
Revert 08e66b5002f3a3a30db72b350b18c61d367ef1fc
youknowone c0b235e
cleanup property and get descriptor codes
youknowone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add getset_descriptor
- Loading branch information
commit 9f5cd17f2bc296ddf97c4e8311000234f8222d43
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /*! Python `attribute` descriptor class. (PyGetSet) | ||
|
|
||
| */ | ||
| use super::objtype::PyClassRef; | ||
| use crate::function::OptionalArg; | ||
| use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue}; | ||
| use crate::slots::PyBuiltinDescriptor; | ||
| use crate::vm::VirtualMachine; | ||
|
|
||
| pub type PyGetter = dyn Fn(PyObjectRef, &VirtualMachine) -> PyResult; | ||
| pub type PySetter = dyn Fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PyResult<()>; | ||
|
|
||
| #[pyclass] | ||
| pub struct PyGetSet { | ||
| // name: String, | ||
| getter: Box<PyGetter>, | ||
| setter: Box<PySetter>, | ||
| // doc: Option<String>, | ||
| } | ||
|
|
||
| impl std::fmt::Debug for PyGetSet { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!( | ||
| f, | ||
| "PyGetSet {{ getter: {:p}, setter: {:p} }}", | ||
| self.getter, self.setter | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl PyValue for PyGetSet { | ||
| fn class(vm: &VirtualMachine) -> PyClassRef { | ||
| vm.ctx.getset_type() | ||
| } | ||
| } | ||
|
|
||
| pub type PyGetSetRef = PyRef<PyGetSet>; | ||
|
|
||
| impl PyBuiltinDescriptor for PyGetSet { | ||
| fn get( | ||
| zelf: PyRef<Self>, | ||
| obj: PyObjectRef, | ||
| _cls: OptionalArg<PyObjectRef>, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult { | ||
| (zelf.getter)(obj, vm) | ||
| } | ||
| } | ||
|
|
||
| impl PyGetSet { | ||
| pub fn new(getter: &'static PyGetter, setter: &'static PySetter) -> Self { | ||
| Self { | ||
| getter: Box::new(getter), | ||
| setter: Box::new(setter), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[pyimpl(with(PyBuiltinDescriptor))] | ||
| impl PyGetSet { | ||
| // Descriptor methods | ||
|
|
||
| #[pymethod(magic)] | ||
| fn set(&self, obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { | ||
| (self.setter)(obj, value, vm) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn init(context: &PyContext) { | ||
| PyGetSet::extend_class(context, &context.types.getset_type); | ||
| } | ||
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.