Skip to content

Commit f753cf7

Browse files
committed
[GR-57317] Update and clean up patches, part #1
PullRequest: graalpython/3449
2 parents f0b7bb6 + e3346b8 commit f753cf7

34 files changed

+113
-627
lines changed

docs/user/Native-Extensions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ permalink: /reference-manual/python/Native-Extensions/
77

88
# Native Extensions Support
99

10-
CPython provides a [native extensions API](https://docs.python.org/3/c-api/index.html){:target="_blank"} for writing Python extensions in C/C++.
10+
CPython provides a [native extensions API](https://docs.python.org/3/c-api/index.html) for writing Python extensions in C/C++.
1111
GraalPy provides experimental support for this API, which allows many packages like NumPy and PyTorch to work well for many use cases.
1212
The support extends only to the API, not the binary interface (ABI), so extensions built for CPython are not binary compatible with GraalPy.
1313
Packages that use the native API must be built and installed with GraalPy, and the prebuilt wheels for CPython from pypi.org cannot be used.
@@ -18,7 +18,7 @@ Please do not update `pip` or use alternative tools such as `uv`.
1818
## Embedding limitations
1919

2020
Python native extensions run by default as native binaries, with full access to the underlying system.
21-
Native code is not sandboxed and can circumvent any protections Truffle or the JVM may provide, up to and including aborting the entire process.
21+
Native code is entirely unrestricted and can circumvent any security protections Truffle or the JVM may provide.
2222
Native data structures are not subject to the Java GC and the combination of them with Java data structures may lead to memory leaks.
2323
Native libraries generally cannot be loaded multiple times into the same process, and they may contain global state that cannot be safely reset.
2424
Thus, it is not possible to create multiple GraalPy contexts that access native modules within the same JVM.

graalpython/com.oracle.graal.python.test/src/tests/test_autopatch_capi.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,50 @@ def check_autopatched(source, expected):
5959

6060
def test_replace_field_access():
6161
check_autopatched(
62-
"""
63-
PyObject* type = obj->ob_type;
64-
PyObject* type = ((PyObject*)obj)->ob_type;
65-
const char* name = obj->ob_type->tp_name;
66-
foo = objs[0]->ob_type;
67-
return obj->ob_type;
68-
return (PyObject*)obj->ob_type;
69-
return ((PyObject*)obj)->ob_type;
70-
(PyObject*)function_call(a, b, c(0))->ob_type->ob_base;
71-
""",
72-
"""
73-
PyObject* type = Py_TYPE(obj);
74-
PyObject* type = Py_TYPE(((PyObject*)obj));
75-
const char* name = Py_TYPE(obj)->tp_name;
76-
foo = Py_TYPE(objs[0]);
77-
return Py_TYPE(obj);
78-
return (PyObject*)Py_TYPE(obj);
79-
return Py_TYPE(((PyObject*)obj));
80-
(PyObject*)Py_TYPE(function_call(a, b, c(0)))->ob_base;
81-
"""
62+
'PyObject* type = obj->ob_type;',
63+
'PyObject* type = Py_TYPE(obj);',
64+
)
65+
check_autopatched(
66+
'PyObject* type = ((PyObject*)obj)->ob_type;',
67+
'PyObject* type = Py_TYPE(((PyObject*)obj));',
68+
)
69+
check_autopatched(
70+
'const char* name = obj->ob_type->tp_name;',
71+
'const char* name = Py_TYPE(obj)->tp_name;',
72+
)
73+
check_autopatched(
74+
'foo = objs[0]->ob_type;',
75+
'foo = Py_TYPE(objs[0]);',
76+
)
77+
check_autopatched(
78+
'return obj->ob_type;',
79+
'return Py_TYPE(obj);',
80+
)
81+
check_autopatched(
82+
'return (PyObject*)obj->ob_type;',
83+
'return (PyObject*)Py_TYPE(obj);',
84+
)
85+
check_autopatched(
86+
'return ((PyObject*)obj)->ob_type;',
87+
'return Py_TYPE(((PyObject*)obj));',
88+
)
89+
check_autopatched(
90+
'(PyObject*)function_call(a, b, c(0))->ob_type->ob_base;',
91+
'(PyObject*)Py_TYPE(function_call(a, b, c(0)))->ob_base;',
92+
)
93+
check_autopatched(
94+
'''
95+
#if SOME_MACRO
96+
obj->ob_type->tp_free(self);
97+
#else
98+
obj->ob_type->tp_free(self);
99+
#endif
100+
''',
101+
'''
102+
#if SOME_MACRO
103+
Py_TYPE(obj)->tp_free(self);
104+
#else
105+
Py_TYPE(obj)->tp_free(self);
106+
#endif
107+
''',
82108
)

graalpython/lib-graalpython/_sysconfig.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ def _get_posix_vars():
102102
g['CFLAGS'] = ' '.join(cflags_default + [gnu_source])
103103
g['LDFLAGS'] = ""
104104
g['CCSHARED'] = fpic
105-
g['LDSHARED_LINUX'] = "%s -shared %s" % (get_toolchain('CC'), fpic)
106105
if darwin_native:
107-
g['LDSHARED'] = get_toolchain('CC') + " -bundle -undefined dynamic_lookup"
108106
g['LDFLAGS'] = "-bundle -undefined dynamic_lookup"
107+
ldshared_common = g['LDFLAGS']
109108
g['LIBPYTHON'] = ''
110109
elif win32_native:
111110
g['LDFLAGS'] = f"-L{__graalpython__.capi_home.replace(os.path.sep, '/')}"
112-
g['LDSHARED_WINDOWS'] = f"{g['LDSHARED_LINUX']} {g['LDFLAGS']}"
113-
g['LDSHARED'] = g['LDSHARED_WINDOWS']
111+
ldshared_common = f"-shared {fpic} {g['LDFLAGS']}"
114112
else:
115-
g['LDSHARED'] = g['LDSHARED_LINUX']
113+
ldshared_common = f"-shared {fpic}"
116114
g['LIBPYTHON'] = ''
115+
g['LDSHARED'] = f"{g['CC']} {ldshared_common}"
116+
g['LDCXXSHARED'] = f"{g['CXX']} {ldshared_common}"
117117
g['SOABI'] = so_abi
118118
g['EXT_SUFFIX'] = "." + so_abi + so_ext
119119
g['SHLIB_SUFFIX'] = so_ext

graalpython/lib-graalpython/modules/autopatch_capi.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def replace_field_access(contents, match, replacement, assignment):
6666
level = 0
6767

6868
def consume_whitespace_backwards(idx):
69-
while idx > 0 and contents[idx].isspace():
69+
while idx >= 0 and contents[idx].isspace():
7070
idx -= 1
7171
return idx
7272

@@ -85,7 +85,7 @@ def consume_whitespace_forward(idx):
8585
def consume_pairwise_backwards(idx, l, r):
8686
level = 1
8787
idx -= 1
88-
while level and idx:
88+
while level and idx >= 0:
8989
c = contents[idx]
9090
if c == l:
9191
level -= 1
@@ -95,31 +95,35 @@ def consume_pairwise_backwards(idx, l, r):
9595
return idx
9696

9797
def consume_identifier_backwards(idx):
98-
while (contents[idx].isidentifier() or contents[idx].isdigit()) and idx:
98+
while idx >= 0 and (contents[idx].isidentifier() or contents[idx].isdigit()):
9999
idx -= 1
100100
return idx
101101

102-
first = True
103-
while idx:
102+
allowed_tokens = {'()', '[]', '.', 'id'}
103+
while idx >= 0:
104104
c = contents[idx]
105-
if c == ')' and first:
105+
if '()' in allowed_tokens and c == ')':
106106
idx = consume_pairwise_backwards(idx, '(', ')')
107-
elif c == ']':
107+
allowed_tokens = {'[]', 'id'}
108+
elif '[]' in allowed_tokens and c == ']':
108109
idx = consume_pairwise_backwards(idx, '[', ']')
109-
elif c.isidentifier() or c.isdigit():
110+
allowed_tokens = {'[]', 'id'}
111+
elif 'id' in allowed_tokens and c.isidentifier() or c.isdigit():
110112
id_start = consume_identifier_backwards(idx)
111113
if contents[id_start + 1: idx + 1] == 'return':
112114
idx += 1
113115
break
114116
idx = id_start
115-
elif c == '.':
117+
allowed_tokens = {'.'}
118+
elif '.' in allowed_tokens and c == '.':
116119
idx -= 1
117-
elif c == '>' and idx > 1 and contents[idx - 1] == '-':
120+
allowed_tokens = {'[]', 'id'}
121+
elif '.' in allowed_tokens and c == '>' and idx > 1 and contents[idx - 1] == '-':
118122
idx -= 2
123+
allowed_tokens = {'[]', 'id'}
119124
else:
120125
idx += 1
121126
break
122-
first = False
123127
idx = consume_whitespace_backwards(idx)
124128

125129
receiver_start = consume_whitespace_forward(idx)

graalpython/lib-graalpython/patches/Cython/metadata.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[[rules]]
2-
version = '== 3.0.10'
2+
version = '>= 3.0.10, < 3.0.12'
33
patch = 'Cython-3.0.10.patch'
44
license = 'Apache-2.0'
55

graalpython/lib-graalpython/patches/atomicwrites/atomicwrites.patch

Lines changed: 0 additions & 12 deletions
This file was deleted.

graalpython/lib-graalpython/patches/atomicwrites/metadata.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[[rules]]
2+
# With pyo3 changes upstreamed, this patch is not needed in recent versions
3+
# This old version is still used in ray's tests
4+
install-priority = 0
25
version = '== 4.0.1'
36
patch = 'bcrypt-4.0.1.patch'
47
license = 'Apache-2.0'

graalpython/lib-graalpython/patches/charset_normalizer/charset_normalizer-2.0.12.patch

Lines changed: 0 additions & 28 deletions
This file was deleted.

graalpython/lib-graalpython/patches/charset_normalizer/charset_normalizer-2.1.1.patch

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)