Skip to content

Fix _ctypes.Array base and metaclass #5620

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 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
from _ctypes import sizeof
from _ctypes import _SimpleCData
from _ctypes import _SimpleCData, Array
from _ctypes import CFuncPtr as _CFuncPtr

from struct import calcsize as _calcsize


assert Array.__class__.__name__ == 'PyCArrayType'
assert Array.__base__.__name__ == '_CData'

DEFAULT_MODE = RTLD_LOCAL
if _os.name == "posix" and _sys.platform == "darwin":
# On OS X 10.3, we use RTLD_GLOBAL as default mode
Expand Down
6 changes: 3 additions & 3 deletions vm/src/stdlib/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ pub(crate) mod union;

use crate::builtins::PyModule;
use crate::class::PyClassImpl;
use crate::stdlib::ctypes::base::{PyCData, PyCSimple, PySimpleMeta};
use crate::stdlib::ctypes::base::{PyCData, PyCSimple, PyCSimpleType};
use crate::{Py, PyRef, VirtualMachine};

pub fn extend_module_nodes(vm: &VirtualMachine, module: &Py<PyModule>) {
let ctx = &vm.ctx;
PySimpleMeta::make_class(ctx);
PyCSimpleType::make_class(ctx);
array::PyCArrayType::make_class(ctx);
extend_module!(vm, module, {
"_CData" => PyCData::make_class(ctx),
"_SimpleCData" => PyCSimple::make_class(ctx),
"ArrayType" => array::PyCArrayType::make_class(ctx),
"Array" => array::PyCArray::make_class(ctx),
"CFuncPtr" => function::PyCFuncPtr::make_class(ctx),
"_Pointer" => pointer::PyCPointer::make_class(ctx),
Expand Down
18 changes: 13 additions & 5 deletions vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::builtins::PyBytes;
use crate::types::Callable;
use crate::{Py, PyObjectRef, PyPayload};
use crate::{PyResult, VirtualMachine, builtins::PyTypeRef, types::Constructor};
use crate::{
PyResult, VirtualMachine,
builtins::{PyType, PyTypeRef},
types::Constructor,
};
use crossbeam_utils::atomic::AtomicCell;
use rustpython_common::lock::PyRwLock;
use rustpython_vm::stdlib::ctypes::base::PyCSimple;
use rustpython_vm::stdlib::ctypes::base::PyCData;

// TODO: make it metaclass
#[pyclass(name = "ArrayType", module = "_ctypes")]
#[pyclass(name = "PyCArrayType", base = "PyType", module = "_ctypes")]
#[derive(PyPayload)]
pub struct PyCArrayType {
pub(super) inner: PyCArray,
Expand Down Expand Up @@ -44,7 +47,12 @@ impl Constructor for PyCArrayType {
#[pyclass(flags(IMMUTABLETYPE), with(Callable, Constructor))]
impl PyCArrayType {}

#[pyclass(name = "Array", base = "PyCSimple", module = "_ctypes")]
#[pyclass(
name = "Array",
base = "PyCData",
metaclass = "PyCArrayType",
module = "_ctypes"
)]
#[derive(PyPayload)]
pub struct PyCArray {
pub(super) typ: PyRwLock<PyTypeRef>,
Expand Down
8 changes: 4 additions & 4 deletions vm/src/stdlib/ctypes/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ pub struct PyCData {
impl PyCData {}

#[pyclass(module = "_ctypes", name = "PyCSimpleType", base = "PyType")]
pub struct PySimpleMeta {}
pub struct PyCSimpleType {}

#[pyclass(flags(BASETYPE))]
impl PySimpleMeta {
impl PyCSimpleType {
#[allow(clippy::new_ret_no_self)]
#[pymethod]
fn new(cls: PyTypeRef, _: OptionalArg, vm: &VirtualMachine) -> PyResult {
Expand All @@ -176,10 +176,10 @@ impl PySimpleMeta {
}

#[pyclass(
module = "_ctypes",
name = "_SimpleCData",
base = "PyCData",
module = "_ctypes",
metaclass = "PySimpleMeta"
metaclass = "PyCSimpleType"
)]
#[derive(PyPayload)]
pub struct PyCSimple {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/stdlib/ctypes/structure.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::base::PyCData;
use crate::builtins::{PyList, PyStr, PyTuple, PyTypeRef};
use crate::function::FuncArgs;
use crate::types::GetAttr;
Expand All @@ -7,7 +8,7 @@ use rustpython_vm::types::Constructor;
use std::collections::HashMap;
use std::fmt::Debug;

#[pyclass(name = "Structure", module = "_ctypes")]
#[pyclass(module = "_ctypes", name = "Structure", base = "PyCData")]
#[derive(PyPayload, Debug)]
pub struct PyCStructure {
#[allow(dead_code)]
Expand Down
5 changes: 4 additions & 1 deletion vm/src/stdlib/ctypes/union.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[pyclass(name = "Union", module = "_ctypes")]
use super::base::PyCData;

// TODO: metaclass = "UnionType"
#[pyclass(module = "_ctypes", name = "Union", base = "PyCData")]
pub struct PyCUnion {}

#[pyclass(flags(BASETYPE, IMMUTABLETYPE))]
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy