summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2020-05-02 11:55:51 +0200
committerStefan Behnel <stefan_ml@behnel.de>2020-05-02 12:36:56 +0200
commitcf2bbce53dd9601004e2cf218f7b4dbb91aee977 (patch)
tree48e18258656825548e687dd694b678a773b10f6a
parent125cf1d16a0f295d0a0de89f2d372c109beddd77 (diff)
downloadcython-cf2bbce53dd9601004e2cf218f7b4dbb91aee977.tar.gz
Improve error handling in "cython.array" creation code to avoid calling Py_BuildValue() with an exception set if "__pyx_format_from_typeinfo()" failed.
-rw-r--r--CHANGES.rst3
-rw-r--r--Cython/Compiler/ExprNodes.py23
2 files changed, 16 insertions, 10 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 5453ee424..5a2572821 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -13,6 +13,9 @@ Bugs fixed
* Error handling early in the module init code could lead to a crash.
+* Error handling in ``cython.array`` creation was improved to avoid calling
+ C-API functions with and error held.
+
* Complex buffer item types of structs of arrays could fail to validate.
Patch by Leo and smutch. (Github issue #1407)
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
index 41ebfb9e5..958e3b055 100644
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -10697,17 +10697,20 @@ class CythonArrayNode(ExprNode):
code.putln(code.error_goto(self.operand.pos))
code.putln("}")
- code.putln("%s = __pyx_format_from_typeinfo(&%s);" %
- (format_temp, type_info))
- buildvalue_fmt = " __PYX_BUILD_PY_SSIZE_T " * len(shapes)
- code.putln('%s = Py_BuildValue((char*) "(" %s ")", %s);' % (
- shapes_temp, buildvalue_fmt, ", ".join(shapes)))
-
- err = "!%s || !%s || !PyBytes_AsString(%s)" % (format_temp,
- shapes_temp,
- format_temp)
- code.putln(code.error_goto_if(err, self.pos))
+ code.putln("%s = __pyx_format_from_typeinfo(&%s); %s" % (
+ format_temp,
+ type_info,
+ code.error_goto_if_null(format_temp, self.pos),
+ ))
code.put_gotref(format_temp)
+
+ buildvalue_fmt = " __PYX_BUILD_PY_SSIZE_T " * len(shapes)
+ code.putln('%s = Py_BuildValue((char*) "(" %s ")", %s); %s' % (
+ shapes_temp,
+ buildvalue_fmt,
+ ", ".join(shapes),
+ code.error_goto_if_null(shapes_temp, self.pos),
+ ))
code.put_gotref(shapes_temp)
tup = (self.result(), shapes_temp, itemsize, format_temp,