Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

_ctypes addressof and Structure #5573

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 1, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
clippy fix
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Feb 28, 2025
commit 2c6e126498cb063d0aa47c441245b135f04bc251
34 changes: 19 additions & 15 deletions 34 vm/src/stdlib/ctypes/structure.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
use std::collections::HashMap;
use crate::builtins::{PyList, PyStr, PyTuple, PyTypeRef};
use crate::function::FuncArgs;
use crate::types::GetAttr;
use crate::{AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine};
use rustpython_common::lock::PyRwLock;
use rustpython_vm::types::Constructor;
use std::collections::HashMap;
use std::fmt::Debug;
use rustpython_common::lock::PyRwLock;
use crate::types::GetAttr;

#[pyclass(name = "Structure", module = "_ctypes")]
#[derive(PyPayload, Debug)]
pub struct PyCStructure {
#[allow(dead_code)]
field_data: PyRwLock<HashMap<String, PyObjectRef>>,
data: PyRwLock<HashMap<String, PyObjectRef>>
data: PyRwLock<HashMap<String, PyObjectRef>>,
}

impl Constructor for PyCStructure {
type Args = FuncArgs;

fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
let fields_attr = cls.get_class_attr(vm.ctx.interned_str("_fields_").unwrap()).ok_or_else(|| {
vm.new_attribute_error("Structure must have a _fields_ attribute".to_string())
})?;
let fields_attr = cls
.get_class_attr(vm.ctx.interned_str("_fields_").unwrap())
.ok_or_else(|| {
vm.new_attribute_error("Structure must have a _fields_ attribute".to_string())
})?;
// downcast into list
let fields = fields_attr.downcast_ref::<PyList>().ok_or_else(|| {
vm.new_type_error("Structure _fields_ attribute must be a list".to_string())
})?;
let fields = fields.borrow_vec();
let mut field_data = HashMap::new();
for field in fields.iter() {
let field = field.downcast_ref::<PyTuple>().ok_or_else(|| {
vm.new_type_error("Field must be a tuple".to_string())
})?;
let name = field.get(0).unwrap().downcast_ref::<PyStr>().ok_or_else(|| {
vm.new_type_error("Field name must be a string".to_string())
})?;
let field = field
.downcast_ref::<PyTuple>()
.ok_or_else(|| vm.new_type_error("Field must be a tuple".to_string()))?;
let name = field
.get(0)
.unwrap()
.downcast_ref::<PyStr>()
.ok_or_else(|| vm.new_type_error("Field name must be a string".to_string()))?;
let typ = field.get(1).unwrap().clone();
field_data.insert(name.as_str().to_string(), typ);
}
Expand All @@ -47,11 +52,10 @@ impl GetAttr for PyCStructure {
let data = zelf.data.read();
match data.get(&name) {
Some(value) => Ok(value.clone()),
None => Err(vm.new_attribute_error(format!("No attribute named {}", name)))
None => Err(vm.new_attribute_error(format!("No attribute named {}", name))),
}
}
}

#[pyclass(flags(BASETYPE, IMMUTABLETYPE))]
impl PyCStructure {}

Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.