Skip to content

Commit d4c8474

Browse files
committed
Resolve symbol errors with Python 3.12
Symbols missing in /opt/hostedtoolcache/Python/3.12.4/x64/lib/libpython3.12.so: PyCode_New PyCode_NewWithPosOnlyArgs PyUnicode_AsUnicode PyUnicode_AsUnicodeAndSize PyUnicode_FromUnicode _Py_GetAllocatedBlocks _Py_PackageContext Python 3.12/3.11 renamed/deprecated some APIs. Update code to skip them. This affects `linux (ubuntu-latest, 3.12, nightly)` CI.
1 parent 4fe0dc7 commit d4c8474

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

python3-sys/src/code.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub const CO_MAXBLOCKS: usize = 20;
152152
extern "C" {
153153
pub static mut PyCode_Type: PyTypeObject;
154154

155+
#[cfg_attr(Py_3_12, link_name = "PyUnstable_Code_New")]
155156
pub fn PyCode_New(
156157
argcount: c_int,
157158
kwonlyargcount: c_int,
@@ -175,6 +176,7 @@ extern "C" {
175176
) -> *mut PyCodeObject;
176177

177178
#[cfg(Py_3_8)]
179+
#[cfg_attr(Py_3_12, link_name = "PyUnstable_Code_NewWithPosOnlyArgs")]
178180
pub fn PyCode_NewWithPosOnlyArgs(
179181
argcount: c_int,
180182
posonlyargcount: c_int,

python3-sys/src/modsupport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject
133133
)
134134
}
135135

136-
#[cfg(not(Py_LIMITED_API))]
136+
#[cfg(all(not(Py_LIMITED_API), not(Py_3_12)))]
137137
#[cfg_attr(windows, link(name = "pythonXY"))]
138138
extern "C" {
139139
pub static mut _Py_PackageContext: *const c_char;

python3-sys/src/objimpl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
#[cfg(all(py_sys_config = "Py_DEBUG", not(Py_3_4)))]
2222
pub fn _PyObject_DebugFree(arg1: *mut c_void);
2323

24-
#[cfg(all(not(Py_LIMITED_API), Py_3_4))]
24+
#[cfg(all(not(Py_LIMITED_API), Py_3_4, not(Py_3_11)))]
2525
pub fn _Py_GetAllocatedBlocks() -> Py_ssize_t;
2626
pub fn PyObject_Init(arg1: *mut PyObject, arg2: *mut PyTypeObject) -> *mut PyObject;
2727
pub fn PyObject_InitVar(

python3-sys/src/unicodeobject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern "C" {
5252
length: Py_ssize_t,
5353
fill_char: Py_UCS4,
5454
) -> Py_ssize_t;
55-
#[cfg(not(Py_LIMITED_API))]
55+
#[cfg(all(not(Py_LIMITED_API), not(Py_3_12)))]
5656
#[deprecated(since = "0.2.1", note = "Deprecated since Python 3.3 / PEP 393")]
5757
pub fn PyUnicode_FromUnicode(u: *const Py_UNICODE, size: Py_ssize_t) -> *mut PyObject;
5858

@@ -78,10 +78,10 @@ extern "C" {
7878
copy_null: c_int,
7979
) -> *mut Py_UCS4;
8080
pub fn PyUnicode_AsUCS4Copy(unicode: *mut PyObject) -> *mut Py_UCS4;
81-
#[cfg(not(Py_LIMITED_API))]
81+
#[cfg(all(not(Py_LIMITED_API), not(Py_3_12)))]
8282
#[deprecated(since = "0.2.1", note = "Deprecated since Python 3.3 / PEP 393")]
8383
pub fn PyUnicode_AsUnicode(unicode: *mut PyObject) -> *mut Py_UNICODE;
84-
#[cfg(not(Py_LIMITED_API))]
84+
#[cfg(all(not(Py_LIMITED_API), not(Py_3_12)))]
8585
#[deprecated(since = "0.2.1", note = "Deprecated since Python 3.3 / PEP 393")]
8686
pub fn PyUnicode_AsUnicodeAndSize(
8787
unicode: *mut PyObject,

tests/check_symbols.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,19 @@ def match_braces(text):
8989
foreign_sections.append(asttree[:endpos])
9090
asttree = asttree[endpos:]
9191

92+
renames = {}
93+
if sys.version_info >= (3, 12):
94+
# Those renames were declared by cfg_attr(Py_3_12, link_name = ...) but it's hard to parse them from AST.
95+
renames.update({"PyCode_New": "PyUnstable_Code_New", "PyCode_NewWithPosOnlyArgs": "PyUnstable_Code_NewWithPosOnlyArgs"})
96+
9297
for section in foreign_sections:
9398
lines = section.split('\n')
9499
for idx in range(len(lines)):
95100
line = lines[idx]
96101
if ('kind: Fn(' in line) or ('kind: Static(' in line):
97-
foreign_symbols.add(re.sub(r'\s*ident: (.*)#[0-9]*,', r'\1', lines[idx-1]))
102+
name = re.sub(r'\s*ident: (.*)#[0-9]*,', r'\1', lines[idx-1])
103+
name = renames.get(name) or name
104+
foreign_symbols.add(name)
98105

99106
assert 'PyList_Type' in foreign_symbols, "Failed getting statics from rustc -Z unpretty=ast-tree,expanded"
100107
assert 'PyList_New' in foreign_symbols, "Failed getting functions from rustc -Z unpretty=ast-tree,expanded"

0 commit comments

Comments
 (0)