-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add _types module #6807
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
Open
youknowone
wants to merge
4
commits into
RustPython:main
Choose a base branch
from
youknowone:_types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add _types module #6807
Changes from all commits
Commits
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
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,33 @@ | ||
| use super::PyType; | ||
| use crate::{Context, Py, PyPayload, PyResult, class::PyClassImpl, types::Representable}; | ||
|
|
||
| /// PyCapsule - a container for C pointers. | ||
| /// In RustPython, this is a minimal implementation for compatibility. | ||
| #[pyclass(module = false, name = "PyCapsule")] | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct PyCapsule { | ||
| // Capsules store opaque pointers; we don't expose the actual pointer functionality | ||
| // since RustPython doesn't have the same C extension model as CPython. | ||
| _private: (), | ||
| } | ||
|
|
||
| impl PyPayload for PyCapsule { | ||
| #[inline] | ||
| fn class(ctx: &Context) -> &'static Py<PyType> { | ||
| ctx.types.capsule_type | ||
| } | ||
| } | ||
|
|
||
| #[pyclass(with(Representable), flags(DISALLOW_INSTANTIATION))] | ||
| impl PyCapsule {} | ||
|
|
||
| impl Representable for PyCapsule { | ||
| #[inline] | ||
| fn repr_str(_zelf: &Py<Self>, _vm: &crate::VirtualMachine) -> PyResult<String> { | ||
| Ok("<capsule object>".to_string()) | ||
| } | ||
| } | ||
|
|
||
| pub fn init(context: &Context) { | ||
| PyCapsule::extend_class(context, context.types.capsule_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| //! Implementation of the `_types` module. | ||
| //! | ||
| //! This module exposes built-in types that are used by the `types` module. | ||
|
|
||
| pub(crate) use _types::module_def; | ||
|
|
||
| #[pymodule] | ||
| #[allow(non_snake_case)] | ||
| mod _types { | ||
| use crate::{PyObjectRef, VirtualMachine}; | ||
|
|
||
| #[pyattr] | ||
| fn AsyncGeneratorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.async_generator.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn BuiltinFunctionType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx | ||
| .types | ||
| .builtin_function_or_method_type | ||
| .to_owned() | ||
| .into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn BuiltinMethodType(vm: &VirtualMachine) -> PyObjectRef { | ||
| // Same as BuiltinFunctionType in CPython | ||
| vm.ctx | ||
| .types | ||
| .builtin_function_or_method_type | ||
| .to_owned() | ||
| .into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn CapsuleType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.capsule_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn CellType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.cell_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn CodeType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.code_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn CoroutineType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.coroutine_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn EllipsisType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.ellipsis_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn FrameType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.frame_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn FunctionType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.function_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn GeneratorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.generator_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn GenericAlias(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.generic_alias_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn GetSetDescriptorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.getset_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn LambdaType(vm: &VirtualMachine) -> PyObjectRef { | ||
| // Same as FunctionType in CPython | ||
| vm.ctx.types.function_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn MappingProxyType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.mappingproxy_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn MemberDescriptorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.member_descriptor_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn MethodDescriptorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.method_descriptor_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn ClassMethodDescriptorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| // TODO: implement as separate type | ||
| vm.ctx.types.method_descriptor_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn MethodType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.bound_method_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn MethodWrapperType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.method_wrapper_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn ModuleType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.module_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn NoneType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.none_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn NotImplementedType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.not_implemented_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn SimpleNamespace(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.namespace_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn TracebackType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.traceback_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn UnionType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.union_type.to_owned().into() | ||
| } | ||
|
|
||
| #[pyattr] | ||
| fn WrapperDescriptorType(vm: &VirtualMachine) -> PyObjectRef { | ||
| vm.ctx.types.wrapper_descriptor_type.to_owned().into() | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClassMethodDescriptorType should not alias MethodDescriptorType.
On Line 109, this TODO means
types.ClassMethodDescriptorTypebecomes identical totypes.MethodDescriptorType, which diverges from CPython and can break code that relies on type identity checks. Please introduce a dedicated type (or avoid exposing this attribute until it exists).🤖 Prompt for AI Agents