summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-12-15 16:58:34 -0600
committerSebastian Berg <sebastian@sipsolutions.net>2021-12-15 17:01:10 -0600
commit6c5a9ac9681dba754728d40e3d2ea0fe4c10abcc (patch)
treef03fc828eeb2238f09f2527f54c982f62029c491
parent43c113dd7aa36ef833315035858ea98a7b4732c5 (diff)
downloadnumpy-6c5a9ac9681dba754728d40e3d2ea0fe4c10abcc.tar.gz
MAINT: Check for buffer interface support rather than try/except
This checks for buffer interface support using `PyObject_CheckBuffer` up-front. That seems to shave off about 100ns e.g. when importing pandas DataFrames (which do not support the buffer interface, but do support `__array__`. The reason is that building a TypeError and then clearing it takes time. It felt like a clear, easy win, since the check should be so fast that checking twice (in the case that it is indeed a buffer) does not really matter. But, if nobody likes to think about it just close :).
-rw-r--r--numpy/core/src/multiarray/ctors.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 487f0d4a9..9b86fca4d 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1301,9 +1301,10 @@ _array_from_array_like(PyObject *op,
* We skip bytes and unicode since they are considered scalars. Unicode
* would fail but bytes would be incorrectly converted to a uint8 array.
*/
- if (!PyBytes_Check(op) && !PyUnicode_Check(op)) {
+ if (PyObject_CheckBuffer(op) && !PyBytes_Check(op) && !PyUnicode_Check(op)) {
PyObject *memoryview = PyMemoryView_FromObject(op);
if (memoryview == NULL) {
+ /* TODO: Should probably not blanket ignore errors. */
PyErr_Clear();
}
else {