10000 BUG: Fix build of third-party extensions with Py_LIMITED_API by charris · Pull Request #20843 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix build of third-party extensions with Py_LIMITED_API #20843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion numpy/core/include/numpy/ufuncobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ typedef struct _tagPyUFuncObject {
* but this was never implemented. (This is also why the above
* selector is called the "legacy" selector.)
*/
vectorcallfunc vectorcall;
#ifndef Py_LIMITED_API
vectorcallfunc vectorcall;
#else
void *vectorcall;
#endif

/* Was previously the `PyUFunc_MaskedInnerLoopSelectionFunc` */
void *_always_null_previously_masked_innerloop_selector;
Expand Down
17 changes: 17 additions & 0 deletions numpy/core/tests/examples/limited_api/limited_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#define Py_LIMITED_API 0x03060000

#include <Python.h>
#include <numpy/arrayobject.h>
#include <numpy/ufuncobject.h>

static PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "limited_api"
};

PyMODINIT_FUNC PyInit_limited_api(void)
{
import_array();
import_umath();
return PyModule_Create(&moduledef);
}
22 changes: 22 additions & 0 deletions numpy/core/tests/examples/limited_api/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Build an example package using the limited Python C API.
"""

import numpy as np
from setuptools import setup, Extension
import os

macros = [("NPY_NO_DEPRECATED_API", 0), ("Py_LIMITED_API", "0x03060000")]

limited_api = Extension(
"limited_api",
sources=[os.path.join('.', "limited_api.c")],
include_dirs=[np.get_include()],
define_macros=macros,
)< 8000 /td>

extensions = [limited_api]

setup(
ext_modules=extensions
)
2 changes: 1 addition & 1 deletion numpy/core/tests/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def install_temp(request, tmp_path):
# Based in part on test_cython from random.tests.test_extending

here = os.path.dirname(__file__)
ext_dir = os.path.join(here, "examples")
ext_dir = os.path.join(here, "examples", "cython")

cytest = str(tmp_path / "cytest")

Expand Down
41 changes: 41 additions & 0 deletions numpy/core/tests/test_limited_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import shutil
import subprocess
import sys
import sysconfig
import pytest


@pytest.mark.xfail(
sysconfig.get_config_var("Py_DEBUG"),
reason=(
"Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, "
"and Py_REF_DEBUG"
),
)
def test_limited_api(tmp_path):
"""Test building a third-party C extension with the limited API."""
# Based in part on test_cython from random.tests.test_extending

here = os.path.dirname(__file__)
ext_dir = os.path.join(here, "examples", "limited_api")

cytest = str(tmp_path / "limited_api")

shutil.copytree(ext_dir, cytest)
# build the examples and "install" them into a temporary directory

install_log = str(tmp_path / "tmp_install_log.txt")
subprocess.check_call(
[
sys.executable,
"setup.py",
"build",
"install",
"--prefix", str(tmp_path / "installdir"),
"--single-version-externally-managed",
"--record",
install_log,
],
cwd=cytest,
)
0