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

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

Potentially problematic release.


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

Files changed (112) hide show
  1. Cython/Build/BuildExecutable.py +1 -14
  2. Cython/Build/Dependencies.py +6 -44
  3. Cython/Build/Inline.py +7 -3
  4. Cython/Build/IpythonMagic.py +0 -1
  5. Cython/Build/SharedModule.py +1 -2
  6. Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
  7. Cython/Build/Tests/TestDependencies.py +3 -3
  8. Cython/Build/Tests/TestInline.py +3 -3
  9. Cython/Build/Tests/TestIpythonMagic.py +35 -19
  10. Cython/Build/Tests/TestStripLiterals.py +2 -2
  11. Cython/Compiler/Buffer.py +5 -5
  12. Cython/Compiler/Builtin.py +57 -46
  13. Cython/Compiler/Code.pxd +12 -9
  14. Cython/Compiler/Code.py +220 -94
  15. Cython/Compiler/Dataclass.py +78 -52
  16. Cython/Compiler/Errors.py +21 -8
  17. Cython/Compiler/ExprNodes.py +871 -459
  18. Cython/Compiler/FlowControl.pxd +8 -6
  19. Cython/Compiler/FlowControl.py +111 -8
  20. Cython/Compiler/FusedNode.py +17 -12
  21. Cython/Compiler/Lexicon.py +123 -122
  22. Cython/Compiler/Main.py +3 -4
  23. Cython/Compiler/MatchCaseNodes.py +1948 -10
  24. Cython/Compiler/MemoryView.py +62 -33
  25. Cython/Compiler/ModuleNode.py +559 -283
  26. Cython/Compiler/Naming.py +4 -0
  27. Cython/Compiler/Nodes.py +333 -197
  28. Cython/Compiler/Optimize.py +268 -112
  29. Cython/Compiler/Options.py +0 -5
  30. Cython/Compiler/ParseTreeTransforms.pxd +2 -1
  31. Cython/Compiler/ParseTreeTransforms.py +180 -76
  32. Cython/Compiler/Parsing.py +55 -38
  33. Cython/Compiler/PyrexTypes.py +364 -128
  34. Cython/Compiler/Scanning.pxd +27 -5
  35. Cython/Compiler/Scanning.py +24 -14
  36. Cython/Compiler/StringEncoding.py +1 -2
  37. Cython/Compiler/Symtab.py +27 -19
  38. Cython/Compiler/Tests/TestBuiltin.py +41 -6
  39. Cython/Compiler/Tests/TestCmdLine.py +3 -2
  40. Cython/Compiler/Tests/TestCode.py +4 -3
  41. Cython/Compiler/Tests/TestFlowControl.py +2 -2
  42. Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
  43. Cython/Compiler/Tests/TestScanning.py +2 -4
  44. Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
  45. Cython/Compiler/Tests/TestStringEncoding.py +2 -3
  46. Cython/Compiler/Tests/TestTypes.py +46 -3
  47. Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
  48. Cython/Compiler/TypeInference.py +20 -0
  49. Cython/Compiler/TypeSlots.py +171 -16
  50. Cython/Compiler/UtilNodes.py +24 -0
  51. Cython/Compiler/UtilityCode.py +1 -0
  52. Cython/Compiler/Visitor.pxd +1 -1
  53. Cython/Compiler/Visitor.py +3 -2
  54. Cython/Debugger/Tests/TestLibCython.py +7 -2
  55. Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
  56. Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
  57. Cython/Includes/cpython/array.pxd +5 -5
  58. Cython/Includes/cpython/dict.pxd +1 -6
  59. Cython/Includes/cpython/float.pxd +19 -10
  60. Cython/Includes/cpython/frozendict.pxd +37 -0
  61. Cython/Includes/cpython/long.pxd +31 -0
  62. Cython/Includes/cpython/object.pxd +9 -12
  63. Cython/Includes/cpython/sentinel.pxd +17 -0
  64. Cython/Includes/cpython/type.pxd +93 -0
  65. Cython/Includes/libcpp/exception.pxd +90 -0
  66. Cython/LZSS.py +170 -0
  67. Cython/Shadow.py +550 -73
  68. Cython/StringIOTree.py +1 -2
  69. Cython/Tempita/_tempita.py +45 -46
  70. Cython/TestUtils.py +50 -8
  71. Cython/Tests/TestCythonUtils.py +2 -2
  72. Cython/Tests/TestJediTyper.py +1 -1
  73. Cython/Tests/TestShadow.py +7 -5
  74. Cython/Tests/TestStringIOTree.py +4 -3
  75. Cython/Tests/TestTestUtils.py +2 -3
  76. Cython/Utility/AsyncGen.c +59 -19
  77. Cython/Utility/Buffer.c +4 -3
  78. Cython/Utility/Builtins.c +139 -17
  79. Cython/Utility/CConvert.pyx +27 -12
  80. Cython/Utility/CommonStructures.c +55 -37
  81. Cython/Utility/Coroutine.c +112 -68
  82. Cython/Utility/CythonFunction.c +607 -330
  83. Cython/Utility/Embed.c +8 -0
  84. Cython/Utility/Exceptions.c +20 -3
  85. Cython/Utility/ExtensionTypes.c +414 -224
  86. Cython/Utility/FunctionArguments.c +83 -81
  87. Cython/Utility/ImportExport.c +72 -61
  88. Cython/Utility/MatchCase.c +979 -0
  89. Cython/Utility/MatchCase_Cy.pyx +12 -0
  90. Cython/Utility/MemoryView.pxd +9 -88
  91. Cython/Utility/MemoryView.pyx +173 -61
  92. Cython/Utility/MemoryView_C.c +3 -3
  93. Cython/Utility/ModuleSetupCode.c +306 -157
  94. Cython/Utility/ObjectHandling.c +483 -404
  95. Cython/Utility/Optimize.c +1117 -164
  96. Cython/Utility/Overflow.c +3 -9
  97. Cython/Utility/Profile.c +4 -0
  98. Cython/Utility/StringTools.c +187 -149
  99. Cython/Utility/Synchronization.c +39 -0
  100. Cython/Utility/TString.c +24 -13
  101. Cython/Utility/TypeConversion.c +405 -250
  102. Cython/Utility/arrayarray.h +24 -22
  103. Cython/Utils.py +11 -23
  104. Cython/__init__.py +3 -1
  105. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
  106. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
  107. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
  108. Cython/Shadow.pyi +0 -521
  109. Cython/Utility/Printing.c +0 -86
  110. Cython/__init__.pyi +0 -7
  111. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
  112. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
@@ -3,8 +3,8 @@ cimport cython
3
3
  from .Visitor cimport CythonTransform, TreeVisitor
4
4
 
5
5
  cdef class ControlBlock:
6
- cdef public set children
7
- cdef public set parents
6
+ cdef public set[ControlBlock] children
7
+ cdef public set[ControlBlock] parents
8
8
  cdef public set positions
9
9
  cdef public list stats
10
10
  cdef public dict gen
@@ -35,6 +35,7 @@ cdef class NameAssignment:
35
35
  cdef public object bit
36
36
  cdef public object inferred_type
37
37
  cdef public object rhs_scope
38
+ cdef public object assignment_type
38
39
 
39
40
  cdef class AssignmentList:
40
41
  cdef public object bit
@@ -42,11 +43,11 @@ cdef class AssignmentList:
42
43
  cdef public list stats
43
44
 
44
45
  cdef class AssignmentCollector(TreeVisitor):
45
- cdef list assignments
46
+ cdef list[tuple] assignments
46
47
 
47
48
  @cython.final
48
49
  cdef class ControlFlow:
49
- cdef public set blocks
50
+ cdef public set[ControlBlock] blocks
50
51
  cdef public set entries
51
52
  cdef public list loops
52
53
  cdef public list exceptions
@@ -64,7 +65,7 @@ cdef class ControlFlow:
64
65
  cpdef bint is_tracked(self, entry)
65
66
  cpdef bint is_statically_assigned(self, entry)
66
67
  cpdef mark_position(self, node)
67
- cpdef mark_assignment(self, lhs, rhs, entry, rhs_scope=*)
68
+ cpdef mark_assignment(self, lhs, rhs, entry, rhs_scope=*, assignment_type=*)
68
69
  cpdef mark_argument(self, lhs, rhs, entry)
69
70
  cpdef mark_deletion(self, node, entry)
70
71
  cpdef mark_reference(self, node, entry)
@@ -92,6 +93,7 @@ cdef class ControlFlowAnalysis(CythonTransform):
92
93
  cdef ControlFlow flow
93
94
  cdef object object_expr
94
95
  cdef bint in_inplace_assignment
96
+ cdef bint in_assignment_expression
95
97
 
96
- cpdef mark_assignment(self, lhs, rhs=*, rhs_scope=*)
98
+ cpdef mark_assignment(self, lhs, rhs=*, rhs_scope=*, assignment_type=*)
97
99
  cpdef mark_position(self, node)
@@ -16,6 +16,8 @@ from . import PyrexTypes
16
16
  from .Visitor import TreeVisitor, CythonTransform
17
17
  from .Errors import error, warning, InternalError
18
18
 
19
+ import enum
20
+
19
21
 
20
22
  class TypedExprNode(ExprNodes.ExprNode):
21
23
  # Used for declaring assignments of a specified type without a known entry.
@@ -183,9 +185,13 @@ class ControlFlow:
183
185
  if self.block:
184
186
  self.block.positions.add(node.pos[:2])
185
187
 
186
- def mark_assignment(self, lhs, rhs, entry, rhs_scope=None):
188
+ def mark_assignment(self, lhs, rhs, entry, rhs_scope=None, assignment_type=None):
187
189
  if self.block and self.is_tracked(entry):
188
- assignment = NameAssignment(lhs, rhs, entry, rhs_scope=rhs_scope)
190
+ if assignment_type is None:
191
+ assignment_type = AssignmentType.Other
192
+ assignment = NameAssignment(
193
+ lhs, rhs, entry, rhs_scope=rhs_scope,
194
+ assignment_type=assignment_type)
189
195
  self.block.stats.append(assignment)
190
196
  self.block.gen[entry] = assignment
191
197
  self.entries.add(entry)
@@ -337,8 +343,14 @@ class ExceptionDescr:
337
343
  self.finally_exit = finally_exit
338
344
 
339
345
 
346
+ class AssignmentType(enum.IntEnum):
347
+ Other = 0
348
+ Parallel = 1
349
+ AssignmentExpression = 2
350
+
351
+
340
352
  class NameAssignment:
341
- def __init__(self, lhs, rhs, entry, rhs_scope=None):
353
+ def __init__(self, lhs, rhs, entry, rhs_scope=None, assignment_type=AssignmentType.Other):
342
354
  if lhs.cf_state is None:
343
355
  lhs.cf_state = set()
344
356
  self.lhs = lhs
@@ -352,6 +364,7 @@ class NameAssignment:
352
364
  # For generator expression targets in comprehensions (and possibly other things),
353
365
  # the rhs can have a different scope than the lhs.
354
366
  self.rhs_scope = rhs_scope
367
+ self.assignment_type = assignment_type
355
368
 
356
369
  def __repr__(self):
357
370
  return '%s(entry=%r)' % (self.__class__.__name__, self.entry)
@@ -726,6 +739,7 @@ class ControlFlowAnalysis(CythonTransform):
726
739
  self.reductions = set()
727
740
 
728
741
  self.in_inplace_assignment = False
742
+ self.in_assignment_expression = False
729
743
  self.env = node.scope
730
744
  self.flow = ControlFlow()
731
745
  self.stack = [] # a stack of (env, flow) tuples
@@ -800,7 +814,7 @@ class ControlFlowAnalysis(CythonTransform):
800
814
  def visit_CTypeDefNode(self, node):
801
815
  return node
802
816
 
803
- def mark_assignment(self, lhs, rhs=None, rhs_scope=None):
817
+ def mark_assignment(self, lhs, rhs=None, rhs_scope=None, assignment_type=None):
804
818
  if not self.flow.block:
805
819
  return
806
820
  if self.flow.exceptions:
@@ -817,7 +831,7 @@ class ControlFlowAnalysis(CythonTransform):
817
831
  entry = self.env.lookup(lhs.name)
818
832
  if entry is None: # TODO: This shouldn't happen...
819
833
  return
820
- self.flow.mark_assignment(lhs, rhs, entry, rhs_scope=(rhs_scope or self.env))
834
+ self.flow.mark_assignment(lhs, rhs, entry, rhs_scope=(rhs_scope or self.env), assignment_type=assignment_type)
821
835
  elif lhs.is_sequence_constructor:
822
836
  for i, arg in enumerate(lhs.args):
823
837
  if arg.is_starred:
@@ -827,7 +841,7 @@ class ControlFlowAnalysis(CythonTransform):
827
841
  item_node = rhs
828
842
  else:
829
843
  item_node = rhs.inferable_item_node(i)
830
- self.mark_assignment(arg, item_node, rhs_scope=rhs_scope)
844
+ self.mark_assignment(arg, item_node, rhs_scope=rhs_scope, assignment_type=assignment_type)
831
845
  else:
832
846
  self._visit(lhs)
833
847
 
@@ -848,12 +862,21 @@ class ControlFlowAnalysis(CythonTransform):
848
862
  self.visitchildren(node)
849
863
  return node
850
864
 
865
+ def visit_AssignmentExpressionNode(self, node):
866
+ in_assignment_expression, self.in_assignment_expression = self.in_assignment_expression, True
867
+ self.visitchildren(node)
868
+ self.in_assignment_expression = in_assignment_expression
869
+ return node
870
+
851
871
  def visit_AssignmentNode(self, node):
852
872
  raise InternalError("Unhandled assignment node %s" % type(node))
853
873
 
854
874
  def visit_SingleAssignmentNode(self, node):
855
875
  self._visit(node.rhs)
856
- self.mark_assignment(node.lhs, node.rhs)
876
+ self.mark_assignment(
877
+ node.lhs, node.rhs,
878
+ assignment_type=AssignmentType.AssignmentExpression
879
+ if self.in_assignment_expression else None)
857
880
  return node
858
881
 
859
882
  def visit_CascadedAssignmentNode(self, node):
@@ -868,7 +891,7 @@ class ControlFlowAnalysis(CythonTransform):
868
891
  for lhs, rhs in collector.assignments:
869
892
  self._visit(rhs)
870
893
  for lhs, rhs in collector.assignments:
871
- self.mark_assignment(lhs, rhs)
894
+ self.mark_assignment(lhs, rhs, assignment_type=AssignmentType.Parallel)
872
895
  return node
873
896
 
874
897
  def visit_InPlaceAssignmentNode(self, node):
@@ -960,6 +983,86 @@ class ControlFlowAnalysis(CythonTransform):
960
983
  self.flow.block = None
961
984
  return node
962
985
 
986
+ def visit_MatchNode(self, node):
987
+ # https://peps.python.org/pep-0634/#side-effects-and-undefined-behavior
988
+ # The specification of structural pattern matching gives
989
+ # a reasonable amount of freedom about when a binding takes
990
+ # effect (especially for matches that fail after a few completed
991
+ # steps).
992
+ # To make it easier to reason about which variables are bound,
993
+ # Cython delays the binding until the entire statement has been
994
+ # evaluated, but before the guard is evaluated.
995
+ # Therefore an unguarded match statement is equivalent to
996
+ #
997
+ # if case1:
998
+ # bound_variable = something
999
+ # block
1000
+ # elif case2...
1001
+ #
1002
+ # while a guarded match statement is equivalent to
1003
+ #
1004
+ # if case1:
1005
+ # bound_variable = something
1006
+ # if guard:
1007
+ # block
1008
+ # goto end
1009
+ # if case2: ...
1010
+ #
1011
+ # Note also that some of the cases may have just been transformed into
1012
+ # if blocks
1013
+ from .MatchCaseNodes import MatchCaseNode, SubstitutedMatchCaseNode
1014
+
1015
+ self._visit(node.subject)
1016
+
1017
+ next_block = self.flow.newblock()
1018
+ orig_parent = self.flow.block
1019
+ guard_block = None
1020
+ for case in node.cases:
1021
+ if isinstance(case, MatchCaseNode):
1022
+ case_block = self.flow.nextblock(orig_parent)
1023
+ if guard_block:
1024
+ guard_block.add_child(case_block)
1025
+ self._visit(case.pattern)
1026
+ self.flow.nextblock()
1027
+ if case.target_assignments:
1028
+ self._visit(case.target_assignments)
1029
+ if case.guard:
1030
+ self.flow.nextblock()
1031
+ self._visit(case.guard)
1032
+ guard_block = self.flow.block
1033
+ else:
1034
+ guard_block = None
1035
+ self.flow.nextblock()
1036
+ self._visit(case.body)
1037
+ if self.flow.block:
1038
+ self.flow.block.add_child(next_block)
1039
+ elif isinstance(case, SubstitutedMatchCaseNode):
1040
+ self.flow.nextblock()
1041
+ if guard_block:
1042
+ guard_block.add_child(self.flow.block)
1043
+ guard_block = None
1044
+ self._visit(case.body)
1045
+ orig_parent = self.flow.block
1046
+ else:
1047
+ assert False, case
1048
+
1049
+ if guard_block:
1050
+ guard_block.add_child(next_block)
1051
+
1052
+ if orig_parent is not None:
1053
+ orig_parent.add_child(next_block)
1054
+ if next_block.parents:
1055
+ self.flow.block = next_block
1056
+ else:
1057
+ self.flow.block = None
1058
+ return node
1059
+
1060
+ def visit_PatternNode(self, node):
1061
+ # avoid visiting anything that might be a target (since they're
1062
+ # handled elsewhere)
1063
+ self.visitchildren(node, attrs=None, exclude=["as_targets", "target", "double_star_capture_target"])
1064
+ return node
1065
+
963
1066
  def visit_AssertStatNode(self, node):
964
1067
  """Essentially an if-condition that wraps a RaiseStatNode.
965
1068
  """
@@ -591,11 +591,11 @@ class FusedCFuncDefNode(StatListNode):
591
591
 
592
592
  return normal_types, buffer_types, pythran_types, has_object_fallback
593
593
 
594
- def _unpack_argument(self, pyx_code, arg, arg_tuple_idx, min_positional_args, default_idx):
594
+ def _unpack_argument(self, pyx_code, arg, arg_tuple_idx, min_positional_args, default_idx, env):
595
595
  pyx_code.put_chunk(
596
596
  f"""
597
597
  # PROCESSING ARGUMENT {arg_tuple_idx}
598
- if {arg_tuple_idx} < len(<tuple>args):
598
+ if {arg_tuple_idx} < arg_count:
599
599
  arg = (<tuple>args)[{arg_tuple_idx}]
600
600
  elif kwargs is not None and '{arg.name}' in <dict>kwargs:
601
601
  arg = (<dict>kwargs)['{arg.name}']
@@ -606,13 +606,12 @@ class FusedCFuncDefNode(StatListNode):
606
606
  if arg.default:
607
607
  pyx_code.putln(
608
608
  f"arg = (<tuple>defaults)[{default_idx}]")
609
- elif arg_tuple_idx < min_positional_args:
610
- pyx_code.putln(
611
- 'raise TypeError("Expected at least %d argument%s, got %d" % ('
612
- f'''{min_positional_args}, {'"s"' if min_positional_args != 1 else '""'}, len(<tuple>args)))'''
613
- )
614
609
  else:
615
- pyx_code.putln(f"""raise TypeError("Missing keyword-only argument: '%s'" % "{arg.name}")""")
610
+ from .Code import UtilityCode
611
+ env.use_utility_code(
612
+ UtilityCode.load_cached("FusedFunctionArgTypeError", "CythonFunction.c"))
613
+ pyx_code.putln(
614
+ f'__Pyx_RaiseFusedFunctionArgTypeError("{arg.name}", {arg_tuple_idx}, {min_positional_args}, arg_count)')
616
615
  pyx_code.dedent()
617
616
 
618
617
  def make_fused_cpdef(self, orig_py_func, env, is_def):
@@ -641,6 +640,10 @@ class FusedCFuncDefNode(StatListNode):
641
640
  # from FusedFunction utility code
642
641
  object __pyx_ff_match_signatures_single(dict signatures, dest_type)
643
642
  object __pyx_ff_match_signatures(dict signatures, tuple dest_sig, dict sigindex)
643
+
644
+ # always raises:
645
+ int __Pyx_RaiseFusedFunctionArgTypeError(
646
+ object arg_name, Py_ssize_t arg_tuple_idx, Py_ssize_t min_positional_args, Py_ssize_t arg_count) except -1
644
647
  """)
645
648
  decl_code.indent()
646
649
 
@@ -650,9 +653,11 @@ class FusedCFuncDefNode(StatListNode):
650
653
  # FIXME: use a typed signature - currently fails badly because
651
654
  # default arguments inherit the types we specify here!
652
655
 
653
- if kwargs is not None and not kwargs:
656
+ if kwargs is not None and not <dict> kwargs:
654
657
  kwargs = None
655
658
 
659
+ arg_count = len(<tuple> args)
660
+
656
661
  # instance check body
657
662
  """)
658
663
 
@@ -675,7 +680,7 @@ class FusedCFuncDefNode(StatListNode):
675
680
  seen_fused_types.add(fused_type)
676
681
 
677
682
  normal_types, buffer_types, pythran_types, has_object_fallback = self._split_fused_types(arg)
678
- self._unpack_argument(pyx_code, arg, i, min_positional_args, default_idx)
683
+ self._unpack_argument(pyx_code, arg, i, min_positional_args, default_idx, env)
679
684
 
680
685
  mapper_arg_types = ['object', 'type']
681
686
  mapper_arg_names = ['arg']
@@ -898,7 +903,7 @@ class FusedCFuncDefNode(StatListNode):
898
903
  """
899
904
  # For the moment, fused functions do not support METH_FASTCALL
900
905
  for node in nodes:
901
- node.entry.signature.use_fastcall = False
906
+ node.entry.signature.use_fastcall = node.entry.signature.FastcallUsed.NO
902
907
 
903
908
  signatures = [StringEncoding.EncodedString(node.specialized_signature_string)
904
909
  for node in nodes]
@@ -947,7 +952,7 @@ class FusedCFuncDefNode(StatListNode):
947
952
  fused_func.generate_evaluation_code(code)
948
953
 
949
954
  code.putln(
950
- f"((__pyx_FusedFunctionObject *) {fused_func.result()})->__signatures__ = {signatures.result()};")
955
+ f"__Pyx_as_FusedFunctionObject({fused_func.result()})->__signatures__ = {signatures.result()};")
951
956
 
952
957
  signatures.generate_giveref(code)
953
958
  signatures.generate_post_assignment_code(code)