Skip to content

Commit dd79572

Browse files
authored
Merge pull request #69 from SylvainCorlay/import-numpy-function
Make import_numpy a function
2 parents 4a672a7 + ab133d8 commit dd79572

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
#include "xtensor/xcontainer.hpp"
3131

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

35+
inline void import_numpy();
36+
3737
/**
3838
* @class pycontainer
3939
* @brief Base class for xtensor containers wrapping numpy arryays.
@@ -242,6 +242,19 @@ namespace xt
242242
*static_cast<derived_type*>(this) = std::move(tmp);
243243
}
244244

245+
/**
246+
* Import the numpy Python module.
247+
*/
248+
inline void import_numpy()
249+
{
250+
#ifdef FORCE_IMPORT_ARRAY
251+
if (_import_array() < 0)
252+
{
253+
PyErr_Print();
254+
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
255+
}
256+
#endif
257+
}
245258
}
246259

247260
#endif

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)