-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add basic capi error support #7787
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
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2562f9d
Add basic capi error support
bschoenmaeckers 40d97ea
Add missing symbols to make tests compile again
bschoenmaeckers 6b79cb5
Add `pyerrors` to dictionary
bschoenmaeckers 71438c6
Return exception in `Py_GetConstantBorrowed`
bschoenmaeckers d708bfe
Remove `allow(dead_code)`
bschoenmaeckers bbf3656
Fix windows
bschoenmaeckers bccd38e
Load stdlib when calling `Py_InitializeEx`
bschoenmaeckers 9e20cf7
Debug tests
bschoenmaeckers 268856b
Revert "Load stdlib when calling `Py_InitializeEx`"
bschoenmaeckers 8432c17
Disable tests on windows for now
bschoenmaeckers 130321e
Truncate `PyType_GetFlags` to be always 32 bits
bschoenmaeckers 30ac061
Add test for exception type checking
bschoenmaeckers 4e4f7df
Remove subclass type flags
bschoenmaeckers 22985ca
Merge remote-tracking branch 'origin/main' into capi-errors
bschoenmaeckers b2c2f69
Use latest pyo3 to make test work on windows
bschoenmaeckers d18d259
Revert "Use latest pyo3 to make test work on windows"
bschoenmaeckers 4e7bcbf
`set_main_interpreter` -> `init_main_interpreter`
bschoenmaeckers 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
Add missing symbols to make tests compile again
- Loading branch information
commit 40d97ea83cf8bfca8c2b50197f775c3354f5e895
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,83 @@ | ||
| use crate::PyObject; | ||
| use crate::pystate::with_vm; | ||
| use core::ffi::{c_int, c_uint, c_ulong}; | ||
| use rustpython_vm::builtins::PyType; | ||
| use rustpython_vm::{AsObject, Context, Py}; | ||
|
|
||
| const PY_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; | ||
| const PY_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25; | ||
| const PY_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; | ||
| const PY_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27; | ||
| const PY_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; | ||
| const PY_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29; | ||
| const PY_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30; | ||
| const PY_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; | ||
|
|
||
| pub type PyTypeObject = Py<PyType>; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject { | ||
| unsafe { (*op).class() } | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn Py_IS_TYPE(op: *mut PyObject, ty: *mut PyTypeObject) -> c_int { | ||
| with_vm(|_vm| { | ||
| let obj = unsafe { &*op }; | ||
| let ty = unsafe { &*ty }; | ||
| obj.class().is(ty) | ||
| }) | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn PyType_GetFlags(ptr: *const PyTypeObject) -> c_ulong { | ||
| let ctx = Context::genesis(); | ||
| let zoo = &ctx.types; | ||
| let exp_zoo = &ctx.exceptions; | ||
|
|
||
| let ty = unsafe { &*ptr }; | ||
| let mut flags = ty.slots.flags.bits(); | ||
|
|
||
| if ty.is_subtype(zoo.int_type) { | ||
| flags |= PY_TPFLAGS_LONG_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.list_type) { | ||
| flags |= PY_TPFLAGS_LIST_SUBCLASS | ||
| } | ||
| if ty.is_subtype(zoo.tuple_type) { | ||
| flags |= PY_TPFLAGS_TUPLE_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.bytes_type) { | ||
| flags |= PY_TPFLAGS_BYTES_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.str_type) { | ||
| flags |= PY_TPFLAGS_UNICODE_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.dict_type) { | ||
| flags |= PY_TPFLAGS_DICT_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(exp_zoo.base_exception_type) { | ||
| flags |= PY_TPFLAGS_BASE_EXC_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.type_type) { | ||
| flags |= PY_TPFLAGS_TYPE_SUBCLASS; | ||
| } | ||
|
|
||
| flags | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject { | ||
| with_vm(|vm| { | ||
| let ctx = &vm.ctx; | ||
| match constant_id { | ||
| 0 => ctx.none.as_object(), | ||
| 1 => ctx.false_value.as_object(), | ||
| 2 => ctx.true_value.as_object(), | ||
| 3 => ctx.ellipsis.as_object(), | ||
| 4 => ctx.not_implemented.as_object(), | ||
| _ => panic!("Invalid constant_id passed to Py_GetConstantBorrowed"), | ||
| } | ||
| .as_raw() | ||
| }) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.