Cython 3.2.6__py3-none-any.whl → 3.3.0a2.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Cython might be problematic. Click here for more details.

Files changed (112) hide show
  1. Cython/Build/BuildExecutable.py +1 -14
  2. Cython/Build/Dependencies.py +6 -44
  3. Cython/Build/Inline.py +7 -3
  4. Cython/Build/IpythonMagic.py +0 -1
  5. Cython/Build/SharedModule.py +1 -2
  6. Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
  7. Cython/Build/Tests/TestDependencies.py +3 -3
  8. Cython/Build/Tests/TestInline.py +3 -3
  9. Cython/Build/Tests/TestIpythonMagic.py +35 -19
  10. Cython/Build/Tests/TestStripLiterals.py +2 -2
  11. Cython/Compiler/Buffer.py +5 -5
  12. Cython/Compiler/Builtin.py +57 -46
  13. Cython/Compiler/Code.pxd +12 -9
  14. Cython/Compiler/Code.py +220 -94
  15. Cython/Compiler/Dataclass.py +78 -52
  16. Cython/Compiler/Errors.py +21 -8
  17. Cython/Compiler/ExprNodes.py +871 -459
  18. Cython/Compiler/FlowControl.pxd +8 -6
  19. Cython/Compiler/FlowControl.py +111 -8
  20. Cython/Compiler/FusedNode.py +17 -12
  21. Cython/Compiler/Lexicon.py +123 -122
  22. Cython/Compiler/Main.py +3 -4
  23. Cython/Compiler/MatchCaseNodes.py +1948 -10
  24. Cython/Compiler/MemoryView.py +62 -33
  25. Cython/Compiler/ModuleNode.py +559 -283
  26. Cython/Compiler/Naming.py +4 -0
  27. Cython/Compiler/Nodes.py +333 -197
  28. Cython/Compiler/Optimize.py +268 -112
  29. Cython/Compiler/Options.py +0 -5
  30. Cython/Compiler/ParseTreeTransforms.pxd +2 -1
  31. Cython/Compiler/ParseTreeTransforms.py +180 -76
  32. Cython/Compiler/Parsing.py +55 -38
  33. Cython/Compiler/PyrexTypes.py +364 -128
  34. Cython/Compiler/Scanning.pxd +27 -5
  35. Cython/Compiler/Scanning.py +24 -14
  36. Cython/Compiler/StringEncoding.py +1 -2
  37. Cython/Compiler/Symtab.py +27 -19
  38. Cython/Compiler/Tests/TestBuiltin.py +41 -6
  39. Cython/Compiler/Tests/TestCmdLine.py +3 -2
  40. Cython/Compiler/Tests/TestCode.py +4 -3
  41. Cython/Compiler/Tests/TestFlowControl.py +2 -2
  42. Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
  43. Cython/Compiler/Tests/TestScanning.py +2 -4
  44. Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
  45. Cython/Compiler/Tests/TestStringEncoding.py +2 -3
  46. Cython/Compiler/Tests/TestTypes.py +46 -3
  47. Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
  48. Cython/Compiler/TypeInference.py +20 -0
  49. Cython/Compiler/TypeSlots.py +171 -16
  50. Cython/Compiler/UtilNodes.py +24 -0
  51. Cython/Compiler/UtilityCode.py +1 -0
  52. Cython/Compiler/Visitor.pxd +1 -1
  53. Cython/Compiler/Visitor.py +3 -2
  54. Cython/Debugger/Tests/TestLibCython.py +7 -2
  55. Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
  56. Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
  57. Cython/Includes/cpython/array.pxd +5 -5
  58. Cython/Includes/cpython/dict.pxd +1 -6
  59. Cython/Includes/cpython/float.pxd +19 -10
  60. Cython/Includes/cpython/frozendict.pxd +37 -0
  61. Cython/Includes/cpython/long.pxd +31 -0
  62. Cython/Includes/cpython/object.pxd +9 -12
  63. Cython/Includes/cpython/sentinel.pxd +17 -0
  64. Cython/Includes/cpython/type.pxd +93 -0
  65. Cython/Includes/libcpp/exception.pxd +90 -0
  66. Cython/LZSS.py +170 -0
  67. Cython/Shadow.py +550 -73
  68. Cython/StringIOTree.py +1 -2
  69. Cython/Tempita/_tempita.py +45 -46
  70. Cython/TestUtils.py +50 -8
  71. Cython/Tests/TestCythonUtils.py +2 -2
  72. Cython/Tests/TestJediTyper.py +1 -1
  73. Cython/Tests/TestShadow.py +7 -5
  74. Cython/Tests/TestStringIOTree.py +4 -3
  75. Cython/Tests/TestTestUtils.py +2 -3
  76. Cython/Utility/AsyncGen.c +59 -19
  77. Cython/Utility/Buffer.c +4 -3
  78. Cython/Utility/Builtins.c +139 -17
  79. Cython/Utility/CConvert.pyx +27 -12
  80. Cython/Utility/CommonStructures.c +55 -37
  81. Cython/Utility/Coroutine.c +112 -68
  82. Cython/Utility/CythonFunction.c +607 -330
  83. Cython/Utility/Embed.c +8 -0
  84. Cython/Utility/Exceptions.c +20 -3
  85. Cython/Utility/ExtensionTypes.c +414 -224
  86. Cython/Utility/FunctionArguments.c +83 -81
  87. Cython/Utility/ImportExport.c +72 -61
  88. Cython/Utility/MatchCase.c +979 -0
  89. Cython/Utility/MatchCase_Cy.pyx +12 -0
  90. Cython/Utility/MemoryView.pxd +9 -88
  91. Cython/Utility/MemoryView.pyx +173 -61
  92. Cython/Utility/MemoryView_C.c +3 -3
  93. Cython/Utility/ModuleSetupCode.c +306 -157
  94. Cython/Utility/ObjectHandling.c +483 -404
  95. Cython/Utility/Optimize.c +1117 -164
  96. Cython/Utility/Overflow.c +3 -9
  97. Cython/Utility/Profile.c +4 -0
  98. Cython/Utility/StringTools.c +187 -149
  99. Cython/Utility/Synchronization.c +39 -0
  100. Cython/Utility/TString.c +24 -13
  101. Cython/Utility/TypeConversion.c +405 -250
  102. Cython/Utility/arrayarray.h +24 -22
  103. Cython/Utils.py +11 -23
  104. Cython/__init__.py +3 -1
  105. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
  106. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
  107. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
  108. Cython/Shadow.pyi +0 -521
  109. Cython/Utility/Printing.c +0 -86
  110. Cython/__init__.pyi +0 -7
  111. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
  112. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,12 @@
1
+ ################### MemoryviewSliceToList #######################
2
+
3
+ cimport cython
4
+
5
+ @cname("__Pyx_MatchCase_SliceMemoryview_{{suffix}}")
6
+ cdef list slice_to_list({{decl_code}} x, Py_ssize_t start, Py_ssize_t stop):
7
+ if stop < 0:
8
+ # use -1 as a flag for "end"
9
+ stop = x.shape[0]
10
+ # This code performs slightly better than [ xi for xi in x ]
11
+ with cython.boundscheck(False), cython.wraparound(False):
12
+ return [ x[i] for i in range(start, stop) ]
@@ -6,6 +6,9 @@ cdef extern from "Python.h":
6
6
  ctypedef void *PyThread_type_lock
7
7
 
8
8
  cdef extern from *:
9
+ """
10
+ typedef int (*__pyx_memoryview_to_dtype_func_type)(char*, PyObject*);
11
+ """
9
12
  ctypedef struct {{memviewslice_name}}:
10
13
  pass
11
14
 
@@ -22,16 +25,8 @@ cdef extern from *:
22
25
 
23
26
  ctypedef int __pyx_atomic_int_type
24
27
 
25
- {{memviewslice_name}} slice_copy_contig "__pyx_memoryview_copy_new_contig"(
26
- {{memviewslice_name}} *from_mvs,
27
- char *mode, int ndim,
28
- size_t sizeof_dtype, int contig_flag,
29
- bint dtype_is_object) except * nogil
30
- bint slice_is_contig "__pyx_memviewslice_is_contig" (
31
- {{memviewslice_name}} mvs, char order, int ndim) nogil
32
- bint slices_overlap "__pyx_slices_overlap" ({{memviewslice_name}} *slice1,
33
- {{memviewslice_name}} *slice2,
34
- int ndim, size_t itemsize) nogil
28
+ ctypedef int (*to_dtype_func_type "__pyx_memoryview_to_dtype_func_type")(char *, object) except 0
29
+
35
30
 
36
31
  @cname("__pyx_array")
37
32
  cdef class array:
@@ -54,9 +49,6 @@ cdef class array:
54
49
  @cname('get_memview')
55
50
  cdef get_memview(self)
56
51
 
57
- @cname("__pyx_array_allocate_buffer")
58
- cdef int _allocate_buffer(array self) except -1
59
-
60
52
  @cname("__pyx_array_new")
61
53
  cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format, const char *c_mode, char *buf)
62
54
 
@@ -82,11 +74,12 @@ cdef class memoryview:
82
74
  cdef bint dtype_is_object
83
75
  cdef const __Pyx_TypeInfo *typeinfo
84
76
 
85
- cdef char *get_item_pointer(memoryview self, object index) except NULL
77
+ cdef char *get_item_pointer(memoryview self, index: tuple) except NULL
86
78
  cdef is_slice(self, obj)
87
79
  cdef setitem_slice_assignment(self, dst, src)
88
80
  cdef setitem_slice_assign_scalar(self, memoryview dst, value)
89
- cdef setitem_indexed(self, index, value)
81
+ cdef setitem_indexed(self, indices: tuple, value)
82
+ cdef setitem_indexed1(self, index, value)
90
83
  cdef convert_item_to_object(self, char *itemp)
91
84
  cdef assign_item_from_object(self, char *itemp, object value)
92
85
  cdef _get_base(self)
@@ -98,13 +91,6 @@ cdef memoryview_cwrapper(object o, int flags, bint dtype_is_object, const __Pyx_
98
91
  cdef inline bint memoryview_check(object o) noexcept:
99
92
  return isinstance(o, memoryview)
100
93
 
101
- @cname('__pyx_memview_slice')
102
- cdef memoryview memview_slice(memoryview memview, object indices)
103
-
104
- @cname('__pyx_pybuffer_index')
105
- cdef char *pybuffer_index(Py_buffer *view, char *bufp, Py_ssize_t index,
106
- Py_ssize_t dim) except NULL
107
-
108
94
  @cname('__pyx_memslice_transpose')
109
95
  cdef int transpose_memslice({{memviewslice_name}} *memslice) except -1 nogil
110
96
 
@@ -112,76 +98,11 @@ cdef int transpose_memslice({{memviewslice_name}} *memslice) except -1 nogil
112
98
  cdef memoryview_fromslice({{memviewslice_name}} memviewslice,
113
99
  int ndim,
114
100
  object (*to_object_func)(char *),
115
- int (*to_dtype_func)(char *, object) except 0,
101
+ to_dtype_func_type to_dtype_func,
116
102
  bint dtype_is_object)
117
103
 
118
- @cname('__pyx_memoryview_get_slice_from_memoryview')
119
- cdef {{memviewslice_name}} *get_slice_from_memview(memoryview memview,
120
- {{memviewslice_name}} *mslice) except NULL
121
-
122
- @cname('__pyx_memoryview_slice_copy')
123
- cdef void slice_copy(memoryview memview, {{memviewslice_name}} *dst) noexcept
124
-
125
- @cname('__pyx_memoryview_copy_object')
126
- cdef memoryview_copy(memoryview memview)
127
-
128
- @cname('__pyx_memoryview_copy_object_from_slice')
129
- cdef memoryview_copy_from_slice(memoryview memview, {{memviewslice_name}} *memviewslice)
130
-
131
- @cname('__pyx_get_best_slice_order')
132
- cdef char get_best_order({{memviewslice_name}} *mslice, int ndim) noexcept nogil
133
-
134
- @cname('__pyx_memoryview_slice_get_size')
135
- cdef Py_ssize_t slice_get_size({{memviewslice_name}} *src, int ndim) noexcept nogil
136
-
137
- @cname('__pyx_fill_contig_strides_array')
138
- cdef Py_ssize_t fill_contig_strides_array(
139
- Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t stride,
140
- int ndim, char order) noexcept nogil
141
-
142
- @cname('__pyx_memoryview_copy_data_to_temp')
143
- cdef void *copy_data_to_temp({{memviewslice_name}} *src,
144
- {{memviewslice_name}} *tmpslice,
145
- char order,
146
- int ndim) except NULL nogil
147
-
148
- @cname('__pyx_memoryview_err_extents')
149
- cdef int _err_extents(int i, Py_ssize_t extent1,
150
- Py_ssize_t extent2) except -1 with gil
151
-
152
- @cname('__pyx_memoryview_err_dim')
153
- cdef int _err_dim(PyObject *error, str msg, int dim) except -1 with gil
154
-
155
- @cname('__pyx_memoryview_err')
156
- cdef int _err(PyObject *error, str msg) except -1 with gil
157
-
158
- @cname('__pyx_memoryview_err_no_memory')
159
- cdef int _err_no_memory() except -1 with gil
160
-
161
104
  @cname('__pyx_memoryview_copy_contents')
162
105
  cdef int memoryview_copy_contents({{memviewslice_name}} src,
163
106
  {{memviewslice_name}} dst,
164
107
  int src_ndim, int dst_ndim,
165
108
  bint dtype_is_object) except -1 nogil
166
-
167
- @cname('__pyx_memoryview_broadcast_leading')
168
- cdef void broadcast_leading({{memviewslice_name}} *mslice,
169
- int ndim,
170
- int ndim_other) noexcept nogil
171
-
172
- @cname('__pyx_memoryview_refcount_copying')
173
- cdef void refcount_copying({{memviewslice_name}} *dst, bint dtype_is_object, int ndim, bint inc) noexcept nogil
174
-
175
- @cname('__pyx_memoryview_refcount_objects_in_slice')
176
- cdef void refcount_objects_in_slice(char *data, Py_ssize_t *shape,
177
- Py_ssize_t *strides, int ndim, bint inc) noexcept
178
-
179
- @cname('__pyx_memoryview_slice_assign_scalar')
180
- cdef void slice_assign_scalar({{memviewslice_name}} *dst, int ndim,
181
- size_t itemsize, void *item,
182
- bint dtype_is_object) noexcept nogil
183
-
184
- @cname('__pyx_memoryview__slice_assign_scalar')
185
- cdef void _slice_assign_scalar(char *data, Py_ssize_t *shape,
186
- Py_ssize_t *strides, int ndim,
187
- size_t itemsize, void *item) noexcept nogil
@@ -15,6 +15,7 @@ cdef extern from "Python.h":
15
15
  int PyIndex_Check(object)
16
16
  PyObject *PyExc_IndexError
17
17
  PyObject *PyExc_ValueError
18
+ PyObject *PyExc_TypeError
18
19
 
19
20
  cdef extern from "pythread.h":
20
21
  ctypedef void *PyThread_type_lock
@@ -32,7 +33,6 @@ cdef extern from *:
32
33
  void PyBuffer_Release(Py_buffer *)
33
34
 
34
35
  ctypedef struct PyObject
35
- ctypedef Py_ssize_t Py_intptr_t
36
36
  void Py_INCREF(PyObject *)
37
37
  void Py_DECREF(PyObject *)
38
38
 
@@ -41,6 +41,11 @@ cdef extern from *:
41
41
  void* PyObject_Malloc(size_t n)
42
42
  void PyObject_Free(void *p)
43
43
 
44
+ PyObject* PyErr_SetString(PyObject* exc_type, const char* message) # No 'except NULL', callers must propagate.
45
+ PyObject* PyErr_Format(PyObject* exc_type, const char* message, ...) # No 'except NULL', callers must propagate.
46
+
47
+ ctypedef size_t uintptr_t "__pyx_uintptr_t"
48
+
44
49
  cdef struct __pyx_memoryview "__pyx_memoryview_obj":
45
50
  Py_buffer view
46
51
  PyObject *obj
@@ -77,11 +82,14 @@ cdef extern from *:
77
82
  pass
78
83
 
79
84
  cdef extern from *:
85
+ """
86
+ typedef int (*__pyx_memoryview_to_dtype_func_type)(char*, PyObject*);
87
+ """
80
88
  ctypedef int __pyx_atomic_int_type
81
89
  {{memviewslice_name}} slice_copy_contig "__pyx_memoryview_copy_new_contig"(
82
90
  {{memviewslice_name}} *from_mvs,
83
91
  const char *mode, int ndim,
84
- size_t sizeof_dtype, int contig_flag,
92
+ Py_ssize_t sizeof_dtype, int contig_flag,
85
93
  bint dtype_is_object) except * nogil
86
94
  bint slice_is_contig "__pyx_memviewslice_is_contig" (
87
95
  {{memviewslice_name}} mvs, char order, int ndim) nogil
@@ -89,6 +97,8 @@ cdef extern from *:
89
97
  {{memviewslice_name}} *slice2,
90
98
  int ndim, size_t itemsize) nogil
91
99
 
100
+ ctypedef int (*to_dtype_func_type "__pyx_memoryview_to_dtype_func_type")(char *, object) except 0
101
+
92
102
  int slice_memviewslice "__pyx_memoryview_slice_memviewslice" (
93
103
  {{memviewslice_name}} *dst,
94
104
  Py_ssize_t shape, Py_ssize_t stride, Py_ssize_t suboffset,
@@ -144,10 +154,10 @@ cdef class array:
144
154
  self.itemsize = itemsize
145
155
 
146
156
  if not self.ndim:
147
- raise ValueError, "Empty shape tuple for cython.array"
157
+ _err_ValueError("Empty shape tuple for cython.array")
148
158
 
149
159
  if itemsize <= 0:
150
- raise ValueError, "itemsize <= 0 for cython.array"
160
+ _err_ValueError("itemsize <= 0 for cython.array")
151
161
 
152
162
  if not isinstance(format, bytes):
153
163
  format = format.encode('ASCII')
@@ -155,7 +165,7 @@ cdef class array:
155
165
  self.format = self._format
156
166
 
157
167
  # use single malloc() for both shape and strides
158
- self._shape = <Py_ssize_t *> PyObject_Malloc(sizeof(Py_ssize_t)*self.ndim*2)
168
+ self._shape = <Py_ssize_t *> PyObject_Malloc(sizeof(Py_ssize_t) * 2 * <size_t> self.ndim)
159
169
  self._strides = self._shape + self.ndim
160
170
 
161
171
  if not self._shape:
@@ -194,7 +204,7 @@ cdef class array:
194
204
  elif self.mode == u"fortran":
195
205
  bufmode = PyBUF_F_CONTIGUOUS | PyBUF_ANY_CONTIGUOUS
196
206
  if not (flags & bufmode):
197
- raise ValueError, "Can only create a buffer that is contiguous in memory."
207
+ _err_ValueError("Can only create a buffer that is contiguous in memory.")
198
208
  info.buf = self.data
199
209
  info.len = self.len
200
210
 
@@ -224,8 +234,9 @@ cdef class array:
224
234
 
225
235
  @property
226
236
  def memview(self):
227
- return self.get_memview()
237
+ return array.get_memview(self)
228
238
 
239
+ # treat as cython_final
229
240
  @cname('get_memview')
230
241
  cdef get_memview(self):
231
242
  flags = PyBUF_ANY_CONTIGUOUS|PyBUF_FORMAT|PyBUF_WRITABLE
@@ -258,7 +269,7 @@ cdef int _allocate_buffer(array self) except -1:
258
269
  cdef PyObject **p
259
270
 
260
271
  self.free_data = True
261
- self.data = <char *>malloc(self.len)
272
+ self.data = <char *>malloc(<size_t> self.len)
262
273
  if not self.data:
263
274
  raise MemoryError, "unable to allocate array data."
264
275
 
@@ -375,7 +386,7 @@ cdef class memoryview:
375
386
  self.dtype_is_object = dtype_is_object
376
387
 
377
388
  with cython.cdivision(True):
378
- assert <Py_intptr_t>(&self.acquisition_count) % sizeof(__pyx_atomic_int_type) == 0
389
+ assert <uintptr_t>(&self.acquisition_count) % sizeof(__pyx_atomic_int_type) == 0
379
390
  self.typeinfo = NULL
380
391
 
381
392
  def __dealloc__(memoryview self):
@@ -399,7 +410,8 @@ cdef class memoryview:
399
410
  else:
400
411
  PyThread_free_lock(self.lock)
401
412
 
402
- cdef char *get_item_pointer(memoryview self, object index) except NULL:
413
+ # treat as cython.final
414
+ cdef char *get_item_pointer(memoryview self, index: tuple) except NULL:
403
415
  cdef Py_ssize_t dim
404
416
  cdef char *itemp = <char *> self.view.buf
405
417
 
@@ -410,6 +422,12 @@ cdef class memoryview:
410
422
 
411
423
  #@cname('__pyx_memoryview_getitem')
412
424
  def __getitem__(memoryview self, object index):
425
+ cdef char *buffer
426
+ if self.view.ndim == 1 and isinstance(index, int):
427
+ buffer = <char *> self.view.buf
428
+ return self.convert_item_to_object(
429
+ pybuffer_index(&self.view, buffer, index, 0))
430
+
413
431
  if index is Ellipsis:
414
432
  return self
415
433
 
@@ -419,24 +437,30 @@ cdef class memoryview:
419
437
  if have_slices:
420
438
  return memview_slice(self, indices)
421
439
  else:
422
- itemp = self.get_item_pointer(indices)
440
+ itemp = memoryview.get_item_pointer(self, <tuple> indices)
423
441
  return self.convert_item_to_object(itemp)
424
442
 
425
443
  def __setitem__(memoryview self, object index, object value):
426
444
  if self.view.readonly:
427
445
  raise TypeError, "Cannot assign to read-only memoryview"
428
446
 
429
- have_slices, index = _unellipsify(index, self.view.ndim)
447
+ if self.view.ndim == 1 and isinstance(index, int):
448
+ memoryview.setitem_indexed1(self, index, value)
449
+ return
450
+
451
+ have_slices, indices = _unellipsify(index, self.view.ndim)
430
452
 
431
453
  if have_slices:
432
- obj = self.is_slice(value)
454
+ obj = memoryview.is_slice(self, value)
455
+ target_slice = memview_slice(self, indices)
433
456
  if obj is not None:
434
- self.setitem_slice_assignment(self[index], obj)
457
+ memoryview.setitem_slice_assignment(self, target_slice, obj)
435
458
  else:
436
- self.setitem_slice_assign_scalar(self[index], value)
459
+ memoryview.setitem_slice_assign_scalar(self, target_slice, value)
437
460
  else:
438
- self.setitem_indexed(index, value)
461
+ memoryview.setitem_indexed(self, <tuple> indices, value)
439
462
 
463
+ # treat as cython.final
440
464
  cdef is_slice(self, obj):
441
465
  if not isinstance(obj, memoryview):
442
466
  try:
@@ -447,6 +471,7 @@ cdef class memoryview:
447
471
 
448
472
  return obj
449
473
 
474
+ # treat as cython.final
450
475
  cdef setitem_slice_assignment(self, dst, src):
451
476
  cdef {{memviewslice_name}} dst_slice
452
477
  cdef {{memviewslice_name}} src_slice
@@ -455,6 +480,7 @@ cdef class memoryview:
455
480
 
456
481
  memoryview_copy_contents(msrc, mdst, src.ndim, dst.ndim, self.dtype_is_object)
457
482
 
483
+ # treat as cython.final
458
484
  cdef setitem_slice_assign_scalar(self, memoryview dst, value):
459
485
  cdef int array[128]
460
486
  cdef void *tmp = NULL
@@ -465,7 +491,7 @@ cdef class memoryview:
465
491
  dst_slice = get_slice_from_memview(dst, &tmp_slice)
466
492
 
467
493
  if <size_t>self.view.itemsize > sizeof(array):
468
- tmp = PyMem_Malloc(self.view.itemsize)
494
+ tmp = PyMem_Malloc(<size_t> self.view.itemsize)
469
495
  if tmp == NULL:
470
496
  raise MemoryError
471
497
  item = tmp
@@ -482,13 +508,20 @@ cdef class memoryview:
482
508
  # to disallow :)
483
509
  if self.view.suboffsets != NULL:
484
510
  assert_direct_dimensions(self.view.suboffsets, self.view.ndim)
485
- slice_assign_scalar(dst_slice, dst.view.ndim, self.view.itemsize,
511
+ slice_assign_scalar(dst_slice, dst.view.ndim, <size_t> self.view.itemsize,
486
512
  item, self.dtype_is_object)
487
513
  finally:
488
514
  PyMem_Free(tmp)
489
515
 
490
- cdef setitem_indexed(self, index, value):
491
- cdef char *itemp = self.get_item_pointer(index)
516
+ # treat as cython.final
517
+ cdef setitem_indexed(self, indices: tuple, value):
518
+ cdef char *itemp = memoryview.get_item_pointer(self, indices)
519
+ self.assign_item_from_object(itemp, value)
520
+
521
+ # treat as cython.final
522
+ cdef setitem_indexed1(self, index, value):
523
+ cdef char *buffer = <char *> self.view.buf
524
+ cdef char *itemp = pybuffer_index(&self.view, buffer, index, 0)
492
525
  self.assign_item_from_object(itemp, value)
493
526
 
494
527
  cdef convert_item_to_object(self, char *itemp):
@@ -501,7 +534,7 @@ cdef class memoryview:
501
534
  try:
502
535
  result = struct.unpack(self.view.format, bytesitem)
503
536
  except struct.error:
504
- raise ValueError, "Unable to convert item to object"
537
+ _err_ValueError("Unable to convert item to object")
505
538
  else:
506
539
  if len(self.view.format) == 1:
507
540
  return result[0]
@@ -526,7 +559,7 @@ cdef class memoryview:
526
559
  @cname('getbuffer')
527
560
  def __getbuffer__(self, Py_buffer *info, int flags):
528
561
  if flags & PyBUF_WRITABLE and self.view.readonly:
529
- raise ValueError, "Cannot create writable memory view from read-only memoryview"
562
+ _err_ValueError("Cannot create writable memory view from read-only memoryview")
530
563
 
531
564
  if flags & PyBUF_ND:
532
565
  info.shape = self.view.shape
@@ -577,7 +610,7 @@ cdef class memoryview:
577
610
  def strides(self):
578
611
  if self.view.strides == NULL:
579
612
  # Note: we always ask for strides, so if this is not set it's a bug
580
- raise ValueError, "Buffer view does not expose strides"
613
+ _err_ValueError("Buffer view does not expose strides")
581
614
 
582
615
  return tuple([stride for stride in self.view.strides[:self.view.ndim]])
583
616
 
@@ -602,11 +635,12 @@ cdef class memoryview:
602
635
 
603
636
  @property
604
637
  def size(self):
638
+ cdef size_t result
605
639
  if self._size is None:
606
640
  result = 1
607
641
 
608
642
  for length in self.view.shape[:self.view.ndim]:
609
- result *= length
643
+ result *= <size_t> length
610
644
 
611
645
  self._size = result
612
646
 
@@ -673,39 +707,96 @@ cdef memoryview_cwrapper(object o, int flags, bint dtype_is_object, const __Pyx_
673
707
  cdef inline bint memoryview_check(object o) noexcept:
674
708
  return isinstance(o, memoryview)
675
709
 
676
- cdef tuple _unellipsify(object index, int ndim):
710
+
711
+ @cname('__pyx_memoryview_err_invalid_index')
712
+ cdef int _err_invalid_index(item) except -1:
713
+ type_name = str(type(item))
714
+ PyErr_Format(PyExc_TypeError, "Cannot index with type '%.200U'", <PyObject*> type_name)
715
+ return -1
716
+
717
+
718
+ cdef tuple _unellipsify_index_tuple(index_tuple: tuple, int ndim):
677
719
  """
678
- Replace all ellipses with full slices and fill incomplete indices with
679
- full slices.
720
+ Replace all ellipses with full slices and fill incomplete indices with full slices.
680
721
  """
681
- cdef Py_ssize_t idx
682
- tup = <tuple>index if isinstance(index, tuple) else (index,)
722
+ cdef Py_ssize_t first_ellipsis_index = -1
723
+ cdef Py_ssize_t idx, ellipsis_end, indices_from_ellipsis
683
724
 
684
- result = [slice(None)] * ndim
685
725
  have_slices = False
686
- seen_ellipsis = False
726
+
687
727
  idx = 0
688
- for item in tup:
728
+ for item in index_tuple:
689
729
  if item is Ellipsis:
690
- if not seen_ellipsis:
691
- idx += ndim - len(tup)
692
- seen_ellipsis = True
693
730
  have_slices = True
694
- else:
695
- if isinstance(item, slice):
696
- have_slices = True
697
- elif not PyIndex_Check(item):
698
- raise TypeError, f"Cannot index with type '{type(item)}'"
699
- result[idx] = item
731
+ if first_ellipsis_index == -1:
732
+ first_ellipsis_index = idx
733
+ elif isinstance(item, slice):
734
+ have_slices = True
735
+ elif not PyIndex_Check(item):
736
+ _err_invalid_index(item)
700
737
  idx += 1
701
738
 
702
- nslices = ndim - idx
703
- return have_slices or nslices, tuple(result)
739
+ if first_ellipsis_index >= 0:
740
+ result = [slice(None)] * ndim
741
+
742
+ for idx in range(first_ellipsis_index):
743
+ result[idx] = index_tuple[idx]
744
+
745
+ indices_from_ellipsis = len(index_tuple) - first_ellipsis_index
746
+ ellipsis_end = ndim - indices_from_ellipsis
747
+
748
+ for idx in range(1, indices_from_ellipsis):
749
+ item = index_tuple[first_ellipsis_index + idx]
750
+ if item is not Ellipsis:
751
+ result[ellipsis_end + idx] = item
752
+
753
+ index_tuple = tuple(result)
754
+
755
+ elif ndim > idx:
756
+ # Fill missing indices at the end with full slice.
757
+ have_slices = True
758
+ index_tuple += (slice(None),) * (ndim - idx)
759
+
760
+ return have_slices, index_tuple
761
+
762
+
763
+ cdef tuple _unellipsify(object index, int ndim):
764
+ """
765
+ Replace all ellipses with full slices and fill incomplete indices with full slices.
766
+ """
767
+ result: tuple
768
+ have_slices = False
769
+
770
+ if index is Ellipsis:
771
+ have_slices = True
772
+ result = (slice(None),) * ndim
773
+
774
+ elif isinstance(index, tuple):
775
+ return _unellipsify_index_tuple(<tuple> index, ndim)
776
+
777
+ else:
778
+ if isinstance(index, slice):
779
+ have_slices = True
780
+ elif not PyIndex_Check(index):
781
+ _err_invalid_index(index)
782
+
783
+ # 1-2 dim are so common that they merit a special case.
784
+ if ndim == 1:
785
+ result = (index,)
786
+ elif ndim == 2:
787
+ have_slices = True
788
+ result = (index, slice(None))
789
+ else:
790
+ have_slices = True
791
+ result = (index,) + (slice(None),) * (ndim - 1)
792
+
793
+ return have_slices, result
794
+
704
795
 
705
796
  cdef int assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim) except -1:
706
797
  for suboffset in suboffsets[:ndim]:
707
798
  if suboffset >= 0:
708
- raise ValueError, "Indirect dimensions not supported"
799
+ _err_ValueError("Indirect dimensions not supported")
709
800
  return 0 # return type just used as an error flag
710
801
 
711
802
  #
@@ -812,10 +903,10 @@ cdef char *pybuffer_index(Py_buffer *view, char *bufp, Py_ssize_t index,
812
903
  if index < 0:
813
904
  index += view.shape[dim]
814
905
  if index < 0:
815
- raise IndexError, f"Out of bounds on buffer access (axis {dim})"
906
+ _err_IndexError("Out of bounds on buffer access (axis %zd)", dim)
816
907
 
817
908
  if index >= shape:
818
- raise IndexError, f"Out of bounds on buffer access (axis {dim})"
909
+ _err_IndexError("Out of bounds on buffer access (axis %zd)", dim)
819
910
 
820
911
  resultp = bufp + index * stride
821
912
  if suboffset >= 0:
@@ -859,7 +950,7 @@ cdef class _memoryviewslice(memoryview):
859
950
  cdef object from_object
860
951
 
861
952
  cdef object (*to_object_func)(char *)
862
- cdef int (*to_dtype_func)(char *, object) except 0
953
+ cdef to_dtype_func_type to_dtype_func
863
954
 
864
955
  def __dealloc__(self):
865
956
  __PYX_XCLEAR_MEMVIEW(&self.from_slice, 1)
@@ -900,7 +991,7 @@ except:
900
991
  cdef memoryview_fromslice({{memviewslice_name}} memviewslice,
901
992
  int ndim,
902
993
  object (*to_object_func)(char *),
903
- int (*to_dtype_func)(char *, object) except 0,
994
+ to_dtype_func_type to_dtype_func,
904
995
  bint dtype_is_object):
905
996
 
906
997
  cdef _memoryviewslice result
@@ -940,6 +1031,7 @@ cdef memoryview_fromslice({{memviewslice_name}} memviewslice,
940
1031
  result.view.suboffsets = <Py_ssize_t *> result.from_slice.suboffsets
941
1032
  break
942
1033
 
1034
+ cdef Py_ssize_t length
943
1035
  result.view.len = result.view.itemsize
944
1036
  for length in result.view.shape[:ndim]:
945
1037
  result.view.len *= length
@@ -990,7 +1082,7 @@ cdef memoryview_copy_from_slice(memoryview memview, {{memviewslice_name}} *memvi
990
1082
  Create a new memoryview object from a given memoryview object and slice.
991
1083
  """
992
1084
  cdef object (*to_object_func)(char *)
993
- cdef int (*to_dtype_func)(char *, object) except 0
1085
+ cdef to_dtype_func_type to_dtype_func
994
1086
 
995
1087
  if isinstance(memview, _memoryviewslice):
996
1088
  to_object_func = (<_memoryviewslice> memview).to_object_func
@@ -1050,7 +1142,7 @@ cdef void _copy_strided_to_strided(char *src_data, Py_ssize_t *src_strides,
1050
1142
  if ndim == 1:
1051
1143
  if (src_stride > 0 and dst_stride > 0 and
1052
1144
  <size_t> src_stride == itemsize == <size_t> dst_stride):
1053
- memcpy(dst_data, src_data, itemsize * dst_extent)
1145
+ memcpy(dst_data, src_data, itemsize * <size_t> dst_extent)
1054
1146
  else:
1055
1147
  for i in range(dst_extent):
1056
1148
  memcpy(dst_data, src_data, itemsize)
@@ -1072,12 +1164,13 @@ cdef void copy_strided_to_strided({{memviewslice_name}} *src,
1072
1164
  src.shape, dst.shape, ndim, itemsize)
1073
1165
 
1074
1166
  @cname('__pyx_memoryview_slice_get_size')
1075
- cdef Py_ssize_t slice_get_size({{memviewslice_name}} *src, int ndim) noexcept nogil:
1167
+ cdef size_t slice_get_size({{memviewslice_name}} *src, int ndim) noexcept nogil:
1076
1168
  "Return the size of the memory occupied by the slice in number of bytes"
1077
- cdef Py_ssize_t shape, size = src.memview.view.itemsize
1169
+ cdef Py_ssize_t shape
1170
+ cdef size_t size = <size_t> src.memview.view.itemsize
1078
1171
 
1079
1172
  for shape in src.shape[:ndim]:
1080
- size *= shape
1173
+ size *= <size_t> shape
1081
1174
 
1082
1175
  return size
1083
1176
 
@@ -1114,7 +1207,7 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
1114
1207
  cdef int i
1115
1208
  cdef void *result
1116
1209
 
1117
- cdef size_t itemsize = src.memview.view.itemsize
1210
+ cdef size_t itemsize = <size_t> src.memview.view.itemsize
1118
1211
  cdef size_t size = slice_get_size(src, ndim)
1119
1212
 
1120
1213
  result = malloc(size)
@@ -1128,7 +1221,7 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
1128
1221
  tmpslice.shape[i] = src.shape[i]
1129
1222
  tmpslice.suboffsets[i] = -1
1130
1223
 
1131
- fill_contig_strides_array(&tmpslice.shape[0], &tmpslice.strides[0], itemsize, ndim, order)
1224
+ fill_contig_strides_array(&tmpslice.shape[0], &tmpslice.strides[0], <Py_ssize_t> itemsize, ndim, order)
1132
1225
 
1133
1226
  # We need to broadcast strides again
1134
1227
  for i in range(ndim):
@@ -1142,25 +1235,44 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
1142
1235
 
1143
1236
  return result
1144
1237
 
1238
+
1145
1239
  # Use 'with gil' functions and avoid 'with gil' blocks, as the code within the blocks
1146
1240
  # has temporaries that need the GIL to clean up
1147
1241
  @cname('__pyx_memoryview_err_extents')
1148
1242
  cdef int _err_extents(int i, Py_ssize_t extent1,
1149
1243
  Py_ssize_t extent2) except -1 with gil:
1150
- raise ValueError, f"got differing extents in dimension {i} (got {extent1} and {extent2})"
1244
+ PyErr_Format(
1245
+ PyExc_ValueError,
1246
+ "got differing extents in dimension %d (got %zd and %zd)",
1247
+ i, extent1, extent2)
1248
+ return -1
1151
1249
 
1152
1250
  @cname('__pyx_memoryview_err_dim')
1153
- cdef int _err_dim(PyObject *error, str msg, int dim) except -1 with gil:
1154
- raise <object>error, msg % dim
1251
+ cdef int _err_dim(PyObject *error, const char* msg, int dim) except -1 with gil:
1252
+ PyErr_Format(error, msg, dim)
1253
+ return -1
1155
1254
 
1156
1255
  @cname('__pyx_memoryview_err')
1157
- cdef int _err(PyObject *error, str msg) except -1 with gil:
1158
- raise <object>error, msg
1256
+ cdef int _err(PyObject *error, const char* msg) except -1 with gil:
1257
+ PyErr_SetString(error, msg)
1258
+ return -1
1159
1259
 
1160
1260
  @cname('__pyx_memoryview_err_no_memory')
1161
1261
  cdef int _err_no_memory() except -1 with gil:
1162
1262
  raise MemoryError
1163
1263
 
1264
+ # Simple exception raising functions.
1265
+
1266
+ @cname('__pyx_memoryview_err_ValueError')
1267
+ cdef int _err_ValueError(const char* msg) except -1:
1268
+ PyErr_SetString(PyExc_ValueError, msg)
1269
+ return -1
1270
+
1271
+ @cname('__pyx_memoryview_err_IndexError')
1272
+ cdef int _err_IndexError(const char* msg, Py_ssize_t index) except -1:
1273
+ PyErr_Format(PyExc_IndexError, msg, index)
1274
+ return -1
1275
+
1164
1276
 
1165
1277
  @cname('__pyx_memoryview_copy_contents')
1166
1278
  cdef int memoryview_copy_contents({{memviewslice_name}} src,
@@ -1172,7 +1284,7 @@ cdef int memoryview_copy_contents({{memviewslice_name}} src,
1172
1284
  Check for overlapping memory and verify the shapes.
1173
1285
  """
1174
1286
  cdef void *tmpdata = NULL
1175
- cdef size_t itemsize = src.memview.view.itemsize
1287
+ cdef size_t itemsize = <size_t> src.memview.view.itemsize
1176
1288
  cdef int i
1177
1289
  cdef char order = get_best_order(&src, src_ndim)
1178
1290
  cdef bint broadcasting = False