Skip to content

Commit

Permalink
smoketests: add a test for the limited API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lazka committed Aug 21, 2023
1 parent 95b1546 commit 68fc140
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions mingw_smoketests.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ def test_site(self):
self.assertEqual(len(site.getsitepackages()), 1)

def test_c_ext_build(self):
self._test_c_ext_build(False)

def test_c_ext_build_limited_api(self):
self._test_c_ext_build(True)

def _test_c_ext_build(self, py_limited_api):
import tempfile
import sys
import subprocess
Expand All @@ -279,32 +285,47 @@ def test_c_ext_build(self):
"""\
from setuptools import setup, Extension
if %(py_limited_api)s:
ext = Extension(
'cwrapper',
py_limited_api=True,
define_macros=[('Py_LIMITED_API', '0x03060000')],
sources=['cwrapper.c'])
else:
ext = Extension(
'cwrapper',
sources=['cwrapper.c'])
setup(
name='cwrapper',
version='1.0',
ext_modules=[
Extension(
'cwrapper',
sources=['cwrapper.c']),
],
ext_modules=[ext],
)
"""
""" % {"py_limited_api": py_limited_api}
)
)

with Path(tmppro, "cwrapper.c").open("w") as f:
f.write(
textwrap.dedent(
"""\
#include <Python.h>
#include <windows.h>
static PyObject *
helloworld(PyObject *self, PyObject *args)
{
printf("Hello World\\n");
Py_RETURN_NONE;
}
static PyObject *
islimited(PyObject *self, PyObject *args)
{
return PyBool_FromLong(GetModuleHandleA("libpython3.dll") != NULL);
}
static PyMethodDef
myMethods[] = {
{ "helloworld", helloworld, METH_NOARGS, "Prints Hello World" },
{ "islimited", islimited, METH_NOARGS, "Returns True if the limited API is used" },
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef cwrapper = {
Expand All @@ -323,6 +344,16 @@ def test_c_ext_build(self):
"""
)
)

if py_limited_api:
with Path(tmppro, "setup.cfg").open("w") as f:
f.write(textwrap.dedent(
"""\
[bdist_wheel]
py_limited_api=cp36
"""
))

subprocess.check_call(
[sys.executable, "-c", "import struct"],
)
Expand All @@ -347,6 +378,10 @@ def test_c_ext_build(self):
subprocess.check_call(
[sys.executable, "-c", "import cwrapper"],
)
# Make sure the resulting extension uses the limited API, or not
subprocess.check_call(
[sys.executable, "-c", f"import cwrapper; assert cwrapper.islimited() == {py_limited_api}"],
)



Expand Down

0 comments on commit 68fc140

Please sign in to comment.