Skip to content

Commit 2e31962

Browse files
committed
smoketests: add a test for the limited API
This builds the test module twice, once normally and once for the limited API. In the later case we check at runtime if libpython3.dll is loaded, to make sure we are actually linked against it and not against libpython3.x.dll. This depends on both: * #148 * msys2/MINGW-packages#18232
1 parent 8aeb46d commit 2e31962

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

mingw_smoketests.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ def test_site(self):
265265
self.assertEqual(len(site.getsitepackages()), 1)
266266

267267
def test_c_ext_build(self):
268+
self._test_c_ext_build(False)
269+
270+
def test_c_ext_build_limited_api(self):
271+
self._test_c_ext_build(True)
272+
273+
def _test_c_ext_build(self, py_limited_api):
268274
import tempfile
269275
import sys
270276
import subprocess
@@ -279,32 +285,47 @@ def test_c_ext_build(self):
279285
"""\
280286
from setuptools import setup, Extension
281287
288+
if %(py_limited_api)s:
289+
ext = Extension(
290+
'cwrapper',
291+
py_limited_api=True,
292+
define_macros=[('Py_LIMITED_API', '0x03060000')],
293+
sources=['cwrapper.c'])
294+
else:
295+
ext = Extension(
296+
'cwrapper',
297+
sources=['cwrapper.c'])
298+
282299
setup(
283300
name='cwrapper',
284301
version='1.0',
285-
ext_modules=[
286-
Extension(
287-
'cwrapper',
288-
sources=['cwrapper.c']),
289-
],
302+
ext_modules=[ext],
290303
)
291-
"""
304+
""" % {"py_limited_api": py_limited_api}
292305
)
293306
)
307+
294308
with Path(tmppro, "cwrapper.c").open("w") as f:
295309
f.write(
296310
textwrap.dedent(
297311
"""\
298312
#include <Python.h>
313+
#include <windows.h>
299314
static PyObject *
300315
helloworld(PyObject *self, PyObject *args)
301316
{
302317
printf("Hello World\\n");
303318
Py_RETURN_NONE;
304319
}
320+
static PyObject *
321+
islimited(PyObject *self, PyObject *args)
322+
{
323+
return PyBool_FromLong(GetModuleHandleA("libpython3.dll") != NULL);
324+
}
305325
static PyMethodDef
306326
myMethods[] = {
307327
{ "helloworld", helloworld, METH_NOARGS, "Prints Hello World" },
328+
{ "islimited", islimited, METH_NOARGS, "Returns True if the limited API is used" },
308329
{ NULL, NULL, 0, NULL }
309330
};
310331
static struct PyModuleDef cwrapper = {
@@ -323,6 +344,16 @@ def test_c_ext_build(self):
323344
"""
324345
)
325346
)
347+
348+
if py_limited_api:
349+
with Path(tmppro, "setup.cfg").open("w") as f:
350+
f.write(textwrap.dedent(
351+
"""\
352+
[bdist_wheel]
353+
py_limited_api=cp36
354+
"""
355+
))
356+
326357
subprocess.check_call(
327358
[sys.executable, "-c", "import struct"],
328359
)
@@ -347,6 +378,10 @@ def test_c_ext_build(self):
347378
subprocess.check_call(
348379
[sys.executable, "-c", "import cwrapper"],
349380
)
381+
# Make sure the resulting extension uses the limited API, or not
382+
subprocess.check_call(
383+
[sys.executable, "-c", f"import cwrapper; assert cwrapper.islimited() == {py_limited_api}"],
384+
)
350385

351386

352387

0 commit comments

Comments
 (0)