Skip to content

Commit 8625802

Browse files
authored
gh-84461: Fix ctypes and test_ctypes on Emscripten (#94142)
- c_longlong and c_longdouble need experimental WASM bigint. - Skip tests that need threading - Define ``CTYPES_MAX_ARGCOUNT`` for Emscripten. libffi-emscripten 2022-06-23 supports up to 1000 args.
1 parent ab077d1 commit 8625802

File tree

9 files changed

+26
-9
lines changed

9 files changed

+26
-9
lines changed

Lib/test/test_code.py

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
check_impl_detail, requires_debug_ranges,
142142
gc_collect)
143143
from test.support.script_helper import assert_python_ok
144+
from test.support import threading_helper
144145
from opcode import opmap
145146
COPY_FREE_VARS = opmap['COPY_FREE_VARS']
146147

@@ -723,6 +724,7 @@ def test_get_set(self):
723724
self.assertEqual(extra.value, 300)
724725
del f
725726

727+
@threading_helper.requires_working_threading()
726728
def test_free_different_thread(self):
727729
# Freeing a code object on a different thread then
728730
# where the co_extra was set should be safe.

Lib/test/test_ctypes/test_as_parameter.py

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def callback(value):
122122
result = f(self.wrap(-10), self.wrap(cb))
123123
self.assertEqual(result, -18)
124124

125+
@need_symbol('c_longlong')
125126
def test_longlong_callbacks(self):
126127

127128
f = dll._testfunc_callback_q_qf

Lib/test/test_ctypes/test_callbacks.py

+3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ def test_long(self):
6565
def test_ulong(self):
6666
self.check_type(c_ulong, 42)
6767

68+
@need_symbol('c_longlong')
6869
def test_longlong(self):
6970
self.check_type(c_longlong, 42)
7071
self.check_type(c_longlong, -42)
7172

73+
@need_symbol('c_ulonglong')
7274
def test_ulonglong(self):
7375
self.check_type(c_ulonglong, 42)
7476

@@ -82,6 +84,7 @@ def test_double(self):
8284
self.check_type(c_double, 3.14)
8385
self.check_type(c_double, -3.14)
8486

87+
@need_symbol('c_longdouble')
8588
def test_longdouble(self):
8689
self.check_type(c_longdouble, 3.14)
8790
self.check_type(c_longdouble, -3.14)

Lib/test/test_ctypes/test_cfuncs.py

+6
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,28 @@ def test_ulong_plus(self):
111111
self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765)
112112
self.assertEqual(self.U(), 4294967295)
113113

114+
@need_symbol('c_longlong')
114115
def test_longlong(self):
115116
self._dll.tf_q.restype = c_longlong
116117
self._dll.tf_q.argtypes = (c_longlong, )
117118
self.assertEqual(self._dll.tf_q(-9223372036854775806), -3074457345618258602)
118119
self.assertEqual(self.S(), -9223372036854775806)
119120

121+
@need_symbol('c_longlong')
120122
def test_longlong_plus(self):
121123
self._dll.tf_bq.restype = c_longlong
122124
self._dll.tf_bq.argtypes = (c_byte, c_longlong)
123125
self.assertEqual(self._dll.tf_bq(0, -9223372036854775806), -3074457345618258602)
124126
self.assertEqual(self.S(), -9223372036854775806)
125127

128+
@need_symbol('c_ulonglong')
126129
def test_ulonglong(self):
127130
self._dll.tf_Q.restype = c_ulonglong
128131
self._dll.tf_Q.argtypes = (c_ulonglong, )
129132
self.assertEqual(self._dll.tf_Q(18446744073709551615), 6148914691236517205)
130133
self.assertEqual(self.U(), 18446744073709551615)
131134

135+
@need_symbol('c_ulonglong')
132136
def test_ulonglong_plus(self):
133137
self._dll.tf_bQ.restype = c_ulonglong
134138
self._dll.tf_bQ.argtypes = (c_byte, c_ulonglong)
@@ -159,12 +163,14 @@ def test_double_plus(self):
159163
self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
160164
self.assertEqual(self.S(), 42)
161165

166+
@need_symbol('c_longdouble')
162167
def test_longdouble(self):
163168
self._dll.tf_D.restype = c_longdouble
164169
self._dll.tf_D.argtypes = (c_longdouble,)
165170
self.assertEqual(self._dll.tf_D(42.), 14.)
166171
self.assertEqual(self.S(), 42)
167172

173+
@need_symbol('c_longdouble')
168174
def test_longdouble_plus(self):
169175
self._dll.tf_bD.restype = c_longdouble
170176
self._dll.tf_bD.argtypes = (c_byte, c_longdouble)

Lib/test/test_ctypes/test_functions.py

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def test_doubleresult(self):
128128
self.assertEqual(result, -21)
129129
self.assertEqual(type(result), float)
130130

131+
@need_symbol('c_longdouble')
131132
def test_longdoubleresult(self):
132133
f = dll._testfunc_D_bhilfD
133134
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]

Modules/_ctypes/ctypes.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* to avoid allocating a massive buffer on the stack.
2020
*/
2121
#ifndef CTYPES_MAX_ARGCOUNT
22-
#define CTYPES_MAX_ARGCOUNT 1024
22+
#ifdef __EMSCRIPTEN__
23+
#define CTYPES_MAX_ARGCOUNT 1000
24+
#else
25+
#define CTYPES_MAX_ARGCOUNT 1024
26+
#endif
2327
#endif
2428

2529
typedef struct tagPyCArgObject PyCArgObject;

Tools/wasm/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ functions.
173173
[bpo-46390](https://bugs.python.org/issue46390).
174174
- Python's object allocator ``obmalloc`` is disabled by default.
175175
- ``ensurepip`` is not available.
176+
- Some ``ctypes`` features like ``c_longlong`` and ``c_longdouble`` may need
177+
NodeJS option ``--experimental-wasm-bigint``.
176178

177179
## wasm32-emscripten in browsers
178180

configure

+3-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+3-3
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,10 @@ if test -z "$HOSTRUNNER"
14861486
then
14871487
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
14881488
[Emscripten/node*], [
1489+
# bigint for ctypes c_longlong, c_longdouble
1490+
HOSTRUNNER="node --experimental-wasm-bigint"
14891491
AS_VAR_IF([enable_wasm_pthreads], [yes], [
1490-
HOSTRUNNER='node --experimental-wasm-threads --experimental-wasm-bulk-memory'
1491-
], [
1492-
HOSTRUNNER='node'
1492+
HOSTRUNNER="$HOSTRUNNER --experimental-wasm-threads --experimental-wasm-bulk-memory"
14931493
])
14941494
],
14951495
dnl TODO: support other WASI runtimes

0 commit comments

Comments
 (0)