diff options
| author | Stefan Behnel <stefan_ml@behnel.de> | 2020-05-17 20:02:55 +0200 |
|---|---|---|
| committer | Stefan Behnel <stefan_ml@behnel.de> | 2020-05-17 20:38:49 +0200 |
| commit | 26853612d1b4fc921db993c34ecf22c4a45b8e26 (patch) | |
| tree | 7adc8b51c4e573c5c73ce0dbb1ce70f86f4e7257 | |
| parent | 40e4f13193ef04dfda013f248cec241b38e45342 (diff) | |
| download | cython-26853612d1b4fc921db993c34ecf22c4a45b8e26.tar.gz | |
Reduce the code overhead of exception raising in generated Cython code and the memoryview code by avoiding explicit calls to create the exception and prepared constant argument tuples. "raise Exc, message" can do this implicitly.
| -rw-r--r-- | Cython/Compiler/ParseTreeTransforms.py | 8 | ||||
| -rw-r--r-- | Cython/Utility/MemoryView.pyx | 36 |
2 files changed, 22 insertions, 22 deletions
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 60319f992..01cc71569 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -1662,7 +1662,7 @@ cdef class NAME: count = 0 INIT_ASSIGNMENTS if IS_UNION and count > 1: - raise ValueError("At most one union member should be specified.") + raise ValueError, "At most one union member should be specified." def __str__(self): return STR_FORMAT % MEMBER_TUPLE def __repr__(self): @@ -1775,9 +1775,9 @@ if VALUE is not None: pickle_func = TreeFragment(u""" def __reduce_cython__(self): - raise TypeError("%(msg)s") + raise TypeError, "%(msg)s" def __setstate_cython__(self, __pyx_state): - raise TypeError("%(msg)s") + raise TypeError, "%(msg)s" """ % {'msg': msg}, level='c_class', pipeline=[NormalizeTree(None)]).substitute({}) pickle_func.analyse_declarations(node.scope) @@ -1801,7 +1801,7 @@ if VALUE is not None: cdef object __pyx_result if __pyx_checksum != %(checksum)s: from pickle import PickleError as __pyx_PickleError - raise __pyx_PickleError("Incompatible checksums (%%s vs %(checksum)s = (%(members)s))" %% __pyx_checksum) + raise __pyx_PickleError, "Incompatible checksums (%%s vs %(checksum)s = (%(members)s))" %% __pyx_checksum __pyx_result = %(class_name)s.__new__(__pyx_type) if __pyx_state is not None: %(unpickle_func_name)s__set_state(<%(class_name)s> __pyx_result, __pyx_state) diff --git a/Cython/Utility/MemoryView.pyx b/Cython/Utility/MemoryView.pyx index f0e4142dc..c457e12c5 100644 --- a/Cython/Utility/MemoryView.pyx +++ b/Cython/Utility/MemoryView.pyx @@ -130,10 +130,10 @@ cdef class array: self.itemsize = itemsize if not self.ndim: - raise ValueError("Empty shape tuple for cython.array") + raise ValueError, "Empty shape tuple for cython.array" if itemsize <= 0: - raise ValueError("itemsize <= 0 for cython.array") + raise ValueError, "itemsize <= 0 for cython.array" if not isinstance(format, bytes): format = format.encode('ASCII') @@ -145,12 +145,12 @@ cdef class array: self._strides = self._shape + self.ndim if not self._shape: - raise MemoryError("unable to allocate shape and strides.") + raise MemoryError, "unable to allocate shape and strides." # cdef Py_ssize_t dim, stride for idx, dim in enumerate(shape): if dim <= 0: - raise ValueError(f"Invalid shape in axis {idx}: {dim}.") + raise ValueError, f"Invalid shape in axis {idx}: {dim}." self._shape[idx] = dim cdef char order @@ -161,7 +161,7 @@ cdef class array: order = b'F' self.mode = u'fortran' else: - raise ValueError(f"Invalid mode, expected 'c' or 'fortran', got {mode}") + raise ValueError, f"Invalid mode, expected 'c' or 'fortran', got {mode}" self.len = fill_contig_strides_array(self._shape, self._strides, itemsize, self.ndim, order) @@ -179,7 +179,7 @@ cdef class array: elif self.mode == u"fortran": bufmode = PyBUF_F_CONTIGUOUS | PyBUF_ANY_CONTIGUOUS if not (flags & bufmode): - raise ValueError("Can only create a buffer that is contiguous in memory.") + raise ValueError, "Can only create a buffer that is contiguous in memory." info.buf = self.data info.len = self.len info.ndim = self.ndim @@ -239,7 +239,7 @@ cdef int _allocate_buffer(array self) except -1: self.free_data = True self.data = <char *>malloc(self.len) if not self.data: - raise MemoryError("unable to allocate array data.") + raise MemoryError, "unable to allocate array data." if self.dtype_is_object: p = <PyObject **> self.data @@ -423,7 +423,7 @@ cdef class memoryview: def __setitem__(memoryview self, object index, object value): if self.view.readonly: - raise TypeError("Cannot assign to read-only memoryview") + raise TypeError, "Cannot assign to read-only memoryview" have_slices, index = _unellipsify(index, self.view.ndim) @@ -500,7 +500,7 @@ cdef class memoryview: try: result = struct.unpack(self.view.format, bytesitem) except struct.error: - raise ValueError("Unable to convert item to object") + raise ValueError, "Unable to convert item to object" else: if len(self.view.format) == 1: return result[0] @@ -525,7 +525,7 @@ cdef class memoryview: @cname('getbuffer') def __getbuffer__(self, Py_buffer *info, int flags): if flags & PyBUF_WRITABLE and self.view.readonly: - raise ValueError("Cannot create writable memory view from read-only memoryview") + raise ValueError, "Cannot create writable memory view from read-only memoryview" if flags & PyBUF_ND: info.shape = self.view.shape @@ -578,7 +578,7 @@ cdef class memoryview: def strides(self): if self.view.strides == NULL: # Note: we always ask for strides, so if this is not set it's a bug - raise ValueError("Buffer view does not expose strides") + raise ValueError, "Buffer view does not expose strides" return tuple([stride for stride in self.view.strides[:self.view.ndim]]) @@ -696,7 +696,7 @@ cdef tuple _unellipsify(object index, int ndim): if isinstance(item, slice): have_slices = True elif not PyIndex_Check(item): - raise TypeError(f"Cannot index with type '{type(item)}'") + raise TypeError, f"Cannot index with type '{type(item)}'" result[idx] = item idx += 1 @@ -706,7 +706,7 @@ cdef tuple _unellipsify(object index, int ndim): cdef assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim): for suboffset in suboffsets[:ndim]: if suboffset >= 0: - raise ValueError("Indirect dimensions not supported") + raise ValueError, "Indirect dimensions not supported" # ### Slicing a memoryview @@ -932,10 +932,10 @@ cdef char *pybuffer_index(Py_buffer *view, char *bufp, Py_ssize_t index, if index < 0: index += view.shape[dim] if index < 0: - raise IndexError(f"Out of bounds on buffer access (axis {dim})") + raise IndexError, f"Out of bounds on buffer access (axis {dim})" if index >= shape: - raise IndexError(f"Out of bounds on buffer access (axis {dim})") + raise IndexError, f"Out of bounds on buffer access (axis {dim})" resultp = bufp + index * stride if suboffset >= 0: @@ -1252,15 +1252,15 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src, @cname('__pyx_memoryview_err_extents') cdef int _err_extents(int i, Py_ssize_t extent1, Py_ssize_t extent2) except -1 with gil: - raise ValueError(f"got differing extents in dimension {i} (got {extent1} and {extent2})") + raise ValueError, f"got differing extents in dimension {i} (got {extent1} and {extent2})" @cname('__pyx_memoryview_err_dim') cdef int _err_dim(PyObject *error, str msg, int dim) except -1 with gil: - raise (<object>error)(msg % dim) + raise <object>error, msg % dim @cname('__pyx_memoryview_err') cdef int _err(PyObject *error, str msg) except -1 with gil: - raise (<object>error)(msg) + raise <object>error, msg @cname('__pyx_memoryview_err_no_memory') cdef int _err_no_memory() except -1 with gil: |
