|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// DuckDB |
| 3 | +// |
| 4 | +// numpy_array.cpp |
| 5 | +// |
| 6 | +// Out-of-line definitions for the NumpyArray facade (numpy_array.hpp). This is the |
| 7 | +// ONLY translation unit that uses the numpy C API, so it does not need |
| 8 | +// PY_ARRAY_UNIQUE_SYMBOL / NO_IMPORT_ARRAY (those coordinate the C-API function |
| 9 | +// pointer table across multiple TUs). |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 13 | +#include <numpy/arrayobject.h> |
| 14 | + |
| 15 | +#include "duckdb_python/numpy/numpy_array.hpp" |
| 16 | + |
| 17 | +#include <stdexcept> |
| 18 | +#include <unordered_map> |
| 19 | + |
| 20 | +namespace duckdb { |
| 21 | +namespace numpy_internal { |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +//! Lazy, guarded one-time init of the numpy C-API function pointer table. numpy is always |
| 26 | +//! already imported by the time we allocate a result array, so import_array should succeed; |
| 27 | +//! if it does not, the returned value is false and the caller raises. Runs exactly once |
| 28 | +//! (function-local static initializer, GIL held on the result path). |
| 29 | +bool EnsureNumpyCApi() { |
| 30 | + static bool ok = []() -> bool { |
| 31 | + // import_array1(ret) expands to `return ret;` on failure, so wrap it in a lambda that |
| 32 | + // returns int and surface success via the return value. |
| 33 | + auto do_import = []() -> int { |
| 34 | + import_array1(-1); |
| 35 | + return 0; |
| 36 | + }; |
| 37 | + return do_import() == 0; |
| 38 | + }(); |
| 39 | + return ok; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +nb::object NumpyEmpty(idx_t count, const string &dtype) { |
| 45 | + // Process-lifetime cache of parsed np.dtype objects, keyed by dtype string. The parse is |
| 46 | + // otherwise repeated per call; a LIST/ARRAY column allocates one array per row. Leaked on |
| 47 | + // purpose (numpy is never unloaded; no Python destructor runs after finalization). Only ever |
| 48 | + // touched on the single-threaded, GIL-held result path. |
| 49 | + static auto &dtype_cache = *new std::unordered_map<string, PyObject *>(); |
| 50 | + PyObject *&descr = dtype_cache[dtype]; |
| 51 | + if (!descr) { |
| 52 | + nb::object d = nb::module_::import_("numpy").attr("dtype")(dtype); |
| 53 | + descr = d.release().ptr(); |
| 54 | + } |
| 55 | + |
| 56 | + if (!EnsureNumpyCApi()) { |
| 57 | + throw std::runtime_error("Failed to initialize the numpy C API (import_array failed)"); |
| 58 | + } |
| 59 | + |
| 60 | + npy_intp dims[1] = {static_cast<npy_intp>(count)}; |
| 61 | + // PyArray_Empty STEALS a reference to descr. descr is a single cached np.dtype reused across |
| 62 | + // every allocation, so hand PyArray_Empty its own reference to consume. |
| 63 | + Py_INCREF(descr); |
| 64 | + PyObject *arr = PyArray_Empty(1, dims, reinterpret_cast<PyArray_Descr *>(descr), 0 /* C order */); |
| 65 | + if (!arr) { |
| 66 | + // PyArray_Empty consumed the stolen reference even on failure; balance the INCREF above so |
| 67 | + // the cached descr is not leaked, then surface the numpy error. |
| 68 | + Py_DECREF(descr); |
| 69 | + throw nb::python_error(); |
| 70 | + } |
| 71 | + // PyArray_Empty returns a NEW reference; hand ownership to nanobind via steal. |
| 72 | + return nb::steal<nb::object>(arr); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace numpy_internal |
| 76 | +} // namespace duckdb |
0 commit comments