]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/python/numpy_interop.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
20 #include "arrow/python/platform.h" // IWYU pragma: export
22 #include <numpy/numpyconfig.h> // IWYU pragma: export
24 // Don't use the deprecated Numpy functions
25 #ifdef NPY_1_7_API_VERSION
26 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
28 #define NPY_ARRAY_NOTSWAPPED NPY_NOTSWAPPED
29 #define NPY_ARRAY_ALIGNED NPY_ALIGNED
30 #define NPY_ARRAY_WRITEABLE NPY_WRITEABLE
31 #define NPY_ARRAY_UPDATEIFCOPY NPY_UPDATEIFCOPY
34 // This is required to be able to access the NumPy C API properly in C++ files
35 // other than init.cc.
36 #define PY_ARRAY_UNIQUE_SYMBOL arrow_ARRAY_API
37 #ifndef NUMPY_IMPORT_ARRAY
38 #define NO_IMPORT_ARRAY
41 #include <numpy/arrayobject.h> // IWYU pragma: export
42 #include <numpy/arrayscalars.h> // IWYU pragma: export
43 #include <numpy/ufuncobject.h> // IWYU pragma: export
45 // A bit subtle. Numpy has 5 canonical integer types:
46 // (or, rather, type pairs: signed and unsigned)
47 // NPY_BYTE, NPY_SHORT, NPY_INT, NPY_LONG, NPY_LONGLONG
48 // It also has 4 fixed-width integer aliases.
49 // When mapping Arrow integer types to these 4 fixed-width aliases,
50 // we always miss one of the canonical types (even though it may
51 // have the same width as one of the aliases).
52 // Which one depends on the platform...
53 // On a LP64 system, NPY_INT64 maps to NPY_LONG and
54 // NPY_LONGLONG needs to be handled separately.
55 // On a LLP64 system, NPY_INT32 maps to NPY_LONG and
56 // NPY_INT needs to be handled separately.
58 #if NPY_BITSOF_LONG == 32 && NPY_BITSOF_LONGLONG == 64
59 #define NPY_INT64_IS_LONG_LONG 1
61 #define NPY_INT64_IS_LONG_LONG 0
64 #if NPY_BITSOF_INT == 32 && NPY_BITSOF_LONG == 64
65 #define NPY_INT32_IS_INT 1
67 #define NPY_INT32_IS_INT 0
73 inline int import_numpy() {
74 #ifdef NUMPY_IMPORT_ARRAY
82 // See above about the missing Numpy integer type numbers
83 inline int fix_numpy_type_num(int type_num
) {
84 #if !NPY_INT32_IS_INT && NPY_BITSOF_INT == 32
85 if (type_num
== NPY_INT
) return NPY_INT32
;
86 if (type_num
== NPY_UINT
) return NPY_UINT32
;
88 #if !NPY_INT64_IS_LONG_LONG && NPY_BITSOF_LONGLONG == 64
89 if (type_num
== NPY_LONGLONG
) return NPY_INT64
;
90 if (type_num
== NPY_ULONGLONG
) return NPY_UINT64
;