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
146695a9
Unverified
Commit
146695a9
authored
Mar 25, 2022
by
Aaron Gokaslan
Committed by
GitHub
Mar 25, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: better exception and error handling for capsules (#3825)
* Make capsule errors better match python
parent
47079b9e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
14 deletions
+21
-14
include/pybind11/numpy.h
+2
-1
include/pybind11/pytypes.h
+19
-13
No files found.
include/pybind11/numpy.h
View file @
146695a9
...
@@ -1288,7 +1288,8 @@ public:
...
@@ -1288,7 +1288,8 @@ public:
static
pybind11
::
dtype
dtype
()
{
static
pybind11
::
dtype
dtype
()
{
list
shape
;
list
shape
;
array_info
<
T
>::
append_extents
(
shape
);
array_info
<
T
>::
append_extents
(
shape
);
return
pybind11
::
dtype
::
from_args
(
pybind11
::
make_tuple
(
base_descr
::
dtype
(),
shape
));
return
pybind11
::
dtype
::
from_args
(
pybind11
::
make_tuple
(
base_descr
::
dtype
(),
std
::
move
(
shape
)));
}
}
};
};
...
...
include/pybind11/pytypes.h
View file @
146695a9
...
@@ -1574,7 +1574,7 @@ public:
...
@@ -1574,7 +1574,7 @@ public:
void
(
*
destructor
)(
PyObject
*
)
=
nullptr
)
void
(
*
destructor
)(
PyObject
*
)
=
nullptr
)
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
name
,
destructor
),
stolen_t
{})
{
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
name
,
destructor
),
stolen_t
{})
{
if
(
!
m_ptr
)
{
if
(
!
m_ptr
)
{
pybind11_fail
(
"Could not allocate capsule object!"
);
throw
error_already_set
(
);
}
}
}
}
...
@@ -1582,34 +1582,42 @@ public:
...
@@ -1582,34 +1582,42 @@ public:
capsule
(
const
void
*
value
,
void
(
*
destruct
)(
PyObject
*
))
capsule
(
const
void
*
value
,
void
(
*
destruct
)(
PyObject
*
))
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
nullptr
,
destruct
),
stolen_t
{})
{
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
nullptr
,
destruct
),
stolen_t
{})
{
if
(
!
m_ptr
)
{
if
(
!
m_ptr
)
{
pybind11_fail
(
"Could not allocate capsule object!"
);
throw
error_already_set
(
);
}
}
}
}
capsule
(
const
void
*
value
,
void
(
*
destructor
)(
void
*
))
{
capsule
(
const
void
*
value
,
void
(
*
destructor
)(
void
*
))
{
m_ptr
=
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
nullptr
,
[](
PyObject
*
o
)
{
m_ptr
=
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
nullptr
,
[](
PyObject
*
o
)
{
auto
destructor
=
reinterpret_cast
<
void
(
*
)(
void
*
)
>
(
PyCapsule_GetContext
(
o
));
auto
destructor
=
reinterpret_cast
<
void
(
*
)(
void
*
)
>
(
PyCapsule_GetContext
(
o
));
if
(
destructor
==
nullptr
)
{
if
(
PyErr_Occurred
())
{
throw
error_already_set
();
}
pybind11_fail
(
"Unable to get capsule context"
);
}
void
*
ptr
=
PyCapsule_GetPointer
(
o
,
nullptr
);
void
*
ptr
=
PyCapsule_GetPointer
(
o
,
nullptr
);
if
(
ptr
==
nullptr
)
{
throw
error_already_set
();
}
destructor
(
ptr
);
destructor
(
ptr
);
});
});
if
(
!
m_ptr
)
{
if
(
!
m_ptr
||
PyCapsule_SetContext
(
m_ptr
,
(
void
*
)
destructor
)
!=
0
)
{
pybind11_fail
(
"Could not allocate capsule object!"
);
throw
error_already_set
();
}
if
(
PyCapsule_SetContext
(
m_ptr
,
(
void
*
)
destructor
)
!=
0
)
{
pybind11_fail
(
"Could not set capsule context!"
);
}
}
}
}
explicit
capsule
(
void
(
*
destructor
)())
{
explicit
capsule
(
void
(
*
destructor
)())
{
m_ptr
=
PyCapsule_New
(
reinterpret_cast
<
void
*>
(
destructor
),
nullptr
,
[](
PyObject
*
o
)
{
m_ptr
=
PyCapsule_New
(
reinterpret_cast
<
void
*>
(
destructor
),
nullptr
,
[](
PyObject
*
o
)
{
auto
destructor
=
reinterpret_cast
<
void
(
*
)()
>
(
PyCapsule_GetPointer
(
o
,
nullptr
));
auto
destructor
=
reinterpret_cast
<
void
(
*
)()
>
(
PyCapsule_GetPointer
(
o
,
nullptr
));
if
(
destructor
==
nullptr
)
{
throw
error_already_set
();
}
destructor
();
destructor
();
});
});
if
(
!
m_ptr
)
{
if
(
!
m_ptr
)
{
pybind11_fail
(
"Could not allocate capsule object!"
);
throw
error_already_set
(
);
}
}
}
}
...
@@ -1624,8 +1632,7 @@ public:
...
@@ -1624,8 +1632,7 @@ public:
const
auto
*
name
=
this
->
name
();
const
auto
*
name
=
this
->
name
();
T
*
result
=
static_cast
<
T
*>
(
PyCapsule_GetPointer
(
m_ptr
,
name
));
T
*
result
=
static_cast
<
T
*>
(
PyCapsule_GetPointer
(
m_ptr
,
name
));
if
(
!
result
)
{
if
(
!
result
)
{
PyErr_Clear
();
throw
error_already_set
();
pybind11_fail
(
"Unable to extract capsule contents!"
);
}
}
return
result
;
return
result
;
}
}
...
@@ -1633,8 +1640,7 @@ public:
...
@@ -1633,8 +1640,7 @@ public:
/// Replaces a capsule's pointer *without* calling the destructor on the existing one.
/// Replaces a capsule's pointer *without* calling the destructor on the existing one.
void
set_pointer
(
const
void
*
value
)
{
void
set_pointer
(
const
void
*
value
)
{
if
(
PyCapsule_SetPointer
(
m_ptr
,
const_cast
<
void
*>
(
value
))
!=
0
)
{
if
(
PyCapsule_SetPointer
(
m_ptr
,
const_cast
<
void
*>
(
value
))
!=
0
)
{
PyErr_Clear
();
throw
error_already_set
();
pybind11_fail
(
"Could not set capsule pointer"
);
}
}
}
}
...
...
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