Skip to content
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

MAINT: Check numpy version in C-API #1

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion numpy/_core/code_generators/generate_numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@

%s

#include "numpy/utils.h"

#if !defined(NO_IMPORT_ARRAY) && !defined(NO_IMPORT)
static int
_import_array(void)
{
int st;
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
char numpy_major_version = get_installed_numpy_major_version();
PyObject *numpy = NULL;
if (numpy_major_version == '2') {
numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
} else {
numpy = PyImport_ImportModule("numpy.core._multiarray_umath");
}
PyObject *c_api = NULL;

if (numpy == NULL) {
Expand Down
10 changes: 9 additions & 1 deletion numpy/_core/code_generators/generate_ufunc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@

%s

#include "numpy/utils.h"

static inline int
_import_umath(void)
{
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
char numpy_major_version = get_installed_numpy_major_version();
PyObject *numpy = NULL;
if (numpy_major_version == '2') {
numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
} else {
numpy = PyImport_ImportModule("numpy.core._multiarray_umath");
}
PyObject *c_api = NULL;

if (numpy == NULL) {
Expand Down
9 changes: 8 additions & 1 deletion numpy/_core/include/numpy/experimental_dtype_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include <Python.h>
#include "ndarraytypes.h"
#include "_dtype_api.h"
#include "utils.h"

/*
* The contents of PyArrayMethodObject are currently opaque (is there a way
Expand Down Expand Up @@ -353,7 +354,13 @@ import_experimental_dtype_api(int version)
return 0;
}

PyObject *multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
char numpy_major_version = get_installed_numpy_major_version();
PyObject *multiarray = NULL;
if (numpy_major_version == '2') {
multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
} else {
multiarray = PyImport_ImportModule("numpy.core._multiarray_umath");
}
if (multiarray == NULL) {
return -1;
}
Expand Down
13 changes: 13 additions & 0 deletions numpy/_core/include/numpy/utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_
#define NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_

#include <Python.h>

#ifndef __COMP_NPY_UNUSED
#if defined(__GNUC__)
#define __COMP_NPY_UNUSED __attribute__ ((__unused__))
Expand Down Expand Up @@ -34,4 +36,15 @@
#define NPY_CAT_(a, b) NPY_CAT__(a, b)
#define NPY_CAT(a, b) NPY_CAT_(a, b)

static char get_installed_numpy_major_version()
{
PyObject *numpy = PyImport_ImportModule("numpy");
PyObject *version_obj = PyObject_GetAttrString(numpy, "__version__");
Py_DECREF(numpy);
char major_version = PyUnicode_ReadChar(version_obj, 0);
Py_DECREF(version_obj);

return major_version;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this to get the major version is a bit overloaded IMO, since we also have it available as a C function in the API table.
So, I think this helper should just import _multiarray_umath, that will also simplify all other code anyway and separate the concern of what the major version is.
Maybe PyObject_HasAttr(numpy, "_core") is enough for a check?

That said, we can probably probably add such a helper, although I think it should also be called _npy_... to more clearly signal that it is off-limits and a NumPy implementation detail.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, using PyObject_HasAttr(numpy, "_core") looks way simpler and it does the same job.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I am not sure it was clear: I would suggest if we have a helper, it should be _npy_import_numpy_multiarray_umath().

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seberg How about _npy_import_from_numpy_core(submodule_name)?

In autogenerated headers _multiarray_umath is imported, but there's also _internal being imported from _core in src/common/npy_ctypes.h. This header is defined as a dependency for libnpymath.a so it's sourcecode ends up in downstream library (if I understand it correctly).

(A downstream lib could use libnpymath.a when compiling and then run with NumPy 1.x - this could cause import error on numpy._core._internal).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header is defined as a dependency for libnpymath.a so it's

Sorry, I am not seeing/understanding this. Those should be private headers, and even if the function exposed there isn't used in libmpymath.a? Where do you see them being a dependency?

I would be surprised to find any use of this in public API accidentally or not. But, it wouldn't be the first surprise like this :).

Copy link
Owner Author

@mtsokol mtsokol Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought common/npy_ctypes.h is included here https://github.com/numpy/numpy/blob/d676a1fe2d495f9d8a86103644bed141c2e69787/numpy/core/meson.build#L544, but it only defines where to search for header files, right?
Then sorry for confusion!

Copy link
Owner Author

@mtsokol mtsokol Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I finished refactoring - I ended up with a simpler version.
For some reason I couldn't import numpy in this initialization function (PyImport_ImportModule("numpy") returns null, maybe due to some a circular import?)


#endif /* NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_ */
Loading