Skip to content

Commit 6fe5cf9

Browse files
committed
Make import_numpy a function
1 parent 4a672a7 commit 6fe5cf9

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

docs/source/basic_usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Example 1: Use an algorithm of the C++ library on a numpy array inplace
2929
3030
PYBIND11_PLUGIN(xtensor_python_test)
3131
{
32-
import_numpy(); //this is actually a macro
32+
xt::import_numpy();
3333
pybind11::module m("xtensor_python_test", "Test module for xtensor python bindings");
3434
3535
m.def("sum_of_sines", sum_of_sines,
@@ -80,7 +80,7 @@ Example 2: Create a universal function from a C++ scalar function
8080
8181
PYBIND11_PLUGIN(xtensor_python_test)
8282
{
83-
import_numpy();
83+
xt::import_numpy();
8484
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
8585
8686
m.def("vectorized_func", xt::pyvectorize(scalar_func), "");

docs/source/numpy_capi.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ When writing an extension module that is self-contained in a single file, its au
1717
points:
1818

1919
- ``FORCE_IMPORT_ARRAY`` must be defined before including any header of ``xtensor-python``.
20-
- ``import_numpy()`` must be called in the function initializing the module.
20+
- ``xt::import_numpy()`` must be called in the function initializing the module.
2121

2222
Thus the basic skeleton of the module looks like:
2323

@@ -29,7 +29,7 @@ Thus the basic skeleton of the module looks like:
2929
3030
PYBIND11_PLUGIN(plugin_name)
3131
{
32-
import_numpy();
32+
xt::import_numpy();
3333
pybind11::module m(//...
3434
//...
3535
return m.ptr();

include/xtensor-python/pycontainer.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#ifndef FORCE_IMPORT_ARRAY
2121
#define NO_IMPORT_ARRAY
22+
static int _import_array() { return 0; };
2223
#endif
2324
#ifndef PY_ARRAY_UNIQUE_SYMBOL
2425
#define PY_ARRAY_UNIQUE_SYMBOL xtensor_python_ARRAY_API
@@ -29,11 +30,21 @@
2930

3031
#include "xtensor/xcontainer.hpp"
3132

32-
#define import_numpy() { if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); } }
33-
3433
namespace xt
3534
{
3635

36+
/**
37+
* Import the numpy Python module.
38+
*/
39+
inline void import_numpy()
40+
{
41+
if (_import_array() < 0)
42+
{
43+
PyErr_Print();
44+
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
45+
}
46+
}
47+
3748
/**
3849
* @class pycontainer
3950
* @brief Base class for xtensor containers wrapping numpy arryays.

test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char* argv[])
2020
{
2121
// Initialize all the things (google-test and Python interpreter)
2222
Py_Initialize();
23-
import_numpy();
23+
xt::import_numpy();
2424
::testing::InitGoogleTest(&argc, argv);
2525

2626
// Run test suite

test_python/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int add(int i, int j)
5151

5252
PYBIND11_PLUGIN(xtensor_python_test)
5353
{
54-
import_numpy();
54+
xt::import_numpy();
5555

5656
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
5757

0 commit comments

Comments
 (0)