Skip to content

Commit

Permalink
python3-sys: port Py_3_12 changes from pyo3-ffi
Browse files Browse the repository at this point in the history
I looked at all Py_3_12 changes in pyo3-ffi/src/cpython and ported them
back. Functions, structs, and fields in the code object not already
defined in python3-sys are ignored. For PyUnicode_READY, I kept a dummy
version so the cpython crate does not need change.
  • Loading branch information
quark-zju committed May 19, 2024
1 parent e815555 commit 4fe0dc7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
7 changes: 6 additions & 1 deletion python3-sys/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ pub struct PyCodeObject {
pub co_names: *mut PyObject,
pub co_exceptiontable: *mut PyObject,
pub co_flags: c_int,
#[cfg(not(Py_3_12))]
pub co_warmup: c_short,
co_linearray_entry_size: c_short,
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,
#[cfg(Py_3_12)]
pub co_framesize: c_int,
pub co_nlocals: c_int,
#[cfg(not(Py_3_12))]
pub co_nplaincellvars: c_int,
pub co_ncellvars: c_int,
pub co_nfreevars: c_int,
#[cfg(Py_3_12)]
pub co_version: u32,
pub co_localsplusnames: *mut PyObject,
pub co_localspluskinds: *mut PyObject,
pub co_filename: *mut PyObject,
Expand Down
13 changes: 13 additions & 0 deletions python3-sys/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ use crate::object::PyObject;
use crate::pyarena::*;
use crate::pythonrun::*;

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(Py_3_12)]
pub struct _PyCompilerSrcLocation {
pub lineno: c_int,
pub end_lineno: c_int,
pub col_offset: c_int,
pub end_col_offset: c_int,
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(not(Py_LIMITED_API))]
pub struct PyFutureFeatures {
pub ff_features: c_int,
#[cfg(not(Py_3_12))]
pub ff_lineno: c_int,
#[cfg(Py_3_12)]
pub ff_location: _PyCompilerSrcLocation,
}

// TODO: PyCF_MASK etc. constants
Expand Down
10 changes: 7 additions & 3 deletions python3-sys/src/initconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub struct PyConfig {
#[cfg(all(Py_3_9, not(Py_3_10)))]
pub _use_peg_parser: c_int,
pub tracemalloc: c_int,
#[cfg(Py_3_12)]
pub perf_profiling: c_int,
pub import_time: c_int,
#[cfg(Py_3_11)]
pub code_debug_ranges: c_int,
Expand Down Expand Up @@ -151,6 +153,8 @@ pub struct PyConfig {
pub use_frozen_modules: c_int,
#[cfg(Py_3_11)]
pub safe_path: c_int,
#[cfg(Py_3_12)]
pub int_max_str_digits: c_int,
// Path configuration inputs:
pub pathconfig_warnings: c_int,
#[cfg(Py_3_10)]
Expand Down Expand Up @@ -180,12 +184,12 @@ pub struct PyConfig {
// Private fields
pub _install_importlib: c_int,
pub _init_main: c_int,
#[cfg(Py_3_9)]
#[cfg(all(Py_3_9, not(Py_3_12)))]
pub _isolated_interpreter: c_int,
#[cfg(all(Py_3_9, not(Py_3_10)))]
pub _orig_argv: PyWideStringList,
#[cfg(Py_3_11)]
pub _is_python_build: c_int,
#[cfg(all(Py_3_9, not(Py_3_10)))]
pub _orig_argv: PyWideStringList,
}

impl Default for PyConfig {
Expand Down
13 changes: 12 additions & 1 deletion python3-sys/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ mod typeobject {
pub tp_finalize: Option<crate::object::destructor>,
#[cfg(Py_3_8)]
pub tp_vectorcall: Option<crate::object::vectorcallfunc>,
#[cfg(Py_3_12)]
pub tp_watched: c_char,
#[cfg(all(Py_3_8, not(Py_3_9)))]
pub tp_print: Option<crate::object::printfunc>,
#[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
Expand Down Expand Up @@ -535,7 +537,16 @@ mod typeobject {
}
}

#[cfg(Py_3_9)]
#[cfg(Py_3_12)]
pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
tp_as_async: 0 as *mut PyAsyncMethods,
tp_vectorcall_offset: 0,
tp_vectorcall: None,
tp_finalize: None,
tp_watched: 0,
);

#[cfg(all(Py_3_9, not(Py_3_12)))]
pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
tp_as_async: 0 as *mut PyAsyncMethods,
tp_vectorcall_offset: 0,
Expand Down
24 changes: 18 additions & 6 deletions python3-sys/src/unicodeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ extern "C" {
#[deprecated(since = "0.6.1", note = "Deprecated since Python 3.3; removed in 3.10")]
pub fn PyUnicode_AsUnicodeCopy(unicode: *mut PyObject) -> *mut Py_UNICODE;

#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, Py_3_12)))]
fn _PyUnicode_Ready(o: *mut PyObject) -> c_int;
}

Expand All @@ -446,6 +446,7 @@ pub struct PyASCIIObject {
pub length: Py_ssize_t,
pub hash: Py_hash_t,
pub state: u32,
#[cfg(not(Py_3_12))]
pub wstr: *mut c_void,
}

Expand All @@ -455,6 +456,7 @@ pub struct PyCompactUnicodeObject {
_base: PyASCIIObject,
utf8_length: Py_ssize_t,
utf8: *mut u8,
#[cfg(not(Py_3_12))]
wstr_length: Py_ssize_t,
}

Expand Down Expand Up @@ -494,6 +496,7 @@ pub const PyUnicode_4BYTE_KIND: u32 = 4;
#[inline]
pub unsafe fn PyUnicode_KIND(o: *mut PyObject) -> u32 {
debug_assert!(PyUnicode_Check(o) > 0);
#[cfg(not(Py_3_12))]
debug_assert!(PyUnicode_IS_READY(o));
let state = (*(o as *mut PyASCIIObject)).state;
(state >> 2) & 7
Expand All @@ -502,6 +505,7 @@ pub unsafe fn PyUnicode_KIND(o: *mut PyObject) -> u32 {
#[cfg(not(Py_LIMITED_API))]
pub unsafe fn PyUnicode_DATA(o: *mut PyObject) -> *mut c_void {
debug_assert!(PyUnicode_Check(o) > 0);
#[cfg(not(Py_3_12))]
debug_assert!(PyUnicode_IS_READY(o));
if PyUnicode_IS_COMPACT(o) {
// fn _PyUnicode_COMPACT_DATA
Expand All @@ -522,11 +526,12 @@ pub unsafe fn PyUnicode_DATA(o: *mut PyObject) -> *mut c_void {
#[inline]
pub unsafe fn PyUnicode_GET_LENGTH(o: *mut PyObject) -> Py_ssize_t {
debug_assert!(PyUnicode_Check(o) > 0);
#[cfg(not(Py_3_12))]
debug_assert!(PyUnicode_IS_READY(o));
(*(o as *mut PyASCIIObject)).length
}

#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, Py_3_12)))]
#[inline]
unsafe fn PyUnicode_IS_READY(o: *mut PyObject) -> bool {
let ready_bit = 1 << 7;
Expand All @@ -538,9 +543,16 @@ unsafe fn PyUnicode_IS_READY(o: *mut PyObject) -> bool {
#[inline]
pub unsafe fn PyUnicode_READY(o: *mut PyObject) -> c_int {
debug_assert!(PyUnicode_Check(o) > 0);
if PyUnicode_IS_READY(o) {
0
} else {
_PyUnicode_Ready(o)
#[cfg(Py_3_12)]
{
return 0;
}
#[cfg(not(Py_3_12))]
{
if PyUnicode_IS_READY(o) {
0
} else {
_PyUnicode_Ready(o)
}
}
}

0 comments on commit 4fe0dc7

Please sign in to comment.