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.
- Cython/Build/BuildExecutable.py +1 -14
- Cython/Build/Dependencies.py +6 -44
- Cython/Build/Inline.py +7 -3
- Cython/Build/IpythonMagic.py +0 -1
- Cython/Build/SharedModule.py +1 -2
- Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
- Cython/Build/Tests/TestDependencies.py +3 -3
- Cython/Build/Tests/TestInline.py +3 -3
- Cython/Build/Tests/TestIpythonMagic.py +35 -19
- Cython/Build/Tests/TestStripLiterals.py +2 -2
- Cython/Compiler/Buffer.py +5 -5
- Cython/Compiler/Builtin.py +57 -46
- Cython/Compiler/Code.pxd +12 -9
- Cython/Compiler/Code.py +220 -94
- Cython/Compiler/Dataclass.py +78 -52
- Cython/Compiler/Errors.py +21 -8
- Cython/Compiler/ExprNodes.py +871 -459
- Cython/Compiler/FlowControl.pxd +8 -6
- Cython/Compiler/FlowControl.py +111 -8
- Cython/Compiler/FusedNode.py +17 -12
- Cython/Compiler/Lexicon.py +123 -122
- Cython/Compiler/Main.py +3 -4
- Cython/Compiler/MatchCaseNodes.py +1948 -10
- Cython/Compiler/MemoryView.py +62 -33
- Cython/Compiler/ModuleNode.py +559 -283
- Cython/Compiler/Naming.py +4 -0
- Cython/Compiler/Nodes.py +333 -197
- Cython/Compiler/Optimize.py +268 -112
- Cython/Compiler/Options.py +0 -5
- Cython/Compiler/ParseTreeTransforms.pxd +2 -1
- Cython/Compiler/ParseTreeTransforms.py +180 -76
- Cython/Compiler/Parsing.py +55 -38
- Cython/Compiler/PyrexTypes.py +364 -128
- Cython/Compiler/Scanning.pxd +27 -5
- Cython/Compiler/Scanning.py +24 -14
- Cython/Compiler/StringEncoding.py +1 -2
- Cython/Compiler/Symtab.py +27 -19
- Cython/Compiler/Tests/TestBuiltin.py +41 -6
- Cython/Compiler/Tests/TestCmdLine.py +3 -2
- Cython/Compiler/Tests/TestCode.py +4 -3
- Cython/Compiler/Tests/TestFlowControl.py +2 -2
- Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
- Cython/Compiler/Tests/TestScanning.py +2 -4
- Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
- Cython/Compiler/Tests/TestStringEncoding.py +2 -3
- Cython/Compiler/Tests/TestTypes.py +46 -3
- Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
- Cython/Compiler/TypeInference.py +20 -0
- Cython/Compiler/TypeSlots.py +171 -16
- Cython/Compiler/UtilNodes.py +24 -0
- Cython/Compiler/UtilityCode.py +1 -0
- Cython/Compiler/Visitor.pxd +1 -1
- Cython/Compiler/Visitor.py +3 -2
- Cython/Debugger/Tests/TestLibCython.py +7 -2
- Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
- Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
- Cython/Includes/cpython/array.pxd +5 -5
- Cython/Includes/cpython/dict.pxd +1 -6
- Cython/Includes/cpython/float.pxd +19 -10
- Cython/Includes/cpython/frozendict.pxd +37 -0
- Cython/Includes/cpython/long.pxd +31 -0
- Cython/Includes/cpython/object.pxd +9 -12
- Cython/Includes/cpython/sentinel.pxd +17 -0
- Cython/Includes/cpython/type.pxd +93 -0
- Cython/Includes/libcpp/exception.pxd +90 -0
- Cython/LZSS.py +170 -0
- Cython/Shadow.py +550 -73
- Cython/StringIOTree.py +1 -2
- Cython/Tempita/_tempita.py +45 -46
- Cython/TestUtils.py +50 -8
- Cython/Tests/TestCythonUtils.py +2 -2
- Cython/Tests/TestJediTyper.py +1 -1
- Cython/Tests/TestShadow.py +7 -5
- Cython/Tests/TestStringIOTree.py +4 -3
- Cython/Tests/TestTestUtils.py +2 -3
- Cython/Utility/AsyncGen.c +59 -19
- Cython/Utility/Buffer.c +4 -3
- Cython/Utility/Builtins.c +139 -17
- Cython/Utility/CConvert.pyx +27 -12
- Cython/Utility/CommonStructures.c +55 -37
- Cython/Utility/Coroutine.c +112 -68
- Cython/Utility/CythonFunction.c +607 -330
- Cython/Utility/Embed.c +8 -0
- Cython/Utility/Exceptions.c +20 -3
- Cython/Utility/ExtensionTypes.c +414 -224
- Cython/Utility/FunctionArguments.c +83 -81
- Cython/Utility/ImportExport.c +72 -61
- Cython/Utility/MatchCase.c +979 -0
- Cython/Utility/MatchCase_Cy.pyx +12 -0
- Cython/Utility/MemoryView.pxd +9 -88
- Cython/Utility/MemoryView.pyx +173 -61
- Cython/Utility/MemoryView_C.c +3 -3
- Cython/Utility/ModuleSetupCode.c +306 -157
- Cython/Utility/ObjectHandling.c +483 -404
- Cython/Utility/Optimize.c +1117 -164
- Cython/Utility/Overflow.c +3 -9
- Cython/Utility/Profile.c +4 -0
- Cython/Utility/StringTools.c +187 -149
- Cython/Utility/Synchronization.c +39 -0
- Cython/Utility/TString.c +24 -13
- Cython/Utility/TypeConversion.c +405 -250
- Cython/Utility/arrayarray.h +24 -22
- Cython/Utils.py +11 -23
- Cython/__init__.py +3 -1
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
- Cython/Shadow.pyi +0 -521
- Cython/Utility/Printing.c +0 -86
- Cython/__init__.pyi +0 -7
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
Cython/Compiler/Optimize.py
CHANGED
|
@@ -11,6 +11,7 @@ import copy
|
|
|
11
11
|
import codecs
|
|
12
12
|
import itertools
|
|
13
13
|
from functools import partial, reduce
|
|
14
|
+
from typing import Optional
|
|
14
15
|
from operator import attrgetter
|
|
15
16
|
|
|
16
17
|
from . import TypeSlots
|
|
@@ -219,14 +220,12 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
219
220
|
return self._optimise_for_loop(node, node.iterator.sequence)
|
|
220
221
|
|
|
221
222
|
def _optimise_for_loop(self, node, iterable, reversed=False):
|
|
222
|
-
iter_type = iterable.type
|
|
223
|
-
annotation_type = None
|
|
224
223
|
if (iterable.is_name or iterable.is_attribute) and iterable.entry and iterable.entry.annotation:
|
|
225
224
|
annotation = iterable.entry.annotation.expr
|
|
226
225
|
if annotation.is_subscript:
|
|
227
226
|
annotation = annotation.base # container base type
|
|
228
227
|
|
|
229
|
-
if
|
|
228
|
+
if iterable.type.is_pyanydict_type:
|
|
230
229
|
# like iterating over dict.keys()
|
|
231
230
|
if reversed:
|
|
232
231
|
# CPython raises an error here: not a sequence
|
|
@@ -234,31 +233,51 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
234
233
|
return self._transform_dict_iteration(
|
|
235
234
|
node, dict_obj=iterable, method=None, keys=True, values=False)
|
|
236
235
|
|
|
237
|
-
if
|
|
238
|
-
Builtin.frozenset_type in (iter_type, annotation_type)):
|
|
236
|
+
if iterable.type.is_pyanyset_type:
|
|
239
237
|
if reversed:
|
|
240
238
|
# CPython raises an error here: not a sequence
|
|
241
239
|
return node
|
|
242
240
|
return self._transform_set_iteration(node, iterable)
|
|
243
241
|
|
|
244
242
|
env = self.current_env()
|
|
243
|
+
unpacked_iterable = unwrap_coerced_node(iterable)
|
|
245
244
|
|
|
246
245
|
# C array (slice) iteration?
|
|
247
|
-
|
|
246
|
+
optimised_node = self._try_optimise_array_iteration(node, unpacked_iterable, env, reversed=reversed)
|
|
247
|
+
if optimised_node is None and unpacked_iterable is not iterable:
|
|
248
|
+
# Try coerced (probably Python object) iterable as well.
|
|
249
|
+
optimised_node = self._try_optimise_array_iteration(node, iterable, env, reversed=reversed)
|
|
250
|
+
|
|
251
|
+
if optimised_node is not None:
|
|
252
|
+
return optimised_node
|
|
253
|
+
|
|
254
|
+
# Optimisable builtin function?
|
|
255
|
+
if isinstance(iterable, ExprNodes.SimpleCallNode):
|
|
256
|
+
optimised_node = self._try_optimise_iterator_function(node, iterable, reversed)
|
|
257
|
+
if optimised_node is not None:
|
|
258
|
+
return optimised_node
|
|
259
|
+
|
|
260
|
+
return node
|
|
261
|
+
|
|
262
|
+
def _try_optimise_array_iteration(self, node, iterable, env ,reversed=False):
|
|
263
|
+
if (iterable.type.is_ptr and not iterable.type.is_string) or iterable.type.is_array:
|
|
248
264
|
return self._transform_carray_iteration(node, iterable, reversed=reversed)
|
|
249
|
-
|
|
265
|
+
|
|
266
|
+
if iterable.is_sequence_constructor:
|
|
250
267
|
# Convert iteration over homogeneous sequences of C types into array iteration.
|
|
251
|
-
# FIXME: using ListNode in this way currently generates invalid C code inside of generator loops.
|
|
252
268
|
item_type = ExprNodes.infer_sequence_item_type(
|
|
253
|
-
env, iterable, seq_type=
|
|
269
|
+
env, iterable, seq_type=iterable.type)
|
|
254
270
|
if item_type and not item_type.is_pyobject and not any(item.is_starred for item in iterable.args):
|
|
255
|
-
|
|
256
|
-
PyrexTypes.
|
|
271
|
+
if not item_type.is_const:
|
|
272
|
+
item_type = PyrexTypes.c_const_type(item_type)
|
|
273
|
+
carray_type = PyrexTypes.c_array_type(item_type, len(iterable.args))
|
|
274
|
+
iterable = ExprNodes.ListNode(iterable.pos, args=iterable.args)
|
|
275
|
+
iterable = iterable.analyse_types(env).coerce_to(carray_type, env)
|
|
257
276
|
return self._transform_carray_iteration(node, iterable, reversed=reversed)
|
|
258
|
-
|
|
277
|
+
|
|
278
|
+
if iterable.is_string_literal:
|
|
259
279
|
# Iterate over C array of single character values.
|
|
260
|
-
|
|
261
|
-
if iter_type is Builtin.unicode_type:
|
|
280
|
+
if iterable.type.is_pystr_type:
|
|
262
281
|
item_type = PyrexTypes.c_py_ucs4_type
|
|
263
282
|
items = map(ord, iterable.value)
|
|
264
283
|
else:
|
|
@@ -267,26 +286,27 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
267
286
|
|
|
268
287
|
as_int_node = partial(ExprNodes.IntNode.for_int, iterable.pos, type=item_type)
|
|
269
288
|
iterable = ExprNodes.ListNode(iterable.pos, args=[as_int_node(ch)for ch in items])
|
|
270
|
-
|
|
289
|
+
carray_type = PyrexTypes.c_array_type(PyrexTypes.c_const_type(item_type), len(iterable.args))
|
|
290
|
+
iterable = iterable.analyse_types(env).coerce_to(carray_type, env)
|
|
271
291
|
return self._transform_carray_iteration(node, iterable, reversed=reversed)
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return self._transform_bytes_iteration(node, iterable, reversed=reversed)
|
|
277
|
-
if iter_type is Builtin.unicode_type:
|
|
292
|
+
|
|
293
|
+
if iterable.type.is_pybytes_type:
|
|
294
|
+
return self._transform_bytes_iteration(node, iterable, reversed=reversed)
|
|
295
|
+
if iterable.type.is_pystr_type:
|
|
278
296
|
return self._transform_unicode_iteration(node, iterable, reversed=reversed)
|
|
297
|
+
|
|
279
298
|
# in principle _transform_indexable_iteration would work on most of the above, and
|
|
280
299
|
# also tuple and list. However, it probably isn't quite as optimized
|
|
281
|
-
if
|
|
300
|
+
if iterable.type.is_pybytearray_type:
|
|
282
301
|
return self._transform_indexable_iteration(node, iterable, is_mutable=True, reversed=reversed)
|
|
283
|
-
if
|
|
284
|
-
return self._transform_indexable_iteration(node, iterable
|
|
302
|
+
if iterable.type.is_memoryviewslice:
|
|
303
|
+
return self._transform_indexable_iteration(node, iterable, is_mutable=False, reversed=reversed)
|
|
285
304
|
|
|
286
|
-
#
|
|
287
|
-
|
|
288
|
-
return node
|
|
305
|
+
# Failed to optimise.
|
|
306
|
+
return None
|
|
289
307
|
|
|
308
|
+
def _try_optimise_iterator_function(self, node, iterable, reversed):
|
|
309
|
+
function = iterable.function
|
|
290
310
|
if iterable.args is None:
|
|
291
311
|
arg_count = iterable.arg_tuple and len(iterable.arg_tuple.args) or 0
|
|
292
312
|
else:
|
|
@@ -294,7 +314,6 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
294
314
|
if arg_count and iterable.self is not None:
|
|
295
315
|
arg_count -= 1
|
|
296
316
|
|
|
297
|
-
function = iterable.function
|
|
298
317
|
# dict iteration?
|
|
299
318
|
if function.is_attribute and not reversed and not arg_count:
|
|
300
319
|
base_obj = iterable.self or function.obj
|
|
@@ -355,7 +374,8 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
355
374
|
else:
|
|
356
375
|
return self._transform_range_iteration(node, iterable, reversed=reversed)
|
|
357
376
|
|
|
358
|
-
|
|
377
|
+
# Failed to optimise.
|
|
378
|
+
return None
|
|
359
379
|
|
|
360
380
|
def _transform_reversed_iteration(self, node, reversed_function):
|
|
361
381
|
args = reversed_function.arg_tuple.args
|
|
@@ -370,7 +390,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
370
390
|
arg = args[0]
|
|
371
391
|
|
|
372
392
|
# reversed(list/tuple) ?
|
|
373
|
-
if arg.type
|
|
393
|
+
if arg.type.is_pytuple_type or arg.type.is_pylist_type:
|
|
374
394
|
node.iterator.sequence = arg.as_none_safe_node("'NoneType' object is not iterable")
|
|
375
395
|
node.iterator.reversed = True
|
|
376
396
|
return node
|
|
@@ -553,7 +573,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
553
573
|
|
|
554
574
|
def _transform_bytes_iteration(self, node, slice_node, reversed=False):
|
|
555
575
|
target_type = node.target.type
|
|
556
|
-
if not target_type.is_int and
|
|
576
|
+
if not target_type.is_int and not target_type.is_pybytes_type:
|
|
557
577
|
# bytes iteration returns bytes objects in Py2, but
|
|
558
578
|
# integers in Py3
|
|
559
579
|
return node
|
|
@@ -607,8 +627,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
607
627
|
exception_value=-1)
|
|
608
628
|
|
|
609
629
|
def _transform_unicode_iteration(self, node, slice_node, reversed=False):
|
|
610
|
-
|
|
611
|
-
if slice_node.is_literal and not env.is_generator_scope:
|
|
630
|
+
if slice_node.is_literal:
|
|
612
631
|
# try to reduce to byte iteration for plain Latin-1 strings
|
|
613
632
|
try:
|
|
614
633
|
bytes_value = bytes_literal(slice_node.value.encode('latin1'), 'iso8859-1')
|
|
@@ -621,7 +640,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
621
640
|
slice_node.pos, value=bytes_value,
|
|
622
641
|
constant_result=bytes_value,
|
|
623
642
|
type=PyrexTypes.c_const_char_ptr_type).coerce_to(
|
|
624
|
-
PyrexTypes.c_const_uchar_ptr_type,
|
|
643
|
+
PyrexTypes.c_const_uchar_ptr_type, self.current_env()),
|
|
625
644
|
start=None,
|
|
626
645
|
stop=ExprNodes.IntNode.for_size(slice_node.pos, len(bytes_value)),
|
|
627
646
|
type=Builtin.unicode_type, # hint for Python conversion
|
|
@@ -653,7 +672,8 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
653
672
|
is_temp = False,
|
|
654
673
|
)
|
|
655
674
|
if target_value.type != node.target.type:
|
|
656
|
-
target_value = target_value.coerce_to(node.target.type,
|
|
675
|
+
target_value = target_value.coerce_to(node.target.type,
|
|
676
|
+
self.current_env())
|
|
657
677
|
target_assign = Nodes.SingleAssignmentNode(
|
|
658
678
|
pos = node.target.pos,
|
|
659
679
|
lhs = node.target,
|
|
@@ -735,7 +755,12 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
735
755
|
return node
|
|
736
756
|
slice_base = slice_node
|
|
737
757
|
start = step = None
|
|
738
|
-
|
|
758
|
+
if isinstance(slice_node.type.size, int):
|
|
759
|
+
stop = ExprNodes.IntNode.for_size(slice_node.pos, slice_node.type.size)
|
|
760
|
+
else:
|
|
761
|
+
# enum name or const variable
|
|
762
|
+
stop = ExprNodes.RawCNameExprNode(
|
|
763
|
+
slice_node.pos, PyrexTypes.c_py_ssize_t_type, slice_node.type.size)
|
|
739
764
|
|
|
740
765
|
else:
|
|
741
766
|
if not slice_node.type.is_pyobject:
|
|
@@ -790,7 +815,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
790
815
|
|
|
791
816
|
if slice_base.type.is_string and node.target.type.is_pyobject:
|
|
792
817
|
# special case: char* -> bytes/unicode
|
|
793
|
-
if slice_node.type
|
|
818
|
+
if slice_node.type.is_pystr_type:
|
|
794
819
|
target_value = ExprNodes.CastNode(
|
|
795
820
|
ExprNodes.DereferenceNode(
|
|
796
821
|
node.target.pos, operand=counter_temp,
|
|
@@ -1065,6 +1090,17 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
1065
1090
|
temps.append(temp)
|
|
1066
1091
|
pos_temp = temp.ref(node.pos)
|
|
1067
1092
|
|
|
1093
|
+
legacy_method = False
|
|
1094
|
+
if method and method.startswith('iter'):
|
|
1095
|
+
assert method in ('iterkeys', 'itervalues', 'iteritems'), method
|
|
1096
|
+
if dict_obj.type.is_pydict_type:
|
|
1097
|
+
method = EncodedString(method[4:])
|
|
1098
|
+
elif dict_obj.type.is_pyfrozendict_type:
|
|
1099
|
+
# Don't apply legacy Python 2 behaviour to Python 3.15 types
|
|
1100
|
+
return node
|
|
1101
|
+
else:
|
|
1102
|
+
legacy_method = True
|
|
1103
|
+
|
|
1068
1104
|
key_target = value_target = tuple_target = None
|
|
1069
1105
|
if keys and values:
|
|
1070
1106
|
if node.target.is_sequence_constructor:
|
|
@@ -1116,7 +1152,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
1116
1152
|
method_node = ExprNodes.NullNode(dict_obj.pos)
|
|
1117
1153
|
dict_obj = dict_obj.as_none_safe_node("'NoneType' object is not iterable")
|
|
1118
1154
|
|
|
1119
|
-
is_dict = ExprNodes.IntNode.for_int(node.pos, int(dict_obj.type
|
|
1155
|
+
is_dict = ExprNodes.IntNode.for_int(node.pos, int(dict_obj.type.is_pyanydict_type))
|
|
1120
1156
|
|
|
1121
1157
|
result_code = [
|
|
1122
1158
|
Nodes.SingleAssignmentNode(
|
|
@@ -1129,9 +1165,11 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
1129
1165
|
lhs = dict_temp,
|
|
1130
1166
|
rhs = ExprNodes.PythonCapiCallNode(
|
|
1131
1167
|
dict_obj.pos,
|
|
1132
|
-
"__Pyx_dict_iterator",
|
|
1168
|
+
"__Pyx_dict_iterator_legacy" if legacy_method else "__Pyx_dict_iterator",
|
|
1133
1169
|
self.PyDict_Iterator_func_type,
|
|
1134
|
-
utility_code = UtilityCode.load_cached(
|
|
1170
|
+
utility_code = UtilityCode.load_cached(
|
|
1171
|
+
"dict_iter_legacy" if legacy_method else "dict_iter",
|
|
1172
|
+
"Optimize.c"),
|
|
1135
1173
|
args = [dict_obj, is_dict, method_node, dict_len_temp_addr, is_dict_temp_addr],
|
|
1136
1174
|
is_temp=True,
|
|
1137
1175
|
)),
|
|
@@ -1201,7 +1239,7 @@ class IterationTransform(Visitor.EnvTransform):
|
|
|
1201
1239
|
iter_next_node = iter_next_node.analyse_expressions(self.current_env())
|
|
1202
1240
|
body.stats[0:0] = [iter_next_node]
|
|
1203
1241
|
|
|
1204
|
-
is_set = ExprNodes.IntNode.for_int(node.pos, int(set_obj.type
|
|
1242
|
+
is_set = ExprNodes.IntNode.for_int(node.pos, int(set_obj.type.is_pyset_type))
|
|
1205
1243
|
|
|
1206
1244
|
result_code = [
|
|
1207
1245
|
Nodes.SingleAssignmentNode(
|
|
@@ -1566,6 +1604,9 @@ class DropRefcountingTransform(Visitor.VisitorTransform):
|
|
|
1566
1604
|
"""
|
|
1567
1605
|
visit_Node = Visitor.VisitorTransform.recurse_to_children
|
|
1568
1606
|
|
|
1607
|
+
in_return_or_yield = False
|
|
1608
|
+
in_parallel = False
|
|
1609
|
+
|
|
1569
1610
|
def visit_ParallelAssignmentNode(self, node):
|
|
1570
1611
|
"""
|
|
1571
1612
|
Parallel swap assignments like 'a,b = b,a' are safe.
|
|
@@ -1653,7 +1694,7 @@ class DropRefcountingTransform(Visitor.VisitorTransform):
|
|
|
1653
1694
|
name_path.append(obj_node.name)
|
|
1654
1695
|
names.append( ('.'.join(name_path[::-1]), node) )
|
|
1655
1696
|
elif node.is_subscript:
|
|
1656
|
-
if node.base.type
|
|
1697
|
+
if not node.base.type.is_pylist_type:
|
|
1657
1698
|
return False
|
|
1658
1699
|
if not node.index.type.is_int:
|
|
1659
1700
|
return False
|
|
@@ -1676,6 +1717,74 @@ class DropRefcountingTransform(Visitor.VisitorTransform):
|
|
|
1676
1717
|
return None
|
|
1677
1718
|
return (base.name, index_val)
|
|
1678
1719
|
|
|
1720
|
+
def visit_ReturnStatNode(self, node):
|
|
1721
|
+
in_return_or_yield, self.in_return_or_yield = self.in_return_or_yield, True
|
|
1722
|
+
result = self.visit_Node(node)
|
|
1723
|
+
self.in_return_or_yield = in_return_or_yield
|
|
1724
|
+
return result
|
|
1725
|
+
|
|
1726
|
+
def visit_YieldExprNode(self, node):
|
|
1727
|
+
return self.visit_ReturnStatNode(node)
|
|
1728
|
+
|
|
1729
|
+
def visit_ParallelStatNode(self, node):
|
|
1730
|
+
in_parallel, self.in_parallel = self.in_parallel, True
|
|
1731
|
+
result = self.visit_Node(node)
|
|
1732
|
+
self.in_parallel = in_parallel
|
|
1733
|
+
return result
|
|
1734
|
+
|
|
1735
|
+
def visit_MemoryViewSliceNode(self, node):
|
|
1736
|
+
result = self.visit_Node(node)
|
|
1737
|
+
if not node.type.is_memoryviewslice or not node.is_temp:
|
|
1738
|
+
return result
|
|
1739
|
+
if self.in_return_or_yield:
|
|
1740
|
+
return result
|
|
1741
|
+
if self.in_parallel:
|
|
1742
|
+
# TODO - I'd like to apply a looser set of rules here.
|
|
1743
|
+
# However parallel blocks appear to do some cleanup even on unmanaged temps.
|
|
1744
|
+
# This needs a further look to see if it's a wider bug, but right now it
|
|
1745
|
+
# means we have to exclude all slicing in parallel from the optimization.
|
|
1746
|
+
return result
|
|
1747
|
+
# What we're trying to work out is whether we can drop
|
|
1748
|
+
# the reference counting for the temp.
|
|
1749
|
+
# We should be fairly conservative here.
|
|
1750
|
+
# We require a local variable name node (so that we know
|
|
1751
|
+
# that nothing external can reassign it while we're working)
|
|
1752
|
+
if not node.base.is_name:
|
|
1753
|
+
return result
|
|
1754
|
+
entry = node.base.entry
|
|
1755
|
+
if not entry or entry.scope.is_module_scope:
|
|
1756
|
+
return result
|
|
1757
|
+
|
|
1758
|
+
# anything in a (non-generator) closure is suspect
|
|
1759
|
+
if entry.from_closure or entry.in_closure:
|
|
1760
|
+
defining_scope = entry.outer_entry.scope if entry.from_closure else entry.scope
|
|
1761
|
+
if defining_scope.is_closure_scope and not defining_scope.is_pure_generator_scope:
|
|
1762
|
+
return result
|
|
1763
|
+
|
|
1764
|
+
# We then exclude any rhs that has a parallel or
|
|
1765
|
+
# a named expression assignment or the basis that they might
|
|
1766
|
+
# be reassigned while the temp is still active.
|
|
1767
|
+
# TODO - this can be more sophisticated and use
|
|
1768
|
+
# the same logic as in https://github.com/cython/cython/pull/4607.
|
|
1769
|
+
# In that case we can probably drop AssignemntType from
|
|
1770
|
+
# NameAssignment (since it was added just to help with this)
|
|
1771
|
+
from .FlowControl import AssignmentType
|
|
1772
|
+
unsafe_assigment_types = (
|
|
1773
|
+
AssignmentType.Parallel,
|
|
1774
|
+
AssignmentType.AssignmentExpression
|
|
1775
|
+
)
|
|
1776
|
+
for assignment in entry.cf_assignments:
|
|
1777
|
+
if assignment.assignment_type in unsafe_assigment_types:
|
|
1778
|
+
return result
|
|
1779
|
+
if self.in_parallel and not assignment.is_arg:
|
|
1780
|
+
# If we're in a parallel block, take the view that
|
|
1781
|
+
# we can't reason about any assignment to the rhs.
|
|
1782
|
+
return result
|
|
1783
|
+
|
|
1784
|
+
node.use_borrowed_ref = True
|
|
1785
|
+
node.use_managed_ref = False
|
|
1786
|
+
return result
|
|
1787
|
+
|
|
1679
1788
|
|
|
1680
1789
|
class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
|
|
1681
1790
|
"""Optimize some common calls to builtin types *before* the type
|
|
@@ -1763,7 +1872,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
|
|
|
1763
1872
|
if len(pos_args) > 1:
|
|
1764
1873
|
self._error_wrong_arg_count('float', node, pos_args, 1)
|
|
1765
1874
|
arg_type = getattr(pos_args[0], 'type', None)
|
|
1766
|
-
if arg_type
|
|
1875
|
+
if arg_type and (arg_type is PyrexTypes.c_double_type or arg_type.is_pyfloat_type):
|
|
1767
1876
|
return pos_args[0]
|
|
1768
1877
|
return node
|
|
1769
1878
|
|
|
@@ -1881,7 +1990,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
|
|
|
1881
1990
|
return node
|
|
1882
1991
|
|
|
1883
1992
|
arg = pos_args[0]
|
|
1884
|
-
if isinstance(arg, ExprNodes.ComprehensionNode) and arg.type
|
|
1993
|
+
if isinstance(arg, ExprNodes.ComprehensionNode) and arg.type.is_pylist_type:
|
|
1885
1994
|
list_node = arg
|
|
1886
1995
|
|
|
1887
1996
|
elif isinstance(arg, ExprNodes.GeneratorExpressionNode):
|
|
@@ -1912,7 +2021,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
|
|
|
1912
2021
|
list_node = ExprNodes.PythonCapiCallNode(
|
|
1913
2022
|
node.pos,
|
|
1914
2023
|
"__Pyx_PySequence_ListKeepNew"
|
|
1915
|
-
if arg.result_in_temp() and arg.type
|
|
2024
|
+
if arg.result_in_temp() and (arg.type is PyrexTypes.py_object_type or arg.type.is_pylist_type)
|
|
1916
2025
|
else "PySequence_List",
|
|
1917
2026
|
self.PySequence_List_func_type,
|
|
1918
2027
|
args=pos_args, is_temp=True)
|
|
@@ -2070,7 +2179,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
|
|
|
2070
2179
|
|
|
2071
2180
|
result_node = ExprNodes.InlinedGeneratorExpressionNode(
|
|
2072
2181
|
node.pos, gen_expr_node,
|
|
2073
|
-
orig_func='set' if target_type
|
|
2182
|
+
orig_func='set' if target_type.is_pyset_type else 'list',
|
|
2074
2183
|
comprehension_type=target_type)
|
|
2075
2184
|
|
|
2076
2185
|
for yield_expression, yield_stat_node in yield_statements:
|
|
@@ -2217,7 +2326,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2217
2326
|
if isinstance(arg, ExprNodes.PyTypeTestNode):
|
|
2218
2327
|
arg = arg.arg
|
|
2219
2328
|
if isinstance(arg, ExprNodes.CoerceToPyTypeNode):
|
|
2220
|
-
if arg.type
|
|
2329
|
+
if arg.type is PyrexTypes.py_object_type or arg.type.is_pybool_type:
|
|
2221
2330
|
return arg.arg.coerce_to_boolean(self.current_env())
|
|
2222
2331
|
return node
|
|
2223
2332
|
|
|
@@ -2236,7 +2345,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2236
2345
|
if arg.function.name == 'float' and len(arg.args) == 1:
|
|
2237
2346
|
# undo redundant Py->C->Py coercion
|
|
2238
2347
|
func_arg = arg.args[0]
|
|
2239
|
-
if func_arg.type
|
|
2348
|
+
if func_arg.type.is_pyfloat_type:
|
|
2240
2349
|
return func_arg.as_none_safe_node("float() argument must be a string or a number, not 'NoneType'")
|
|
2241
2350
|
elif func_arg.type.is_pyobject and arg.function.cname == "__Pyx_PyObject_AsDouble":
|
|
2242
2351
|
return ExprNodes.PythonCapiCallNode(
|
|
@@ -2271,11 +2380,11 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2271
2380
|
node.type.is_int and isinstance(arg, ExprNodes.BoolNode)):
|
|
2272
2381
|
return arg.coerce_to(node.type, self.current_env())
|
|
2273
2382
|
elif isinstance(arg, ExprNodes.CoerceToPyTypeNode):
|
|
2274
|
-
if arg.type is PyrexTypes.py_object_type:
|
|
2383
|
+
if arg.type is PyrexTypes.py_object_type or arg.type.is_builtin_type:
|
|
2275
2384
|
if node.type.assignable_from(arg.arg.type):
|
|
2276
2385
|
# completely redundant C->Py->C coercion
|
|
2277
2386
|
return arg.arg.coerce_to(node.type, self.current_env())
|
|
2278
|
-
elif arg.type
|
|
2387
|
+
elif arg.type.is_pystr_type:
|
|
2279
2388
|
if arg.arg.type.is_unicode_char and node.type.is_unicode_char:
|
|
2280
2389
|
return arg.arg.coerce_to(node.type, self.current_env())
|
|
2281
2390
|
elif isinstance(arg, ExprNodes.SimpleCallNode):
|
|
@@ -2462,7 +2571,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2462
2571
|
return ExprNodes.UnicodeNode(node.pos, value=EncodedString())
|
|
2463
2572
|
return node
|
|
2464
2573
|
arg = pos_args[0]
|
|
2465
|
-
if arg.type
|
|
2574
|
+
if arg.type.is_pystr_type:
|
|
2466
2575
|
if not arg.may_be_none():
|
|
2467
2576
|
return arg
|
|
2468
2577
|
cname = "__Pyx_PyUnicode_Unicode"
|
|
@@ -2485,7 +2594,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2485
2594
|
a call to the unicode() builtin, which is optimised right above.
|
|
2486
2595
|
"""
|
|
2487
2596
|
self.visitchildren(node)
|
|
2488
|
-
if node.value.type
|
|
2597
|
+
if node.value.type.is_pystr_type and not node.c_format_spec and not node.format_spec:
|
|
2489
2598
|
if not node.conversion_char or node.conversion_char == 's':
|
|
2490
2599
|
# value is definitely a unicode string and we don't format it any special
|
|
2491
2600
|
return self._handle_simple_function_unicode(node, None, [node.value])
|
|
@@ -2493,7 +2602,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2493
2602
|
|
|
2494
2603
|
PyDict_Copy_func_type = PyrexTypes.CFuncType(
|
|
2495
2604
|
Builtin.dict_type, [
|
|
2496
|
-
PyrexTypes.CFuncTypeArg("dict",
|
|
2605
|
+
PyrexTypes.CFuncTypeArg("dict", PyrexTypes.py_object_type, None)
|
|
2497
2606
|
])
|
|
2498
2607
|
|
|
2499
2608
|
def _handle_simple_function_dict(self, node, function, pos_args):
|
|
@@ -2502,7 +2611,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2502
2611
|
if len(pos_args) != 1:
|
|
2503
2612
|
return node
|
|
2504
2613
|
arg = pos_args[0]
|
|
2505
|
-
if arg.type
|
|
2614
|
+
if arg.type.is_pydict_type:
|
|
2506
2615
|
arg = arg.as_none_safe_node("'NoneType' is not iterable")
|
|
2507
2616
|
return ExprNodes.PythonCapiCallNode(
|
|
2508
2617
|
node.pos, "PyDict_Copy", self.PyDict_Copy_func_type,
|
|
@@ -2525,7 +2634,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2525
2634
|
node.pos,
|
|
2526
2635
|
"__Pyx_PySequence_ListKeepNew"
|
|
2527
2636
|
if (node.result_in_temp() and arg.result_in_temp() and
|
|
2528
|
-
arg.type
|
|
2637
|
+
(arg.type is PyrexTypes.py_object_type or arg.type.is_pylist_type))
|
|
2529
2638
|
else "PySequence_List",
|
|
2530
2639
|
self.PySequence_List_func_type,
|
|
2531
2640
|
args=pos_args,
|
|
@@ -2543,9 +2652,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2543
2652
|
if len(pos_args) != 1 or not node.result_in_temp():
|
|
2544
2653
|
return node
|
|
2545
2654
|
arg = pos_args[0]
|
|
2546
|
-
if arg.type
|
|
2655
|
+
if arg.type.is_pytuple_type and not arg.may_be_none():
|
|
2547
2656
|
return arg
|
|
2548
|
-
if arg.type
|
|
2657
|
+
if arg.type.is_pylist_type:
|
|
2549
2658
|
pos_args[0] = arg.as_none_safe_node(
|
|
2550
2659
|
"'NoneType' object is not iterable")
|
|
2551
2660
|
|
|
@@ -2600,7 +2709,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2600
2709
|
pos_args = [ExprNodes.NullNode(node.pos)]
|
|
2601
2710
|
elif len(pos_args) > 1:
|
|
2602
2711
|
return node
|
|
2603
|
-
elif pos_args[0].type
|
|
2712
|
+
elif pos_args[0].type.is_pyfrozenset_type and not pos_args[0].may_be_none():
|
|
2604
2713
|
return pos_args[0]
|
|
2605
2714
|
# PyFrozenSet_New(it) is better than a generic Python call to frozenset(it)
|
|
2606
2715
|
return ExprNodes.PythonCapiCallNode(
|
|
@@ -2647,16 +2756,16 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2647
2756
|
arg = pos_args[0].as_none_safe_node(
|
|
2648
2757
|
"float() argument must be a string or a number, not 'NoneType'")
|
|
2649
2758
|
|
|
2650
|
-
if func_arg.type
|
|
2759
|
+
if func_arg.type.is_pybytes_type:
|
|
2651
2760
|
cfunc_name = "__Pyx_PyBytes_AsDouble"
|
|
2652
2761
|
utility_code_name = 'pybytes_as_double'
|
|
2653
|
-
elif func_arg.type
|
|
2762
|
+
elif func_arg.type.is_pybytearray_type:
|
|
2654
2763
|
cfunc_name = "__Pyx_PyByteArray_AsDouble"
|
|
2655
2764
|
utility_code_name = 'pybytes_as_double'
|
|
2656
|
-
elif func_arg.type
|
|
2765
|
+
elif func_arg.type.is_pystr_type:
|
|
2657
2766
|
cfunc_name = "__Pyx_PyUnicode_AsDouble"
|
|
2658
2767
|
utility_code_name = 'pyunicode_as_double'
|
|
2659
|
-
elif func_arg.type
|
|
2768
|
+
elif func_arg.type.is_pyint_type:
|
|
2660
2769
|
cfunc_name = "PyLong_AsDouble"
|
|
2661
2770
|
utility_code_name = None
|
|
2662
2771
|
else:
|
|
@@ -2774,19 +2883,29 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2774
2883
|
],
|
|
2775
2884
|
exception_value=-1)
|
|
2776
2885
|
|
|
2777
|
-
_map_to_capi_len_function = {
|
|
2778
|
-
Builtin.unicode_type: "__Pyx_PyUnicode_GET_LENGTH",
|
|
2779
|
-
Builtin.bytes_type: "__Pyx_PyBytes_GET_SIZE",
|
|
2780
|
-
Builtin.bytearray_type: '__Pyx_PyByteArray_GET_SIZE',
|
|
2781
|
-
Builtin.list_type: "__Pyx_PyList_GET_SIZE",
|
|
2782
|
-
Builtin.tuple_type: "__Pyx_PyTuple_GET_SIZE",
|
|
2783
|
-
Builtin.set_type: "__Pyx_PySet_GET_SIZE",
|
|
2784
|
-
Builtin.frozenset_type: "__Pyx_PySet_GET_SIZE",
|
|
2785
|
-
Builtin.dict_type: "PyDict_Size",
|
|
2786
|
-
}.get
|
|
2787
|
-
|
|
2788
2886
|
_ext_types_with_pysize = {"cpython.array.array"}
|
|
2789
2887
|
|
|
2888
|
+
def _find_special_capi_len_function(self, typ) -> Optional[str]:
|
|
2889
|
+
if typ.is_extension_type:
|
|
2890
|
+
if typ.entry.qualified_name in self._ext_types_with_pysize:
|
|
2891
|
+
return 'Py_SIZE'
|
|
2892
|
+
elif typ.is_builtin_type:
|
|
2893
|
+
if typ.is_pystr_type:
|
|
2894
|
+
return "__Pyx_PyUnicode_GET_LENGTH"
|
|
2895
|
+
if typ.is_pybytes_type:
|
|
2896
|
+
return "__Pyx_PyBytes_GET_SIZE"
|
|
2897
|
+
if typ.is_pybytearray_type:
|
|
2898
|
+
return "__Pyx_PyByteArray_GET_SIZE"
|
|
2899
|
+
if typ.is_pylist_type:
|
|
2900
|
+
return "__Pyx_PyList_GET_SIZE"
|
|
2901
|
+
if typ.is_pytuple_type:
|
|
2902
|
+
return "__Pyx_PyTuple_GET_SIZE"
|
|
2903
|
+
if typ.is_pyanyset_type:
|
|
2904
|
+
return "__Pyx_PySet_GET_SIZE"
|
|
2905
|
+
if typ.is_pyanydict_type:
|
|
2906
|
+
return "PyDict_Size"
|
|
2907
|
+
return None
|
|
2908
|
+
|
|
2790
2909
|
def _handle_simple_function_len(self, node, function, pos_args):
|
|
2791
2910
|
"""Replace len(char*) by the equivalent call to strlen(),
|
|
2792
2911
|
len(Py_UNICODE) by the equivalent Py_UNICODE_strlen() and
|
|
@@ -2818,14 +2937,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
2818
2937
|
node.pos, "__Pyx_MemoryView_Len", func_type,
|
|
2819
2938
|
args=[arg], is_temp=node.is_temp)
|
|
2820
2939
|
elif arg.type.is_pyobject:
|
|
2821
|
-
cfunc_name = self.
|
|
2940
|
+
cfunc_name = self._find_special_capi_len_function(arg.type)
|
|
2822
2941
|
if cfunc_name is None:
|
|
2823
|
-
|
|
2824
|
-
if ((arg_type.is_extension_type or arg_type.is_builtin_type)
|
|
2825
|
-
and arg_type.entry.qualified_name in self._ext_types_with_pysize):
|
|
2826
|
-
cfunc_name = 'Py_SIZE'
|
|
2827
|
-
else:
|
|
2828
|
-
return node
|
|
2942
|
+
return node
|
|
2829
2943
|
arg = arg.as_none_safe_node(
|
|
2830
2944
|
"object of type 'NoneType' has no len()")
|
|
2831
2945
|
new_node = ExprNodes.PythonCapiCallNode(
|
|
@@ -3316,6 +3430,11 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3316
3430
|
may_return_none = True,
|
|
3317
3431
|
utility_code = load_c_utility("dict_getitem_default"))
|
|
3318
3432
|
|
|
3433
|
+
# frozendict shares the read-only method handlers with dict.
|
|
3434
|
+
# Mutating handlers (pop, setdefault) are intentionally NOT aliased:
|
|
3435
|
+
# frozendict has no such methods, and we want those calls to fail loud.
|
|
3436
|
+
_handle_simple_method_frozendict_get = _handle_simple_method_dict_get
|
|
3437
|
+
|
|
3319
3438
|
Pyx_PyDict_SetDefault_func_type = PyrexTypes.CFuncType(
|
|
3320
3439
|
PyrexTypes.py_object_type, [
|
|
3321
3440
|
PyrexTypes.CFuncTypeArg("dict", PyrexTypes.py_object_type, None),
|
|
@@ -3392,7 +3511,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3392
3511
|
PyrexTypes.CFuncTypeArg("zerodiv_check", PyrexTypes.c_bint_type, None),
|
|
3393
3512
|
], exception_value=None if ret_type.is_pyobject else ret_type.exception_value)
|
|
3394
3513
|
for ctype in (PyrexTypes.c_long_type, PyrexTypes.c_double_type)
|
|
3395
|
-
for ret_type in (PyrexTypes.py_object_type, PyrexTypes.c_bint_type)
|
|
3514
|
+
for ret_type in (Builtin.float_type, Builtin.int_type, PyrexTypes.py_object_type, PyrexTypes.c_bint_type)
|
|
3396
3515
|
}
|
|
3397
3516
|
|
|
3398
3517
|
def _handle_simple_method_object___add__(self, node, function, args, is_unbound_method):
|
|
@@ -3503,7 +3622,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3503
3622
|
if len(args) != 2:
|
|
3504
3623
|
return node
|
|
3505
3624
|
|
|
3506
|
-
if node.type.
|
|
3625
|
+
if node.type.is_pyint_type or node.type.is_pyfloat_type:
|
|
3626
|
+
ret_type = node.type
|
|
3627
|
+
elif node.type.is_pyobject:
|
|
3507
3628
|
ret_type = PyrexTypes.py_object_type
|
|
3508
3629
|
elif node.type is PyrexTypes.c_bint_type and operator in ('Eq', 'Ne'):
|
|
3509
3630
|
ret_type = PyrexTypes.c_bint_type
|
|
@@ -3511,7 +3632,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3511
3632
|
return node
|
|
3512
3633
|
|
|
3513
3634
|
result = optimise_numeric_binop(operator, node, ret_type, args[0], args[1])
|
|
3514
|
-
if
|
|
3635
|
+
if result is None:
|
|
3515
3636
|
return node
|
|
3516
3637
|
func_cname, utility_code, extra_args, num_type = result
|
|
3517
3638
|
assert all([arg.type.is_pyobject for arg in args])
|
|
@@ -3522,7 +3643,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3522
3643
|
func_cname,
|
|
3523
3644
|
self.Pyx_BinopInt_func_types[(num_type, ret_type)],
|
|
3524
3645
|
'__%s__' % operator[:3].lower(), is_unbound_method, args,
|
|
3525
|
-
may_return_none=
|
|
3646
|
+
may_return_none=ret_type is PyrexTypes.py_object_type,
|
|
3526
3647
|
with_none_check=False,
|
|
3527
3648
|
utility_code=utility_code)
|
|
3528
3649
|
|
|
@@ -3954,7 +4075,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
3954
4075
|
string_node = string_node.arg
|
|
3955
4076
|
|
|
3956
4077
|
string_type = string_node.type
|
|
3957
|
-
if string_type
|
|
4078
|
+
if string_type.is_pybytes_type or string_type.is_pybytearray_type:
|
|
3958
4079
|
if is_unbound_method:
|
|
3959
4080
|
string_node = string_node.as_none_safe_node(
|
|
3960
4081
|
"descriptor '%s' requires a '%s' object but received a 'NoneType'",
|
|
@@ -4030,7 +4151,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
4030
4151
|
stop = ExprNodes.IntNode(node.pos, value='PY_SSIZE_T_MAX',
|
|
4031
4152
|
constant_result=ExprNodes.not_a_constant)
|
|
4032
4153
|
helper_func_type = self._decode_bytes_func_type
|
|
4033
|
-
if string_type
|
|
4154
|
+
if string_type.is_pybytes_type:
|
|
4034
4155
|
utility_code_name = 'decode_bytes'
|
|
4035
4156
|
else:
|
|
4036
4157
|
utility_code_name = 'decode_bytearray'
|
|
@@ -4095,7 +4216,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
|
|
|
4095
4216
|
encoding = node.value.decode('ISO-8859-1')
|
|
4096
4217
|
node = ExprNodes.BytesNode(
|
|
4097
4218
|
node.pos, value=node.value, type=PyrexTypes.c_const_char_ptr_type)
|
|
4098
|
-
elif node.type
|
|
4219
|
+
elif node.type.is_pybytes_type:
|
|
4099
4220
|
encoding = None
|
|
4100
4221
|
node = node.coerce_to(PyrexTypes.c_const_char_ptr_type, self.current_env())
|
|
4101
4222
|
elif node.type.is_string:
|
|
@@ -4214,12 +4335,12 @@ def optimise_numeric_binop(operator, node, ret_type, arg0, arg1):
|
|
|
4214
4335
|
# Prefer constants on RHS as they allows better size control for some operators.
|
|
4215
4336
|
num_nodes = (ExprNodes.IntNode, ExprNodes.FloatNode)
|
|
4216
4337
|
if isinstance(arg1, num_nodes):
|
|
4217
|
-
if arg0.type is not PyrexTypes.py_object_type and arg0.type
|
|
4338
|
+
if arg0.type is not PyrexTypes.py_object_type and not arg0.type.is_pyint_type:
|
|
4218
4339
|
return None
|
|
4219
4340
|
numval = arg1
|
|
4220
4341
|
arg_order = 'ObjC'
|
|
4221
4342
|
elif isinstance(arg0, num_nodes):
|
|
4222
|
-
if arg1.type is not PyrexTypes.py_object_type and arg1.type
|
|
4343
|
+
if arg1.type is not PyrexTypes.py_object_type and not arg1.type.is_pyint_type:
|
|
4223
4344
|
return None
|
|
4224
4345
|
numval = arg0
|
|
4225
4346
|
arg_order = 'CObj'
|
|
@@ -4773,7 +4894,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
|
|
|
4773
4894
|
"""Unpack *args in place if we can."""
|
|
4774
4895
|
self.visitchildren(node)
|
|
4775
4896
|
|
|
4776
|
-
is_set = node.type
|
|
4897
|
+
is_set = node.type.is_pyset_type
|
|
4777
4898
|
args = []
|
|
4778
4899
|
values = []
|
|
4779
4900
|
|
|
@@ -4961,13 +5082,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
|
|
|
4961
5082
|
self.visitchildren(node)
|
|
4962
5083
|
if isinstance(node.loop, Nodes.StatListNode) and not node.loop.stats:
|
|
4963
5084
|
# loop was pruned already => transform into literal
|
|
4964
|
-
if node.type
|
|
5085
|
+
if node.type.is_pylist_type:
|
|
4965
5086
|
return ExprNodes.ListNode(
|
|
4966
5087
|
node.pos, args=[], constant_result=[])
|
|
4967
|
-
elif node.type
|
|
5088
|
+
elif node.type.is_pyset_type:
|
|
4968
5089
|
return ExprNodes.SetNode(
|
|
4969
5090
|
node.pos, args=[], constant_result=set())
|
|
4970
|
-
elif node.type
|
|
5091
|
+
elif node.type.is_pydict_type:
|
|
4971
5092
|
return ExprNodes.DictNode(
|
|
4972
5093
|
node.pos, key_value_pairs=[], constant_result={})
|
|
4973
5094
|
return node
|
|
@@ -5031,6 +5152,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
|
|
|
5031
5152
|
# will raise an error
|
|
5032
5153
|
return node
|
|
5033
5154
|
|
|
5155
|
+
def visit_MatchNode(self, node):
|
|
5156
|
+
# Not strictly constant-folding, but it refactor_cases needs to be done
|
|
5157
|
+
# before constant folding on the node children.
|
|
5158
|
+
node.refactor_cases()
|
|
5159
|
+
self.visitchildren(node)
|
|
5160
|
+
return node
|
|
5161
|
+
|
|
5034
5162
|
# in the future, other nodes can have their own handler method here
|
|
5035
5163
|
# that can replace them with a constant result node
|
|
5036
5164
|
|
|
@@ -5110,7 +5238,8 @@ class FinalOptimizePhase(Visitor.EnvTransform, Visitor.NodeRefCleanupMixin):
|
|
|
5110
5238
|
# function self object was moved into a CloneNode => undo
|
|
5111
5239
|
function.obj = function.obj.arg
|
|
5112
5240
|
node = self.replace(node, ExprNodes.PyMethodCallNode.from_node(
|
|
5113
|
-
node,
|
|
5241
|
+
node, env=self.current_env(),
|
|
5242
|
+
function=function, arg_tuple=node.arg_tuple, type=node.type,
|
|
5114
5243
|
unpack=self._check_optimize_method_calls(node)))
|
|
5115
5244
|
return node
|
|
5116
5245
|
|
|
@@ -5128,8 +5257,20 @@ class FinalOptimizePhase(Visitor.EnvTransform, Visitor.NodeRefCleanupMixin):
|
|
|
5128
5257
|
if not ExprNodes.PyMethodCallNode.can_be_used_for_function(function):
|
|
5129
5258
|
return node
|
|
5130
5259
|
|
|
5260
|
+
kwnames = kwvalues = kwdict = None
|
|
5261
|
+
if node.keyword_args and node.keyword_args.is_dict_literal:
|
|
5262
|
+
kwnames = ExprNodes.TupleNode(
|
|
5263
|
+
node.pos,
|
|
5264
|
+
args=[kvp.key for kvp in node.keyword_args.key_value_pairs])
|
|
5265
|
+
kwnames = kwnames.analyse_types(self.current_env(), skip_children=True)
|
|
5266
|
+
kwvalues = [kvp.value for kvp in node.keyword_args.key_value_pairs]
|
|
5267
|
+
elif node.keyword_args:
|
|
5268
|
+
kwdict = node.keyword_args
|
|
5269
|
+
|
|
5131
5270
|
node = self.replace(node, ExprNodes.PyMethodCallNode.from_node(
|
|
5132
|
-
node,
|
|
5271
|
+
node,
|
|
5272
|
+
function=function, arg_tuple=node.positional_args, kwdict=kwdict,
|
|
5273
|
+
kwnames=kwnames, kwvalues=kwvalues,
|
|
5133
5274
|
type=node.type, unpack=self._check_optimize_method_calls(node)))
|
|
5134
5275
|
return node
|
|
5135
5276
|
|
|
@@ -5225,23 +5366,38 @@ class FinalOptimizePhase(Visitor.EnvTransform, Visitor.NodeRefCleanupMixin):
|
|
|
5225
5366
|
seen = {}
|
|
5226
5367
|
values = node.values[:]
|
|
5227
5368
|
for i, fnode in enumerate(node.values):
|
|
5228
|
-
if
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5241
|
-
|
|
5242
|
-
|
|
5369
|
+
if isinstance(fnode, FormattedValueNode):
|
|
5370
|
+
fnode_value_node = fnode.value
|
|
5371
|
+
c_format_spec = fnode.c_format_spec
|
|
5372
|
+
format_spec = fnode.format_spec
|
|
5373
|
+
conversion_char = fnode.conversion_char
|
|
5374
|
+
|
|
5375
|
+
if isinstance(fnode.value, CoerceToPyTypeNode):
|
|
5376
|
+
# Coerced C values are probably safe.
|
|
5377
|
+
fnode_value_node = fnode_value_node.arg
|
|
5378
|
+
elif fnode.c_format_spec is not None:
|
|
5379
|
+
# Simple formatted C values are safe.
|
|
5380
|
+
pass
|
|
5381
|
+
elif fnode_value_node.type.is_builtin_type:
|
|
5382
|
+
# Most builtin Python types are probably safe as well.
|
|
5383
|
+
# FIXME: Except when a container type formats user defined values...
|
|
5384
|
+
# Thus, we might want to be more specific and allow only simple Python types.
|
|
5385
|
+
pass
|
|
5386
|
+
else:
|
|
5387
|
+
# Other Python objects are not safe as they can change their formatting on each access.
|
|
5388
|
+
continue
|
|
5389
|
+
|
|
5390
|
+
elif isinstance(fnode, ExprNodes.PythonCapiCallNode):
|
|
5391
|
+
if fnode.function.name != 'unicode':
|
|
5392
|
+
continue
|
|
5393
|
+
# f'{s}' formatting for a known str value?
|
|
5394
|
+
fnode_value_node = fnode.args[0]
|
|
5395
|
+
if not fnode_value_node.type.is_pystr_type:
|
|
5396
|
+
continue
|
|
5397
|
+
format_spec = c_format_spec = conversion_char = None
|
|
5398
|
+
|
|
5243
5399
|
else:
|
|
5244
|
-
#
|
|
5400
|
+
# Unicode string constants are deduplicated already.
|
|
5245
5401
|
continue
|
|
5246
5402
|
|
|
5247
5403
|
if not (fnode_value_node.is_name and fnode_value_node.is_simple()):
|
|
@@ -5251,7 +5407,7 @@ class FinalOptimizePhase(Visitor.EnvTransform, Visitor.NodeRefCleanupMixin):
|
|
|
5251
5407
|
# Otherwise, we'd have to stop with 'break' instead of allowing 'continue'.
|
|
5252
5408
|
continue
|
|
5253
5409
|
|
|
5254
|
-
key = (fnode_value_node.name,
|
|
5410
|
+
key = (fnode_value_node.name, c_format_spec, format_spec, conversion_char or 's')
|
|
5255
5411
|
seen_fnode = seen.setdefault(key, fnode)
|
|
5256
5412
|
if seen_fnode is fnode:
|
|
5257
5413
|
continue
|