Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
1f54cd92
Commit
1f54cd92
authored
Jun 22, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use object instead of ptrs in numpy descriptors
parent
2a7acb6d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
11 deletions
+12
-11
include/pybind11/numpy.h
+12
-11
No files found.
include/pybind11/numpy.h
View file @
1f54cd92
...
@@ -96,7 +96,7 @@ public:
...
@@ -96,7 +96,7 @@ public:
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
PyObject
*
descr
=
object
(
detail
::
npy_format_descriptor
<
Type
>::
descr
(),
true
).
release
().
ptr
();
PyObject
*
descr
=
detail
::
npy_format_descriptor
<
Type
>::
descr
(
).
release
().
ptr
();
Py_intptr_t
shape
=
(
Py_intptr_t
)
size
;
Py_intptr_t
shape
=
(
Py_intptr_t
)
size
;
object
tmp
=
object
(
api
.
PyArray_NewFromDescr_
(
object
tmp
=
object
(
api
.
PyArray_NewFromDescr_
(
api
.
PyArray_Type_
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
),
false
);
api
.
PyArray_Type_
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
),
false
);
...
@@ -147,7 +147,7 @@ public:
...
@@ -147,7 +147,7 @@ public:
if
(
ptr
==
nullptr
)
if
(
ptr
==
nullptr
)
return
nullptr
;
return
nullptr
;
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
PyObject
*
descr
=
object
(
detail
::
npy_format_descriptor
<
T
>::
descr
(),
true
).
release
().
ptr
();
PyObject
*
descr
=
detail
::
npy_format_descriptor
<
T
>::
descr
(
).
release
().
ptr
();
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_ENSURE_ARRAY_
|
ExtraFlags
,
nullptr
);
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_ENSURE_ARRAY_
|
ExtraFlags
,
nullptr
);
if
(
!
result
)
if
(
!
result
)
PyErr_Clear
();
PyErr_Clear
();
...
@@ -178,8 +178,8 @@ private:
...
@@ -178,8 +178,8 @@ private:
array
::
API
::
NPY_INT_
,
array
::
API
::
NPY_UINT_
,
array
::
API
::
NPY_LONGLONG_
,
array
::
API
::
NPY_ULONGLONG_
};
array
::
API
::
NPY_INT_
,
array
::
API
::
NPY_UINT_
,
array
::
API
::
NPY_LONGLONG_
,
array
::
API
::
NPY_ULONGLONG_
};
public
:
public
:
static
int
typenum
()
{
return
values
[
detail
::
log2
(
sizeof
(
T
))
*
2
+
(
std
::
is_unsigned
<
T
>::
value
?
1
:
0
)];
}
static
int
typenum
()
{
return
values
[
detail
::
log2
(
sizeof
(
T
))
*
2
+
(
std
::
is_unsigned
<
T
>::
value
?
1
:
0
)];
}
static
PyObject
*
descr
()
{
static
py
::
object
descr
()
{
if
(
auto
obj
=
array
::
lookup_api
().
PyArray_DescrFromType_
(
typenum
()))
return
obj
;
if
(
auto
ptr
=
array
::
lookup_api
().
PyArray_DescrFromType_
(
typenum
()))
return
py
::
object
(
ptr
,
true
)
;
else
pybind11_fail
(
"Unsupported buffer format!"
);
else
pybind11_fail
(
"Unsupported buffer format!"
);
}
}
template
<
typename
T2
=
T
,
typename
std
::
enable_if
<
std
::
is_signed
<
T2
>::
value
,
int
>::
type
=
0
>
template
<
typename
T2
=
T
,
typename
std
::
enable_if
<
std
::
is_signed
<
T2
>::
value
,
int
>::
type
=
0
>
...
@@ -192,8 +192,8 @@ template <typename T> constexpr const int npy_format_descriptor<
...
@@ -192,8 +192,8 @@ template <typename T> constexpr const int npy_format_descriptor<
#define DECL_FMT(Type, NumPyName, Name) template<> struct npy_format_descriptor<Type> { \
#define DECL_FMT(Type, NumPyName, Name) template<> struct npy_format_descriptor<Type> { \
static int typenum() { return array::API::NumPyName; } \
static int typenum() { return array::API::NumPyName; } \
static
PyObject* descr() {
\
static
py::object descr() {
\
if (auto
obj = array::lookup_api().PyArray_DescrFromType_(typenum())) return obj
; \
if (auto
ptr = array::lookup_api().PyArray_DescrFromType_(typenum())) return py::object(ptr, true)
; \
else pybind11_fail("Unsupported buffer format!"); \
else pybind11_fail("Unsupported buffer format!"); \
} \
} \
static PYBIND11_DESCR name() { return _(Name); } }
static PYBIND11_DESCR name() { return _(Name); } }
...
@@ -207,7 +207,7 @@ DECL_FMT(std::complex<double>, NPY_CDOUBLE_, "complex128");
...
@@ -207,7 +207,7 @@ DECL_FMT(std::complex<double>, NPY_CDOUBLE_, "complex128");
struct
field_descriptor
{
struct
field_descriptor
{
const
char
*
name
;
const
char
*
name
;
int
offset
;
int
offset
;
PyObject
*
descr
;
py
::
object
descr
;
};
};
template
<
typename
T
>
struct
npy_format_descriptor
template
<
typename
T
>
struct
npy_format_descriptor
...
@@ -220,10 +220,10 @@ template <typename T> struct npy_format_descriptor
...
@@ -220,10 +220,10 @@ template <typename T> struct npy_format_descriptor
{
{
static
PYBIND11_DESCR
name
()
{
return
_
(
"user-defined"
);
}
static
PYBIND11_DESCR
name
()
{
return
_
(
"user-defined"
);
}
static
PyObject
*
descr
()
{
static
py
::
object
descr
()
{
if
(
!
descr_
())
if
(
!
descr_
())
pybind11_fail
(
"NumPy: unsupported buffer format!"
);
pybind11_fail
(
"NumPy: unsupported buffer format!"
);
return
descr_
(
);
return
py
::
object
(
descr_
(),
true
);
}
}
static
const
char
*
format_str
()
{
static
const
char
*
format_str
()
{
...
@@ -239,7 +239,7 @@ template <typename T> struct npy_format_descriptor
...
@@ -239,7 +239,7 @@ template <typename T> struct npy_format_descriptor
pybind11_fail
(
"NumPy: unsupported field dtype"
);
pybind11_fail
(
"NumPy: unsupported field dtype"
);
names
.
append
(
py
::
str
(
field
.
name
));
names
.
append
(
py
::
str
(
field
.
name
));
offsets
.
append
(
py
::
int_
(
field
.
offset
));
offsets
.
append
(
py
::
int_
(
field
.
offset
));
formats
.
append
(
object
(
field
.
descr
,
true
)
);
formats
.
append
(
field
.
descr
);
}
}
args
[
"names"
]
=
names
;
args
[
"names"
]
=
names
;
args
[
"offsets"
]
=
offsets
;
args
[
"offsets"
]
=
offsets
;
...
@@ -265,7 +265,8 @@ private:
...
@@ -265,7 +265,8 @@ private:
#define FIELD_DESCRIPTOR(Type, Field) \
#define FIELD_DESCRIPTOR(Type, Field) \
::pybind11::detail::field_descriptor { \
::pybind11::detail::field_descriptor { \
#Field, offsetof(Type, Field), \
#Field, offsetof(Type, Field), \
::pybind11::detail::npy_format_descriptor<decltype(static_cast<Type*>(0)->Field)>::descr() }
::pybind11::detail::npy_format_descriptor<decltype(static_cast<Type*>(0)->Field)>::descr() \
}
// The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
// The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
// (C) William Swanson, Paul Fultz
// (C) William Swanson, Paul Fultz
...
...
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