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
@@ -14,7 +14,7 @@ from .Code import UtilityCode, PyxCodeWriter
14
14
  from .Visitor import VisitorTransform
15
15
  from .StringEncoding import EncodedString
16
16
  from .TreeFragment import TreeFragment
17
- from .ParseTreeTransforms import NormalizeTree, SkipDeclarations
17
+ from .ParseTreeTransforms import NormalizeTree, SkipDeclarations, InterpretCompilerDirectives
18
18
  from .Options import copy_inherited_directives
19
19
 
20
20
  def make_dataclasses_module_callnode(pos):
@@ -153,6 +153,8 @@ class TemplateCode:
153
153
  pipeline=[NormalizeTree(None)],
154
154
  ).substitute(self.placeholders)
155
155
 
156
+ stat_list_node = InterpretCompilerDirectives(None, {})(stat_list_node)
157
+
156
158
  stat_list_node.stats += self.extra_stats
157
159
  return stat_list_node
158
160
 
@@ -161,7 +163,7 @@ class TemplateCode:
161
163
  return TemplateCode(
162
164
  writer=new_writer,
163
165
  placeholders=self.placeholders,
164
- extra_stats=self.extra_stats
166
+ extra_stats=self.extra_stats,
165
167
  )
166
168
 
167
169
 
@@ -343,13 +345,28 @@ def handle_cclass_dataclass(node, dataclass_args, analyse_decs_transform):
343
345
  stats = Nodes.StatListNode(node.pos,
344
346
  stats=[dataclass_params_assignment] + dataclass_fields_stats)
345
347
 
348
+ critical_section_substitution = ExprNodes.NameNode(
349
+ node.pos,
350
+ name="critical_section",
351
+ cython_attribute="critical_section"
352
+ )
353
+
346
354
  code = TemplateCode()
347
- generate_init_code(code, kwargs['init'], node, fields, kw_only)
355
+ critical_section_placeholder_name = code.new_placeholder(
356
+ fields,
357
+ critical_section_substitution
358
+ )
359
+ generate_init_code(code, kwargs['init'], node, fields, kw_only,
360
+ critical_section_placeholder_name=critical_section_placeholder_name)
348
361
  generate_match_args(code, kwargs['match_args'], node, fields, kw_only)
349
- generate_repr_code(code, kwargs['repr'], node, fields)
350
- generate_eq_code(code, kwargs['eq'], node, fields)
351
- generate_order_code(code, kwargs['order'], node, fields)
352
- generate_hash_code(code, kwargs['unsafe_hash'], kwargs['eq'], kwargs['frozen'], node, fields)
362
+ generate_repr_code(code, kwargs['repr'], node, fields,
363
+ critical_section_placeholder_name=critical_section_placeholder_name)
364
+ generate_eq_code(code, kwargs['eq'], node, fields,
365
+ critical_section_placeholder_name=critical_section_placeholder_name)
366
+ generate_order_code(code, kwargs['order'], node, fields,
367
+ critical_section_placeholder_name=critical_section_placeholder_name)
368
+ generate_hash_code(code, kwargs['unsafe_hash'], kwargs['eq'], kwargs['frozen'], node, fields,
369
+ critical_section_placeholder_name=critical_section_placeholder_name)
353
370
 
354
371
  stats.stats += code.generate_tree().stats
355
372
 
@@ -369,7 +386,7 @@ def handle_cclass_dataclass(node, dataclass_args, analyse_decs_transform):
369
386
  node.body.stats.append(comp_directives)
370
387
 
371
388
 
372
- def generate_init_code(code, init, node, fields, kw_only):
389
+ def generate_init_code(code, init, node, fields, kw_only, *, critical_section_placeholder_name):
373
390
  """
374
391
  Notes on CPython generated "__init__":
375
392
  * Implemented in `_init_fn`.
@@ -401,6 +418,7 @@ def generate_init_code(code, init, node, fields, kw_only):
401
418
  function_start_point = code.insertion_point()
402
419
  code = code.insertion_point()
403
420
  code.indent()
421
+ code.indent() # second indent is for "with critical_section" block
404
422
 
405
423
  # create a temp to get _HAS_DEFAULT_FACTORY
406
424
  dataclass_module = make_dataclasses_module_callnode(node.pos)
@@ -468,6 +486,10 @@ def generate_init_code(code, init, node, fields, kw_only):
468
486
 
469
487
  args = ", ".join(args)
470
488
  function_start_point.add_code_line(f"def __init__({args}):")
489
+ function_start_point.indent()
490
+ # Although __init__ is usually called on the only reference to self, it doesn't
491
+ # have to be.
492
+ function_start_point.add_code_line(f"with {critical_section_placeholder_name}({selfname}):")
471
493
 
472
494
 
473
495
  def generate_match_args(code, match_args, node, fields, global_kw_only):
@@ -489,7 +511,7 @@ def generate_match_args(code, match_args, node, fields, global_kw_only):
489
511
  code.add_code_line("__match_args__ = %s" % str(tuple(positional_arg_names)))
490
512
 
491
513
 
492
- def generate_repr_code(code, repr, node, fields):
514
+ def generate_repr_code(code, repr, node, fields, *, critical_section_placeholder_name):
493
515
  """
494
516
  The core of the CPython implementation is just:
495
517
  ['return self.__class__.__qualname__ + f"(' +
@@ -538,64 +560,64 @@ def generate_repr_code(code, repr, node, fields):
538
560
  try:
539
561
  """)
540
562
  code.indent()
541
-
542
- strs = ["%s={self.%s!r}" % (name, name)
543
- for name, field in fields.items()
544
- if field.repr.value and not field.is_initvar]
545
- format_string = ", ".join(strs)
546
-
547
- code.add_code_chunk(f'''
548
- name = getattr(type(self), "__qualname__", None) or type(self).__name__
549
- return f'{{name}}({format_string})'
550
- ''')
563
+ code.add_code_line('name = getattr(type(self), "__qualname__", None) or type(self).__name__')
564
+ with code.indenter(f"with {critical_section_placeholder_name}(self):"):
565
+ strs = ["%s={self.%s!r}" % (name, name)
566
+ for name, field in fields.items()
567
+ if field.repr.value and not field.is_initvar]
568
+ format_string = ", ".join(strs)
569
+
570
+ code.add_code_line(f"return f'{{name}}({format_string})'")
551
571
  if needs_recursive_guard:
552
572
  code.dedent()
553
573
  with code.indenter("finally:"):
554
574
  code.add_code_line("guard_set.remove(key)")
555
575
 
556
576
 
557
- def generate_cmp_code(code, op, funcname, node, fields):
577
+ def generate_cmp_code(code, op, funcname, node, fields, *, critical_section_placeholder_name):
558
578
  if node.scope.lookup_here(funcname):
559
579
  return
560
580
 
561
581
  names = [name for name, field in fields.items() if (field.compare.value and not field.is_initvar)]
562
582
 
563
583
  with code.indenter(f"def {funcname}(self, other):"):
584
+ code.add_code_line(f"cdef {node.class_name} other_cast")
564
585
  code.add_code_chunk(f"""
565
- if other.__class__ is not self.__class__: return NotImplemented
566
-
567
- cdef {node.class_name} other_cast
568
- other_cast = <{node.class_name}>other
569
- """)
586
+ if other.__class__ is not self.__class__: return NotImplemented
570
587
 
571
- # The Python implementation of dataclasses.py does a tuple comparison
572
- # (roughly):
573
- # return self._attributes_to_tuple() {op} other._attributes_to_tuple()
574
- #
575
- # For the Cython implementation a tuple comparison isn't an option because
576
- # not all attributes can be converted to Python objects and stored in a tuple
577
- #
578
- # TODO - better diagnostics of whether the types support comparison before
579
- # generating the code. Plus, do we want to convert C structs to dicts and
580
- # compare them that way (I think not, but it might be in demand)?
581
- checks = []
582
- op_without_equals = op.replace('=', '')
583
-
584
- for name in names:
585
- if op != '==':
586
- # tuple comparison rules - early elements take precedence
587
- code.add_code_line(f"if self.{name} {op_without_equals} other_cast.{name}: return True")
588
- code.add_code_line(f"if self.{name} != other_cast.{name}: return False")
589
- code.add_code_line(f"return {'True' if '=' in op else 'False'}") # "() == ()" is True
590
-
591
-
592
- def generate_eq_code(code, eq, node, fields):
588
+ other_cast = <{node.class_name}>other
589
+ """)
590
+ with code.indenter(f"with {critical_section_placeholder_name}(self, other):"):
591
+ # The Python implementation of dataclasses.py does a tuple comparison
592
+ # (roughly):
593
+ # return self._attributes_to_tuple() {op} other._attributes_to_tuple()
594
+ #
595
+ # For the Cython implementation a tuple comparison isn't an option because
596
+ # not all attributes can be converted to Python objects and stored in a tuple
597
+ #
598
+ # TODO - better diagnostics of whether the types support comparison before
599
+ # generating the code. Plus, do we want to convert C structs to dicts and
600
+ # compare them that way (I think not, but it might be in demand)?
601
+ op_without_equals = op.replace('=', '')
602
+
603
+ for name in names:
604
+ if op != '==':
605
+ # tuple comparison rules - early elements take precedence
606
+ code.add_code_line(f"if self.{name} {op_without_equals} other_cast.{name}: return True")
607
+ code.add_code_line(f"if self.{name} != other_cast.{name}: return False")
608
+ code.add_code_line(f"return {'True' if '=' in op else 'False'}") # "() == ()" is True
609
+
610
+
611
+ def generate_eq_code(code, eq, node, fields, *, critical_section_placeholder_name):
593
612
  if not eq:
594
613
  return
595
- generate_cmp_code(code, "==", "__eq__", node, fields)
614
+ generate_cmp_code(
615
+ code, "==", "__eq__", node, fields,
616
+ critical_section_placeholder_name=critical_section_placeholder_name
617
+ )
596
618
 
597
619
 
598
- def generate_order_code(code, order, node, fields):
620
+ def generate_order_code(code, order, node, fields, *, critical_section_placeholder_name):
599
621
  if not order:
600
622
  return
601
623
 
@@ -603,10 +625,13 @@ def generate_order_code(code, order, node, fields):
603
625
  ("<=", "__le__"),
604
626
  (">", "__gt__"),
605
627
  (">=", "__ge__")]:
606
- generate_cmp_code(code, op, name, node, fields)
628
+ generate_cmp_code(
629
+ code, op, name, node, fields,
630
+ critical_section_placeholder_name=critical_section_placeholder_name
631
+ )
607
632
 
608
633
 
609
- def generate_hash_code(code, unsafe_hash, eq, frozen, node, fields):
634
+ def generate_hash_code(code, unsafe_hash, eq, frozen, node, fields, *, critical_section_placeholder_name):
610
635
  """
611
636
  Copied from CPython implementation - the intention is to follow this as far as
612
637
  is possible:
@@ -679,7 +704,8 @@ def generate_hash_code(code, unsafe_hash, eq, frozen, node, fields):
679
704
 
680
705
  # if we're here we want to generate a hash
681
706
  with code.indenter("def __hash__(self):"):
682
- code.add_code_line(f"return hash(({hash_tuple_items}))")
707
+ with code.indenter(f"with {critical_section_placeholder_name}(self):"):
708
+ code.add_code_line(f"return hash(({hash_tuple_items}))")
683
709
 
684
710
 
685
711
  def get_field_type(pos, entry):
Cython/Compiler/Errors.py CHANGED
@@ -12,7 +12,22 @@ try:
12
12
  except ImportError:
13
13
  class _threadlocal: pass
14
14
 
15
- threadlocal = _threadlocal()
15
+ class _ErrorsThreadLocals(_threadlocal):
16
+ # defaults
17
+ cython_errors_count = 0
18
+ cython_errors_listing_file = None
19
+ cython_errors_echo_file = None
20
+
21
+ def __init__(self):
22
+ # mutable defaults
23
+ self.cython_errors_warn_once_seen = set()
24
+ self.cython_errors_stack = []
25
+
26
+ def reset(self):
27
+ self.__dict__.clear()
28
+ self.__init__()
29
+
30
+ threadlocal = _ErrorsThreadLocals()
16
31
 
17
32
  from ..Utils import open_new_file
18
33
  from . import DebugFlags
@@ -283,15 +298,13 @@ def local_errors(ignore=False):
283
298
  # Keep all global state in thread local storage to support parallel cythonisation in distutils.
284
299
 
285
300
  def init_thread():
286
- threadlocal.cython_errors_count = 0
287
- threadlocal.cython_errors_listing_file = None
288
- threadlocal.cython_errors_echo_file = None
289
- threadlocal.cython_errors_warn_once_seen = set()
290
- threadlocal.cython_errors_stack = []
301
+ # init thread is no longer necessary
302
+ # (the thread locals are now arranged to have usable defaults)
303
+ # but is left for the benefit of any external users.
304
+ reset()
291
305
 
292
306
  def reset():
293
- threadlocal.cython_errors_warn_once_seen.clear()
294
- del threadlocal.cython_errors_stack[:]
307
+ threadlocal.reset()
295
308
 
296
309
  def get_errors_count():
297
310
  return threadlocal.cython_errors_count