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 — Preserve ctypes py_object provenance across casts and pointer writes by 1ndahous3 · Pull Request #9391 · RustPython/RustPython

Preserve ctypes py_object provenance across casts and pointer writes #9391

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 11 commits into RustPython:main from 1ndahous3:ctypes_pyobject_owners
Oct 27, 2026

Uh oh!

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

Merged

Preserve ctypes py_object provenance across casts and pointer writes #9391

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
154d1c5
Preserve ctypes py_object provenance across casts and pointer writes
1ndahous3 Oct 26, 2026
2078378
Validate ctypes writer paths across casts and retargeting
1ndahous3 Oct 26, 2026
0739832
Preserve ctypes writer roots and avoid nested storage scans
1ndahous3 Oct 26, 2026
1d94693
Register ctypes exporters and cache pointer-field writers
1ndahous3 Oct 26, 2026
a3a072d
Register overlapping ctypes views and index retained objects
1ndahous3 Oct 26, 2026
842238c
Track shared ctypes buffer owners and cache cast fields
1ndahous3 Oct 26, 2026
1e11d82
Index ctypes buffer aliases and register pointer exporters
1ndahous3 Oct 26, 2026
c65a11a
Publish disjoint ctypes byref writes to borrowed sources
1ndahous3 Oct 26, 2026
9ad3d90
Index ctypes write provenance and validate private field paths
1ndahous3 Oct 27, 2026
1b6337a
Resolve retained ctypes values after pointer retargeting
1ndahous3 Oct 27, 2026
37c84fb
Merge main into ctypes_pyobject_owners
1ndahous3 Oct 27, 2026
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

There are no files selected for viewing

20 changes: 11 additions & 9 deletions 20 crates/vm/src/builtins/memory.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1553,20 +1553,22 @@ static BUFFER_WINDOW_METHODS: BufferMethods = BufferMethods {
/// or from_address view can alias another object without sharing its owner.
/// Return None for that storage so either operand forces a source snapshot.
fn root_exporter(buffer: &PyBuffer) -> Option<PyObjectRef> {
let mut obj = buffer.obj.clone();
let obj = buffer_owner(&buffer.obj);
(!crate::stdlib::is_borrowed_ctypes_buffer(obj)).then(|| obj.to_owned())
}

// Follow forwarding payloads without reacquiring a buffer or invoking Python.
pub(crate) fn buffer_owner(mut obj: &PyObject) -> &PyObject {
loop {
let next = if let Some(view) = obj.downcast_ref::<PyMemoryView>() {
view.buffer.obj.clone()
obj = if let Some(view) = obj.downcast_ref::<PyMemoryView>() {
&view.buffer.obj
} else if let Some(wrapper) = obj.downcast_ref::<PyBufferWrapper>() {
wrapper.view.obj.clone()
&wrapper.view.obj
} else if let Some(window) = obj.downcast_ref::<PyBufferWindow>() {
window.source.obj.clone()
} else if crate::stdlib::is_borrowed_ctypes_buffer(&obj) {
return None;
&window.source.obj
} else {
return Some(obj);
return obj;
};
obj = next;
}
}

Expand Down
1 change: 1 addition & 0 deletions 1 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 @@ -16,6 +16,7 @@ use crate::{
};

pub(super) use array::PyCArray;
pub(crate) use base::BufferViewRegistry;
pub(super) use base::{CArgValue, PyCData, PyCField, StgInfo, StgInfoFlags};
pub(super) use pointer::PyCPointer;
pub(super) use simple::{PyCSimple, PyCSimpleType};
Expand Down
20 changes: 16 additions & 4 deletions 20 crates/vm/src/stdlib/_ctypes/array.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::StgInfo;
use super::base::{CDATA_BUFFER_METHODS, PyCData};
use super::{StgInfo, StgInfoFlags};
use crate::common::lock::LazyLock;
use crate::sliceable::SaturatedSliceIter;
use crate::{
Expand Down Expand Up @@ -560,8 +560,9 @@ impl PyCArray {
let address = rustpython_host_env::ctypes::read_pointer_from_buffer(
&buffer[offset..offset + element_size],
);
let slot = buffer.as_ptr().wrapping_add(offset) as usize;
drop(buffer);
return zelf.0.object_at(address, Some(index), vm);
return zelf.0.object_at(address, slot, Some(index), vm);
}
if let Some(element_type) = stg.as_ref().and_then(|info| info.element_type.as_ref())
&& super::simple::is_swapped(element_type)
Expand Down Expand Up @@ -620,15 +621,26 @@ impl PyCArray {
return Err(vm.new_index_error("invalid index"));
}
let index = index as usize;
// A view's buffer already points to its element. Its base can be a
// pointer object, whose own buffer stores an address rather than data.
let write = if element_type
.stg_info(vm)?
.flags
.intersects(StgInfoFlags::TYPEFLAG_ISPOINTER | StgInfoFlags::TYPEFLAG_HASPOINTER)
{
zelf.0
.register_pyobject_view(index, index * element_size, element_size, vm)?
} else {
super::base::CDataWrite::default()
};
let prepared = super::base::CDataValue::new(
&element_type,
value,
element_size,
super::simple::is_swapped(&element_type),
vm,
)?;
// A view's buffer already points to its element. Its base can be a
// pointer object, whose own buffer stores an address rather than data.
write.publish(vm)?;
zelf.0
.write_bytes_at_offset(index * element_size, &prepared.bytes);
prepared.keep(&zelf.0, index, vm)
Expand Down
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.