Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
87dfad65
Commit
87dfad65
authored
Jan 17, 2016
by
Wenzel Jakob
Browse files
avoid naming clashes with numpy (fixes #36)
parent
4177ed43
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
53 deletions
+49
-53
include/pybind11/numpy.h
include/pybind11/numpy.h
+49
-53
No files found.
include/pybind11/numpy.h
View file @
87dfad65
...
...
@@ -30,65 +30,62 @@ public:
API_PyArray_FromAny
=
69
,
API_PyArray_NewCopy
=
85
,
API_PyArray_NewFromDescr
=
94
,
NPY_C_CONTIGUOUS
=
0x0001
,
NPY_F_CONTIGUOUS
=
0x0002
,
NPY_ARRAY_FORCECAST
=
0x0010
,
NPY_ENSURE_ARRAY
=
0x0040
,
NPY_BOOL
=
0
,
NPY_BYTE
,
NPY_UBYTE
,
NPY_SHORT
,
NPY_USHORT
,
NPY_INT
,
NPY_UINT
,
NPY_LONG
,
NPY_ULONG
,
NPY_LONGLONG
,
NPY_ULONGLONG
,
NPY_FLOAT
,
NPY_DOUBLE
,
NPY_LONGDOUBLE
,
NPY_CFLOAT
,
NPY_CDOUBLE
,
NPY_CLONGDOUBLE
NPY_C_CONTIGUOUS_
=
0x0001
,
NPY_F_CONTIGUOUS_
=
0x0002
,
NPY_ARRAY_FORCECAST_
=
0x0010
,
NPY_ENSURE_ARRAY_
=
0x0040
,
NPY_BOOL_
=
0
,
NPY_BYTE_
,
NPY_UBYTE_
,
NPY_SHORT_
,
NPY_USHORT_
,
NPY_INT_
,
NPY_UINT_
,
NPY_LONG_
,
NPY_ULONG_
,
NPY_LONGLONG_
,
NPY_ULONGLONG_
,
NPY_FLOAT_
,
NPY_DOUBLE_
,
NPY_LONGDOUBLE_
,
NPY_CFLOAT_
,
NPY_CDOUBLE_
,
NPY_CLONGDOUBLE_
};
static
API
lookup
()
{
PyObject
*
numpy
=
PyImport_ImportModule
(
"numpy.core.multiarray"
);
PyO
bject
*
capsule
=
numpy
?
PyObject_GetAttrString
(
numpy
,
"_ARRAY_API"
)
:
nullptr
;
module
m
=
module
::
import
(
"numpy.core.multiarray"
);
o
bject
c
=
(
object
)
m
.
attr
(
"_ARRAY_API"
);
#if PY_MAJOR_VERSION >= 3
void
**
api_ptr
=
(
void
**
)
(
c
apsule
?
PyCapsule_GetPointer
(
c
apsule
,
NULL
)
:
nullptr
);
void
**
api_ptr
=
(
void
**
)
(
c
?
PyCapsule_GetPointer
(
c
.
ptr
()
,
NULL
)
:
nullptr
);
#else
void
**
api_ptr
=
(
void
**
)
(
c
apsule
?
PyCObject_AsVoidPtr
(
c
apsule
)
:
nullptr
);
void
**
api_ptr
=
(
void
**
)
(
c
?
PyCObject_AsVoidPtr
(
c
.
ptr
()
)
:
nullptr
);
#endif
Py_XDECREF
(
capsule
);
Py_XDECREF
(
numpy
);
if
(
api_ptr
==
nullptr
)
throw
std
::
runtime_error
(
"Could not acquire pointer to NumPy API!"
);
API
api
;
api
.
PyArray_Type
=
(
decltype
(
api
.
PyArray_Type
))
api_ptr
[
API_PyArray_Type
];
api
.
PyArray_DescrFromType
=
(
decltype
(
api
.
PyArray_DescrFromType
))
api_ptr
[
API_PyArray_DescrFromType
];
api
.
PyArray_FromAny
=
(
decltype
(
api
.
PyArray_FromAny
))
api_ptr
[
API_PyArray_FromAny
];
api
.
PyArray_NewCopy
=
(
decltype
(
api
.
PyArray_NewCopy
))
api_ptr
[
API_PyArray_NewCopy
];
api
.
PyArray_NewFromDescr
=
(
decltype
(
api
.
PyArray_NewFromDescr
))
api_ptr
[
API_PyArray_NewFromDescr
];
api
.
PyArray_Type
_
=
(
decltype
(
api
.
PyArray_Type
_
))
api_ptr
[
API_PyArray_Type
];
api
.
PyArray_DescrFromType
_
=
(
decltype
(
api
.
PyArray_DescrFromType
_
))
api_ptr
[
API_PyArray_DescrFromType
];
api
.
PyArray_FromAny
_
=
(
decltype
(
api
.
PyArray_FromAny
_
))
api_ptr
[
API_PyArray_FromAny
];
api
.
PyArray_NewCopy
_
=
(
decltype
(
api
.
PyArray_NewCopy
_
))
api_ptr
[
API_PyArray_NewCopy
];
api
.
PyArray_NewFromDescr
_
=
(
decltype
(
api
.
PyArray_NewFromDescr
_
))
api_ptr
[
API_PyArray_NewFromDescr
];
return
api
;
}
bool
PyArray_Check
(
PyObject
*
obj
)
const
{
return
(
bool
)
PyObject_TypeCheck
(
obj
,
PyArray_Type
);
}
bool
PyArray_Check
_
(
PyObject
*
obj
)
const
{
return
(
bool
)
PyObject_TypeCheck
(
obj
,
PyArray_Type
_
);
}
PyObject
*
(
*
PyArray_DescrFromType
)(
int
);
PyObject
*
(
*
PyArray_NewFromDescr
)
PyObject
*
(
*
PyArray_DescrFromType
_
)(
int
);
PyObject
*
(
*
PyArray_NewFromDescr
_
)
(
PyTypeObject
*
,
PyObject
*
,
int
,
Py_intptr_t
*
,
Py_intptr_t
*
,
void
*
,
int
,
PyObject
*
);
PyObject
*
(
*
PyArray_NewCopy
)(
PyObject
*
,
int
);
PyTypeObject
*
PyArray_Type
;
PyObject
*
(
*
PyArray_FromAny
)
(
PyObject
*
,
PyObject
*
,
int
,
int
,
int
,
PyObject
*
);
PyObject
*
(
*
PyArray_NewCopy
_
)(
PyObject
*
,
int
);
PyTypeObject
*
PyArray_Type
_
;
PyObject
*
(
*
PyArray_FromAny
_
)
(
PyObject
*
,
PyObject
*
,
int
,
int
,
int
,
PyObject
*
);
};
PYBIND11_OBJECT_DEFAULT
(
array
,
buffer
,
lookup_api
().
PyArray_Check
)
PYBIND11_OBJECT_DEFAULT
(
array
,
buffer
,
lookup_api
().
PyArray_Check
_
)
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType
(
npy_format_descriptor
<
Type
>::
value
);
PyObject
*
descr
=
api
.
PyArray_DescrFromType
_
(
npy_format_descriptor
<
Type
>::
value
);
if
(
descr
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unsupported buffer format!"
);
Py_intptr_t
shape
=
(
Py_intptr_t
)
size
;
PyObject
*
tmp
=
api
.
PyArray_NewFromDescr
(
api
.
PyArray_Type
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
);
PyObject
*
tmp
=
api
.
PyArray_NewFromDescr
_
(
api
.
PyArray_Type
_
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
);
if
(
tmp
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unable to create array!"
);
m_ptr
=
api
.
PyArray_NewCopy
(
tmp
,
-
1
/* any order */
);
m_ptr
=
api
.
PyArray_NewCopy
_
(
tmp
,
-
1
/* any order */
);
Py_DECREF
(
tmp
);
if
(
m_ptr
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unable to copy array!"
);
...
...
@@ -99,19 +96,18 @@ public:
if
((
info
.
format
.
size
()
<
1
)
||
(
info
.
format
.
size
()
>
2
))
throw
std
::
runtime_error
(
"Unsupported buffer format!"
);
int
fmt
=
(
int
)
info
.
format
[
0
];
if
(
info
.
format
==
"Zd"
)
fmt
=
API
::
NPY_CDOUBLE
;
else
if
(
info
.
format
==
"Zf"
)
fmt
=
API
::
NPY_CFLOAT
;
PyObject
*
descr
=
api
.
PyArray_DescrFromType
(
fmt
);
if
(
info
.
format
==
"Zd"
)
fmt
=
API
::
NPY_CDOUBLE_
;
else
if
(
info
.
format
==
"Zf"
)
fmt
=
API
::
NPY_CFLOAT_
;
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
fmt
);
if
(
descr
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unsupported buffer format '"
+
info
.
format
+
"'!"
);
PyObject
*
tmp
=
api
.
PyArray_NewFromDescr
(
api
.
PyArray_Type
,
descr
,
info
.
ndim
,
(
Py_intptr_t
*
)
&
info
.
shape
[
0
],
PyObject
*
tmp
=
api
.
PyArray_NewFromDescr
_
(
api
.
PyArray_Type
_
,
descr
,
info
.
ndim
,
(
Py_intptr_t
*
)
&
info
.
shape
[
0
],
(
Py_intptr_t
*
)
&
info
.
strides
[
0
],
info
.
ptr
,
0
,
nullptr
);
if
(
tmp
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unable to create array!"
);
m_ptr
=
api
.
PyArray_NewCopy
(
tmp
,
-
1
/* any order */
);
m_ptr
=
api
.
PyArray_NewCopy
_
(
tmp
,
-
1
/* any order */
);
Py_DECREF
(
tmp
);
if
(
m_ptr
==
nullptr
)
throw
std
::
runtime_error
(
"NumPy: unable to copy array!"
);
...
...
@@ -133,19 +129,19 @@ public:
if
(
ptr
==
nullptr
)
return
nullptr
;
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType
(
npy_format_descriptor
<
T
>::
value
);
return
api
.
PyArray_FromAny
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_C_CONTIGUOUS
|
API
::
NPY_ENSURE_ARRAY
|
API
::
NPY_ARRAY_FORCECAST
,
nullptr
);
PyObject
*
descr
=
api
.
PyArray_DescrFromType
_
(
npy_format_descriptor
<
T
>::
value
);
return
api
.
PyArray_FromAny
_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_C_CONTIGUOUS
_
|
API
::
NPY_ENSURE_ARRAY
_
|
API
::
NPY_ARRAY_FORCECAST
_
,
nullptr
);
}
};
#define DECL_FMT(t, n) template<> struct npy_format_descriptor<t> { enum { value = array::API::n }; }
DECL_FMT
(
int8_t
,
NPY_BYTE
);
DECL_FMT
(
uint8_t
,
NPY_UBYTE
);
DECL_FMT
(
int16_t
,
NPY_SHORT
);
DECL_FMT
(
uint16_t
,
NPY_USHORT
);
DECL_FMT
(
int32_t
,
NPY_INT
);
DECL_FMT
(
uint32_t
,
NPY_UINT
);
DECL_FMT
(
int64_t
,
NPY_LONGLONG
);
DECL_FMT
(
uint64_t
,
NPY_ULONGLONG
);
DECL_FMT
(
float
,
NPY_FLOAT
);
DECL_FMT
(
double
,
NPY_DOUBLE
);
DECL_FMT
(
bool
,
NPY_BOOL
);
DECL_FMT
(
std
::
complex
<
float
>
,
NPY_CFLOAT
);
DECL_FMT
(
std
::
complex
<
double
>
,
NPY_CDOUBLE
);
DECL_FMT
(
int8_t
,
NPY_BYTE
_
);
DECL_FMT
(
uint8_t
,
NPY_UBYTE
_
);
DECL_FMT
(
int16_t
,
NPY_SHORT
_
);
DECL_FMT
(
uint16_t
,
NPY_USHORT
_
);
DECL_FMT
(
int32_t
,
NPY_INT
_
);
DECL_FMT
(
uint32_t
,
NPY_UINT
_
);
DECL_FMT
(
int64_t
,
NPY_LONGLONG
_
);
DECL_FMT
(
uint64_t
,
NPY_ULONGLONG
_
);
DECL_FMT
(
float
,
NPY_FLOAT
_
);
DECL_FMT
(
double
,
NPY_DOUBLE
_
);
DECL_FMT
(
bool
,
NPY_BOOL
_
);
DECL_FMT
(
std
::
complex
<
float
>
,
NPY_CFLOAT
_
);
DECL_FMT
(
std
::
complex
<
double
>
,
NPY_CDOUBLE
_
);
#undef DECL_FMT
NAMESPACE_BEGIN
(
detail
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment