Skip to content

Commit

Permalink
Merge pull request #276 from larsoner/np
Browse files Browse the repository at this point in the history
Fixes for build/use of PyEx extensions also on NumPy 2.0, with backwards compatibility to NumPy1.x runtimes.
  • Loading branch information
kleinerm authored Aug 4, 2024
2 parents bba06e7 + 5336a7e commit b20942f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ void mxSetField(PyObject* pStructOuter, int index, const char* fieldName, PyObje
{
if (psych_refcount_debug && pStructInner)
printf("PTB-DEBUG: In mxSetField: refcount of external object %p at enter is %li. %s\n",
pStructInner, PyArray_REFCOUNT(pStructInner),
(PyArray_REFCOUNT(pStructInner) > 1) ? "MIGHT leak if caller does not take care." : "");
pStructInner, Py_REFCNT(pStructInner),
(Py_REFCNT(pStructInner) > 1) ? "MIGHT leak if caller does not take care." : "");

if (!mxIsStruct(pStructOuter)) {
Py_XDECREF(pStructInner);
Expand Down Expand Up @@ -1123,9 +1123,9 @@ PyObject* PsychScriptingGluePythonDispatch(PyObject* self, PyObject* args)
// and just need to return return arguments and clean up:
if (psych_refcount_debug) {
for (i = 0; i < MAX_OUTPUT_ARGS; i++) {
if (plhsGLUE[recLevel][i] && (PyArray_REFCOUNT(plhsGLUE[recLevel][i]) >= psych_refcount_debug))
if (plhsGLUE[recLevel][i] && (Py_REFCNT(plhsGLUE[recLevel][i]) >= psych_refcount_debug))
printf("PTB-DEBUG: At non-error exit of PsychScriptingGluePythonDispatch: Refcount of plhsGLUE[recLevel %i][arg %i] = %li.\n",
recLevel, i, PyArray_REFCOUNT(plhsGLUE[recLevel][i]));
recLevel, i, Py_REFCNT(plhsGLUE[recLevel][i]));
}
}

Expand Down Expand Up @@ -1202,9 +1202,9 @@ PyObject* PsychScriptingGluePythonDispatch(PyObject* self, PyObject* args)
// Release "orphaned" output arguments that haven't been returned to the interpreter,
// e.g., because of a PythonFunctionCleanup - error return:
for (i = 0; i < MAX_OUTPUT_ARGS; i++) {
if (psych_refcount_debug && plhsGLUE[recLevel][i] && (PyArray_REFCOUNT(plhsGLUE[recLevel][i]) >= psych_refcount_debug))
if (psych_refcount_debug && plhsGLUE[recLevel][i] && (Py_REFCNT(plhsGLUE[recLevel][i]) >= psych_refcount_debug))
printf("PTB-DEBUG: Orphaned output argument at cleanup: Refcount of plhsGLUE[recLevel %i][arg %i] = %li --> unref --> %li.\n",
recLevel, i, PyArray_REFCOUNT(plhsGLUE[recLevel][i]), PyArray_REFCOUNT(plhsGLUE[recLevel][i]) - 1);
recLevel, i, Py_REFCNT(plhsGLUE[recLevel][i]), Py_REFCNT(plhsGLUE[recLevel][i]) - 1);

Py_XDECREF(plhsGLUE[recLevel][i]);
plhsGLUE[recLevel][i] = NULL;
Expand Down Expand Up @@ -1254,9 +1254,9 @@ static PyObject* PsychPyArgGet(int position)
{
PyObject *ret = prhsGLUE[recLevel][position];

if (psych_refcount_debug && (PyArray_REFCOUNT(prhsGLUE[recLevel][position]) >= psych_refcount_debug))
if (psych_refcount_debug && (Py_REFCNT(prhsGLUE[recLevel][position]) >= psych_refcount_debug))
printf("PTB-DEBUG:%s:PsychPyArgGet: Before convert: Refcount of prhsGLUE[recLevel %i][arg %i] = %li.\n",
PsychGetFunctionName(), recLevel, position, PyArray_REFCOUNT(prhsGLUE[recLevel][position]));
PsychGetFunctionName(), recLevel, position, Py_REFCNT(prhsGLUE[recLevel][position]));

// Does this input argument need conversion to a NumPy array of suitable format?
if (prhsNeedsConversion[recLevel][position]) {
Expand All @@ -1279,7 +1279,7 @@ static PyObject* PsychPyArgGet(int position)
if (DEBUG_PTBPYTHONGLUE || psych_refcount_debug)
printf("PTB-DEBUG:%s:PsychPyArgGet: Arg %i: Conversion to NumPy array of %s triggered [refcount now %li]: %s\n",
PsychGetFunctionName(),
position, use_C_memory_layout[recLevel] ? "C layout" : "Fortran layout", PyArray_REFCOUNT(ret),
position, use_C_memory_layout[recLevel] ? "C layout" : "Fortran layout", Py_REFCNT(ret),
(ret == prhsGLUE[recLevel][position]) ? "No-Op passthrough." : "New object.");

// Now that the new ret is a NumPy array in the wanted final format,
Expand All @@ -1296,9 +1296,9 @@ static PyObject* PsychPyArgGet(int position)
// by dropping its refcount by one to zero.
}

if (psych_refcount_debug && (PyArray_REFCOUNT(prhsGLUE[recLevel][position]) >= psych_refcount_debug))
if (psych_refcount_debug && (Py_REFCNT(prhsGLUE[recLevel][position]) >= psych_refcount_debug))
printf("PTB-DEBUG:%s:PsychPyArgGet: After convert: Refcount of prhsGLUE[recLevel %i][arg %i] = %li.\n",
PsychGetFunctionName(), recLevel, position, PyArray_REFCOUNT(prhsGLUE[recLevel][position]));
PsychGetFunctionName(), recLevel, position, Py_REFCNT(prhsGLUE[recLevel][position]));

return(ret);
}
Expand Down Expand Up @@ -2980,8 +2980,8 @@ int PsychRuntimePutVariable(const char* workspace, const char* variable, PsychGe

if (psych_refcount_debug && pcontent)
printf("PTB-DEBUG: In mxSetField: refcount of external object %p at enter is %li. %s\n",
pcontent, PyArray_REFCOUNT(pcontent),
(PyArray_REFCOUNT(pcontent) > 1) ? "MIGHT leak if caller does not take care." : "");
pcontent, Py_REFCNT(pcontent),
(Py_REFCNT(pcontent) > 1) ? "MIGHT leak if caller does not take care." : "");

// Drop one reference, so the function steals a reference to pcontent and the
// calling client is no longer responsible for managing it, regardless of success
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ requires = [
"oldest-supported-numpy; python_version<'3.7'",
# we need to build with at least 1.16.5 for a limited API bugfix
"numpy==1.16.5; python_version=='3.7'",
"oldest-supported-numpy; python_version>='3.8'"
"oldest-supported-numpy; python_version=='3.8'",
"numpy>=2,<3; python_version>='3.9'",
]
build-backend = "setuptools.build_meta"
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,13 @@ def get_basesources(name, osname):
py_limited_api = py_limited_api,
)
ext_modules.append(IOPort)
description = 'Pieces of Psychtoolbox-3 ported to CPython.'

setup (name = 'psychtoolbox',
version = version,
description = 'Pieces of Psychtoolbox-3 ported to CPython.',
long_description = 'Pieces of Psychtoolbox-3 ported to CPython.',
description = description,
long_description = description,
long_description_content_type = 'text/x-rst',
author = 'Mario Kleiner',
author_email = '[email protected]',
url = 'http://psychtoolbox.org',
Expand Down

0 comments on commit b20942f

Please sign in to comment.