Skip to content

Commit

Permalink
Use unique symbol for ufunc API
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Jul 16, 2024
1 parent b6edc01 commit bab245d
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 95 deletions.
9 changes: 4 additions & 5 deletions asciidtype/asciidtype/src/asciidtype_main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
Expand All @@ -21,6 +23,7 @@ PyMODINIT_FUNC
PyInit__asciidtype_main(void)
{
import_array();
import_umath();

PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
Expand All @@ -47,14 +50,10 @@ PyInit__asciidtype_main(void)
goto error;
}

PyObject *numpy = init_ufuncs();

if (numpy == NULL) {
if (init_ufuncs() == -1) {
goto error;
}

Py_DECREF(numpy);

return m;

error:
Expand Down
2 changes: 2 additions & 0 deletions asciidtype/asciidtype/src/casts.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"
Expand Down
2 changes: 2 additions & 0 deletions asciidtype/asciidtype/src/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
// clang-format on

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
Expand Down
12 changes: 6 additions & 6 deletions asciidtype/asciidtype/src/umath.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
Expand Down Expand Up @@ -219,14 +221,12 @@ init_equal_ufunc(PyObject *numpy)
return 0;
}

PyObject *
int
init_ufuncs(void)
{
import_umath();

PyObject *numpy = PyImport_ImportModule("numpy");
if (numpy == NULL) {
return NULL;
return -1;
}

if (init_add_ufunc(numpy) < 0) {
Expand All @@ -237,9 +237,9 @@ init_ufuncs(void)
goto error;
}

return numpy;
return 0;

error:
Py_DECREF(numpy);
return NULL;
return -1;
}
2 changes: 1 addition & 1 deletion asciidtype/asciidtype/src/umath.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NPY_UFUNC_H
#define _NPY_UFUNC_H

PyObject *
int
init_ufuncs(void);

#endif /*_NPY_UFUNC_H */
2 changes: 2 additions & 0 deletions metadatadtype/metadatadtype/src/casts.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"
Expand Down
7 changes: 7 additions & 0 deletions metadatadtype/metadatadtype/src/dtype.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// clang-format off
#include <Python.h>
#include "structmember.h"
// clang-format on

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"
Expand Down
5 changes: 0 additions & 5 deletions metadatadtype/metadatadtype/src/dtype.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#ifndef _NPY_DTYPE_H
#define _NPY_DTYPE_H

// clang-format off
#include <Python.h>
#include "structmember.h"
// clang-format on

typedef struct {
PyArray_Descr base;
PyObject *metadata;
Expand Down
5 changes: 4 additions & 1 deletion metadatadtype/metadatadtype/src/metadatadtype_main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "umath.h"
Expand All @@ -20,6 +22,7 @@ PyMODINIT_FUNC
PyInit__metadatadtype_main(void)
{
import_array();
import_umath();

PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
Expand Down Expand Up @@ -47,7 +50,7 @@ PyInit__metadatadtype_main(void)
goto error;
}

if (init_ufuncs(m) == NULL) {
if (init_ufuncs() == -1) {
goto error;
}

Expand Down
13 changes: 6 additions & 7 deletions metadatadtype/metadatadtype/src/umath.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
Expand Down Expand Up @@ -103,11 +105,9 @@ add_wrapping_loop(const char *ufunc_name, PyArray_DTypeMeta **dtypes,
return res;
}

PyObject *
init_ufuncs(PyObject *module)
int
init_ufuncs(void)
{
import_umath();

PyArray_DTypeMeta *binary_orig_dtypes[3] = {&MetadataDType, &MetadataDType,
&MetadataDType};
PyArray_DTypeMeta *binary_wrapped_dtypes[3] = {
Expand All @@ -126,9 +126,8 @@ init_ufuncs(PyObject *module)
goto error;
}

return module;
return 0;
error:

Py_DECREF(module);
return NULL;
return -1;
}
4 changes: 2 additions & 2 deletions metadatadtype/metadatadtype/src/umath.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NPY_UFUNC_H
#define _NPY_UFUNC_H

PyObject *
init_ufuncs(PyObject *module);
int
init_ufuncs(void);

#endif /*_NPY_UFUNC_H */
2 changes: 2 additions & 0 deletions mpfdtype/mpfdtype/src/casts.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC

extern "C" {
#include <Python.h>
Expand Down
2 changes: 2 additions & 0 deletions mpfdtype/mpfdtype/src/dtype.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/ndarraytypes.h"
#include "numpy/dtype_api.h"
Expand Down
13 changes: 5 additions & 8 deletions mpfdtype/mpfdtype/src/mpfdtype_main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
Expand All @@ -19,9 +21,8 @@ static struct PyModuleDef moduledef = {
PyMODINIT_FUNC
PyInit__mpfdtype_main(void)
{
if (_import_array() < 0) {
return NULL;
}
import_array();
import_umath();

PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
Expand All @@ -44,14 +45,10 @@ PyInit__mpfdtype_main(void)
goto error;
}

PyObject *numpy = init_mpf_umath();

if (numpy == NULL) {
if (init_mpf_umath() == -1) {
goto error;
}

Py_DECREF(numpy);

if (init_terrible_hacks() < 0) {
goto error;
}
Expand Down
13 changes: 7 additions & 6 deletions mpfdtype/mpfdtype/src/umath.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

#include "scalar.h"
#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC

extern "C" {
#include <Python.h>
Expand Down Expand Up @@ -592,17 +594,15 @@ init_comps(PyObject *numpy)
/*
* Function that adds our multiply loop to NumPy's multiply ufunc.
*/
PyObject *
int
init_mpf_umath(void)
{
import_umath();

/*
* Get the multiply ufunc:
*/
PyObject *numpy = PyImport_ImportModule("numpy");
if (numpy == NULL) {
return NULL;
return -1;
}

if (init_unary_ops(numpy) < 0) {
Expand All @@ -615,10 +615,11 @@ init_mpf_umath(void)
goto err;
}

return numpy;
Py_DECREF(numpy);
return 0;

err:
Py_DECREF(numpy);
return NULL;
return -1;
}

2 changes: 1 addition & 1 deletion mpfdtype/mpfdtype/src/umath.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern "C" {
#endif

PyObject *
int
init_mpf_umath(void);

#ifdef __cplusplus
Expand Down
6 changes: 1 addition & 5 deletions quaddtype/quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ PyInit__quaddtype_main(void)
goto error;
}

PyObject *numpy = init_multiply_ufunc();

if (numpy == NULL) {
if (init_multiply_ufunc() == -1) {
PyErr_SetString(PyExc_TypeError, "Failed to initialize the quadscalar multiply ufunc.");
goto error;
}

Py_DECREF(numpy);

return m;

error:
Expand Down
Loading

0 comments on commit bab245d

Please sign in to comment.