Skip to content

Commit

Permalink
Add updated definition of PyCodeObject for 3.11
Browse files Browse the repository at this point in the history
The ordering of the fields has completely changed, so we give it
a different definition.
  • Loading branch information
Techcable committed Jun 19, 2022
1 parent e7a4bb9 commit 33cd7d3
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions python3-sys/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,44 @@ use crate::pyport::Py_ssize_t;
pub struct _PyOpcache {
_private: [u8; 0],
}

#[derive(Copy)]
#[repr(C)]
#[cfg(Py_3_11)]
#[cfg(Py_3_11)]
// The field orderings have completely changed in 3.11,
// so we seperate it out into a different type declaration
//
// the justification for the reordering was "optimization"
pub struct PyCodeObject {
_private: [u8; 0],
pub ob_base: PyObject,
pub co_consts: *mut PyObject,
pub co_names: *mut PyObject,
pub co_exceptiontable: *mut PyObject,
pub co_flags: c_int,
pub co_warmup: c_int,
pub co_argcount: c_int,
pub co_posonlyargcount: c_int,
pub co_kwonlyargcount: c_int,
pub co_stacksize: c_int,
pub co_firstlineno: c_int,
pub co_nlocalsplus: c_int,
pub co_nlocals: c_int,
pub co_nplaincellvars: c_int,
pub co_ncellvars: c_int,
pub co_nfreevars: c_int,
pub co_localsplusnames: *mut PyObject,
pub co_localspluskinds: *mut PyObject,
pub co_filename: *mut PyObject,
pub co_name: *mut PyObject,
pub co_qualname: *mut PyObject,
pub co_linetable: *mut PyObject,
pub co_weakreflist: *mut PyObject,
pub co_extra: *mut c_void,
pub co_code_adaptive: [c_char; 1],
}

#[repr(C)]
#[derive(Copy)]
// So many fields (and there orderings) have changed in 3.11,
// it's best to just make it opaque
#[cfg(not(Py_3_11))]
pub struct PyCodeObject {
pub ob_base: PyObject,
Expand Down

0 comments on commit 33cd7d3

Please sign in to comment.