GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ctypes struct/union/array by youknowone · Pull Request #6309 · RustPython/RustPython

ctypes struct/union/array #6309

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.

Sign up for GitHub

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
youknowone merged 5 commits into RustPython:main from youknowone:ctypes-struct
Nov 29, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

ctypes struct/union/array #6309

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
4b52c56
PyCStructure
youknowone Nov 28, 2025
0a39899
impl union
youknowone Nov 29, 2025
2585c5e
array
youknowone Nov 29, 2025
efebed6
temp remove
youknowone Nov 29, 2025
711ea51
apply review
youknowone Nov 29, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

64 changes: 59 additions & 5 deletions 64 crates/vm/src/stdlib/ctypes.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub fn extend_module_nodes(vm: &VirtualMachine, module: &Py<PyModule>) {
array::PyCArrayType::make_class(ctx);
field::PyCFieldType::make_class(ctx);
pointer::PyCPointerType::make_class(ctx);
structure::PyCStructType::make_class(ctx);
union::PyCUnionType::make_class(ctx);
extend_module!(vm, module, {
"_CData" => PyCData::make_class(ctx),
"_SimpleCData" => PyCSimple::make_class(ctx),
Expand All @@ -46,9 +48,10 @@ pub(crate) mod _ctypes {
use super::base::PyCSimple;
use crate::builtins::PyTypeRef;
use crate::class::StaticType;
use crate::convert::ToPyObject;
use crate::function::{Either, FuncArgs, OptionalArg};
use crate::stdlib::ctypes::library;
use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine};
use crate::{AsObject, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine};
use crossbeam_utils::atomic::AtomicCell;
use std::ffi::{
c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong,
Expand All @@ -57,6 +60,23 @@ pub(crate) mod _ctypes {
use std::mem;
use widestring::WideChar;

/// CArgObject - returned by byref()
#[pyclass(name = "CArgObject", module = "_ctypes", no_attr)]
#[derive(Debug, PyPayload)]
pub struct CArgObject {
pub obj: PyObjectRef,
#[allow(dead_code)]
pub offset: isize,
}

#[pyclass]
impl CArgObject {
#[pygetset]
fn _obj(&self) -> PyObjectRef {
self.obj.clone()
}
}

#[pyattr(name = "__version__")]
const __VERSION__: &str = "1.1.0";

Expand Down Expand Up @@ -322,9 +342,28 @@ pub(crate) mod _ctypes {
}

#[pyfunction]
fn byref(_args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
// TODO: RUSTPYTHON
Err(vm.new_value_error("not implemented"))
fn byref(obj: PyObjectRef, offset: OptionalArg<isize>, vm: &VirtualMachine) -> PyResult {
use super::base::PyCData;
use crate::class::StaticType;

// Check if obj is a ctypes instance
if !obj.fast_isinstance(PyCData::static_type())
&& !obj.fast_isinstance(PyCSimple::static_type())
{
return Err(vm.new_type_error(format!(
"byref() argument must be a ctypes instance, not '{}'",
obj.class().name()
)));
}

let offset_val = offset.unwrap_or(0);

// Create CArgObject to hold the reference
Ok(CArgObject {
obj,
offset: offset_val,
}
.to_pyobject(vm))
}

#[pyfunction]
Expand Down Expand Up @@ -380,9 +419,24 @@ pub(crate) mod _ctypes {
f as usize
}

#[pyattr]
fn _wstring_at_addr(_vm: &VirtualMachine) -> usize {
// Return address of wcsnlen or similar wide string function
#[cfg(not(target_os = "windows"))]
{
let f = libc::wcslen;
f as usize
}
#[cfg(target_os = "windows")]
{
// FIXME: On Windows, use wcslen from ucrt
0
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


#[pyattr]
fn _cast_addr(_vm: &VirtualMachine) -> usize {
// TODO: RUSTPYTHON
// todo!("Implement _cast_addr")
0
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.