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/ExprNodes.py
CHANGED
|
@@ -8,22 +8,25 @@ cython.declare(error=object, warning=object, warn_once=object, InternalError=obj
|
|
|
8
8
|
CompileError=object, UtilityCode=object, TempitaUtilityCode=object,
|
|
9
9
|
StringEncoding=object, operator=object, local_errors=object, report_error=object,
|
|
10
10
|
Naming=object, Nodes=object, PyrexTypes=object, py_object_type=object,
|
|
11
|
-
list_type=object, tuple_type=object, set_type=object, dict_type=object,
|
|
12
|
-
unicode_type=object, bytes_type=object, type_type=object,
|
|
11
|
+
list_type=object, tuple_type=object, set_type=object, dict_type=object, bool_type=object,
|
|
12
|
+
unicode_type=object, bytes_type=object, type_type=object, int_type=object,
|
|
13
13
|
Builtin=object, Symtab=object, Utils=object, find_coercion_error=object,
|
|
14
14
|
debug_disposal_code=object, debug_temp_alloc=object, debug_coercion=object,
|
|
15
|
-
bytearray_type=object, slice_type=object,
|
|
15
|
+
bytearray_type=object, slice_type=object,
|
|
16
16
|
builtin_sequence_types=object, build_line_table=object,
|
|
17
|
-
inspect=object, copy=object, os=object,
|
|
17
|
+
inspect=object, copy=object, os=object, re=object, sys=object,
|
|
18
|
+
itertools=object, defaultdict=object,
|
|
18
19
|
)
|
|
19
20
|
|
|
20
21
|
import copy
|
|
21
22
|
import inspect
|
|
23
|
+
import itertools
|
|
22
24
|
import operator
|
|
23
25
|
import os.path
|
|
24
|
-
import pathlib
|
|
25
26
|
import re
|
|
26
27
|
import sys
|
|
28
|
+
from collections import defaultdict
|
|
29
|
+
from functools import partial
|
|
27
30
|
from typing import Optional
|
|
28
31
|
|
|
29
32
|
from .Errors import (
|
|
@@ -41,8 +44,8 @@ from .PyrexTypes import c_char_ptr_type, py_object_type, typecast, error_type, \
|
|
|
41
44
|
from . import TypeSlots
|
|
42
45
|
from .Builtin import (
|
|
43
46
|
list_type, tuple_type, set_type, dict_type, type_type,
|
|
44
|
-
unicode_type, bytes_type, bytearray_type,
|
|
45
|
-
slice_type
|
|
47
|
+
unicode_type, bytes_type, bytearray_type, int_type, bool_type,
|
|
48
|
+
slice_type
|
|
46
49
|
)
|
|
47
50
|
from . import Builtin
|
|
48
51
|
from . import Symtab
|
|
@@ -106,6 +109,13 @@ coercion_error_dict = {
|
|
|
106
109
|
def find_coercion_error(type_tuple, default, env):
|
|
107
110
|
err = coercion_error_dict.get(type_tuple)
|
|
108
111
|
if err is None:
|
|
112
|
+
type1, type2 = type_tuple
|
|
113
|
+
if type1.supports_container_type and type2.supports_container_type:
|
|
114
|
+
# Typed containers. Validate the coercion of their subscripted types.
|
|
115
|
+
for subscripted_type1, subscripted_type2 in zip(type1.subscripted_types, type2.subscripted_types):
|
|
116
|
+
if (ret := find_coercion_error((subscripted_type1, subscripted_type2), default, env)) == default:
|
|
117
|
+
continue
|
|
118
|
+
return ret
|
|
109
119
|
return default
|
|
110
120
|
elif (env.directives['c_string_encoding'] and
|
|
111
121
|
any(t in type_tuple for t in (PyrexTypes.c_char_ptr_type, PyrexTypes.c_uchar_ptr_type,
|
|
@@ -148,7 +158,7 @@ def infer_sequence_item_type(env, seq_node, index_node=None, seq_type=None):
|
|
|
148
158
|
if not seq_node.is_sequence_constructor:
|
|
149
159
|
if seq_type is None:
|
|
150
160
|
seq_type = seq_node.infer_type(env)
|
|
151
|
-
if seq_type
|
|
161
|
+
if seq_type.is_pytuple_type:
|
|
152
162
|
# tuples are immutable => we can safely follow assignments
|
|
153
163
|
if seq_node.cf_state and len(seq_node.cf_state) == 1:
|
|
154
164
|
try:
|
|
@@ -172,24 +182,25 @@ def infer_sequence_item_type(env, seq_node, index_node=None, seq_type=None):
|
|
|
172
182
|
infer_sequence_item_type(env, item) if item.is_starred else item.infer_type(env)
|
|
173
183
|
for item in args_without_none
|
|
174
184
|
}
|
|
175
|
-
if None in item_types:
|
|
176
|
-
# Could not infer all starred types.
|
|
185
|
+
if None in item_types or py_object_type in item_types:
|
|
186
|
+
# Could not infer all (starred) types to specific types.
|
|
177
187
|
return None
|
|
178
|
-
|
|
179
|
-
|
|
188
|
+
item_type = PyrexTypes.reduce_spanning_types(
|
|
189
|
+
# Unpack the Python types to optimise and avoid a mix of inferred C and Python.
|
|
180
190
|
item_type.equivalent_type if item_type.is_pyobject and item_type.equivalent_type else item_type
|
|
181
191
|
for item_type in item_types
|
|
182
|
-
|
|
183
|
-
if
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
192
|
+
)
|
|
193
|
+
if item_type is py_object_type:
|
|
194
|
+
# Normalise "cannot infer anything specific" to "cannot infer".
|
|
195
|
+
return None
|
|
196
|
+
if has_none and not item_type.is_pyobject:
|
|
197
|
+
# Must be a Python type to cover 'None'.
|
|
198
|
+
item_type = item_type.equivalent_type # 'equivalent_type' may be None => cannot infer type
|
|
199
|
+
elif not has_none and (item_type.is_pybytes_type or item_type.is_pystr_type):
|
|
200
|
+
# Infer special case of single character sequences as single character type.
|
|
201
|
+
if all(arg.is_string_literal and arg.can_coerce_to_char_literal() for arg in args_without_none):
|
|
202
|
+
item_type = PyrexTypes.c_py_ucs4_type if item_type.is_pystr_type else PyrexTypes.c_uchar_type
|
|
203
|
+
return item_type
|
|
193
204
|
return None
|
|
194
205
|
|
|
195
206
|
|
|
@@ -331,6 +342,7 @@ class ExprNode(Node):
|
|
|
331
342
|
# annotation ExprNode or None PEP526 annotation for names or expressions
|
|
332
343
|
# generator_arg_tag None or Node A tag to mark ExprNodes that potentially need to
|
|
333
344
|
# be changed to a generator argument
|
|
345
|
+
# use_borrowed_ref boolean only relevant if is_temp; avoid disposing of the temp
|
|
334
346
|
|
|
335
347
|
result_ctype = None
|
|
336
348
|
type = None
|
|
@@ -342,6 +354,7 @@ class ExprNode(Node):
|
|
|
342
354
|
is_numpy_attribute = False
|
|
343
355
|
generator_arg_tag = None
|
|
344
356
|
in_parallel_block = False
|
|
357
|
+
use_borrowed_ref = False
|
|
345
358
|
|
|
346
359
|
# The Analyse Expressions phase for expressions is split
|
|
347
360
|
# into two sub-phases:
|
|
@@ -896,7 +909,7 @@ class ExprNode(Node):
|
|
|
896
909
|
if self.has_temp_moved:
|
|
897
910
|
code.globalstate.use_utility_code(
|
|
898
911
|
UtilityCode.load_cached("MoveIfSupported", "CppSupport.cpp"))
|
|
899
|
-
if self.is_temp:
|
|
912
|
+
if self.is_temp and not self.use_borrowed_ref:
|
|
900
913
|
if self.type.is_string or self.type.is_pyunicode_ptr:
|
|
901
914
|
# postponed from self.generate_evaluation_code()
|
|
902
915
|
self.generate_subexpr_disposal_code(code)
|
|
@@ -1088,7 +1101,7 @@ class ExprNode(Node):
|
|
|
1088
1101
|
# Apply a type check on assignment.
|
|
1089
1102
|
src = PyTypeTestNode(src, dst_type, env)
|
|
1090
1103
|
else:
|
|
1091
|
-
if dst_type
|
|
1104
|
+
if dst_type.is_pybytes_type and src.type.is_int:
|
|
1092
1105
|
src = CoerceIntToBytesNode(src, env)
|
|
1093
1106
|
else:
|
|
1094
1107
|
src = CoerceToPyTypeNode(src, env, type=dst_type)
|
|
@@ -1436,9 +1449,9 @@ class ConstNode(AtomicExprNode):
|
|
|
1436
1449
|
cls = IntNode
|
|
1437
1450
|
elif type.is_float:
|
|
1438
1451
|
cls = FloatNode
|
|
1439
|
-
elif type
|
|
1452
|
+
elif type.is_pybytes_type:
|
|
1440
1453
|
cls = BytesNode
|
|
1441
|
-
elif type
|
|
1454
|
+
elif type.is_pystr_type:
|
|
1442
1455
|
cls = UnicodeNode
|
|
1443
1456
|
|
|
1444
1457
|
if cls.type is type:
|
|
@@ -1457,7 +1470,7 @@ class BoolNode(ConstNode):
|
|
|
1457
1470
|
assert value is True or value is False, repr(value)
|
|
1458
1471
|
super().__init__(pos, value=value, constant_result=value)
|
|
1459
1472
|
if type is not None and type is not self.type:
|
|
1460
|
-
assert type
|
|
1473
|
+
assert type.is_pybool_type, type
|
|
1461
1474
|
self.type = type
|
|
1462
1475
|
|
|
1463
1476
|
def calculate_constant_result(self):
|
|
@@ -1485,7 +1498,7 @@ class BoolNode(ConstNode):
|
|
|
1485
1498
|
def coerce_to(self, dst_type, env):
|
|
1486
1499
|
if dst_type == self.type:
|
|
1487
1500
|
return self
|
|
1488
|
-
if dst_type is py_object_type and self.type
|
|
1501
|
+
if dst_type is py_object_type and self.type.is_pybool_type:
|
|
1489
1502
|
return self
|
|
1490
1503
|
if dst_type.is_pyobject and self.type.is_int:
|
|
1491
1504
|
return BoolNode(self.pos, value=self.value, type=Builtin.bool_type)
|
|
@@ -1580,7 +1593,7 @@ class IntNode(ConstNode):
|
|
|
1580
1593
|
def coerce_to(self, dst_type, env):
|
|
1581
1594
|
if self.type is dst_type:
|
|
1582
1595
|
return self
|
|
1583
|
-
elif dst_type.is_float or dst_type
|
|
1596
|
+
elif dst_type.is_float or dst_type.is_pyfloat_type:
|
|
1584
1597
|
if self.has_constant_result():
|
|
1585
1598
|
return FloatNode(self.pos, value='%d.0' % int(self.constant_result), type=dst_type,
|
|
1586
1599
|
constant_result=float(self.constant_result))
|
|
@@ -1773,7 +1786,7 @@ class BytesNode(ConstNode):
|
|
|
1773
1786
|
|
|
1774
1787
|
def as_sliced_node(self, start, stop, step=None):
|
|
1775
1788
|
value = StringEncoding.bytes_literal(self.value[start:stop:step], self.value.encoding)
|
|
1776
|
-
return BytesNode(self
|
|
1789
|
+
return BytesNode.from_node(self, value=value, constant_result=value)
|
|
1777
1790
|
|
|
1778
1791
|
def compile_time_value(self, denv):
|
|
1779
1792
|
return self.value.byteencode()
|
|
@@ -1799,12 +1812,11 @@ class BytesNode(ConstNode):
|
|
|
1799
1812
|
if dst_type.is_unicode_char:
|
|
1800
1813
|
error(self.pos, "Bytes literals cannot coerce to Py_UNICODE/Py_UCS4, use a unicode literal instead.")
|
|
1801
1814
|
return self
|
|
1802
|
-
return CharNode(self
|
|
1803
|
-
constant_result=ord(self.value))
|
|
1815
|
+
return CharNode.from_node(self, value=self.value, constant_result=ord(self.value))
|
|
1804
1816
|
|
|
1805
|
-
node = BytesNode(self
|
|
1817
|
+
node = BytesNode.from_node(self, value=self.value)
|
|
1806
1818
|
if dst_type.is_pyobject:
|
|
1807
|
-
if dst_type
|
|
1819
|
+
if dst_type.may_be_pybytes_type:
|
|
1808
1820
|
node.type = Builtin.bytes_type
|
|
1809
1821
|
else:
|
|
1810
1822
|
self.check_for_coercion_error(dst_type, env, fail=True)
|
|
@@ -1816,6 +1828,12 @@ class BytesNode(ConstNode):
|
|
|
1816
1828
|
node.type = (PyrexTypes.c_const_char_ptr_type if dst_type.base_type.is_const
|
|
1817
1829
|
else PyrexTypes.c_char_ptr_type)
|
|
1818
1830
|
return CastNode(node, dst_type)
|
|
1831
|
+
elif dst_type.is_array and dst_type.base_type.is_int:
|
|
1832
|
+
if len(self.value) == 0:
|
|
1833
|
+
error(self.pos, "Only non-empty bytes literals can be coerced into C arrays.")
|
|
1834
|
+
as_uchar = partial(IntNode.for_int, self.pos, type=PyrexTypes.c_uchar_type)
|
|
1835
|
+
return ListNode.from_node(
|
|
1836
|
+
self, args=[as_uchar(ch) for ch in self.value]).analyse_types(env).coerce_to(dst_type, env)
|
|
1819
1837
|
elif dst_type.assignable_from(PyrexTypes.c_char_ptr_type):
|
|
1820
1838
|
# Exclude the case of passing a C string literal into a non-const C++ string.
|
|
1821
1839
|
if not dst_type.is_cpp_class or dst_type.is_const:
|
|
@@ -1863,7 +1881,7 @@ class UnicodeNode(ConstNode):
|
|
|
1863
1881
|
super().__init__(pos, value=value, constant_result=value)
|
|
1864
1882
|
if bytes_value is not None:
|
|
1865
1883
|
self.bytes_value = bytes_value
|
|
1866
|
-
if type is not None and
|
|
1884
|
+
if type is not None and not type.is_pystr_type:
|
|
1867
1885
|
self.type = type
|
|
1868
1886
|
|
|
1869
1887
|
def calculate_constant_result(self):
|
|
@@ -1884,7 +1902,7 @@ class UnicodeNode(ConstNode):
|
|
|
1884
1902
|
|
|
1885
1903
|
def coerce_to(self, dst_type, env):
|
|
1886
1904
|
if dst_type is self.type:
|
|
1887
|
-
|
|
1905
|
+
return self
|
|
1888
1906
|
elif dst_type.is_unicode_char:
|
|
1889
1907
|
if not self.can_coerce_to_char_literal():
|
|
1890
1908
|
error(self.pos,
|
|
@@ -1895,25 +1913,35 @@ class UnicodeNode(ConstNode):
|
|
|
1895
1913
|
return IntNode.for_int(self.pos, int_value, type=dst_type)
|
|
1896
1914
|
elif dst_type.is_pyunicode_ptr:
|
|
1897
1915
|
return UnicodeNode(self.pos, value=self.value, type=dst_type)
|
|
1898
|
-
elif
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
if
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1916
|
+
elif dst_type.is_string or dst_type.is_cpp_string or dst_type.is_int or (
|
|
1917
|
+
dst_type.is_ptr and dst_type.base_type.is_void):
|
|
1918
|
+
# Allow using '-3' enforced unicode literals in a C char/char*/void* context.
|
|
1919
|
+
if self.bytes_value is not None:
|
|
1920
|
+
if dst_type.is_array:
|
|
1921
|
+
# Prevent an invalid assignment from a C string array and use a pointer instead.
|
|
1922
|
+
dst_type = dst_type.element_ptr_type()
|
|
1923
|
+
return BytesNode.from_node(
|
|
1924
|
+
self, value=self.bytes_value, constant_result=self.bytes_value).coerce_to(dst_type, env)
|
|
1925
|
+
if env.directives['c_string_encoding']:
|
|
1926
|
+
try:
|
|
1927
|
+
byte_string = self.value.encode(env.directives['c_string_encoding'])
|
|
1928
|
+
except (UnicodeEncodeError, LookupError):
|
|
1929
|
+
pass
|
|
1930
|
+
else:
|
|
1931
|
+
return BytesNode.from_node(self, value=byte_string).coerce_to(dst_type, env)
|
|
1932
|
+
if self.value.isascii():
|
|
1933
|
+
bytes_value = StringEncoding.BytesLiteral(self.value.encode('ascii'))
|
|
1934
|
+
return BytesNode.from_node(
|
|
1935
|
+
self, value=bytes_value, constant_result=bytes_value).coerce_to(dst_type, env)
|
|
1936
|
+
elif (dst_type.is_array or dst_type.is_ptr) and dst_type.base_type.is_int:
|
|
1937
|
+
# Coerce the sequence of characters to a bare integer C array, including Py_UCS4[].
|
|
1938
|
+
if len(self.value) == 0:
|
|
1939
|
+
error(self.pos, "Only non-empty Unicode string literals can be coerced into C arrays.")
|
|
1940
|
+
as_pyucs4 = partial(IntNode.for_int, self.pos, type=PyrexTypes.c_py_ucs4_type)
|
|
1941
|
+
return ListNode.from_node(
|
|
1942
|
+
self, args=[as_pyucs4(ord(ch)) for ch in self.value]).analyse_types(env).coerce_to(dst_type, env)
|
|
1943
|
+
|
|
1944
|
+
if not dst_type.is_pyobject:
|
|
1917
1945
|
error(self.pos,
|
|
1918
1946
|
"Unicode literals do not support coercion to C types other "
|
|
1919
1947
|
"than Py_UCS4/Py_UNICODE (for characters), Py_UNICODE* "
|
|
@@ -1929,17 +1957,17 @@ class UnicodeNode(ConstNode):
|
|
|
1929
1957
|
bool_value = bool(self.value)
|
|
1930
1958
|
return BoolNode(self.pos, value=bool_value)
|
|
1931
1959
|
|
|
1932
|
-
def
|
|
1960
|
+
def get_ustring_kind(self):
|
|
1933
1961
|
# Most strings will probably be ASCII.
|
|
1934
1962
|
if self.value.isascii():
|
|
1935
|
-
return
|
|
1963
|
+
return 0
|
|
1936
1964
|
max_charval = ord(max(self.value))
|
|
1937
1965
|
if max_charval <= 255:
|
|
1938
|
-
return
|
|
1966
|
+
return 1
|
|
1939
1967
|
elif max_charval <= 65535:
|
|
1940
|
-
return
|
|
1968
|
+
return 2
|
|
1941
1969
|
else:
|
|
1942
|
-
return
|
|
1970
|
+
return 4
|
|
1943
1971
|
|
|
1944
1972
|
def generate_evaluation_code(self, code):
|
|
1945
1973
|
if self.type.is_pyobject:
|
|
@@ -2121,7 +2149,7 @@ class NameNode(AtomicExprNode):
|
|
|
2121
2149
|
# Unfortunately the type attribute of type objects
|
|
2122
2150
|
# is used for the pointer to the type they represent.
|
|
2123
2151
|
return type_type
|
|
2124
|
-
elif (self.entry.type
|
|
2152
|
+
elif (self.entry.type.is_pystr_type and
|
|
2125
2153
|
self.entry == self.entry.type.entry and self.name in ('unicode', 'basestring')):
|
|
2126
2154
|
# Keep recognising the old Py2 names for 'str' as type.
|
|
2127
2155
|
return type_type
|
|
@@ -2322,6 +2350,8 @@ class NameNode(AtomicExprNode):
|
|
|
2322
2350
|
elif (self.entry and self.entry.is_inherited and
|
|
2323
2351
|
self.annotation and env.is_c_dataclass_scope):
|
|
2324
2352
|
error(self.pos, "Cannot redeclare inherited fields in Cython dataclasses")
|
|
2353
|
+
elif self.entry and self.annotation and env.directives['annotation_typing']:
|
|
2354
|
+
error(self.pos, f"'{self.name}' redeclared")
|
|
2325
2355
|
if not self.entry:
|
|
2326
2356
|
if env.directives['warn.undeclared']:
|
|
2327
2357
|
warning(self.pos, "implicit declaration of '%s'" % self.name, 1)
|
|
@@ -3107,8 +3137,10 @@ class IteratorNode(ScopedExprNode):
|
|
|
3107
3137
|
return CppIteratorNode(self.pos, sequence=sequence, reversed=True).analyse_types(env)
|
|
3108
3138
|
else:
|
|
3109
3139
|
self.sequence = self.sequence.coerce_to_pyobject(env)
|
|
3110
|
-
if self.sequence.type
|
|
3140
|
+
if self.sequence.type.is_pylist_type or self.sequence.type.is_pytuple_type:
|
|
3111
3141
|
self.sequence = self.sequence.as_none_safe_node("'NoneType' object is not iterable")
|
|
3142
|
+
if self.sequence.type.supports_container_type:
|
|
3143
|
+
self.type = self.sequence.type
|
|
3112
3144
|
self.is_temp = 1
|
|
3113
3145
|
return self
|
|
3114
3146
|
|
|
@@ -3143,6 +3175,8 @@ class IteratorNode(ScopedExprNode):
|
|
|
3143
3175
|
|
|
3144
3176
|
def infer_type(self, env):
|
|
3145
3177
|
sequence_type = self.sequence.infer_type(env)
|
|
3178
|
+
if sequence_type.supports_container_type and (item_type := sequence_type.infer_iterator_type()):
|
|
3179
|
+
return item_type
|
|
3146
3180
|
if sequence_type.is_array or sequence_type.is_ptr:
|
|
3147
3181
|
return sequence_type
|
|
3148
3182
|
elif sequence_type.is_cpp_class:
|
|
@@ -3160,7 +3194,7 @@ class IteratorNode(ScopedExprNode):
|
|
|
3160
3194
|
if sequence_type.is_array or sequence_type.is_ptr:
|
|
3161
3195
|
raise InternalError("for in carray slice not transformed")
|
|
3162
3196
|
|
|
3163
|
-
is_builtin_sequence = sequence_type
|
|
3197
|
+
is_builtin_sequence = sequence_type.is_pylist_type or sequence_type.is_pytuple_type
|
|
3164
3198
|
if not is_builtin_sequence:
|
|
3165
3199
|
# reversed() not currently optimised (see Optimize.py)
|
|
3166
3200
|
assert not self.reversed, "internal error: reversed() only implemented for list/tuple objects"
|
|
@@ -3180,7 +3214,7 @@ class IteratorNode(ScopedExprNode):
|
|
|
3180
3214
|
self.counter_cname = code.funcstate.allocate_temp(
|
|
3181
3215
|
PyrexTypes.c_py_ssize_t_type, manage_ref=False)
|
|
3182
3216
|
if self.reversed:
|
|
3183
|
-
if sequence_type
|
|
3217
|
+
if sequence_type.is_pylist_type:
|
|
3184
3218
|
len_func = '__Pyx_PyList_GET_SIZE'
|
|
3185
3219
|
else:
|
|
3186
3220
|
len_func = '__Pyx_PyTuple_GET_SIZE'
|
|
@@ -3263,12 +3297,12 @@ class IteratorNode(ScopedExprNode):
|
|
|
3263
3297
|
sequence_type = self.sequence.type
|
|
3264
3298
|
if self.reversed:
|
|
3265
3299
|
code.putln(f"if ({self.counter_cname} < 0) break;")
|
|
3266
|
-
if sequence_type
|
|
3300
|
+
if sequence_type.is_pylist_type:
|
|
3267
3301
|
self.generate_next_sequence_item('List', result_name, code)
|
|
3268
3302
|
code.putln(code.error_goto_if_null(result_name, self.pos))
|
|
3269
3303
|
code.put_gotref(result_name, py_object_type)
|
|
3270
3304
|
return
|
|
3271
|
-
elif sequence_type
|
|
3305
|
+
elif sequence_type.is_pytuple_type:
|
|
3272
3306
|
self.generate_next_sequence_item('Tuple', result_name, code)
|
|
3273
3307
|
code.putln(code.error_goto_if_null(result_name, self.pos))
|
|
3274
3308
|
code.put_gotref(result_name, py_object_type)
|
|
@@ -3485,18 +3519,29 @@ class NextNode(AtomicExprNode):
|
|
|
3485
3519
|
iterator_type = self.iterator.infer_type(env)
|
|
3486
3520
|
if iterator_type.is_ptr or iterator_type.is_array:
|
|
3487
3521
|
return iterator_type.base_type
|
|
3488
|
-
|
|
3522
|
+
if iterator_type.is_cpp_class:
|
|
3489
3523
|
item_type = env.lookup_operator_for_types(self.pos, "*", [iterator_type]).type.return_type
|
|
3490
3524
|
item_type = PyrexTypes.remove_cv_ref(item_type, remove_fakeref=True)
|
|
3491
3525
|
return item_type
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3526
|
+
sequence_node = self.iterator.sequence
|
|
3527
|
+
if not (
|
|
3528
|
+
sequence_node.is_sequence_constructor or
|
|
3529
|
+
sequence_node.is_dict_literal or
|
|
3530
|
+
sequence_node.is_set_literal
|
|
3531
|
+
):
|
|
3532
|
+
# Here we infer only non-literal sequences. Literals are inferred via special infer_sequence_item_type().
|
|
3533
|
+
sequence_type = sequence_node.infer_type(env)
|
|
3534
|
+
if sequence_type.supports_container_type:
|
|
3535
|
+
iterator_type = sequence_type.infer_iterator_type()
|
|
3536
|
+
if iterator_type:
|
|
3537
|
+
return iterator_type
|
|
3538
|
+
# Avoid duplication of complicated logic.
|
|
3539
|
+
fake_index_node = IndexNode(
|
|
3540
|
+
self.pos,
|
|
3541
|
+
base=self.iterator.sequence,
|
|
3542
|
+
index=IntNode(self.pos, value='PY_SSIZE_T_MAX',
|
|
3543
|
+
type=PyrexTypes.c_py_ssize_t_type))
|
|
3544
|
+
return fake_index_node.infer_type(env)
|
|
3500
3545
|
|
|
3501
3546
|
def analyse_types(self, env):
|
|
3502
3547
|
item_type = self.infer_type(env, self.iterator.type)
|
|
@@ -3771,40 +3816,40 @@ class JoinedStrNode(ExprNode):
|
|
|
3771
3816
|
num_items = len(self.values)
|
|
3772
3817
|
use_stack_memory = num_items < 32
|
|
3773
3818
|
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
elif (isinstance(node, FormattedValueNode) and
|
|
3780
|
-
node.c_format_spec != 'c' and node.value.type.is_numeric):
|
|
3781
|
-
# formatted C numbers are always ASCII
|
|
3782
|
-
pass
|
|
3783
|
-
elif isinstance(node, CloneNode):
|
|
3784
|
-
# we already know the result
|
|
3785
|
-
pass
|
|
3786
|
-
else:
|
|
3787
|
-
unknown_nodes.add(node)
|
|
3819
|
+
known_length = 0
|
|
3820
|
+
unknown_lengths = []
|
|
3821
|
+
unknown_nodes = []
|
|
3822
|
+
ustring_kind = 0
|
|
3823
|
+
node_occurrences = defaultdict(int)
|
|
3788
3824
|
|
|
3789
|
-
|
|
3790
|
-
counts = {}
|
|
3791
|
-
charval_parts = [str(max_char_value)]
|
|
3792
|
-
for node in self.values:
|
|
3825
|
+
for i, node in enumerate(self.values):
|
|
3793
3826
|
node.generate_evaluation_code(code)
|
|
3794
3827
|
|
|
3795
3828
|
if isinstance(node, UnicodeNode):
|
|
3796
|
-
|
|
3829
|
+
ustring_kind = max(ustring_kind, node.get_ustring_kind())
|
|
3830
|
+
known_length += len(node.value)
|
|
3831
|
+
continue
|
|
3832
|
+
elif isinstance(node, CloneNode) and node.arg in node_occurrences:
|
|
3833
|
+
# We already know the result but need to add the length.
|
|
3834
|
+
node_occurrences[node.arg] += 1
|
|
3835
|
+
continue
|
|
3797
3836
|
else:
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
charval_parts.append(f"__Pyx_PyUnicode_MAX_CHAR_VALUE({node.py_result()})")
|
|
3837
|
+
node_occurrences[node] += 1
|
|
3838
|
+
|
|
3839
|
+
unknown_lengths.append(i)
|
|
3802
3840
|
|
|
3803
|
-
if
|
|
3804
|
-
|
|
3841
|
+
if (isinstance(node, FormattedValueNode) and
|
|
3842
|
+
node.c_format_spec != 'c' and node.value.type.is_numeric):
|
|
3843
|
+
# Formatted C numbers are always ASCII.
|
|
3844
|
+
pass
|
|
3805
3845
|
else:
|
|
3806
|
-
|
|
3807
|
-
|
|
3846
|
+
unknown_nodes.append(i)
|
|
3847
|
+
|
|
3848
|
+
index_repetitions = {
|
|
3849
|
+
i: node_occurrences[node]
|
|
3850
|
+
for i, node in enumerate(self.values)
|
|
3851
|
+
if node_occurrences[node] > 1
|
|
3852
|
+
} or None
|
|
3808
3853
|
|
|
3809
3854
|
if use_stack_memory:
|
|
3810
3855
|
values_array = code.funcstate.allocate_temp(
|
|
@@ -3820,23 +3865,80 @@ class JoinedStrNode(ExprNode):
|
|
|
3820
3865
|
for i, node in enumerate(self.values):
|
|
3821
3866
|
code.putln('%s[%d] = %s;' % (values_array, i, node.py_result()))
|
|
3822
3867
|
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3868
|
+
def aggregate(indices, result_temp, result_temp_type, initial_value, cfunc_name, op, factors):
|
|
3869
|
+
assert op in '+|', op
|
|
3870
|
+
code.putln(f"{result_temp} = {initial_value};")
|
|
3871
|
+
if not indices:
|
|
3872
|
+
return
|
|
3873
|
+
|
|
3874
|
+
code.putln("#if __Pyx_PyUnicode_Join_CAN_USE_KIND_AND_LENGTH")
|
|
3875
|
+
|
|
3876
|
+
if len(indices) == 1:
|
|
3877
|
+
index = indices[0]
|
|
3878
|
+
factor = f" * {factors[index]}" if factors and index in factors else ''
|
|
3879
|
+
code.putln(f"{result_temp} {op}= {cfunc_name}({values_array}[{index}]){factor};")
|
|
3880
|
+
code.putln("#endif")
|
|
3881
|
+
return
|
|
3882
|
+
|
|
3883
|
+
index_count = len(indices)
|
|
3884
|
+
if index_count > 3:
|
|
3885
|
+
# Reduce code overhead for some common patterns.
|
|
3886
|
+
steps = {indices[i] - indices[i-1] for i in range(1, index_count)}
|
|
3887
|
+
if steps < {1, 2}:
|
|
3888
|
+
# steps all 1 => placeholder concatenation, all included
|
|
3889
|
+
# steps all 2 => alternating between text and placeholders
|
|
3890
|
+
step = steps.pop()
|
|
3891
|
+
min_index, max_index = indices[0], indices[-1]
|
|
3892
|
+
assert indices == list(range(min_index, max_index+1, step)), indices
|
|
3893
|
+
|
|
3894
|
+
code.putln(f"for (Py_ssize_t i={min_index}; i <= {max_index}; i += {step}) ""{")
|
|
3895
|
+
code.putln(f"{result_temp_type} l = {cfunc_name}({values_array}[i]);")
|
|
3896
|
+
if factors:
|
|
3897
|
+
# Duplicate substrings are rare, so just factor them in conditionally.
|
|
3898
|
+
code.putln("switch(i) {")
|
|
3899
|
+
by_count = operator.itemgetter(1)
|
|
3900
|
+
factors_max_first = sorted(factors.items(), key=by_count, reverse=True)
|
|
3901
|
+
for factor, groups in itertools.groupby(factors_max_first, key=by_count):
|
|
3902
|
+
code.put(''.join(f"case {i:d}: " for i, _ in sorted(groups)))
|
|
3903
|
+
code.putln(f"l *= {factor}; break;")
|
|
3904
|
+
code.putln("}")
|
|
3905
|
+
code.putln(f"{result_temp} {op}= l;")
|
|
3906
|
+
code.putln("}")
|
|
3907
|
+
code.putln("#endif")
|
|
3908
|
+
return
|
|
3909
|
+
|
|
3910
|
+
factor_strings = {index: f" * {factors[index]}" for index in factors} if factors else {}
|
|
3911
|
+
get_factor = factor_strings.get
|
|
3912
|
+
|
|
3913
|
+
expressions = [f"{cfunc_name}({values_array}[{i}]){get_factor(i, '')}" for i in indices]
|
|
3914
|
+
result = f' {op} '.join(expressions)
|
|
3915
|
+
code.putln(f"{result_temp} {op}= {result};")
|
|
3916
|
+
code.putln("#endif")
|
|
3827
3917
|
|
|
3828
3918
|
code.mark_pos(self.pos)
|
|
3919
|
+
|
|
3920
|
+
# Assume that getting the length of an exact Unicode string object will not fail.
|
|
3921
|
+
length_temp = code.funcstate.allocate_temp(PyrexTypes.c_py_ssize_t_type, manage_ref=False)
|
|
3922
|
+
aggregate(unknown_lengths, length_temp, 'Py_ssize_t', known_length, "__Pyx_PyUnicode_GET_LENGTH", '+', index_repetitions)
|
|
3923
|
+
|
|
3924
|
+
ukind_temp = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False)
|
|
3925
|
+
if ustring_kind == 4:
|
|
3926
|
+
# Cannot get larger.
|
|
3927
|
+
code.putln(f"{ukind_temp} = 4;")
|
|
3928
|
+
else:
|
|
3929
|
+
# or-ing isn't entirely correct here since it can produce value 3 from 1|2,
|
|
3930
|
+
# but we handle that in __Pyx_PyUnicode_Join().
|
|
3931
|
+
aggregate(unknown_nodes, ukind_temp, 'int', ustring_kind, "__Pyx_PyUnicode_KIND_04", '|', None)
|
|
3932
|
+
|
|
3829
3933
|
self.allocate_temp_result(code)
|
|
3934
|
+
|
|
3830
3935
|
code.globalstate.use_utility_code(UtilityCode.load_cached("JoinPyUnicode", "StringTools.c"))
|
|
3831
|
-
code.putln(
|
|
3832
|
-
self.result(),
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
# but we crop that in __Pyx_PyUnicode_Join().
|
|
3838
|
-
' | '.join(charval_parts),
|
|
3839
|
-
))
|
|
3936
|
+
code.putln(
|
|
3937
|
+
f'{self.result()} = __Pyx_PyUnicode_Join({values_array}, {num_items:d}, {length_temp}, {ukind_temp});'
|
|
3938
|
+
)
|
|
3939
|
+
|
|
3940
|
+
code.funcstate.release_temp(ukind_temp)
|
|
3941
|
+
code.funcstate.release_temp(length_temp)
|
|
3840
3942
|
|
|
3841
3943
|
if not use_stack_memory:
|
|
3842
3944
|
code.putln("PyMem_Free(%s);" % values_array)
|
|
@@ -3889,7 +3991,7 @@ class FormattedValueNode(ExprNode):
|
|
|
3889
3991
|
if self.c_format_spec is None:
|
|
3890
3992
|
self.value = self.value.coerce_to_pyobject(env)
|
|
3891
3993
|
if not self.format_spec and (not self.conversion_char or self.conversion_char == 's'):
|
|
3892
|
-
if resolved_type
|
|
3994
|
+
if resolved_type.is_pystr_type and not self.value.may_be_none():
|
|
3893
3995
|
# value is definitely a unicode string and we don't format it any special
|
|
3894
3996
|
return self.value
|
|
3895
3997
|
return self
|
|
@@ -3906,7 +4008,7 @@ class FormattedValueNode(ExprNode):
|
|
|
3906
4008
|
return
|
|
3907
4009
|
|
|
3908
4010
|
value_result = self.value.py_result()
|
|
3909
|
-
value_is_unicode = self.value.type
|
|
4011
|
+
value_is_unicode = self.value.type.is_pystr_type and not self.value.may_be_none()
|
|
3910
4012
|
if self.format_spec:
|
|
3911
4013
|
format_func = '__Pyx_PyObject_Format'
|
|
3912
4014
|
format_spec = self.format_spec.py_result()
|
|
@@ -4017,8 +4119,7 @@ class _IndexingBaseNode(ExprNode):
|
|
|
4017
4119
|
def is_ephemeral(self):
|
|
4018
4120
|
# in most cases, indexing will return a safe reference to an object in a container,
|
|
4019
4121
|
# so we consider the result safe if the base object is
|
|
4020
|
-
return self.base.is_ephemeral() or self.base.type
|
|
4021
|
-
unicode_type, bytes_type, bytearray_type)
|
|
4122
|
+
return self.base.is_ephemeral() or self.base.type.is_bytes_or_str_or_bytearray
|
|
4022
4123
|
|
|
4023
4124
|
def check_const_addr(self):
|
|
4024
4125
|
return self.base.check_const_addr() and self.index.check_const()
|
|
@@ -4076,7 +4177,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4076
4177
|
if base_type:
|
|
4077
4178
|
if base_type.is_string:
|
|
4078
4179
|
return False
|
|
4079
|
-
if base_type
|
|
4180
|
+
if base_type.is_bytes_or_str_or_bytearray:
|
|
4080
4181
|
return False
|
|
4081
4182
|
if isinstance(self.index, SliceNode):
|
|
4082
4183
|
# slicing!
|
|
@@ -4092,7 +4193,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4092
4193
|
def analyse_as_type(self, env):
|
|
4093
4194
|
modifier = self.base.as_cython_attribute()
|
|
4094
4195
|
|
|
4095
|
-
if modifier is not None and modifier in ('pointer', 'const', 'volatile'):
|
|
4196
|
+
if modifier is not None and modifier in ('pointer', 'const', 'volatile', 'restrict'):
|
|
4096
4197
|
base_type = self.index.analyse_as_type(env)
|
|
4097
4198
|
if base_type is None:
|
|
4098
4199
|
error(self.base.pos, f"invalid use of '{modifier}', argument is not a type")
|
|
@@ -4103,7 +4204,8 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4103
4204
|
|
|
4104
4205
|
# const[base_type] or volatile[base_type]
|
|
4105
4206
|
is_const = modifier == 'const'
|
|
4106
|
-
is_volatile =
|
|
4207
|
+
is_volatile = modifier == 'volatile'
|
|
4208
|
+
is_restrict = modifier == 'restrict'
|
|
4107
4209
|
if base_type.is_cv_qualified:
|
|
4108
4210
|
if base_type.is_const:
|
|
4109
4211
|
if is_const:
|
|
@@ -4113,12 +4215,16 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4113
4215
|
if is_volatile:
|
|
4114
4216
|
error(self.base.pos, "Duplicate 'volatile'")
|
|
4115
4217
|
is_volatile = True
|
|
4218
|
+
if base_type.is_restrict:
|
|
4219
|
+
if is_restrict:
|
|
4220
|
+
error(self.base.pos, "Duplicate 'restrict'")
|
|
4221
|
+
is_restrict = True
|
|
4116
4222
|
base_type = base_type.cv_base_type
|
|
4117
4223
|
if base_type.is_memoryviewslice:
|
|
4118
4224
|
error(self.base.pos,
|
|
4119
4225
|
f"Cannot declare memory view variable as '{modifier}'. Did you mean '{modifier}[item_type][:]' ?")
|
|
4120
|
-
return PyrexTypes.
|
|
4121
|
-
base_type, is_const=is_const, is_volatile=
|
|
4226
|
+
return PyrexTypes.c_qualifier_type(
|
|
4227
|
+
base_type, is_const=is_const, is_volatile=is_volatile, is_restrict=is_restrict)
|
|
4122
4228
|
|
|
4123
4229
|
base_type = self.base.analyse_as_type(env)
|
|
4124
4230
|
if base_type:
|
|
@@ -4147,6 +4253,10 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4147
4253
|
# C array
|
|
4148
4254
|
index = self.index.compile_time_value(env)
|
|
4149
4255
|
if index is not None:
|
|
4256
|
+
if self.index.is_name:
|
|
4257
|
+
entry = index # See NameNode.compile_time_value()
|
|
4258
|
+
if entry.type.is_enum:
|
|
4259
|
+
return PyrexTypes.CArrayType(base_type, entry.cname)
|
|
4150
4260
|
try:
|
|
4151
4261
|
index = int(index)
|
|
4152
4262
|
except (ValueError, TypeError):
|
|
@@ -4183,7 +4293,11 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4183
4293
|
elif base_type.is_pyunicode_ptr:
|
|
4184
4294
|
# sliced Py_UNICODE* strings must coerce to Python
|
|
4185
4295
|
return unicode_type
|
|
4186
|
-
elif base_type
|
|
4296
|
+
elif base_type.supports_container_type and not base_type.has_uniform_element_type:
|
|
4297
|
+
# Slicing a container with non-uniform element types (tuple[int, str]) depends on the slice indices,
|
|
4298
|
+
# so we can't infer a more specific type here.
|
|
4299
|
+
return base_type.get_container_type()
|
|
4300
|
+
elif base_type.is_builtin_sequence:
|
|
4187
4301
|
# slicing these returns the same type
|
|
4188
4302
|
return base_type
|
|
4189
4303
|
elif base_type.is_memoryviewslice:
|
|
@@ -4192,10 +4306,16 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4192
4306
|
# TODO: Handle buffers (hopefully without too much redundancy).
|
|
4193
4307
|
return py_object_type
|
|
4194
4308
|
|
|
4309
|
+
if base_type.supports_container_type:
|
|
4310
|
+
if not (self.base.is_sequence_constructor or self.base.is_dict_literal or self.base.is_set_literal):
|
|
4311
|
+
sub_type = base_type.infer_indexed_type(self.index.constant_result)
|
|
4312
|
+
if sub_type:
|
|
4313
|
+
return sub_type
|
|
4314
|
+
|
|
4195
4315
|
index_type = self.index.infer_type(env)
|
|
4196
4316
|
if index_type and index_type.is_int or isinstance(self.index, IntNode):
|
|
4197
4317
|
# indexing!
|
|
4198
|
-
if base_type
|
|
4318
|
+
if base_type.is_pystr_type:
|
|
4199
4319
|
# Py_UCS4 will automatically coerce to a unicode string
|
|
4200
4320
|
# if required, so this is safe. We only infer Py_UCS4
|
|
4201
4321
|
# when the index is a C integer type. Otherwise, we may
|
|
@@ -4204,9 +4324,9 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4204
4324
|
# to receive it, throw it away, and potentially rebuild it
|
|
4205
4325
|
# on a subsequent PyObject coercion.
|
|
4206
4326
|
return PyrexTypes.c_py_ucs4_type
|
|
4207
|
-
elif base_type
|
|
4327
|
+
elif base_type.is_pybytearray_type or base_type.is_pybytes_type:
|
|
4208
4328
|
return PyrexTypes.c_uchar_type
|
|
4209
|
-
elif base_type
|
|
4329
|
+
elif base_type.is_pytuple_type or base_type.is_pylist_type or base_type.is_pyset_type:
|
|
4210
4330
|
# if base is a literal, take a look at its values
|
|
4211
4331
|
item_type = infer_sequence_item_type(
|
|
4212
4332
|
env, self.base, self.index, seq_type=base_type)
|
|
@@ -4259,7 +4379,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4259
4379
|
return PythranExpr(pythran_indexing_type(base_type, [index_with_type]))
|
|
4260
4380
|
|
|
4261
4381
|
# may be slicing or indexing, we don't know
|
|
4262
|
-
if base_type
|
|
4382
|
+
if base_type.is_pystr_type:
|
|
4263
4383
|
# always returns its own type on Python indexing/slicing
|
|
4264
4384
|
return base_type
|
|
4265
4385
|
|
|
@@ -4349,17 +4469,17 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4349
4469
|
|
|
4350
4470
|
def analyse_as_pyobject(self, env, is_slice, getting, setting):
|
|
4351
4471
|
base_type = self.base.type
|
|
4352
|
-
if self.index.type.is_unicode_char and
|
|
4472
|
+
if self.index.type.is_unicode_char and not base_type.is_pyanydict_type:
|
|
4353
4473
|
# TODO: eventually fold into case below and remove warning, once people have adapted their code
|
|
4354
4474
|
warning(self.pos,
|
|
4355
4475
|
"Item lookup of unicode character codes now always converts to a Unicode string. "
|
|
4356
4476
|
"Use an explicit C integer cast to get back the previous integer lookup behaviour.", level=1)
|
|
4357
4477
|
self.index = self.index.coerce_to_pyobject(env)
|
|
4358
4478
|
self.is_temp = 1
|
|
4359
|
-
elif self.index.type.is_int and
|
|
4479
|
+
elif self.index.type.is_int and not base_type.is_pyanydict_type:
|
|
4360
4480
|
if (getting
|
|
4361
4481
|
and not env.directives['boundscheck']
|
|
4362
|
-
and (base_type
|
|
4482
|
+
and (base_type.is_pybytearray_type or base_type.is_pylist_type or base_type.is_pytuple_type)
|
|
4363
4483
|
and (not self.index.type.signed
|
|
4364
4484
|
or not env.directives['wraparound']
|
|
4365
4485
|
or (isinstance(self.index, IntNode) and
|
|
@@ -4375,27 +4495,27 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4375
4495
|
self.is_temp = 1
|
|
4376
4496
|
|
|
4377
4497
|
is_int_indexing = not is_slice and self.index.type.is_int
|
|
4378
|
-
if is_int_indexing and base_type
|
|
4498
|
+
if is_int_indexing and base_type.is_pystr_type:
|
|
4379
4499
|
# Py_UNICODE/Py_UCS4 will automatically coerce to a unicode string
|
|
4380
4500
|
# if required, so this is fast and safe
|
|
4381
4501
|
self.type = PyrexTypes.c_py_ucs4_type
|
|
4382
|
-
elif is_int_indexing and base_type
|
|
4502
|
+
elif is_int_indexing and (base_type.is_pybytearray_type or base_type.is_pybytes_type):
|
|
4383
4503
|
if setting:
|
|
4384
4504
|
self.type = PyrexTypes.c_uchar_type
|
|
4385
4505
|
else:
|
|
4386
4506
|
# not using 'uchar' to enable fast and safe error reporting as '-1'
|
|
4387
4507
|
self.type = PyrexTypes.c_int_type
|
|
4388
|
-
elif is_slice and base_type
|
|
4508
|
+
elif is_slice and (base_type.is_bytes_or_str_or_bytearray or base_type.is_pylist_type or base_type.is_pytuple_type):
|
|
4389
4509
|
self.type = base_type
|
|
4390
4510
|
else:
|
|
4391
4511
|
item_type = None
|
|
4392
|
-
if is_int_indexing and base_type
|
|
4512
|
+
if is_int_indexing and (base_type.is_pylist_type or base_type.is_pytuple_type):
|
|
4393
4513
|
item_type = infer_sequence_item_type(env, self.base, self.index, seq_type=base_type)
|
|
4394
4514
|
elif self.base.is_literal:
|
|
4395
4515
|
# Infer homogeneous item type when looping over container literals.
|
|
4396
4516
|
item_type = infer_sequence_item_type(env, self.base, seq_type=base_type)
|
|
4397
4517
|
|
|
4398
|
-
if base_type
|
|
4518
|
+
if base_type.is_pylist_type or base_type.is_pytuple_type or base_type.is_pyanydict_type:
|
|
4399
4519
|
# Do the None check explicitly (not in a helper, and regardless of 'nonecheck') to allow optimising it away.
|
|
4400
4520
|
self.base = self.base.as_none_safe_node("'NoneType' object is not subscriptable")
|
|
4401
4521
|
|
|
@@ -4408,6 +4528,13 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4408
4528
|
self.type = item_type
|
|
4409
4529
|
|
|
4410
4530
|
self.wrap_in_nonecheck_node(env, getting)
|
|
4531
|
+
|
|
4532
|
+
if base_type.supports_container_type and (sub_type := base_type.infer_indexed_type(self.index.constant_result)):
|
|
4533
|
+
if getting and not is_slice:
|
|
4534
|
+
return self.coerce_to(sub_type, env)
|
|
4535
|
+
elif setting:
|
|
4536
|
+
self.type = sub_type
|
|
4537
|
+
|
|
4411
4538
|
return self
|
|
4412
4539
|
|
|
4413
4540
|
def analyse_as_c_array(self, env, is_slice):
|
|
@@ -4653,29 +4780,30 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4653
4780
|
gil_message = "Indexing Python object"
|
|
4654
4781
|
|
|
4655
4782
|
def calculate_result_code(self):
|
|
4656
|
-
|
|
4783
|
+
base_type = self.base.type
|
|
4784
|
+
if base_type.is_pylist_type or base_type.is_pytuple_type or base_type.is_pybytearray_type:
|
|
4657
4785
|
# Note - These functions are missing error checks in not CYTHON_ASSUME_SAFE_MACROS.
|
|
4658
4786
|
# Since they're only used in optimized modes without boundschecking, I think this is
|
|
4659
4787
|
# a reasonable optimization to make.
|
|
4660
|
-
if
|
|
4788
|
+
if base_type.is_pylist_type:
|
|
4661
4789
|
index_code = "__Pyx_PyList_GET_ITEM(%s, %s)"
|
|
4662
|
-
elif
|
|
4790
|
+
elif base_type.is_pytuple_type:
|
|
4663
4791
|
index_code = "__Pyx_PyTuple_GET_ITEM(%s, %s)"
|
|
4664
|
-
elif
|
|
4792
|
+
elif base_type.is_pybytearray_type:
|
|
4665
4793
|
index_code = "((unsigned char)(__Pyx_PyByteArray_AsString(%s)[%s]))"
|
|
4666
4794
|
else:
|
|
4667
|
-
assert False, "unexpected base type in indexing: %s" %
|
|
4668
|
-
elif
|
|
4795
|
+
assert False, "unexpected base type in indexing: %s" % base_type
|
|
4796
|
+
elif base_type.is_cfunction:
|
|
4669
4797
|
return "%s<%s>" % (
|
|
4670
4798
|
self.base.result(),
|
|
4671
4799
|
",".join([param.empty_declaration_code() for param in self.type_indices]))
|
|
4672
|
-
elif
|
|
4800
|
+
elif base_type.is_ctuple:
|
|
4673
4801
|
index = self.index.constant_result
|
|
4674
4802
|
if index < 0:
|
|
4675
|
-
index +=
|
|
4803
|
+
index += base_type.size
|
|
4676
4804
|
return "%s.f%s" % (self.base.result(), index)
|
|
4677
4805
|
else:
|
|
4678
|
-
if (self.type.is_ptr or self.type.is_array) and self.type ==
|
|
4806
|
+
if (self.type.is_ptr or self.type.is_array) and self.type == base_type:
|
|
4679
4807
|
error(self.pos, "Invalid use of pointer slice")
|
|
4680
4808
|
return
|
|
4681
4809
|
index_code = "(%s[%s])"
|
|
@@ -4683,7 +4811,6 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4683
4811
|
|
|
4684
4812
|
def extra_index_params(self, code):
|
|
4685
4813
|
if self.index.type.is_int:
|
|
4686
|
-
is_list = self.base.type is list_type
|
|
4687
4814
|
wraparound = (
|
|
4688
4815
|
bool(code.globalstate.directives['wraparound']) and
|
|
4689
4816
|
self.original_index_type.signed and
|
|
@@ -4692,7 +4819,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4692
4819
|
boundscheck = bool(code.globalstate.directives['boundscheck'])
|
|
4693
4820
|
has_gil = not self.in_nogil_context
|
|
4694
4821
|
unsafe_shared = self.base.may_be_unsafe_shared()
|
|
4695
|
-
if (self.base.type
|
|
4822
|
+
if (self.base.type.is_pybytearray_type and not has_gil and boundscheck
|
|
4696
4823
|
and code.globalstate.directives['freethreading_compatible']):
|
|
4697
4824
|
# In principle this applies to 'wraparound' too. The warning only applies to
|
|
4698
4825
|
# 'boundscheck' just so there's an easy way to turn it off.
|
|
@@ -4700,11 +4827,11 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4700
4827
|
self.pos, "Indexing a bytearray with 'boundscheck' enabled and without the GIL "
|
|
4701
4828
|
"is not thread-safe on freethreaded Python "
|
|
4702
4829
|
"(disable 'boundscheck' to turn off this warning)", 1)
|
|
4703
|
-
return ", %s, %d, %s, %d, %d, %d, %
|
|
4830
|
+
return ", %s, %d, %s, %d, %d, %d, %s" % (
|
|
4704
4831
|
self.original_index_type.empty_declaration_code(),
|
|
4705
4832
|
self.original_index_type.signed and 1 or 0,
|
|
4706
4833
|
self.original_index_type.to_py_function,
|
|
4707
|
-
|
|
4834
|
+
wraparound, boundscheck, has_gil, unsafe_shared)
|
|
4708
4835
|
else:
|
|
4709
4836
|
return ""
|
|
4710
4837
|
|
|
@@ -4719,18 +4846,18 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4719
4846
|
if self.type.is_pyobject:
|
|
4720
4847
|
error_value = 'NULL'
|
|
4721
4848
|
if self.index.type.is_int:
|
|
4722
|
-
if base_type
|
|
4849
|
+
if base_type.is_pylist_type:
|
|
4723
4850
|
function = "__Pyx_GetItemInt_List"
|
|
4724
|
-
elif base_type
|
|
4851
|
+
elif base_type.is_pytuple_type:
|
|
4725
4852
|
function = "__Pyx_GetItemInt_Tuple"
|
|
4726
4853
|
else:
|
|
4727
4854
|
function = "__Pyx_GetItemInt"
|
|
4728
4855
|
utility_code = TempitaUtilityCode.load_cached("GetItemInt", "ObjectHandling.c")
|
|
4729
4856
|
else:
|
|
4730
|
-
if base_type
|
|
4857
|
+
if base_type.is_pyanydict_type:
|
|
4731
4858
|
function = "__Pyx_PyDict_GetItem"
|
|
4732
4859
|
utility_code = UtilityCode.load_cached("DictGetItem", "ObjectHandling.c")
|
|
4733
|
-
elif base_type is py_object_type and self.index.type
|
|
4860
|
+
elif base_type is py_object_type and self.index.type.is_pystr_type:
|
|
4734
4861
|
# obj[str] is probably doing a dict lookup
|
|
4735
4862
|
function = "__Pyx_PyObject_Dict_GetItem"
|
|
4736
4863
|
utility_code = UtilityCode.load_cached("DictGetItem", "ObjectHandling.c")
|
|
@@ -4739,18 +4866,18 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4739
4866
|
code.globalstate.use_utility_code(
|
|
4740
4867
|
TempitaUtilityCode.load_cached("GetItemInt", "ObjectHandling.c"))
|
|
4741
4868
|
utility_code = UtilityCode.load_cached("ObjectGetItem", "ObjectHandling.c")
|
|
4742
|
-
elif self.type.is_unicode_char and base_type
|
|
4869
|
+
elif self.type.is_unicode_char and base_type.is_pystr_type:
|
|
4743
4870
|
assert self.index.type.is_int
|
|
4744
4871
|
function = "__Pyx_GetItemInt_Unicode"
|
|
4745
4872
|
error_value = '(Py_UCS4)-1'
|
|
4746
4873
|
utility_code = UtilityCode.load_cached("GetItemIntUnicode", "StringTools.c")
|
|
4747
|
-
elif base_type
|
|
4874
|
+
elif base_type.is_pybytes_type:
|
|
4748
4875
|
assert self.index.type.is_int
|
|
4749
4876
|
assert self.type.is_int
|
|
4750
4877
|
function = "__Pyx_GetItemInt_Bytes"
|
|
4751
4878
|
error_value = '-1'
|
|
4752
4879
|
utility_code = UtilityCode.load_cached("GetItemIntBytes", "StringTools.c")
|
|
4753
|
-
elif base_type
|
|
4880
|
+
elif base_type.is_pybytearray_type:
|
|
4754
4881
|
assert self.index.type.is_int
|
|
4755
4882
|
assert self.type.is_int
|
|
4756
4883
|
function = "__Pyx_GetItemInt_ByteArray"
|
|
@@ -4789,7 +4916,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4789
4916
|
|
|
4790
4917
|
def generate_setitem_code(self, value_code, code):
|
|
4791
4918
|
if self.index.type.is_int:
|
|
4792
|
-
if self.base.type
|
|
4919
|
+
if self.base.type.is_pybytearray_type:
|
|
4793
4920
|
code.globalstate.use_utility_code(
|
|
4794
4921
|
UtilityCode.load_cached("SetItemIntByteArray", "StringTools.c"))
|
|
4795
4922
|
function = "__Pyx_SetItemInt_ByteArray"
|
|
@@ -4800,7 +4927,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4800
4927
|
index_code = self.index.result()
|
|
4801
4928
|
else:
|
|
4802
4929
|
index_code = self.index.py_result()
|
|
4803
|
-
if self.base.type
|
|
4930
|
+
if self.base.type.is_pydict_type:
|
|
4804
4931
|
function = "PyDict_SetItem"
|
|
4805
4932
|
# It would seem that we could specialized lists/tuples, but that
|
|
4806
4933
|
# shouldn't happen here.
|
|
@@ -4826,7 +4953,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4826
4953
|
|
|
4827
4954
|
if self.type.is_pyobject:
|
|
4828
4955
|
self.generate_setitem_code(rhs.py_result(), code)
|
|
4829
|
-
elif self.base.type
|
|
4956
|
+
elif self.base.type.is_pybytearray_type:
|
|
4830
4957
|
value_code = self._check_byte_value(code, rhs)
|
|
4831
4958
|
self.generate_setitem_code(value_code, code)
|
|
4832
4959
|
elif self.base.type.is_cpp_class and self.exception_check and self.exception_check == '+':
|
|
@@ -4899,7 +5026,7 @@ class IndexNode(_IndexingBaseNode):
|
|
|
4899
5026
|
UtilityCode.load_cached("DelItemInt", "ObjectHandling.c"))
|
|
4900
5027
|
else:
|
|
4901
5028
|
index_code = self.index.py_result()
|
|
4902
|
-
if self.base.type
|
|
5029
|
+
if self.base.type.is_pydict_type:
|
|
4903
5030
|
function = "PyDict_DelItem"
|
|
4904
5031
|
else:
|
|
4905
5032
|
function = "PyObject_DelItem"
|
|
@@ -5234,6 +5361,7 @@ class MemoryViewSliceNode(MemoryViewIndexNode):
|
|
|
5234
5361
|
is_memview_scalar_assignment = False
|
|
5235
5362
|
is_memview_index = False
|
|
5236
5363
|
is_memview_broadcast = False
|
|
5364
|
+
use_borrowed_ref = False
|
|
5237
5365
|
|
|
5238
5366
|
def analyse_ellipsis_noop(self, env, getting):
|
|
5239
5367
|
"""Slicing operations needing no evaluation, i.e. m[...] or m[:, :]"""
|
|
@@ -5349,7 +5477,8 @@ class MemoryViewSliceNode(MemoryViewIndexNode):
|
|
|
5349
5477
|
buffer_entry.generate_buffer_slice_code(
|
|
5350
5478
|
code, self.original_indices, self.result(), self.type,
|
|
5351
5479
|
have_gil=have_gil, have_slices=have_slices,
|
|
5352
|
-
directives=code.globalstate.directives
|
|
5480
|
+
directives=code.globalstate.directives,
|
|
5481
|
+
drop_temp_refcounting=self.use_borrowed_ref)
|
|
5353
5482
|
|
|
5354
5483
|
def generate_assignment_code(self, rhs, code, overloaded_assignment=False):
|
|
5355
5484
|
if self.is_ellipsis_noop:
|
|
@@ -5495,8 +5624,11 @@ class SliceIndexNode(ExprNode):
|
|
|
5495
5624
|
return bytes_type
|
|
5496
5625
|
elif base_type.is_pyunicode_ptr:
|
|
5497
5626
|
return unicode_type
|
|
5498
|
-
elif base_type
|
|
5499
|
-
|
|
5627
|
+
elif base_type.supports_container_type and not base_type.has_uniform_element_type:
|
|
5628
|
+
# slicing a container with non-uniform element types (tuple[int, str]) depends on the slice indices,
|
|
5629
|
+
# so we can't infer a more specific type here.
|
|
5630
|
+
return base_type.get_container_type()
|
|
5631
|
+
elif base_type.is_builtin_sequence:
|
|
5500
5632
|
return base_type
|
|
5501
5633
|
elif base_type.is_ptr or base_type.is_array:
|
|
5502
5634
|
return PyrexTypes.c_array_type(base_type.base_type, None)
|
|
@@ -5516,8 +5648,7 @@ class SliceIndexNode(ExprNode):
|
|
|
5516
5648
|
if base_type:
|
|
5517
5649
|
if base_type.is_string:
|
|
5518
5650
|
return False
|
|
5519
|
-
if base_type
|
|
5520
|
-
list_type, tuple_type):
|
|
5651
|
+
if base_type.is_builtin_sequence:
|
|
5521
5652
|
return False
|
|
5522
5653
|
return ExprNode.may_be_none(self)
|
|
5523
5654
|
|
|
@@ -5560,7 +5691,9 @@ class SliceIndexNode(ExprNode):
|
|
|
5560
5691
|
def analyse_types(self, env, getting=True):
|
|
5561
5692
|
self.base = self.base.analyse_types(env)
|
|
5562
5693
|
|
|
5563
|
-
if self.base.type.is_buffer or self.base.type.is_pythran_expr or
|
|
5694
|
+
if (self.base.type.is_buffer or self.base.type.is_pythran_expr or
|
|
5695
|
+
self.base.type.is_memoryviewslice or self.base.type.is_pyanydict_type
|
|
5696
|
+
):
|
|
5564
5697
|
none_node = NoneNode(self.pos)
|
|
5565
5698
|
index = SliceNode(self.pos,
|
|
5566
5699
|
start=self.start or none_node,
|
|
@@ -5687,9 +5820,8 @@ class SliceIndexNode(ExprNode):
|
|
|
5687
5820
|
"SliceObject", "ObjectHandling.c", context={'access': 'Set'})
|
|
5688
5821
|
|
|
5689
5822
|
def coerce_to(self, dst_type, env):
|
|
5690
|
-
if
|
|
5691
|
-
|
|
5692
|
-
if (dst_type is unicode_type and not env.directives['c_string_encoding']):
|
|
5823
|
+
if self.base.type.is_c_or_cpp_string and dst_type.is_bytes_or_str_or_bytearray:
|
|
5824
|
+
if (dst_type.is_pystr_type and not env.directives['c_string_encoding']):
|
|
5693
5825
|
error(self.pos,
|
|
5694
5826
|
"default encoding required for conversion from '%s' to '%s'" %
|
|
5695
5827
|
(self.base.type, dst_type))
|
|
@@ -5714,12 +5846,12 @@ class SliceIndexNode(ExprNode):
|
|
|
5714
5846
|
|
|
5715
5847
|
if base_type.is_string:
|
|
5716
5848
|
base_result = self.base.result_as(PyrexTypes.c_const_char_ptr_type)
|
|
5717
|
-
if self.type
|
|
5849
|
+
if self.type.is_pybytearray_type:
|
|
5718
5850
|
# TODO - arguably bytearray should be protected by a critical section, but it's
|
|
5719
5851
|
# hard to generate good code for this, and it's hard to imagine a good use for slicing
|
|
5720
5852
|
# a volatile bytearray.
|
|
5721
5853
|
type_name = 'ByteArray'
|
|
5722
|
-
elif self.type
|
|
5854
|
+
elif self.type.is_pystr_type:
|
|
5723
5855
|
type_name = 'Unicode'
|
|
5724
5856
|
else:
|
|
5725
5857
|
type_name = self.type.name.title()
|
|
@@ -5738,7 +5870,7 @@ class SliceIndexNode(ExprNode):
|
|
|
5738
5870
|
else:
|
|
5739
5871
|
call = f"__Pyx_PyUnicode_FromUnicodeAndLength({base_result} + {start_code}, {stop_code} - {start_code})"
|
|
5740
5872
|
|
|
5741
|
-
elif base_type
|
|
5873
|
+
elif base_type.is_pystr_type:
|
|
5742
5874
|
base_result = self.base.result()
|
|
5743
5875
|
code.globalstate.use_utility_code(
|
|
5744
5876
|
UtilityCode.load_cached("PyUnicode_Substring", "StringTools.c"))
|
|
@@ -5757,11 +5889,11 @@ class SliceIndexNode(ExprNode):
|
|
|
5757
5889
|
|
|
5758
5890
|
else:
|
|
5759
5891
|
base_result = self.base.py_result()
|
|
5760
|
-
if base_type
|
|
5892
|
+
if base_type.is_pylist_type:
|
|
5761
5893
|
code.globalstate.use_utility_code(
|
|
5762
5894
|
TempitaUtilityCode.load_cached("SliceTupleAndList", "ObjectHandling.c"))
|
|
5763
5895
|
cfunc = '__Pyx_PyList_GetSlice'
|
|
5764
|
-
elif base_type
|
|
5896
|
+
elif base_type.is_pytuple_type:
|
|
5765
5897
|
code.globalstate.use_utility_code(
|
|
5766
5898
|
TempitaUtilityCode.load_cached("SliceTupleAndList", "ObjectHandling.c"))
|
|
5767
5899
|
cfunc = '__Pyx_PyTuple_GetSlice'
|
|
@@ -6084,7 +6216,7 @@ class CallNode(ExprNode):
|
|
|
6084
6216
|
if function.is_attribute:
|
|
6085
6217
|
method_obj_type = function.obj.infer_type(env)
|
|
6086
6218
|
if method_obj_type.is_builtin_type:
|
|
6087
|
-
result_type = Builtin.find_return_type_of_builtin_method(method_obj_type, function.attribute)
|
|
6219
|
+
result_type = Builtin.find_return_type_of_builtin_method(self.pos, env, method_obj_type, function.attribute)
|
|
6088
6220
|
if result_type is not py_object_type:
|
|
6089
6221
|
return result_type
|
|
6090
6222
|
entry = getattr(function, 'entry', None)
|
|
@@ -6145,7 +6277,7 @@ class CallNode(ExprNode):
|
|
|
6145
6277
|
return False
|
|
6146
6278
|
return ExprNode.may_be_none(self)
|
|
6147
6279
|
|
|
6148
|
-
def set_py_result_type(self, function, func_type=None):
|
|
6280
|
+
def set_py_result_type(self, env, function, func_type=None):
|
|
6149
6281
|
# Default to 'object' and then try to find a better type.
|
|
6150
6282
|
self.type = py_object_type
|
|
6151
6283
|
if func_type is None:
|
|
@@ -6171,7 +6303,7 @@ class CallNode(ExprNode):
|
|
|
6171
6303
|
self.may_return_none = False
|
|
6172
6304
|
elif function.is_attribute and function.obj.type.is_builtin_type:
|
|
6173
6305
|
method_obj_type = function.obj.type
|
|
6174
|
-
result_type = Builtin.find_return_type_of_builtin_method(method_obj_type, function.attribute)
|
|
6306
|
+
result_type = Builtin.find_return_type_of_builtin_method(self.pos, env, method_obj_type, function.attribute)
|
|
6175
6307
|
self.may_return_none = result_type is py_object_type
|
|
6176
6308
|
if result_type.is_pyobject:
|
|
6177
6309
|
self.type = result_type
|
|
@@ -6276,15 +6408,31 @@ class SimpleCallNode(CallNode):
|
|
|
6276
6408
|
self.constant_result = method(*args)
|
|
6277
6409
|
|
|
6278
6410
|
@classmethod
|
|
6279
|
-
def
|
|
6411
|
+
def for_cproperty_get(cls, pos, obj, entry):
|
|
6280
6412
|
# Create a call node for C property access.
|
|
6281
6413
|
property_scope = entry.scope
|
|
6282
|
-
getter_entry = property_scope.lookup_here(
|
|
6414
|
+
getter_entry = property_scope.lookup_here("__get__")
|
|
6283
6415
|
assert getter_entry, "Getter not found in scope %s: %s" % (property_scope, property_scope.entries)
|
|
6284
6416
|
function = NameNode(pos, name=entry.name, entry=getter_entry, type=getter_entry.type)
|
|
6285
6417
|
node = cls(pos, function=function, args=[obj])
|
|
6286
6418
|
return node
|
|
6287
6419
|
|
|
6420
|
+
@classmethod
|
|
6421
|
+
def for_cproperty_set(cls, pos, obj, entry):
|
|
6422
|
+
# Create a call node for C property access.
|
|
6423
|
+
# This actually returns a utility node that wraps the call node.
|
|
6424
|
+
property_scope = entry.scope
|
|
6425
|
+
setter_entry = property_scope.lookup_here("__set__")
|
|
6426
|
+
if not setter_entry:
|
|
6427
|
+
error(pos, "Assignment to a read-only property")
|
|
6428
|
+
return None
|
|
6429
|
+
from . import UtilNodes
|
|
6430
|
+
function = NameNode(pos, name=entry.name, entry=setter_entry, type=setter_entry.type)
|
|
6431
|
+
arg_type = setter_entry.type.args[1].type
|
|
6432
|
+
arg1 = RawCNameExprNode(pos, type=arg_type)
|
|
6433
|
+
node = cls(pos, function=function, args=[obj, arg1])
|
|
6434
|
+
return UtilNodes.CPropertySetNode(pos, call_node=node, type=arg_type, arg1=arg1)
|
|
6435
|
+
|
|
6288
6436
|
def analyse_as_type(self, env):
|
|
6289
6437
|
attr = self.function.as_cython_attribute()
|
|
6290
6438
|
if attr == 'pointer':
|
|
@@ -6343,7 +6491,7 @@ class SimpleCallNode(CallNode):
|
|
|
6343
6491
|
self.arg_tuple = TupleNode(self.pos, args = self.args)
|
|
6344
6492
|
self.arg_tuple = self.arg_tuple.analyse_types(env).coerce_to_pyobject(env)
|
|
6345
6493
|
self.args = None
|
|
6346
|
-
self.set_py_result_type(function, func_type)
|
|
6494
|
+
self.set_py_result_type(env, function, func_type)
|
|
6347
6495
|
self.is_temp = 1
|
|
6348
6496
|
else:
|
|
6349
6497
|
self.args = [ arg.analyse_types(env) for arg in self.args ]
|
|
@@ -6485,7 +6633,7 @@ class SimpleCallNode(CallNode):
|
|
|
6485
6633
|
for i in range(max_nargs, actual_nargs):
|
|
6486
6634
|
arg = args[i]
|
|
6487
6635
|
if arg.type.is_pyobject:
|
|
6488
|
-
if arg.type
|
|
6636
|
+
if arg.type.is_pystr_type:
|
|
6489
6637
|
# TODO: require "arg.type.bytes_value"?
|
|
6490
6638
|
arg_ctype = PyrexTypes.c_char_ptr_type
|
|
6491
6639
|
else:
|
|
@@ -6774,15 +6922,18 @@ class PyMethodCallNode(CallNode):
|
|
|
6774
6922
|
# function ExprNode the function/method object to call
|
|
6775
6923
|
# arg_tuple TupleNode the arguments for the args tuple
|
|
6776
6924
|
# kwdict ExprNode or None keyword dictionary (if present)
|
|
6777
|
-
#
|
|
6925
|
+
# kwnames TupleNode or None names of keyword arguments
|
|
6926
|
+
# kwvalues list[ExprNode] | None values of keyword arguments
|
|
6927
|
+
# Arguments to constructor only:
|
|
6778
6928
|
# function_obj ExprNode or None == self.function.obj when using PyObject_VectorcallMethod()
|
|
6779
6929
|
# unpack bool
|
|
6780
6930
|
|
|
6781
|
-
subexprs = ['function', 'arg_tuple', 'kwdict', '
|
|
6931
|
+
subexprs = ['function', 'arg_tuple', 'kwdict', 'kwnames', 'kwvalues']
|
|
6782
6932
|
is_temp = True
|
|
6783
6933
|
use_method_vectorcall = False
|
|
6784
6934
|
kwdict = None
|
|
6785
|
-
|
|
6935
|
+
kwnames = None
|
|
6936
|
+
kwvalues = None
|
|
6786
6937
|
function_obj = None
|
|
6787
6938
|
|
|
6788
6939
|
def __init__(self, pos, **kw):
|
|
@@ -6790,9 +6941,6 @@ class PyMethodCallNode(CallNode):
|
|
|
6790
6941
|
if self.can_avoid_attribute_lookup():
|
|
6791
6942
|
self.use_method_vectorcall = True
|
|
6792
6943
|
self.function_obj = self.function.obj
|
|
6793
|
-
if self.kwdict and self.kwdict.is_dict_literal:
|
|
6794
|
-
self.kwargs_key_value_pairs = self.kwdict.key_value_pairs
|
|
6795
|
-
self.kwdict = None
|
|
6796
6944
|
|
|
6797
6945
|
def can_avoid_attribute_lookup(self):
|
|
6798
6946
|
# Essentially, if the signature matches PyObject_VectorcallMethod
|
|
@@ -6932,38 +7080,42 @@ class PyMethodCallNode(CallNode):
|
|
|
6932
7080
|
code.putln("#endif") # CYTHON_UNPACK_METHODS
|
|
6933
7081
|
# TODO may need to deal with unused variables in the #else case
|
|
6934
7082
|
|
|
6935
|
-
def generate_keyvalue_args(self, code, args,
|
|
6936
|
-
|
|
6937
|
-
|
|
6938
|
-
|
|
6939
|
-
|
|
6940
|
-
code.put_gotref(kwnames_temp, py_object_type)
|
|
7083
|
+
def generate_keyvalue_args(self, code, args, kwnames, kwvalues, kwnames_temp):
|
|
7084
|
+
arg_indices_to_check = [
|
|
7085
|
+
n for n, arg in enumerate(kwnames.args)
|
|
7086
|
+
if not arg.type.is_pystr_type or arg.may_be_none()
|
|
7087
|
+
]
|
|
6941
7088
|
|
|
6942
|
-
|
|
6943
|
-
|
|
6944
|
-
|
|
6945
|
-
|
|
6946
|
-
|
|
6947
|
-
|
|
6948
|
-
|
|
6949
|
-
|
|
6950
|
-
|
|
6951
|
-
|
|
6952
|
-
|
|
6953
|
-
|
|
7089
|
+
code.putln("#if CYTHON_VECTORCALL")
|
|
7090
|
+
code.putln(f"{kwnames_temp} = {kwnames.result()};")
|
|
7091
|
+
code.putln(code.error_goto_if_null(kwnames_temp, self.pos))
|
|
7092
|
+
code.put_incref(kwnames_temp, py_object_type)
|
|
7093
|
+
for arg_index in arg_indices_to_check:
|
|
7094
|
+
code.put_error_if_neg(kwnames.pos, f"__Pyx_CheckVectorcallKwarg({kwnames_temp}, {arg_index})")
|
|
7095
|
+
code.putln("#else")
|
|
7096
|
+
code.putln("{")
|
|
7097
|
+
kwnames.generate_sequence_as_array_code(code, Naming.quick_temp_cname)
|
|
7098
|
+
for arg_index in arg_indices_to_check:
|
|
7099
|
+
code.put_error_if_neg(kwnames.pos, f"__Pyx_CheckVectorcallKwarg({Naming.quick_temp_cname}, {arg_index})")
|
|
7100
|
+
code.putln(f"{kwnames_temp} = __Pyx_MakeKwargDict({Naming.quick_temp_cname}, "
|
|
7101
|
+
f"{Naming.callargs_cname}+{len(args)+1}, {len(kwvalues)});")
|
|
7102
|
+
code.putln(code.error_goto_if_null(kwnames_temp, self.pos))
|
|
7103
|
+
code.put_gotref(kwnames_temp, py_object_type)
|
|
7104
|
+
code.putln("}")
|
|
7105
|
+
code.putln("#endif")
|
|
6954
7106
|
|
|
6955
7107
|
def select_utility_code(self, code):
|
|
6956
7108
|
# ... and return the utility function's cname.
|
|
6957
7109
|
if self.use_method_vectorcall:
|
|
6958
|
-
if self.
|
|
6959
|
-
name = "
|
|
6960
|
-
cfunc = "
|
|
7110
|
+
if self.kwnames:
|
|
7111
|
+
name = "PyObjectVectorcallMethodKwds"
|
|
7112
|
+
cfunc = "__Pyx_Object_VectorcallMethodKwds"
|
|
6961
7113
|
else:
|
|
6962
7114
|
name = "PyObjectFastCallMethod"
|
|
6963
7115
|
cfunc = "__Pyx_PyObject_FastCallMethod"
|
|
6964
|
-
elif self.
|
|
6965
|
-
name = "
|
|
6966
|
-
cfunc = "
|
|
7116
|
+
elif self.kwnames:
|
|
7117
|
+
name = "PyObjectVectorcallKwds"
|
|
7118
|
+
cfunc = "__Pyx_Object_VectorcallKwds"
|
|
6967
7119
|
elif self.kwdict:
|
|
6968
7120
|
name = "PyObjectFastCall"
|
|
6969
7121
|
cfunc = "__Pyx_PyObject_FastCallDict"
|
|
@@ -6979,7 +7131,8 @@ class PyMethodCallNode(CallNode):
|
|
|
6979
7131
|
code.mark_pos(self.pos)
|
|
6980
7132
|
self.allocate_temp_result(code)
|
|
6981
7133
|
|
|
6982
|
-
|
|
7134
|
+
kwnames = self.kwnames
|
|
7135
|
+
kwvalues = self.kwvalues
|
|
6983
7136
|
kwdict = self.kwdict
|
|
6984
7137
|
|
|
6985
7138
|
self_arg = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
|
|
@@ -6990,9 +7143,10 @@ class PyMethodCallNode(CallNode):
|
|
|
6990
7143
|
for arg in args:
|
|
6991
7144
|
arg.generate_evaluation_code(code)
|
|
6992
7145
|
|
|
6993
|
-
if
|
|
6994
|
-
|
|
6995
|
-
|
|
7146
|
+
if kwnames:
|
|
7147
|
+
kwnames.generate_evaluation_code(code)
|
|
7148
|
+
for value in kwvalues:
|
|
7149
|
+
value.generate_evaluation_code(code)
|
|
6996
7150
|
elif kwdict:
|
|
6997
7151
|
kwdict.generate_evaluation_code(code)
|
|
6998
7152
|
|
|
@@ -7015,16 +7169,16 @@ class PyMethodCallNode(CallNode):
|
|
|
7015
7169
|
# To avoid passing an out-of-bounds argument pointer in the no-args case,
|
|
7016
7170
|
# we need at least two entries, so we pad with NULL and point to that.
|
|
7017
7171
|
# See https://github.com/cython/cython/issues/5668
|
|
7018
|
-
|
|
7019
|
-
|
|
7172
|
+
args_and_kwargs = args + (kwvalues or [])
|
|
7173
|
+
args_list = ', '.join(arg.py_result() for arg in args_and_kwargs) if args_and_kwargs else "NULL"
|
|
7020
7174
|
code.putln(
|
|
7021
|
-
f"PyObject *{Naming.callargs_cname}[{(len(
|
|
7175
|
+
f"PyObject *{Naming.callargs_cname}[{(len(args_and_kwargs) + 1) if args_and_kwargs else 2:d}] = {{{self_arg}, {args_list}}};"
|
|
7022
7176
|
)
|
|
7023
7177
|
|
|
7024
7178
|
keyword_variable = ""
|
|
7025
|
-
if
|
|
7179
|
+
if kwnames:
|
|
7026
7180
|
keyword_variable = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
|
|
7027
|
-
self.generate_keyvalue_args(code, args,
|
|
7181
|
+
self.generate_keyvalue_args(code, args, kwnames, kwvalues, keyword_variable)
|
|
7028
7182
|
elif kwdict:
|
|
7029
7183
|
keyword_variable = kwdict.result()
|
|
7030
7184
|
|
|
@@ -7047,10 +7201,12 @@ class PyMethodCallNode(CallNode):
|
|
|
7047
7201
|
arg.generate_disposal_code(code)
|
|
7048
7202
|
arg.free_temps(code)
|
|
7049
7203
|
|
|
7050
|
-
if
|
|
7051
|
-
|
|
7052
|
-
|
|
7053
|
-
|
|
7204
|
+
if kwnames:
|
|
7205
|
+
kwnames.generate_disposal_code(code)
|
|
7206
|
+
kwnames.free_temps(code)
|
|
7207
|
+
for value in kwvalues:
|
|
7208
|
+
value.generate_disposal_code(code)
|
|
7209
|
+
value.free_temps(code)
|
|
7054
7210
|
code.put_decref_clear(keyword_variable, py_object_type)
|
|
7055
7211
|
code.funcstate.release_temp(keyword_variable)
|
|
7056
7212
|
elif kwdict:
|
|
@@ -7328,7 +7484,7 @@ class GeneralCallNode(CallNode):
|
|
|
7328
7484
|
self.positional_args = self.positional_args.analyse_types(env)
|
|
7329
7485
|
self.positional_args = \
|
|
7330
7486
|
self.positional_args.coerce_to_pyobject(env)
|
|
7331
|
-
self.set_py_result_type(self.function)
|
|
7487
|
+
self.set_py_result_type(env, self.function)
|
|
7332
7488
|
self.is_temp = 1
|
|
7333
7489
|
return self
|
|
7334
7490
|
|
|
@@ -7511,7 +7667,7 @@ class AsTupleNode(ExprNode):
|
|
|
7511
7667
|
|
|
7512
7668
|
def analyse_types(self, env):
|
|
7513
7669
|
self.arg = self.arg.analyse_types(env).coerce_to_pyobject(env)
|
|
7514
|
-
if self.arg.type
|
|
7670
|
+
if self.arg.type.is_pytuple_type:
|
|
7515
7671
|
return self.arg.as_none_safe_node("'NoneType' object is not iterable")
|
|
7516
7672
|
self.type = tuple_type
|
|
7517
7673
|
return self
|
|
@@ -7523,7 +7679,7 @@ class AsTupleNode(ExprNode):
|
|
|
7523
7679
|
gil_message = "Constructing Python tuple"
|
|
7524
7680
|
|
|
7525
7681
|
def generate_result_code(self, code):
|
|
7526
|
-
cfunc = "__Pyx_PySequence_Tuple" if self.arg.type
|
|
7682
|
+
cfunc = "__Pyx_PySequence_Tuple" if self.arg.type.may_be_pytuple_type else "PySequence_Tuple"
|
|
7527
7683
|
code.putln(
|
|
7528
7684
|
"%s = %s(%s); %s" % (
|
|
7529
7685
|
self.result(),
|
|
@@ -7608,8 +7764,10 @@ class MergedDictNode(ExprNode):
|
|
|
7608
7764
|
args = iter(self.keyword_args)
|
|
7609
7765
|
item = next(args)
|
|
7610
7766
|
item.generate_evaluation_code(code)
|
|
7611
|
-
if item.type
|
|
7767
|
+
if not item.type.is_pydict_type:
|
|
7612
7768
|
# CPython supports calling functions with non-dicts, so do we
|
|
7769
|
+
code.globalstate.use_utility_code(UtilityCode.load_cached(
|
|
7770
|
+
"PyFrozenDict", "Builtins.c"))
|
|
7613
7771
|
code.putln('if (likely(PyDict_CheckExact(%s))) {' %
|
|
7614
7772
|
item.py_result())
|
|
7615
7773
|
|
|
@@ -7638,7 +7796,7 @@ class MergedDictNode(ExprNode):
|
|
|
7638
7796
|
if item.result_in_temp():
|
|
7639
7797
|
code.putln("}")
|
|
7640
7798
|
|
|
7641
|
-
if item.type
|
|
7799
|
+
if not item.type.is_pydict_type:
|
|
7642
7800
|
code.putln('} else {')
|
|
7643
7801
|
code.globalstate.use_utility_code(UtilityCode.load_cached(
|
|
7644
7802
|
"PyObjectCallOneArg", "ObjectHandling.c"))
|
|
@@ -7724,6 +7882,7 @@ class AttributeNode(ExprNode):
|
|
|
7724
7882
|
is_memslice_transpose = False
|
|
7725
7883
|
is_special_lookup = False
|
|
7726
7884
|
is_py_attr = 0
|
|
7885
|
+
mangle_private_names = True # perform mangling of class.__attr names
|
|
7727
7886
|
|
|
7728
7887
|
def as_cython_attribute(self):
|
|
7729
7888
|
if (isinstance(self.obj, NameNode) and
|
|
@@ -7992,9 +8151,9 @@ class AttributeNode(ExprNode):
|
|
|
7992
8151
|
error(self.pos, "Assignment to an immutable object field")
|
|
7993
8152
|
elif self.entry and self.entry.is_cproperty:
|
|
7994
8153
|
if not target:
|
|
7995
|
-
return SimpleCallNode.
|
|
7996
|
-
|
|
7997
|
-
|
|
8154
|
+
return SimpleCallNode.for_cproperty_get(self.pos, self.obj, self.entry).analyse_types(env)
|
|
8155
|
+
else:
|
|
8156
|
+
return SimpleCallNode.for_cproperty_set(self.pos, self.obj, self.entry).analyse_types(env)
|
|
7998
8157
|
#elif self.type.is_memoryviewslice and not target:
|
|
7999
8158
|
# self.is_temp = True
|
|
8000
8159
|
return self
|
|
@@ -8072,8 +8231,9 @@ class AttributeNode(ExprNode):
|
|
|
8072
8231
|
def analyse_as_python_attribute(self, env, obj_type=None, immutable_obj=False):
|
|
8073
8232
|
if obj_type is None:
|
|
8074
8233
|
obj_type = self.obj.type
|
|
8075
|
-
|
|
8076
|
-
|
|
8234
|
+
if self.mangle_private_names:
|
|
8235
|
+
# mangle private '__*' Python attributes used inside of a class
|
|
8236
|
+
self.attribute = env.mangle_class_private_name(self.attribute)
|
|
8077
8237
|
self.member = self.attribute
|
|
8078
8238
|
self.type = py_object_type
|
|
8079
8239
|
self.is_py_attr = 1
|
|
@@ -8538,25 +8698,27 @@ class SequenceNode(ExprNode):
|
|
|
8538
8698
|
else:
|
|
8539
8699
|
size_factor = ' * (%s)' % (c_mult,)
|
|
8540
8700
|
|
|
8541
|
-
if ((self.type
|
|
8701
|
+
if ((self.type.is_pytuple_type or self.type.is_pylist_type) and
|
|
8542
8702
|
(self.is_literal or self.slow) and
|
|
8543
8703
|
not c_mult and
|
|
8544
8704
|
len(self.args) > 0):
|
|
8545
|
-
# use
|
|
8546
|
-
if self.type
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
|
|
8553
|
-
|
|
8705
|
+
# use Py*_FromArray() to avoid generating huge amounts of one-time code
|
|
8706
|
+
type_name = 'List' if self.type.is_pylist_type else 'Tuple'
|
|
8707
|
+
pack_name = f'__Pyx_Py{type_name}_FromArray'
|
|
8708
|
+
code.globalstate.use_utility_code(
|
|
8709
|
+
UtilityCode.load_cached(f'{type_name}FromArray', 'ObjectHandling.c')
|
|
8710
|
+
)
|
|
8711
|
+
code.putln('{')
|
|
8712
|
+
code.putln(f'PyObject* {Naming.quick_temp_cname}[{len(self.args)}] = '
|
|
8713
|
+
f"{{{', '.join(arg.py_result() for arg in self.args)}}};")
|
|
8714
|
+
code.putln('%s = %s(%s, %d); %s' % (
|
|
8554
8715
|
target,
|
|
8555
8716
|
pack_name,
|
|
8717
|
+
Naming.quick_temp_cname,
|
|
8556
8718
|
len(self.args),
|
|
8557
|
-
', '.join(arg.py_result() for arg in self.args),
|
|
8558
8719
|
code.error_goto_if_null(target, self.pos)))
|
|
8559
8720
|
code.put_gotref(target, py_object_type)
|
|
8721
|
+
code.putln('}')
|
|
8560
8722
|
self.needs_subexpr_disposal = True
|
|
8561
8723
|
elif self.type.is_ctuple:
|
|
8562
8724
|
for i, arg in enumerate(self.args):
|
|
@@ -8564,9 +8726,9 @@ class SequenceNode(ExprNode):
|
|
|
8564
8726
|
target, i, arg.result()))
|
|
8565
8727
|
else:
|
|
8566
8728
|
# build the tuple/list step by step, potentially multiplying it as we go
|
|
8567
|
-
if self.type
|
|
8729
|
+
if self.type.is_pylist_type:
|
|
8568
8730
|
create_func, set_item_func = 'PyList_New', '__Pyx_PyList_SET_ITEM'
|
|
8569
|
-
elif self.type
|
|
8731
|
+
elif self.type.is_pytuple_type:
|
|
8570
8732
|
create_func, set_item_func = 'PyTuple_New', '__Pyx_PyTuple_SET_ITEM'
|
|
8571
8733
|
else:
|
|
8572
8734
|
raise InternalError("sequence packing for unexpected type %s" % self.type)
|
|
@@ -8622,6 +8784,13 @@ class SequenceNode(ExprNode):
|
|
|
8622
8784
|
code.putln('%s = %s;' % (target, Naming.quick_temp_cname))
|
|
8623
8785
|
code.putln('}')
|
|
8624
8786
|
|
|
8787
|
+
def generate_sequence_as_array_code(self, code, target):
|
|
8788
|
+
# Currently only suitable for fixed-size sequences
|
|
8789
|
+
assert not self.mult_factor
|
|
8790
|
+
code.put(f"PyObject *{target}[{len(self.args)}] = {{")
|
|
8791
|
+
code.put(', '.join(arg.result() for arg in self.args))
|
|
8792
|
+
code.putln("};")
|
|
8793
|
+
|
|
8625
8794
|
def generate_subexpr_disposal_code(self, code):
|
|
8626
8795
|
if self.needs_subexpr_disposal:
|
|
8627
8796
|
super().generate_subexpr_disposal_code(code)
|
|
@@ -8658,8 +8827,7 @@ class SequenceNode(ExprNode):
|
|
|
8658
8827
|
# is evaluated twice, within each if-block.
|
|
8659
8828
|
for item in self.unpacked_items:
|
|
8660
8829
|
item.allocate(code)
|
|
8661
|
-
special_unpack = (rhs.type
|
|
8662
|
-
or rhs.type in (tuple_type, list_type)
|
|
8830
|
+
special_unpack = (rhs.type.may_be_pytuple_type or rhs.type.may_be_pylist_type
|
|
8663
8831
|
or not rhs.type.is_builtin_type)
|
|
8664
8832
|
long_enough_for_a_loop = len(self.unpacked_items) > 3
|
|
8665
8833
|
|
|
@@ -8681,12 +8849,12 @@ class SequenceNode(ExprNode):
|
|
|
8681
8849
|
def generate_special_parallel_unpacking_code(self, code, rhs, use_loop):
|
|
8682
8850
|
sequence_type_test = '1'
|
|
8683
8851
|
none_check = "likely(%s != Py_None)" % rhs.py_result()
|
|
8684
|
-
if rhs.type
|
|
8852
|
+
if rhs.type.is_pylist_type:
|
|
8685
8853
|
sequence_types = ['List']
|
|
8686
8854
|
get_size_func = "__Pyx_PyList_GET_SIZE"
|
|
8687
8855
|
if rhs.may_be_none():
|
|
8688
8856
|
sequence_type_test = none_check
|
|
8689
|
-
elif rhs.type
|
|
8857
|
+
elif rhs.type.is_pytuple_type:
|
|
8690
8858
|
sequence_types = ['Tuple']
|
|
8691
8859
|
get_size_func = "__Pyx_PyTuple_GET_SIZE"
|
|
8692
8860
|
if rhs.may_be_none():
|
|
@@ -8881,7 +9049,7 @@ class SequenceNode(ExprNode):
|
|
|
8881
9049
|
code.putln("%s = %s(%s); %s" % (
|
|
8882
9050
|
target_list,
|
|
8883
9051
|
"__Pyx_PySequence_ListKeepNew" if (
|
|
8884
|
-
not iterator_temp and rhs.result_in_temp() and rhs.type
|
|
9052
|
+
not iterator_temp and rhs.result_in_temp() and rhs.type.may_be_pylist_type)
|
|
8885
9053
|
else "PySequence_List",
|
|
8886
9054
|
iterator_temp or rhs.py_result(),
|
|
8887
9055
|
code.error_goto_if_null(target_list, self.pos)))
|
|
@@ -8961,7 +9129,7 @@ class TupleNode(SequenceNode):
|
|
|
8961
9129
|
arg_types = [arg.infer_type(env) for arg in self.args]
|
|
8962
9130
|
if any(type.is_pyobject or type.is_memoryviewslice or type.is_unspecified or type.is_fused
|
|
8963
9131
|
for type in arg_types):
|
|
8964
|
-
return tuple_type
|
|
9132
|
+
return tuple_type.specialize_here(self.pos, env, arg_types)
|
|
8965
9133
|
return env.declare_tuple_type(self.pos, arg_types).type
|
|
8966
9134
|
|
|
8967
9135
|
def analyse_types(self, env, skip_children=False):
|
|
@@ -9021,7 +9189,7 @@ class TupleNode(SequenceNode):
|
|
|
9021
9189
|
if self.type.is_ctuple:
|
|
9022
9190
|
if dst_type.is_ctuple and self.type.size == dst_type.size:
|
|
9023
9191
|
return self.coerce_to_ctuple(dst_type, env)
|
|
9024
|
-
elif dst_type
|
|
9192
|
+
elif dst_type.may_be_pytuple_type:
|
|
9025
9193
|
coerced_args = [arg.coerce_to_pyobject(env) for arg in self.args]
|
|
9026
9194
|
return TupleNode(
|
|
9027
9195
|
self.pos,
|
|
@@ -9038,10 +9206,11 @@ class TupleNode(SequenceNode):
|
|
|
9038
9206
|
return SequenceNode.coerce_to(self, dst_type, env)
|
|
9039
9207
|
|
|
9040
9208
|
def as_list(self):
|
|
9041
|
-
|
|
9042
|
-
if isinstance(
|
|
9043
|
-
|
|
9044
|
-
return
|
|
9209
|
+
constant_result = self.constant_result
|
|
9210
|
+
if isinstance(constant_result, tuple):
|
|
9211
|
+
constant_result = list(constant_result)
|
|
9212
|
+
return ListNode.from_node(
|
|
9213
|
+
self, args=self.args, mult_factor=self.mult_factor, constant_result=constant_result)
|
|
9045
9214
|
|
|
9046
9215
|
def is_simple(self):
|
|
9047
9216
|
# either temp or constant => always simple
|
|
@@ -9109,7 +9278,7 @@ class ListNode(SequenceNode):
|
|
|
9109
9278
|
# List constructor.
|
|
9110
9279
|
|
|
9111
9280
|
# obj_conversion_errors [PyrexError] used internally
|
|
9112
|
-
#
|
|
9281
|
+
# original_args [ExprNode] used internally
|
|
9113
9282
|
|
|
9114
9283
|
obj_conversion_errors = []
|
|
9115
9284
|
type = list_type
|
|
@@ -9149,22 +9318,7 @@ class ListNode(SequenceNode):
|
|
|
9149
9318
|
if not self.type.subtype_of(dst_type):
|
|
9150
9319
|
error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
|
|
9151
9320
|
elif (dst_type.is_array or dst_type.is_ptr) and dst_type.base_type is not PyrexTypes.c_void_type:
|
|
9152
|
-
|
|
9153
|
-
if self.mult_factor:
|
|
9154
|
-
if isinstance(self.mult_factor.constant_result, int):
|
|
9155
|
-
if self.mult_factor.constant_result <= 0:
|
|
9156
|
-
error(self.pos, "Cannot coerce non-positively multiplied list to '%s'" % dst_type)
|
|
9157
|
-
else:
|
|
9158
|
-
array_length *= self.mult_factor.constant_result
|
|
9159
|
-
else:
|
|
9160
|
-
error(self.pos, "Cannot coerce dynamically multiplied list to '%s'" % dst_type)
|
|
9161
|
-
base_type = dst_type.base_type
|
|
9162
|
-
self.type = PyrexTypes.CArrayType(base_type, array_length)
|
|
9163
|
-
for i in range(len(self.original_args)):
|
|
9164
|
-
arg = self.args[i]
|
|
9165
|
-
if isinstance(arg, CoerceToPyTypeNode):
|
|
9166
|
-
arg = arg.arg
|
|
9167
|
-
self.args[i] = arg.coerce_to(base_type, env)
|
|
9321
|
+
return self.as_carray(dst_type.base_type, env, is_const=dst_type.is_array)
|
|
9168
9322
|
elif dst_type.is_cpp_class:
|
|
9169
9323
|
# TODO(robertwb): Avoid object conversion for vector/list/set.
|
|
9170
9324
|
return TypecastNode(self.pos, operand=self, type=PyrexTypes.py_object_type).coerce_to(dst_type, env)
|
|
@@ -9195,24 +9349,47 @@ class ListNode(SequenceNode):
|
|
|
9195
9349
|
return self
|
|
9196
9350
|
|
|
9197
9351
|
def as_tuple(self):
|
|
9198
|
-
|
|
9199
|
-
if isinstance(
|
|
9200
|
-
|
|
9201
|
-
return
|
|
9352
|
+
constant_result = self.constant_result
|
|
9353
|
+
if isinstance(constant_result, list):
|
|
9354
|
+
constant_result = tuple(constant_result)
|
|
9355
|
+
return TupleNode.from_node(
|
|
9356
|
+
self, args=self.args, mult_factor=self.mult_factor, constant_result=constant_result)
|
|
9202
9357
|
|
|
9203
|
-
def
|
|
9204
|
-
|
|
9205
|
-
|
|
9206
|
-
|
|
9207
|
-
|
|
9358
|
+
def as_carray(self, item_type, env, is_const):
|
|
9359
|
+
array_length = len(self.args)
|
|
9360
|
+
|
|
9361
|
+
if self.mult_factor:
|
|
9362
|
+
if isinstance(self.mult_factor.constant_result, int):
|
|
9363
|
+
if self.mult_factor.constant_result <= 0:
|
|
9364
|
+
error(self.pos, f"Cannot coerce non-positively multiplied list to '{item_type}[]'")
|
|
9365
|
+
else:
|
|
9366
|
+
array_length *= self.mult_factor.constant_result
|
|
9208
9367
|
else:
|
|
9209
|
-
|
|
9210
|
-
|
|
9211
|
-
|
|
9212
|
-
|
|
9213
|
-
|
|
9214
|
-
|
|
9215
|
-
|
|
9368
|
+
error(self.pos, f"Cannot coerce dynamically multiplied list to '{item_type}[]'")
|
|
9369
|
+
|
|
9370
|
+
coerced_args = []
|
|
9371
|
+
is_literal = True
|
|
9372
|
+
for i in range(len(self.original_args)):
|
|
9373
|
+
arg = self.args[i]
|
|
9374
|
+
if isinstance(arg, CoerceToPyTypeNode):
|
|
9375
|
+
arg = arg.arg
|
|
9376
|
+
arg = arg.coerce_to(item_type, env)
|
|
9377
|
+
if not arg.is_literal:
|
|
9378
|
+
is_literal = False
|
|
9379
|
+
coerced_args.append(arg)
|
|
9380
|
+
|
|
9381
|
+
if is_literal and is_const and not item_type.is_const:
|
|
9382
|
+
item_type = PyrexTypes.c_const_type(item_type)
|
|
9383
|
+
array_type = PyrexTypes.CArrayType(item_type, array_length)
|
|
9384
|
+
|
|
9385
|
+
return CArrayNode.from_node(
|
|
9386
|
+
self,
|
|
9387
|
+
type=array_type,
|
|
9388
|
+
args=coerced_args,
|
|
9389
|
+
mult_factor=self.mult_factor,
|
|
9390
|
+
original_args=self.original_args,
|
|
9391
|
+
in_module_scope=self.in_module_scope,
|
|
9392
|
+
)
|
|
9216
9393
|
|
|
9217
9394
|
def calculate_constant_result(self):
|
|
9218
9395
|
if self.mult_factor:
|
|
@@ -9227,35 +9404,11 @@ class ListNode(SequenceNode):
|
|
|
9227
9404
|
return l
|
|
9228
9405
|
|
|
9229
9406
|
def generate_operation_code(self, code):
|
|
9407
|
+
assert not self.type.is_array # should use CArrayNode instead
|
|
9230
9408
|
if self.type.is_pyobject:
|
|
9231
9409
|
for err in self.obj_conversion_errors:
|
|
9232
9410
|
report_error(err)
|
|
9233
9411
|
self.generate_sequence_packing_code(code)
|
|
9234
|
-
elif self.type.is_array:
|
|
9235
|
-
if self.mult_factor:
|
|
9236
|
-
code.putln("{")
|
|
9237
|
-
code.putln("Py_ssize_t %s;" % Naming.quick_temp_cname)
|
|
9238
|
-
code.putln("for ({i} = 0; {i} < {count}; {i}++) {{".format(
|
|
9239
|
-
i=Naming.quick_temp_cname, count=self.mult_factor.result()))
|
|
9240
|
-
offset = '+ (%d * %s)' % (len(self.args), Naming.quick_temp_cname)
|
|
9241
|
-
else:
|
|
9242
|
-
offset = ''
|
|
9243
|
-
for i, arg in enumerate(self.args):
|
|
9244
|
-
if arg.type.is_array:
|
|
9245
|
-
code.globalstate.use_utility_code(UtilityCode.load_cached("IncludeStringH", "StringTools.c"))
|
|
9246
|
-
code.putln("memcpy(&(%s[%s%s]), %s, sizeof(%s[0]));" % (
|
|
9247
|
-
self.result(), i, offset,
|
|
9248
|
-
arg.result(), self.result()
|
|
9249
|
-
))
|
|
9250
|
-
else:
|
|
9251
|
-
code.putln("%s[%s%s] = %s;" % (
|
|
9252
|
-
self.result(),
|
|
9253
|
-
i,
|
|
9254
|
-
offset,
|
|
9255
|
-
arg.result()))
|
|
9256
|
-
if self.mult_factor:
|
|
9257
|
-
code.putln("}")
|
|
9258
|
-
code.putln("}")
|
|
9259
9412
|
elif self.type.is_struct:
|
|
9260
9413
|
for arg, member in zip(self.args, self.type.scope.var_entries):
|
|
9261
9414
|
code.putln("%s.%s = %s;" % (
|
|
@@ -9266,6 +9419,97 @@ class ListNode(SequenceNode):
|
|
|
9266
9419
|
raise InternalError("List type never specified")
|
|
9267
9420
|
|
|
9268
9421
|
|
|
9422
|
+
class CArrayNode(ListNode):
|
|
9423
|
+
"""A homogeneous C array of C values.
|
|
9424
|
+
|
|
9425
|
+
Created through coercion from ListNode.
|
|
9426
|
+
"""
|
|
9427
|
+
|
|
9428
|
+
is_temp = False
|
|
9429
|
+
array_cname = None
|
|
9430
|
+
_array_cname_temp = None
|
|
9431
|
+
|
|
9432
|
+
def _non_const_array_type(self):
|
|
9433
|
+
array_type = self.type
|
|
9434
|
+
if array_type.base_type.is_const:
|
|
9435
|
+
array_type = PyrexTypes.c_array_type(
|
|
9436
|
+
PyrexTypes.remove_cv_ref(array_type.base_type), array_type.size)
|
|
9437
|
+
return array_type
|
|
9438
|
+
|
|
9439
|
+
def _allocate_carray(self, code, as_const):
|
|
9440
|
+
array_type = self.type
|
|
9441
|
+
if as_const:
|
|
9442
|
+
# Declare static const array in place.
|
|
9443
|
+
self.array_cname = code.globalstate.new_const_cname('carray')
|
|
9444
|
+
elif self.in_module_scope:
|
|
9445
|
+
# Statically allocated in the module init function.
|
|
9446
|
+
self.array_cname = code.funcstate.allocate_temp(
|
|
9447
|
+
array_type, manage_ref=False, static=True, reusable=False)
|
|
9448
|
+
self._array_cname_temp = self.array_cname
|
|
9449
|
+
elif code.funcstate.closure_temps is not None:
|
|
9450
|
+
# Allocate in the generator closure, cannot be const.
|
|
9451
|
+
array_type = self._non_const_array_type()
|
|
9452
|
+
self.array_cname = code.funcstate.closure_temps.allocate_carray(array_type, self.pos)
|
|
9453
|
+
else:
|
|
9454
|
+
# To be valid C++, we must allocate the memory on the stack
|
|
9455
|
+
# manually and be sure not to reuse it for something else.
|
|
9456
|
+
# Yes, this means that we leak a temp array variable.
|
|
9457
|
+
array_type = self._non_const_array_type()
|
|
9458
|
+
self.array_cname = code.funcstate.allocate_temp(
|
|
9459
|
+
array_type, manage_ref=False, reusable=False)
|
|
9460
|
+
self._array_cname_temp = self.array_cname
|
|
9461
|
+
return self.array_cname
|
|
9462
|
+
|
|
9463
|
+
def _release_carray(self, code):
|
|
9464
|
+
# If the array is in a temp variable, it will not be reused elsewhere,
|
|
9465
|
+
# so we can release the temp here and still use it as our node result.
|
|
9466
|
+
if self._array_cname_temp is not None:
|
|
9467
|
+
code.funcstate.release_temp(self._array_cname_temp)
|
|
9468
|
+
self._array_cname_temp = None
|
|
9469
|
+
|
|
9470
|
+
def generate_operation_code(self, code):
|
|
9471
|
+
all_const = self.type.base_type.is_const and all(arg.is_literal for arg in self.args)
|
|
9472
|
+
array_cname = self._allocate_carray(code, all_const)
|
|
9473
|
+
|
|
9474
|
+
if all_const:
|
|
9475
|
+
values = [item.result() for item in self.args]
|
|
9476
|
+
|
|
9477
|
+
if self.mult_factor:
|
|
9478
|
+
assert self.mult_factor.has_constant_result(), self.mult_factor
|
|
9479
|
+
values *= self.mult_factor.constant_result
|
|
9480
|
+
|
|
9481
|
+
code.putln(f"static {self.type.declaration_code(array_cname)} = {{{','.join(values)}}};")
|
|
9482
|
+
|
|
9483
|
+
else:
|
|
9484
|
+
if self.mult_factor:
|
|
9485
|
+
counter_type = 'int' if self.mult_factor.has_constant_result() and self.mult_factor.constant_result.bit_count() < 16 else 'Py_ssize_t'
|
|
9486
|
+
loop_var = Naming.quick_temp_cname
|
|
9487
|
+
code.putln(f"for ({counter_type} {loop_var} = 0; {loop_var} < {self.mult_factor.result()}; {loop_var}++) {{")
|
|
9488
|
+
offset = f'+ ({len(self.args):d} * {loop_var})'
|
|
9489
|
+
else:
|
|
9490
|
+
offset = ''
|
|
9491
|
+
|
|
9492
|
+
needs_memcpy = False
|
|
9493
|
+
for i, arg in enumerate(self.args):
|
|
9494
|
+
if arg.type.is_array:
|
|
9495
|
+
needs_memcpy = True
|
|
9496
|
+
code.putln(f"memcpy(&({array_cname}[{i}{offset}]), {arg.result()}, sizeof({array_cname}[0]));")
|
|
9497
|
+
else:
|
|
9498
|
+
code.putln(f"{array_cname}[{i}{offset}] = {arg.result()};")
|
|
9499
|
+
|
|
9500
|
+
if needs_memcpy:
|
|
9501
|
+
code.globalstate.use_utility_code(
|
|
9502
|
+
UtilityCode.load_cached("IncludeStringH", "StringTools.c"))
|
|
9503
|
+
|
|
9504
|
+
if self.mult_factor:
|
|
9505
|
+
code.putln("}")
|
|
9506
|
+
|
|
9507
|
+
self._release_carray(code)
|
|
9508
|
+
|
|
9509
|
+
def calculate_result_code(self):
|
|
9510
|
+
return self.array_cname
|
|
9511
|
+
|
|
9512
|
+
|
|
9269
9513
|
class ComprehensionNode(ScopedExprNode):
|
|
9270
9514
|
# A list/set/dict comprehension
|
|
9271
9515
|
|
|
@@ -9307,11 +9551,11 @@ class ComprehensionNode(ScopedExprNode):
|
|
|
9307
9551
|
self.generate_operation_code(code)
|
|
9308
9552
|
|
|
9309
9553
|
def generate_operation_code(self, code):
|
|
9310
|
-
if self.type
|
|
9554
|
+
if self.type.is_pylist_type:
|
|
9311
9555
|
create_code = 'PyList_New(0)'
|
|
9312
|
-
elif self.type
|
|
9556
|
+
elif self.type.is_pyset_type:
|
|
9313
9557
|
create_code = 'PySet_New(NULL)'
|
|
9314
|
-
elif self.type
|
|
9558
|
+
elif self.type.is_pydict_type:
|
|
9315
9559
|
create_code = 'PyDict_New()'
|
|
9316
9560
|
else:
|
|
9317
9561
|
raise InternalError("illegal type for comprehension: %s" % self.type)
|
|
@@ -9342,23 +9586,29 @@ class ComprehensionAppendNode(Node):
|
|
|
9342
9586
|
return self
|
|
9343
9587
|
|
|
9344
9588
|
def generate_execution_code(self, code):
|
|
9345
|
-
|
|
9589
|
+
steal_temp = False
|
|
9590
|
+
if self.target.type.is_pylist_type:
|
|
9591
|
+
steal_temp = self.expr.is_temp
|
|
9346
9592
|
code.globalstate.use_utility_code(
|
|
9347
|
-
UtilityCode.load_cached("ListCompAppend", "Optimize.c"))
|
|
9348
|
-
function = "__Pyx_ListComp_Append"
|
|
9349
|
-
elif self.target.type
|
|
9593
|
+
UtilityCode.load_cached("ListCompAppendAndDecref" if steal_temp else "ListCompAppend", "Optimize.c"))
|
|
9594
|
+
function = "__Pyx_ListComp_AppendAndDecref" if steal_temp else "__Pyx_ListComp_Append"
|
|
9595
|
+
elif self.target.type.is_pyset_type:
|
|
9350
9596
|
function = "PySet_Add"
|
|
9351
9597
|
else:
|
|
9352
9598
|
raise InternalError(
|
|
9353
9599
|
"Invalid type for comprehension node: %s" % self.target.type)
|
|
9354
9600
|
|
|
9355
9601
|
self.expr.generate_evaluation_code(code)
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
), self.pos))
|
|
9361
|
-
|
|
9602
|
+
if steal_temp:
|
|
9603
|
+
code.put_giveref(self.expr.result(), self.expr.ctype())
|
|
9604
|
+
|
|
9605
|
+
code.putln(code.error_goto_if(
|
|
9606
|
+
f"{function}({self.target.result()}, {self.expr.py_result()})", self.pos))
|
|
9607
|
+
|
|
9608
|
+
if steal_temp:
|
|
9609
|
+
self.expr.generate_post_assignment_code(code)
|
|
9610
|
+
else:
|
|
9611
|
+
self.expr.generate_disposal_code(code)
|
|
9362
9612
|
self.expr.free_temps(code)
|
|
9363
9613
|
|
|
9364
9614
|
def generate_function_definitions(self, env, code):
|
|
@@ -9422,7 +9672,7 @@ class InlinedGeneratorExpressionNode(ExprNode):
|
|
|
9422
9672
|
gbody = gen.def_node.gbody
|
|
9423
9673
|
gbody.is_inlined = True
|
|
9424
9674
|
if comprehension_type is not None:
|
|
9425
|
-
assert comprehension_type
|
|
9675
|
+
assert comprehension_type.is_pylist_type or comprehension_type.is_pyset_type or comprehension_type.is_pydict_type, comprehension_type
|
|
9426
9676
|
gbody.inlined_comprehension_type = comprehension_type
|
|
9427
9677
|
kwargs.update(
|
|
9428
9678
|
target=RawCNameExprNode(pos, comprehension_type, Naming.retval_cname),
|
|
@@ -9460,9 +9710,9 @@ class MergedSequenceNode(ExprNode):
|
|
|
9460
9710
|
gil_message = "Constructing Python collection"
|
|
9461
9711
|
|
|
9462
9712
|
def __init__(self, pos, args, type):
|
|
9463
|
-
if type
|
|
9713
|
+
if (type.is_pylist_type or type.is_pytuple_type) and args and args[0].is_sequence_constructor:
|
|
9464
9714
|
# construct a list directly from the first argument that we can then extend
|
|
9465
|
-
if args[0].type
|
|
9715
|
+
if not args[0].type.is_pylist_type:
|
|
9466
9716
|
args[0] = ListNode(args[0].pos, args=args[0].args, is_temp=True, mult_factor=args[0].mult_factor)
|
|
9467
9717
|
ExprNode.__init__(self, pos, args=args, type=type)
|
|
9468
9718
|
|
|
@@ -9479,12 +9729,12 @@ class MergedSequenceNode(ExprNode):
|
|
|
9479
9729
|
else:
|
|
9480
9730
|
items = item.constant_result
|
|
9481
9731
|
result.extend(items)
|
|
9482
|
-
if self.type
|
|
9732
|
+
if self.type.is_pyset_type:
|
|
9483
9733
|
result = set(result)
|
|
9484
|
-
elif self.type
|
|
9734
|
+
elif self.type.is_pytuple_type:
|
|
9485
9735
|
result = tuple(result)
|
|
9486
9736
|
else:
|
|
9487
|
-
assert self.type
|
|
9737
|
+
assert self.type.is_pylist_type, self.type
|
|
9488
9738
|
self.constant_result = result
|
|
9489
9739
|
|
|
9490
9740
|
def compile_time_value(self, denv):
|
|
@@ -9499,15 +9749,15 @@ class MergedSequenceNode(ExprNode):
|
|
|
9499
9749
|
else:
|
|
9500
9750
|
items = item.compile_time_value(denv)
|
|
9501
9751
|
result.extend(items)
|
|
9502
|
-
if self.type
|
|
9752
|
+
if self.type.is_pyset_type:
|
|
9503
9753
|
try:
|
|
9504
9754
|
result = set(result)
|
|
9505
9755
|
except Exception as e:
|
|
9506
9756
|
self.compile_time_value_error(e)
|
|
9507
|
-
elif self.type
|
|
9757
|
+
elif self.type.is_pytuple_type:
|
|
9508
9758
|
result = tuple(result)
|
|
9509
9759
|
else:
|
|
9510
|
-
assert self.type
|
|
9760
|
+
assert self.type.is_pylist_type, self.type
|
|
9511
9761
|
return result
|
|
9512
9762
|
|
|
9513
9763
|
def type_dependencies(self, env):
|
|
@@ -9528,7 +9778,7 @@ class MergedSequenceNode(ExprNode):
|
|
|
9528
9778
|
# strip this intermediate node and use the bare collection
|
|
9529
9779
|
return args[0]
|
|
9530
9780
|
|
|
9531
|
-
assert self.type
|
|
9781
|
+
assert self.type.is_pyset_type or self.type.is_pylist_type or self.type.is_pytuple_type, self.type
|
|
9532
9782
|
|
|
9533
9783
|
self.args = args
|
|
9534
9784
|
return self
|
|
@@ -9540,20 +9790,20 @@ class MergedSequenceNode(ExprNode):
|
|
|
9540
9790
|
code.mark_pos(self.pos)
|
|
9541
9791
|
self.allocate_temp_result(code)
|
|
9542
9792
|
|
|
9543
|
-
is_set = self.type
|
|
9793
|
+
is_set = self.type.is_pyset_type
|
|
9544
9794
|
|
|
9545
9795
|
args = iter(self.args)
|
|
9546
9796
|
item = next(args)
|
|
9547
9797
|
item.generate_evaluation_code(code)
|
|
9548
9798
|
if (is_set and item.is_set_literal or
|
|
9549
|
-
not is_set and item.is_sequence_constructor and item.type
|
|
9799
|
+
not is_set and item.is_sequence_constructor and item.type.is_pylist_type):
|
|
9550
9800
|
code.putln("%s = %s;" % (self.result(), item.py_result()))
|
|
9551
9801
|
item.generate_post_assignment_code(code)
|
|
9552
9802
|
else:
|
|
9553
9803
|
code.putln("%s = %s(%s); %s" % (
|
|
9554
9804
|
self.result(),
|
|
9555
9805
|
'PySet_New' if is_set
|
|
9556
|
-
else "__Pyx_PySequence_ListKeepNew" if item.result_in_temp() and item.type
|
|
9806
|
+
else "__Pyx_PySequence_ListKeepNew" if item.result_in_temp() and item.type.may_be_pylist_type
|
|
9557
9807
|
else "PySequence_List",
|
|
9558
9808
|
item.py_result(),
|
|
9559
9809
|
code.error_goto_if_null(self.result(), self.pos)))
|
|
@@ -9576,11 +9826,18 @@ class MergedSequenceNode(ExprNode):
|
|
|
9576
9826
|
helpers.add(("ListCompAppend", "Optimize.c"))
|
|
9577
9827
|
for arg in item.args:
|
|
9578
9828
|
arg.generate_evaluation_code(code)
|
|
9829
|
+
can_steal_list_item = not is_set and arg.is_temp
|
|
9830
|
+
if can_steal_list_item:
|
|
9831
|
+
helpers.add(("ListCompAppendAndDecref", "Optimize.c"))
|
|
9579
9832
|
code.put_error_if_neg(arg.pos, "%s(%s, %s)" % (
|
|
9580
|
-
add_func,
|
|
9833
|
+
"__Pyx_ListComp_AppendAndDecref" if can_steal_list_item else add_func,
|
|
9581
9834
|
self.result(),
|
|
9582
9835
|
arg.py_result()))
|
|
9583
|
-
|
|
9836
|
+
if can_steal_list_item:
|
|
9837
|
+
code.put_giveref(arg.result(), arg.ctype())
|
|
9838
|
+
arg.generate_post_assignment_code(code)
|
|
9839
|
+
else:
|
|
9840
|
+
arg.generate_disposal_code(code)
|
|
9584
9841
|
arg.free_temps(code)
|
|
9585
9842
|
continue
|
|
9586
9843
|
|
|
@@ -9597,7 +9854,7 @@ class MergedSequenceNode(ExprNode):
|
|
|
9597
9854
|
item.generate_disposal_code(code)
|
|
9598
9855
|
item.free_temps(code)
|
|
9599
9856
|
|
|
9600
|
-
if self.type
|
|
9857
|
+
if self.type.is_pytuple_type:
|
|
9601
9858
|
code.putln("{")
|
|
9602
9859
|
code.putln("PyObject *%s = PyList_AsTuple(%s);" % (
|
|
9603
9860
|
Naming.quick_temp_cname,
|
|
@@ -9905,7 +10162,7 @@ class SortedDictKeysNode(ExprNode):
|
|
|
9905
10162
|
|
|
9906
10163
|
def analyse_types(self, env):
|
|
9907
10164
|
arg = self.arg.analyse_types(env)
|
|
9908
|
-
if arg.type
|
|
10165
|
+
if arg.type.is_pyanydict_type:
|
|
9909
10166
|
arg = arg.as_none_safe_node(
|
|
9910
10167
|
"'NoneType' object is not iterable")
|
|
9911
10168
|
self.arg = arg
|
|
@@ -9916,7 +10173,7 @@ class SortedDictKeysNode(ExprNode):
|
|
|
9916
10173
|
|
|
9917
10174
|
def generate_result_code(self, code):
|
|
9918
10175
|
dict_result = self.arg.py_result()
|
|
9919
|
-
if self.arg.type
|
|
10176
|
+
if self.arg.type.is_pyanydict_type:
|
|
9920
10177
|
code.putln('%s = PyDict_Keys(%s); %s' % (
|
|
9921
10178
|
self.result(), dict_result,
|
|
9922
10179
|
code.error_goto_if_null(self.result(), self.pos)))
|
|
@@ -10430,10 +10687,16 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin):
|
|
|
10430
10687
|
def_node = self.def_node
|
|
10431
10688
|
|
|
10432
10689
|
if self.specialized_cpdefs or self.is_specialization:
|
|
10690
|
+
if code.funcstate.scope.context.shared_utility_qualified_name:
|
|
10691
|
+
code.globalstate.use_utility_code(
|
|
10692
|
+
UtilityCode.load_cached("FusedFunctionFromSharedModule", "CythonFunction.c"))
|
|
10433
10693
|
code.globalstate.use_utility_code(
|
|
10434
10694
|
UtilityCode.load_cached("FusedFunction", "CythonFunction.c"))
|
|
10435
10695
|
constructor = "__pyx_FusedFunction_New"
|
|
10436
10696
|
else:
|
|
10697
|
+
if code.funcstate.scope.context.shared_utility_qualified_name:
|
|
10698
|
+
code.globalstate.use_utility_code(
|
|
10699
|
+
UtilityCode.load_cached("CythonFunctionFromSharedModule", "CythonFunction.c"))
|
|
10437
10700
|
code.globalstate.use_utility_code(
|
|
10438
10701
|
UtilityCode.load_cached("CythonFunction", "CythonFunction.c"))
|
|
10439
10702
|
constructor = "__Pyx_CyFunction_New"
|
|
@@ -10596,11 +10859,9 @@ class CodeObjectNode(ExprNode):
|
|
|
10596
10859
|
|
|
10597
10860
|
func_name_result = code.get_py_string_const(func.name, identifier=True)
|
|
10598
10861
|
# FIXME: better way to get the module file path at module init time? Encoding to use?
|
|
10599
|
-
file_path = func.pos[0].
|
|
10600
|
-
if os.path.isabs(file_path):
|
|
10601
|
-
file_path = func.pos[0].get_description()
|
|
10862
|
+
file_path = func.pos[0].get_relative_path()
|
|
10602
10863
|
# Always use / as separator
|
|
10603
|
-
file_path = StringEncoding.EncodedString(
|
|
10864
|
+
file_path = StringEncoding.EncodedString(file_path.as_posix())
|
|
10604
10865
|
file_path_result = code.get_py_string_const(file_path)
|
|
10605
10866
|
|
|
10606
10867
|
if func.node_positions:
|
|
@@ -10635,7 +10896,7 @@ class CodeObjectNode(ExprNode):
|
|
|
10635
10896
|
num_posonly_args = func.num_posonly_args # Py3.8+ only
|
|
10636
10897
|
kwonly_argcount = func.num_kwonly_args
|
|
10637
10898
|
nlocals = len(self.varnames)
|
|
10638
|
-
flags = '(unsigned int)(%s)' % '|'.join(flags)
|
|
10899
|
+
flags = '(unsigned int)(%s)' % '|'.join(flags)
|
|
10639
10900
|
|
|
10640
10901
|
# See "generate_codeobject_constants()" in Code.py.
|
|
10641
10902
|
code.putln("{")
|
|
@@ -11597,7 +11858,10 @@ class TypecastNode(ExprNode):
|
|
|
11597
11858
|
if not self.type.is_numeric and not self.type.is_cpp_class:
|
|
11598
11859
|
error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
|
|
11599
11860
|
if to_py and not from_py:
|
|
11600
|
-
if self.type is
|
|
11861
|
+
if self.type is bool_type:
|
|
11862
|
+
# Allow "<bool>" to cast C values directly to Python bool.
|
|
11863
|
+
return self.operand.coerce_to_boolean(env)
|
|
11864
|
+
elif self.type.is_pybytes_type and self.operand.type.is_int:
|
|
11601
11865
|
return CoerceIntToBytesNode(self.operand, env)
|
|
11602
11866
|
elif self.operand.type.can_coerce_to_pyobject(env):
|
|
11603
11867
|
self.result_ctype = py_object_type
|
|
@@ -11621,6 +11885,20 @@ class TypecastNode(ExprNode):
|
|
|
11621
11885
|
warning(self.pos, "No conversion from %s to %s, python object pointer used." % (
|
|
11622
11886
|
self.type, self.operand.type))
|
|
11623
11887
|
elif from_py and to_py:
|
|
11888
|
+
if self.type.is_builtin_type and self.operand.type.is_builtin_type:
|
|
11889
|
+
self_type = self.type.get_container_type() or self.type
|
|
11890
|
+
operand_type = self.operand.type.get_container_type() or self.operand.type
|
|
11891
|
+
if self_type != operand_type:
|
|
11892
|
+
if operand_type is bool_type and self_type is int_type:
|
|
11893
|
+
# 'bool' is an 'int' subclass, so this cast is not entirely useless.
|
|
11894
|
+
# It is difficult to test, though, because bool/int are shadowed by C bint/int.
|
|
11895
|
+
pass
|
|
11896
|
+
else:
|
|
11897
|
+
warning(
|
|
11898
|
+
self.pos,
|
|
11899
|
+
f"Cast from '{self.operand.type}' to '{self_type.name}' is invalid at runtime. "
|
|
11900
|
+
f"Did you mean to call '{self_type.name}()'?"
|
|
11901
|
+
)
|
|
11624
11902
|
if self.typecheck:
|
|
11625
11903
|
self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
|
|
11626
11904
|
elif isinstance(self.operand, SliceIndexNode):
|
|
@@ -12042,7 +12320,7 @@ class TypeidNode(ExprNode):
|
|
|
12042
12320
|
# C++ typeid operator applied to a type or variable
|
|
12043
12321
|
#
|
|
12044
12322
|
# operand ExprNode
|
|
12045
|
-
# arg_type
|
|
12323
|
+
# arg_type PyrexType
|
|
12046
12324
|
# is_variable boolean
|
|
12047
12325
|
|
|
12048
12326
|
subexprs = ['operand']
|
|
@@ -12057,7 +12335,7 @@ class TypeidNode(ExprNode):
|
|
|
12057
12335
|
env_module = env_module.outer_scope
|
|
12058
12336
|
typeinfo_module = env_module.find_module('libcpp.typeinfo', self.pos)
|
|
12059
12337
|
typeinfo_entry = typeinfo_module.lookup('type_info')
|
|
12060
|
-
return PyrexTypes.CFakeReferenceType(PyrexTypes.
|
|
12338
|
+
return PyrexTypes.CFakeReferenceType(PyrexTypes.c_qualifier_type(typeinfo_entry.type, is_const=True))
|
|
12061
12339
|
|
|
12062
12340
|
cpp_message = 'typeid operator'
|
|
12063
12341
|
|
|
@@ -12070,7 +12348,7 @@ class TypeidNode(ExprNode):
|
|
|
12070
12348
|
self.error("The 'libcpp.typeinfo' module must be cimported to use the typeid() operator")
|
|
12071
12349
|
return self
|
|
12072
12350
|
if self.operand is None:
|
|
12073
|
-
return self # already analysed, no need to repeat
|
|
12351
|
+
return self # already analysed to "self.is_type", no need to repeat
|
|
12074
12352
|
self.type = type_info
|
|
12075
12353
|
as_type = self.operand.analyse_as_specialized_type(env)
|
|
12076
12354
|
if as_type:
|
|
@@ -12078,16 +12356,15 @@ class TypeidNode(ExprNode):
|
|
|
12078
12356
|
self.is_type = True
|
|
12079
12357
|
self.operand = None # nothing further uses self.operand - will only cause problems if its used in code generation
|
|
12080
12358
|
else:
|
|
12081
|
-
self.
|
|
12359
|
+
self.operand = self.operand.analyse_types(env)
|
|
12082
12360
|
self.is_type = False
|
|
12083
|
-
self.operand
|
|
12084
|
-
if self.arg_type.type.is_pyobject:
|
|
12361
|
+
if self.operand.type.is_pyobject:
|
|
12085
12362
|
self.error("Cannot use typeid on a Python object")
|
|
12086
12363
|
return self
|
|
12087
|
-
elif self.
|
|
12364
|
+
elif self.operand.type.is_void:
|
|
12088
12365
|
self.error("Cannot use typeid on void")
|
|
12089
12366
|
return self
|
|
12090
|
-
elif not self.
|
|
12367
|
+
elif not self.operand.type.is_complete():
|
|
12091
12368
|
self.error("Cannot use typeid on incomplete type '%s'" % self.arg_type.type)
|
|
12092
12369
|
return self
|
|
12093
12370
|
env.use_utility_code(UtilityCode.load_cached("CppExceptionConversion", "CppSupport.cpp"))
|
|
@@ -12108,7 +12385,7 @@ class TypeidNode(ExprNode):
|
|
|
12108
12385
|
if self.is_type:
|
|
12109
12386
|
arg_code = self.arg_type.empty_declaration_code()
|
|
12110
12387
|
else:
|
|
12111
|
-
arg_code = self.
|
|
12388
|
+
arg_code = self.operand.result()
|
|
12112
12389
|
translate_cpp_exception(code, self.pos,
|
|
12113
12390
|
"%s = typeid(%s);" % (self.temp_code, arg_code),
|
|
12114
12391
|
None, None, self.in_nogil_context)
|
|
@@ -12552,9 +12829,29 @@ class NumBinopNode(BinopNode):
|
|
|
12552
12829
|
|
|
12553
12830
|
def py_operation_function(self, code):
|
|
12554
12831
|
function_name = self.py_functions[self.operator]
|
|
12555
|
-
|
|
12556
|
-
|
|
12557
|
-
|
|
12832
|
+
inplace_function_name = function_name.replace('PyNumber_', 'PyNumber_InPlace')
|
|
12833
|
+
|
|
12834
|
+
if self.operator in self.fast_pyops:
|
|
12835
|
+
type1 = self.operand1.type
|
|
12836
|
+
type2 = self.operand2.type
|
|
12837
|
+
if self.is_specialised_binop_type(type1) and self.is_specialised_binop_type(type2):
|
|
12838
|
+
code.globalstate.use_utility_code(
|
|
12839
|
+
TempitaUtilityCode.load_cached("PyNumberBinop", "Optimize.c", context={
|
|
12840
|
+
'op_name': function_name,
|
|
12841
|
+
'inplace_op_name': inplace_function_name,
|
|
12842
|
+
'c_op': self.operator,
|
|
12843
|
+
'type1': type1.name,
|
|
12844
|
+
'type2': type2.name,
|
|
12845
|
+
}))
|
|
12846
|
+
return f'__Pyx_{inplace_function_name if self.inplace else function_name}_{type1.name}_{type2.name}'
|
|
12847
|
+
|
|
12848
|
+
return inplace_function_name if self.inplace else function_name
|
|
12849
|
+
|
|
12850
|
+
@staticmethod
|
|
12851
|
+
def is_specialised_binop_type(typ):
|
|
12852
|
+
return typ.may_be_pyint_type or typ.may_be_pyfloat_type
|
|
12853
|
+
|
|
12854
|
+
fast_pyops = {'+', '-', '*', '^', '&', '|'}
|
|
12558
12855
|
|
|
12559
12856
|
py_functions = {
|
|
12560
12857
|
"|": "PyNumber_Or",
|
|
@@ -12631,10 +12928,14 @@ class AddNode(NumBinopNode):
|
|
|
12631
12928
|
def infer_builtin_types_operation(self, type1, type2):
|
|
12632
12929
|
# b'abc' + 'abc' raises an exception in Py3,
|
|
12633
12930
|
# so we can safely infer a mix here.
|
|
12634
|
-
|
|
12635
|
-
|
|
12636
|
-
|
|
12637
|
-
|
|
12931
|
+
if type1.is_pybytes_type:
|
|
12932
|
+
if type2.is_pybytes_type or type2.is_pybytearray_type:
|
|
12933
|
+
return type2 # bytes or bytearray
|
|
12934
|
+
elif type1.is_pybytearray_type:
|
|
12935
|
+
if type2.is_pybytes_type or type2.is_pybytearray_type:
|
|
12936
|
+
return type1 # bytearray
|
|
12937
|
+
elif type1.is_pystr_type and type2.is_pystr_type:
|
|
12938
|
+
return type1
|
|
12638
12939
|
return super().infer_builtin_types_operation(type1, type2)
|
|
12639
12940
|
|
|
12640
12941
|
def compute_c_result_type(self, type1, type2):
|
|
@@ -12650,8 +12951,8 @@ class AddNode(NumBinopNode):
|
|
|
12650
12951
|
def py_operation_function(self, code):
|
|
12651
12952
|
type1, type2 = self.operand1.type, self.operand2.type
|
|
12652
12953
|
func = None
|
|
12653
|
-
if type1
|
|
12654
|
-
if type1
|
|
12954
|
+
if type1.is_pystr_type or type2.is_pystr_type:
|
|
12955
|
+
if type1.is_pystr_type and type2.is_pystr_type:
|
|
12655
12956
|
is_unicode_concat = True
|
|
12656
12957
|
elif isinstance(self.operand1, FormattedValueNode) or isinstance(self.operand2, FormattedValueNode):
|
|
12657
12958
|
# Assume that even if we don't know the second type, it's going to be a string.
|
|
@@ -12708,24 +13009,24 @@ class MulNode(NumBinopNode):
|
|
|
12708
13009
|
return self.analyse_sequence_mul(env, operand1, operand2)
|
|
12709
13010
|
elif operand2.is_sequence_constructor and operand2.mult_factor is None:
|
|
12710
13011
|
return self.analyse_sequence_mul(env, operand2, operand1)
|
|
12711
|
-
elif operand1.type
|
|
13012
|
+
elif operand1.type and operand1.type.is_builtin_sequence:
|
|
12712
13013
|
self.operand2 = operand2.coerce_to_index(env)
|
|
12713
|
-
elif operand2.type
|
|
13014
|
+
elif operand2.type and operand2.type.is_builtin_sequence:
|
|
12714
13015
|
self.operand1 = operand1.coerce_to_index(env)
|
|
12715
13016
|
|
|
12716
13017
|
return self.analyse_operation(env)
|
|
12717
13018
|
|
|
12718
13019
|
@staticmethod
|
|
12719
13020
|
def is_builtin_seqmul_type(type):
|
|
12720
|
-
return type.is_builtin_type and type
|
|
13021
|
+
return type.is_builtin_type and type.is_builtin_sequence and not type.is_pymemoryview_type
|
|
12721
13022
|
|
|
12722
13023
|
def calculate_is_sequence_mul(self):
|
|
12723
13024
|
type1 = self.operand1.type
|
|
12724
13025
|
type2 = self.operand2.type
|
|
12725
|
-
if type1
|
|
13026
|
+
if type1.is_pyint_type or type1.is_int:
|
|
12726
13027
|
# normalise to (X * int)
|
|
12727
13028
|
type1, type2 = type2, type1
|
|
12728
|
-
if type2
|
|
13029
|
+
if type2.is_pyint_type or type2.is_int:
|
|
12729
13030
|
if type1.is_string or type1.is_ctuple:
|
|
12730
13031
|
return True
|
|
12731
13032
|
if self.is_builtin_seqmul_type(type1):
|
|
@@ -12839,11 +13140,11 @@ class DivNode(NumBinopNode):
|
|
|
12839
13140
|
if result_type is not None and self.operator == '/':
|
|
12840
13141
|
if self.truedivision or self.ctruedivision:
|
|
12841
13142
|
# Result of truedivision is not an integer
|
|
12842
|
-
if result_type
|
|
13143
|
+
if result_type.is_pyint_type:
|
|
12843
13144
|
return PyrexTypes.c_double_type
|
|
12844
13145
|
elif result_type.is_int:
|
|
12845
13146
|
return PyrexTypes.widest_numeric_type(PyrexTypes.c_double_type, result_type)
|
|
12846
|
-
elif result_type
|
|
13147
|
+
elif result_type.is_pyint_type or result_type.is_int:
|
|
12847
13148
|
# Cannot infer 'int' since the result might be a 'float' in Python 3
|
|
12848
13149
|
result_type = None
|
|
12849
13150
|
return result_type
|
|
@@ -13004,7 +13305,7 @@ class ModNode(DivNode):
|
|
|
13004
13305
|
or NumBinopNode.is_py_operation_types(self, type1, type2))
|
|
13005
13306
|
|
|
13006
13307
|
def infer_builtin_types_operation(self, type1, type2):
|
|
13007
|
-
if type1
|
|
13308
|
+
if type1.is_bytes_or_str_or_bytearray:
|
|
13008
13309
|
# 'None % xyz' may be implemented by the RHS, but everything else will do string formatting.
|
|
13009
13310
|
if type2.is_builtin_type or not type2.is_pyobject or not self.operand1.may_be_none():
|
|
13010
13311
|
return type1
|
|
@@ -13057,7 +13358,7 @@ class ModNode(DivNode):
|
|
|
13057
13358
|
def py_operation_function(self, code):
|
|
13058
13359
|
type1, type2 = self.operand1.type, self.operand2.type
|
|
13059
13360
|
# ("..." % x) must call "x.__rmod__()" for string subtypes.
|
|
13060
|
-
if type1
|
|
13361
|
+
if type1.is_pystr_type:
|
|
13061
13362
|
if self.operand1.may_be_none() or (
|
|
13062
13363
|
type2.is_extension_type and type2.subtype_of(type1) or
|
|
13063
13364
|
type2 is py_object_type and not isinstance(self.operand2, CoerceToPyTypeNode)):
|
|
@@ -13169,10 +13470,12 @@ class PowNode(NumBinopNode):
|
|
|
13169
13470
|
typecast(self.operand2))
|
|
13170
13471
|
|
|
13171
13472
|
def py_operation_function(self, code):
|
|
13172
|
-
if (
|
|
13173
|
-
|
|
13174
|
-
|
|
13175
|
-
|
|
13473
|
+
if (
|
|
13474
|
+
self.type.is_pyobject and
|
|
13475
|
+
self.operand1.constant_result == 2 and
|
|
13476
|
+
isinstance(self.operand1.constant_result, int) and
|
|
13477
|
+
self.operand2.type.may_be_pyint_type
|
|
13478
|
+
):
|
|
13176
13479
|
code.globalstate.use_utility_code(UtilityCode.load_cached('PyNumberPow2', 'Optimize.c'))
|
|
13177
13480
|
if self.inplace:
|
|
13178
13481
|
return '__Pyx_PyNumber_InPlacePowerOf2'
|
|
@@ -13698,7 +14001,7 @@ class CmpNode:
|
|
|
13698
14001
|
return type2
|
|
13699
14002
|
elif type1_can_be_int:
|
|
13700
14003
|
if type2_can_be_int:
|
|
13701
|
-
if
|
|
14004
|
+
if type1.is_pystr_type or type2.is_pystr_type:
|
|
13702
14005
|
return PyrexTypes.c_py_ucs4_type
|
|
13703
14006
|
else:
|
|
13704
14007
|
return PyrexTypes.c_uchar_type
|
|
@@ -13811,9 +14114,9 @@ class CmpNode:
|
|
|
13811
14114
|
def is_c_string_contains(self):
|
|
13812
14115
|
return self.operator in ('in', 'not_in') and \
|
|
13813
14116
|
((self.operand1.type.is_int
|
|
13814
|
-
and (self.operand2.type.is_string or self.operand2.type
|
|
14117
|
+
and (self.operand2.type.is_string or self.operand2.type.is_pybytes_type)) or
|
|
13815
14118
|
(self.operand1.type.is_unicode_char
|
|
13816
|
-
and self.operand2.type
|
|
14119
|
+
and self.operand2.type.is_pystr_type))
|
|
13817
14120
|
|
|
13818
14121
|
def is_ptr_contains(self):
|
|
13819
14122
|
if self.operator in ('in', 'not_in'):
|
|
@@ -13826,14 +14129,23 @@ class CmpNode:
|
|
|
13826
14129
|
if self.operator in ('==', '!='):
|
|
13827
14130
|
type1, type2 = operand1.type, self.operand2.type
|
|
13828
14131
|
if result_is_bool or (type1.is_builtin_type and type2.is_builtin_type):
|
|
13829
|
-
if type1
|
|
13830
|
-
|
|
13831
|
-
|
|
13832
|
-
|
|
13833
|
-
|
|
13834
|
-
|
|
13835
|
-
|
|
13836
|
-
|
|
14132
|
+
if type1.is_pystr_type or type2.is_pystr_type:
|
|
14133
|
+
if operand1.is_string_literal and operand1.can_coerce_to_char_literal():
|
|
14134
|
+
# We need to keep the signature (obj1, obj2, eq), so we generate one macro function per character.
|
|
14135
|
+
character = ord(operand1.value[0])
|
|
14136
|
+
is_str = type2.is_pystr_type
|
|
14137
|
+
self.special_bool_cmp_utility_code = TempitaUtilityCode.load_cached(
|
|
14138
|
+
"UnicodeEquals_uchar", "StringTools.c", context={'CHAR': character, 'IS_STR': is_str, 'REVERSE': True})
|
|
14139
|
+
self.special_bool_cmp_function = f"__Pyx_PyObject_Equals_ch{character}_{'str' if is_str else 'obj'}"
|
|
14140
|
+
return True
|
|
14141
|
+
elif self.operand2.is_string_literal and self.operand2.can_coerce_to_char_literal():
|
|
14142
|
+
# We need to keep the signature (obj1, obj2, eq), so we generate one macro function per character.
|
|
14143
|
+
character = ord(self.operand2.value[0])
|
|
14144
|
+
is_str = type1.is_pystr_type
|
|
14145
|
+
self.special_bool_cmp_utility_code = TempitaUtilityCode.load_cached(
|
|
14146
|
+
"UnicodeEquals_uchar", "StringTools.c", context={'CHAR': character, 'IS_STR': is_str, 'REVERSE': False})
|
|
14147
|
+
self.special_bool_cmp_function = f"__Pyx_PyObject_Equals_{'str' if is_str else 'obj'}_ch{character}"
|
|
14148
|
+
return True
|
|
13837
14149
|
elif result_is_bool:
|
|
13838
14150
|
from .Optimize import optimise_numeric_binop
|
|
13839
14151
|
result = optimise_numeric_binop(
|
|
@@ -13850,17 +14162,17 @@ class CmpNode:
|
|
|
13850
14162
|
_) = result
|
|
13851
14163
|
return True
|
|
13852
14164
|
elif self.operator in ('in', 'not_in'):
|
|
13853
|
-
if self.operand2.type
|
|
14165
|
+
if self.operand2.type.is_pyanydict_type:
|
|
13854
14166
|
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
|
|
13855
14167
|
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PyDictContains", "ObjectHandling.c")
|
|
13856
14168
|
self.special_bool_cmp_function = "__Pyx_PyDict_ContainsTF"
|
|
13857
14169
|
return True
|
|
13858
|
-
elif self.operand2.type
|
|
14170
|
+
elif self.operand2.type.is_pyset_type:
|
|
13859
14171
|
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
|
|
13860
14172
|
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PySetContains", "ObjectHandling.c")
|
|
13861
14173
|
self.special_bool_cmp_function = "__Pyx_PySet_ContainsTF"
|
|
13862
14174
|
return True
|
|
13863
|
-
elif self.operand2.type
|
|
14175
|
+
elif self.operand2.type.is_pystr_type:
|
|
13864
14176
|
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
|
|
13865
14177
|
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PyUnicodeContains", "StringTools.c")
|
|
13866
14178
|
self.special_bool_cmp_function = "__Pyx_PyUnicode_ContainsTF"
|
|
@@ -13873,6 +14185,32 @@ class CmpNode:
|
|
|
13873
14185
|
return True
|
|
13874
14186
|
return False
|
|
13875
14187
|
|
|
14188
|
+
_optimised_compare_types = {
|
|
14189
|
+
py_object_type,
|
|
14190
|
+
Builtin.int_type,
|
|
14191
|
+
Builtin.float_type,
|
|
14192
|
+
Builtin.unicode_type,
|
|
14193
|
+
Builtin.bytes_type,
|
|
14194
|
+
Builtin.bytearray_type,
|
|
14195
|
+
}
|
|
14196
|
+
|
|
14197
|
+
def find_compare_function(self, code, operand1):
|
|
14198
|
+
if self.operator in ('==', '!=', '<', '<=', '>=', '>'):
|
|
14199
|
+
type1, type2 = operand1.type, self.operand2.type
|
|
14200
|
+
if {type1, type2} < self._optimised_compare_types:
|
|
14201
|
+
op_name = {'==': 'Eq', '!=': 'Ne', '<': 'Lt', '<=': 'Le', '>=': 'Ge', '>': 'Gt'}[self.operator]
|
|
14202
|
+
code.globalstate.use_utility_code(TempitaUtilityCode.load_cached(
|
|
14203
|
+
"PyObjectCompare", "Optimize.c", context={
|
|
14204
|
+
'type1': type1.name,
|
|
14205
|
+
'type2': type2.name,
|
|
14206
|
+
'c_op': self.operator,
|
|
14207
|
+
'op': op_name,
|
|
14208
|
+
'return_obj': self.type.is_pyobject,
|
|
14209
|
+
}))
|
|
14210
|
+
return f"__Pyx_PyObject_Compare{'' if self.type.is_pyobject else 'Bool'}{op_name}_{type1.name}_{type2.name}"
|
|
14211
|
+
|
|
14212
|
+
return "PyObject_RichCompare" if self.type.is_pyobject else "__Pyx_PyObject_RichCompareBool"
|
|
14213
|
+
|
|
13876
14214
|
def generate_operation_code(self, code, result_code,
|
|
13877
14215
|
operand1, op, operand2):
|
|
13878
14216
|
if self.type.is_pyobject:
|
|
@@ -13916,14 +14254,12 @@ class CmpNode:
|
|
|
13916
14254
|
elif operand1.type.is_pyobject and op not in ('is', 'is_not'):
|
|
13917
14255
|
assert op not in ('in', 'not_in'), op
|
|
13918
14256
|
assert self.type.is_pyobject or self.type is PyrexTypes.c_bint_type
|
|
13919
|
-
|
|
13920
|
-
|
|
13921
|
-
|
|
13922
|
-
|
|
13923
|
-
|
|
13924
|
-
|
|
13925
|
-
got_ref,
|
|
13926
|
-
error_clause(result_code, self.pos)))
|
|
14257
|
+
function = self.find_compare_function(code, operand1)
|
|
14258
|
+
op1 = operand1.py_result()
|
|
14259
|
+
op2 = operand2.py_result()
|
|
14260
|
+
error_goto = error_clause(result_code, self.pos)
|
|
14261
|
+
code.putln(
|
|
14262
|
+
f"{result_code} = {function}({op1}, {op2}, {richcmp_constants[op]}); {got_ref}{error_goto}")
|
|
13927
14263
|
|
|
13928
14264
|
elif operand1.type.is_complex:
|
|
13929
14265
|
code.putln("%s = %s(%s%s(%s, %s));" % (
|
|
@@ -14034,7 +14370,6 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14034
14370
|
if is_pythran_expr(type1) or is_pythran_expr(type2):
|
|
14035
14371
|
if is_pythran_supported_type(type1) and is_pythran_supported_type(type2):
|
|
14036
14372
|
self.type = PythranExpr(pythran_binop_type(self.operator, type1, type2))
|
|
14037
|
-
self.is_pycmp = False
|
|
14038
14373
|
return self
|
|
14039
14374
|
|
|
14040
14375
|
if self.analyse_memoryviewslice_comparison(env):
|
|
@@ -14045,17 +14380,16 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14045
14380
|
|
|
14046
14381
|
if self.operator in ('in', 'not_in'):
|
|
14047
14382
|
if self.is_c_string_contains():
|
|
14048
|
-
self.is_pycmp = False
|
|
14049
14383
|
common_type = None
|
|
14050
14384
|
if self.cascade:
|
|
14051
14385
|
error(self.pos, "Cascading comparison not yet supported for 'int_val in string'.")
|
|
14052
14386
|
return self
|
|
14053
|
-
if self.operand2.type
|
|
14387
|
+
if self.operand2.type.is_pystr_type:
|
|
14054
14388
|
env.use_utility_code(UtilityCode.load_cached("PyUCS4InUnicode", "StringTools.c"))
|
|
14055
14389
|
else:
|
|
14056
14390
|
if self.operand1.type is PyrexTypes.c_uchar_type:
|
|
14057
14391
|
self.operand1 = self.operand1.coerce_to(PyrexTypes.c_char_type, env)
|
|
14058
|
-
if self.operand2.type
|
|
14392
|
+
if not self.operand2.type.is_pybytes_type:
|
|
14059
14393
|
self.operand2 = self.operand2.coerce_to(bytes_type, env)
|
|
14060
14394
|
env.use_utility_code(UtilityCode.load_cached("BytesContains", "StringTools.c"))
|
|
14061
14395
|
self.operand2 = self.operand2.as_none_safe_node(
|
|
@@ -14070,18 +14404,19 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14070
14404
|
if not self.operand1.type.is_pyobject:
|
|
14071
14405
|
self.operand1 = self.operand1.coerce_to_pyobject(env)
|
|
14072
14406
|
common_type = None # if coercion needed, the method call above has already done it
|
|
14073
|
-
self.
|
|
14407
|
+
self.is_temp = True # error return
|
|
14074
14408
|
else:
|
|
14075
14409
|
common_type = py_object_type
|
|
14076
|
-
self.
|
|
14410
|
+
self.is_temp = True # owned reference
|
|
14077
14411
|
elif self.find_special_bool_compare_function(env, self.operand1):
|
|
14078
14412
|
if not self.operand1.type.is_pyobject:
|
|
14079
14413
|
self.operand1 = self.operand1.coerce_to_pyobject(env)
|
|
14080
14414
|
common_type = None # if coercion needed, the method call above has already done it
|
|
14081
|
-
self.
|
|
14415
|
+
self.is_temp = True # error return
|
|
14082
14416
|
else:
|
|
14083
14417
|
common_type = self.find_common_type(env, self.operator, self.operand1)
|
|
14084
|
-
|
|
14418
|
+
if common_type.is_pyobject:
|
|
14419
|
+
self.is_temp = True # owned reference
|
|
14085
14420
|
|
|
14086
14421
|
if common_type is not None and not common_type.is_error:
|
|
14087
14422
|
if self.operand1.type != common_type:
|
|
@@ -14089,25 +14424,25 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14089
14424
|
self.coerce_operands_to(common_type, env)
|
|
14090
14425
|
|
|
14091
14426
|
if self.cascade:
|
|
14427
|
+
self.is_temp = True # reused value
|
|
14092
14428
|
self.operand2 = self.operand2.coerce_to_simple(env)
|
|
14093
14429
|
self.cascade.coerce_cascaded_operands_to_temp(env)
|
|
14094
14430
|
operand2 = self.cascade.optimise_comparison(self.operand2, env)
|
|
14095
14431
|
if operand2 is not self.operand2:
|
|
14096
14432
|
self.coerced_operand2 = operand2
|
|
14433
|
+
|
|
14097
14434
|
if self.is_python_result():
|
|
14098
14435
|
self.type = PyrexTypes.py_object_type
|
|
14436
|
+
self.is_temp = True # owned reference
|
|
14099
14437
|
else:
|
|
14100
14438
|
self.type = PyrexTypes.c_bint_type
|
|
14101
14439
|
self.unify_cascade_type()
|
|
14102
|
-
|
|
14103
|
-
# 1) owned reference, 2) reused value, 3) potential function error return value
|
|
14104
|
-
self.is_temp = 1
|
|
14440
|
+
|
|
14105
14441
|
return self
|
|
14106
14442
|
|
|
14107
14443
|
def analyse_cpp_comparison(self, env):
|
|
14108
14444
|
type1 = self.operand1.type
|
|
14109
14445
|
type2 = self.operand2.type
|
|
14110
|
-
self.is_pycmp = False
|
|
14111
14446
|
entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
|
|
14112
14447
|
if entry is None:
|
|
14113
14448
|
error(self.pos, "Invalid types for '%s' (%s, %s)" %
|
|
@@ -14137,7 +14472,6 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14137
14472
|
self.operand2.type.is_memoryviewslice)
|
|
14138
14473
|
ops = ('==', '!=', 'is', 'is_not')
|
|
14139
14474
|
if have_slice and have_none and self.operator in ops:
|
|
14140
|
-
self.is_pycmp = False
|
|
14141
14475
|
self.type = PyrexTypes.c_bint_type
|
|
14142
14476
|
self.is_memslice_nonecheck = True
|
|
14143
14477
|
return True
|
|
@@ -14145,22 +14479,24 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14145
14479
|
return False
|
|
14146
14480
|
|
|
14147
14481
|
def coerce_to_boolean(self, env):
|
|
14148
|
-
if self.
|
|
14149
|
-
|
|
14150
|
-
|
|
14151
|
-
|
|
14152
|
-
|
|
14153
|
-
|
|
14154
|
-
|
|
14155
|
-
|
|
14156
|
-
|
|
14157
|
-
|
|
14158
|
-
|
|
14159
|
-
|
|
14160
|
-
|
|
14161
|
-
|
|
14162
|
-
|
|
14163
|
-
|
|
14482
|
+
if self.type is PyrexTypes.c_bint_type:
|
|
14483
|
+
return self
|
|
14484
|
+
|
|
14485
|
+
# coercing to bool => may allow for more efficient comparison code
|
|
14486
|
+
self.find_special_bool_compare_function(env, self.operand1, result_is_bool=True)
|
|
14487
|
+
|
|
14488
|
+
if self.cascade:
|
|
14489
|
+
operand2 = self.cascade.optimise_comparison(
|
|
14490
|
+
self.operand2, env, result_is_bool=True)
|
|
14491
|
+
if operand2 is not self.operand2:
|
|
14492
|
+
self.coerced_operand2 = operand2
|
|
14493
|
+
|
|
14494
|
+
# Now make sure that we return a bint, however we calculate the result.
|
|
14495
|
+
self.type = PyrexTypes.c_bint_type
|
|
14496
|
+
self.is_temp = 1
|
|
14497
|
+
self.unify_cascade_type()
|
|
14498
|
+
|
|
14499
|
+
return self
|
|
14164
14500
|
|
|
14165
14501
|
def has_python_operands(self):
|
|
14166
14502
|
return (self.operand1.type.is_pyobject
|
|
@@ -14186,7 +14522,7 @@ class PrimaryCmpNode(ExprNode, CmpNode):
|
|
|
14186
14522
|
operand1.result(),
|
|
14187
14523
|
operand2.result())
|
|
14188
14524
|
elif self.is_c_string_contains():
|
|
14189
|
-
if operand2.type
|
|
14525
|
+
if operand2.type.is_pystr_type:
|
|
14190
14526
|
method = "__Pyx_UnicodeContainsUCS4"
|
|
14191
14527
|
else:
|
|
14192
14528
|
method = "__Pyx_BytesContains"
|
|
@@ -14295,7 +14631,6 @@ class CascadedCmpNode(Node, CmpNode):
|
|
|
14295
14631
|
|
|
14296
14632
|
def optimise_comparison(self, operand1, env, result_is_bool=False):
|
|
14297
14633
|
if self.find_special_bool_compare_function(env, operand1, result_is_bool):
|
|
14298
|
-
self.is_pycmp = False
|
|
14299
14634
|
self.type = PyrexTypes.c_bint_type
|
|
14300
14635
|
if not operand1.type.is_pyobject:
|
|
14301
14636
|
operand1 = operand1.coerce_to_pyobject(env)
|
|
@@ -14307,7 +14642,7 @@ class CascadedCmpNode(Node, CmpNode):
|
|
|
14307
14642
|
|
|
14308
14643
|
def coerce_operands_to_pyobjects(self, env):
|
|
14309
14644
|
self.operand2 = self.operand2.coerce_to_pyobject(env)
|
|
14310
|
-
if self.operand2.type
|
|
14645
|
+
if self.operand2.type.is_pyanydict_type and self.operator in ('in', 'not_in'):
|
|
14311
14646
|
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
|
|
14312
14647
|
if self.cascade:
|
|
14313
14648
|
self.cascade.coerce_operands_to_pyobjects(env)
|
|
@@ -14674,9 +15009,16 @@ class CoerceToPyTypeNode(CoercionNode):
|
|
|
14674
15009
|
self.type = unicode_type
|
|
14675
15010
|
elif arg.type.is_complex:
|
|
14676
15011
|
self.type = Builtin.complex_type
|
|
15012
|
+
elif arg.type.equivalent_type is not None and arg.type.equivalent_type.is_pyobject:
|
|
15013
|
+
# Includes bint.
|
|
15014
|
+
self.type = arg.type.equivalent_type
|
|
15015
|
+
elif arg.type.is_int:
|
|
15016
|
+
self.type = Builtin.int_type
|
|
15017
|
+
elif arg.type.is_float:
|
|
15018
|
+
self.type = Builtin.float_type
|
|
14677
15019
|
self.target_type = self.type
|
|
14678
15020
|
elif arg.type.is_string or arg.type.is_cpp_string:
|
|
14679
|
-
if (type not
|
|
15021
|
+
if (not type.is_pybytes_type and not type.is_pybytearray_type
|
|
14680
15022
|
and not env.directives['c_string_encoding']):
|
|
14681
15023
|
error(arg.pos,
|
|
14682
15024
|
"default encoding required for conversion from '%s' to '%s'" %
|
|
@@ -14694,7 +15036,7 @@ class CoerceToPyTypeNode(CoercionNode):
|
|
|
14694
15036
|
|
|
14695
15037
|
def coerce_to_boolean(self, env):
|
|
14696
15038
|
arg_type = self.arg.type
|
|
14697
|
-
if arg_type is PyrexTypes.c_bint_type or arg_type
|
|
15039
|
+
if arg_type is PyrexTypes.c_bint_type or arg_type.is_pybool_type:
|
|
14698
15040
|
return self.arg.coerce_to_temp(env)
|
|
14699
15041
|
elif arg_type.is_string:
|
|
14700
15042
|
# Test for 0-length string with "ptr[0] != '\0'" instead of just "ptr != 0".
|
|
@@ -14803,7 +15145,7 @@ class CoerceFromPyTypeNode(CoercionNode):
|
|
|
14803
15145
|
def generate_result_code(self, code):
|
|
14804
15146
|
from_py_function = None
|
|
14805
15147
|
# for certain source types, we can do better than the generic coercion
|
|
14806
|
-
if self.type.is_string and self.arg.type
|
|
15148
|
+
if self.type.is_string and self.arg.type.is_pybytes_type:
|
|
14807
15149
|
if self.type.from_py_function.startswith('__Pyx_PyObject_As'):
|
|
14808
15150
|
from_py_function = '__Pyx_PyBytes' + self.type.from_py_function[len('__Pyx_PyObject'):]
|
|
14809
15151
|
NoneCheckNode.generate_if_needed(self.arg, code, "expected bytes, NoneType found")
|
|
@@ -14826,26 +15168,40 @@ class CoerceToBooleanNode(CoercionNode):
|
|
|
14826
15168
|
|
|
14827
15169
|
type = PyrexTypes.c_bint_type
|
|
14828
15170
|
|
|
14829
|
-
# Note that all of these need a check if CYTHON_ASSUME_SAFE_SIZE is false.
|
|
14830
|
-
# They should also all return something compatible with Py_ssize_t
|
|
14831
|
-
# (i.e. Py_ssize_t or a smaller int type).
|
|
14832
|
-
_special_builtins = {
|
|
14833
|
-
Builtin.list_type: '__Pyx_PyList_GET_SIZE',
|
|
14834
|
-
Builtin.tuple_type: '__Pyx_PyTuple_GET_SIZE',
|
|
14835
|
-
Builtin.set_type: '__Pyx_PySet_GET_SIZE',
|
|
14836
|
-
Builtin.frozenset_type: '__Pyx_PySet_GET_SIZE',
|
|
14837
|
-
Builtin.bytes_type: '__Pyx_PyBytes_GET_SIZE',
|
|
14838
|
-
Builtin.bytearray_type: '__Pyx_PyByteArray_GET_SIZE',
|
|
14839
|
-
Builtin.unicode_type: '__Pyx_PyUnicode_IS_TRUE',
|
|
14840
|
-
}
|
|
14841
|
-
|
|
14842
15171
|
def __init__(self, arg, env):
|
|
14843
15172
|
CoercionNode.__init__(self, arg)
|
|
14844
15173
|
if arg.type.is_pyobject:
|
|
14845
15174
|
self.is_temp = 1
|
|
14846
15175
|
|
|
15176
|
+
@staticmethod
|
|
15177
|
+
def _find_special_builtin_function(typ) -> Optional[str]:
|
|
15178
|
+
if not typ.is_builtin_type:
|
|
15179
|
+
return None
|
|
15180
|
+
# Note that all of these need a check if CYTHON_ASSUME_SAFE_SIZE is false.
|
|
15181
|
+
# They should also all return something compatible with Py_ssize_t
|
|
15182
|
+
# (i.e. Py_ssize_t or a smaller int type).
|
|
15183
|
+
if typ.is_pylist_type:
|
|
15184
|
+
return '__Pyx_PyList_GET_SIZE'
|
|
15185
|
+
if typ.is_pytuple_type:
|
|
15186
|
+
return '__Pyx_PyTuple_GET_SIZE'
|
|
15187
|
+
if typ.is_pyanyset_type:
|
|
15188
|
+
return '__Pyx_PySet_GET_SIZE'
|
|
15189
|
+
if typ.is_pyanydict_type:
|
|
15190
|
+
return '__Pyx_PyDict_GET_SIZE'
|
|
15191
|
+
if typ.is_pybytes_type:
|
|
15192
|
+
return '__Pyx_PyBytes_GET_SIZE'
|
|
15193
|
+
if typ.is_pybytearray_type:
|
|
15194
|
+
return '__Pyx_PyByteArray_GET_SIZE'
|
|
15195
|
+
if typ.is_pystr_type:
|
|
15196
|
+
return '__Pyx_PyUnicode_IS_TRUE'
|
|
15197
|
+
if typ.is_pyint_type:
|
|
15198
|
+
return '__Pyx_PyLong_IsNonZero'
|
|
15199
|
+
if typ.is_pyfloat_type:
|
|
15200
|
+
return '__Pyx_PyFloat_IsNonZero'
|
|
15201
|
+
return None
|
|
15202
|
+
|
|
14847
15203
|
def nogil_check(self, env):
|
|
14848
|
-
if self.arg.type.is_pyobject and self.
|
|
15204
|
+
if self.arg.type.is_pyobject and self._find_special_builtin_function(self.arg.type) is None:
|
|
14849
15205
|
self.gil_error()
|
|
14850
15206
|
|
|
14851
15207
|
gil_message = "Truth-testing Python object"
|
|
@@ -14862,17 +15218,26 @@ class CoerceToBooleanNode(CoercionNode):
|
|
|
14862
15218
|
def generate_result_code(self, code):
|
|
14863
15219
|
if not self.is_temp:
|
|
14864
15220
|
return
|
|
14865
|
-
test_func = self.
|
|
15221
|
+
test_func = self._find_special_builtin_function(self.arg.type)
|
|
14866
15222
|
if test_func is not None:
|
|
14867
15223
|
if self.arg.may_be_none():
|
|
14868
15224
|
code.putln(f"if ({self.arg.py_result()} == Py_None) {self.result()} = 0;")
|
|
14869
15225
|
code.putln("else")
|
|
14870
15226
|
code.putln("{")
|
|
15227
|
+
|
|
14871
15228
|
# Be aware that __Pyx_PyUnicode_IS_TRUE isn't strictly returning a size (but it does
|
|
14872
15229
|
# return an int which fits into a Py_ssize_t).
|
|
14873
15230
|
code.putln(f"Py_ssize_t {Naming.quick_temp_cname} = {test_func}({self.arg.py_result()});")
|
|
15231
|
+
|
|
15232
|
+
if self.arg.type.is_pyint_type:
|
|
15233
|
+
is_safe_test_func = "CYTHON_USE_PYLONG_INTERNALS"
|
|
15234
|
+
elif self.arg.type.is_pyfloat_type:
|
|
15235
|
+
is_safe_test_func = "CYTHON_ASSUME_SAFE_MACROS"
|
|
15236
|
+
else:
|
|
15237
|
+
is_safe_test_func = "CYTHON_ASSUME_SAFE_SIZE"
|
|
15238
|
+
|
|
14874
15239
|
code.putln(code.error_goto_if(
|
|
14875
|
-
f"((!
|
|
15240
|
+
f"((!{is_safe_test_func}) && {Naming.quick_temp_cname} < 0)", self.pos))
|
|
14876
15241
|
code.putln(f"{self.result()} = ({Naming.quick_temp_cname} != 0);")
|
|
14877
15242
|
code.putln("}")
|
|
14878
15243
|
code.putln()
|
|
@@ -15122,6 +15487,53 @@ class CloneNode(CoercionNode):
|
|
|
15122
15487
|
pass
|
|
15123
15488
|
|
|
15124
15489
|
|
|
15490
|
+
class CompilerDirectivesExprNode(Nodes.CompilerDirectivesMixin, ProxyNode):
|
|
15491
|
+
# Like compiler directives node, but for an expression
|
|
15492
|
+
# directives {string:value} A dictionary holding the right value for
|
|
15493
|
+
# *all* possible directives.
|
|
15494
|
+
# arg ExprNode
|
|
15495
|
+
|
|
15496
|
+
def __init__(self, arg, directives):
|
|
15497
|
+
super(CompilerDirectivesExprNode, self).__init__(arg)
|
|
15498
|
+
self.directives = directives
|
|
15499
|
+
|
|
15500
|
+
@property
|
|
15501
|
+
def is_temp(self):
|
|
15502
|
+
return self.arg.is_temp
|
|
15503
|
+
|
|
15504
|
+
def infer_type(self, env):
|
|
15505
|
+
with self.apply_directives(env):
|
|
15506
|
+
return super(CompilerDirectivesExprNode, self).infer_type(env)
|
|
15507
|
+
|
|
15508
|
+
def analyse_declarations(self, env):
|
|
15509
|
+
with self.apply_directives(env):
|
|
15510
|
+
self.arg.analyse_declarations(env)
|
|
15511
|
+
|
|
15512
|
+
def analyse_types(self, env):
|
|
15513
|
+
with self.apply_directives(env):
|
|
15514
|
+
return super(CompilerDirectivesExprNode, self).analyse_types(env)
|
|
15515
|
+
|
|
15516
|
+
def generate_result_code(self, code):
|
|
15517
|
+
with self.apply_directives(code.globalstate):
|
|
15518
|
+
super(CompilerDirectivesExprNode, self).generate_result_code(code)
|
|
15519
|
+
|
|
15520
|
+
def generate_evaluation_code(self, code):
|
|
15521
|
+
with self.apply_directives(code.globalstate):
|
|
15522
|
+
super(CompilerDirectivesExprNode, self).generate_evaluation_code(code)
|
|
15523
|
+
|
|
15524
|
+
def generate_disposal_code(self, code):
|
|
15525
|
+
with self.apply_directives(code.globalstate):
|
|
15526
|
+
super(CompilerDirectivesExprNode, self).generate_disposal_code(code)
|
|
15527
|
+
|
|
15528
|
+
def free_temps(self, code):
|
|
15529
|
+
with self.apply_directives(code.globalstate):
|
|
15530
|
+
super(CompilerDirectivesExprNode, self).free_temps(code)
|
|
15531
|
+
|
|
15532
|
+
def annotate(self, code):
|
|
15533
|
+
with self.apply_directives(code.globalstate):
|
|
15534
|
+
self.arg.annotate(code)
|
|
15535
|
+
|
|
15536
|
+
|
|
15125
15537
|
class CppOptionalTempCoercion(CoercionNode):
|
|
15126
15538
|
"""
|
|
15127
15539
|
Used only in CoerceCppTemps - handles cases the temp is actually a OptionalCppClassType (and thus needs dereferencing when on the rhs)
|