Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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