pystachy 0.2.0__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.
Files changed (158) hide show
  1. _pystachy_source/VERSION +1 -0
  2. _pystachy_source/lib/_argparse_help.py +188 -0
  3. _pystachy_source/lib/_markupbase.py +399 -0
  4. _pystachy_source/lib/_pyrandom.py +272 -0
  5. _pystachy_source/lib/_pystachy__io.py +43 -0
  6. _pystachy_source/lib/_pystachy_abc.py +105 -0
  7. _pystachy_source/lib/_pystachy_atexit.py +76 -0
  8. _pystachy_source/lib/_pystachy_bfile.py +245 -0
  9. _pystachy_source/lib/_pystachy_binascii.py +412 -0
  10. _pystachy_source/lib/_pystachy_bytescan.py +28 -0
  11. _pystachy_source/lib/_pystachy_codecs.py +524 -0
  12. _pystachy_source/lib/_pystachy_copy.py +43 -0
  13. _pystachy_source/lib/_pystachy_iterators.py +207 -0
  14. _pystachy_source/lib/_pystachy_itertools.py +996 -0
  15. _pystachy_source/lib/_pystachy_json_any.py +271 -0
  16. _pystachy_source/lib/_pystachy_json_int.py +6 -0
  17. _pystachy_source/lib/_pystachy_mappingproxy.py +94 -0
  18. _pystachy_source/lib/_pystachy_object.py +13 -0
  19. _pystachy_source/lib/_pystachy_percent.py +343 -0
  20. _pystachy_source/lib/_pystachy_range.py +139 -0
  21. _pystachy_source/lib/_pystachy_re_specialized.py +33 -0
  22. _pystachy_source/lib/_pystachy_string.py +175 -0
  23. _pystachy_source/lib/_pystachy_struct.py +447 -0
  24. _pystachy_source/lib/_pystachy_thread.py +433 -0
  25. _pystachy_source/lib/_pystachy_time.py +15 -0
  26. _pystachy_source/lib/_pystachy_types.py +13 -0
  27. _pystachy_source/lib/_pystachy_unicodedata.py +873 -0
  28. _pystachy_source/lib/_pystachy_unicodedata_db.py +1371 -0
  29. _pystachy_source/lib/_pystachy_unicodedata_names.py +7634 -0
  30. _pystachy_source/lib/_pystachy_weakref.py +111 -0
  31. _pystachy_source/lib/_weakrefset.py +205 -0
  32. _pystachy_source/lib/argparse.py +315 -0
  33. _pystachy_source/lib/bisect.py +118 -0
  34. _pystachy_source/lib/codecs.py +1135 -0
  35. _pystachy_source/lib/collections/__init__.py +1282 -0
  36. _pystachy_source/lib/colorsys.py +166 -0
  37. _pystachy_source/lib/contextlib.py +300 -0
  38. _pystachy_source/lib/copy.py +213 -0
  39. _pystachy_source/lib/csv.py +1039 -0
  40. _pystachy_source/lib/curses/__init__.py +4 -0
  41. _pystachy_source/lib/curses/ascii.py +99 -0
  42. _pystachy_source/lib/datetime.py +3044 -0
  43. _pystachy_source/lib/difflib.py +2083 -0
  44. _pystachy_source/lib/enum.py +854 -0
  45. _pystachy_source/lib/fnmatch.py +249 -0
  46. _pystachy_source/lib/functools.py +407 -0
  47. _pystachy_source/lib/genericpath.py +200 -0
  48. _pystachy_source/lib/getopt.py +214 -0
  49. _pystachy_source/lib/gettext.py +657 -0
  50. _pystachy_source/lib/glob.py +224 -0
  51. _pystachy_source/lib/heapq.py +636 -0
  52. _pystachy_source/lib/html/__init__.py +149 -0
  53. _pystachy_source/lib/html/entities.py +2514 -0
  54. _pystachy_source/lib/html/parser.py +602 -0
  55. _pystachy_source/lib/io.py +1183 -0
  56. _pystachy_source/lib/json.py +843 -0
  57. _pystachy_source/lib/keyword.py +74 -0
  58. _pystachy_source/lib/logging/__init__.py +2506 -0
  59. _pystachy_source/lib/mpz.py +127 -0
  60. _pystachy_source/lib/operator.py +455 -0
  61. _pystachy_source/lib/pathlib.py +739 -0
  62. _pystachy_source/lib/posixpath.py +578 -0
  63. _pystachy_source/lib/pyobj.py +475 -0
  64. _pystachy_source/lib/random.py +1174 -0
  65. _pystachy_source/lib/re/__init__.py +2516 -0
  66. _pystachy_source/lib/re/_constants.py +111 -0
  67. _pystachy_source/lib/re/_sre.py +2108 -0
  68. _pystachy_source/lib/re/_unicode.py +304 -0
  69. _pystachy_source/lib/shlex.py +381 -0
  70. _pystachy_source/lib/shutil.py +205 -0
  71. _pystachy_source/lib/stat.py +212 -0
  72. _pystachy_source/lib/statistics.py +1954 -0
  73. _pystachy_source/lib/string.py +352 -0
  74. _pystachy_source/lib/struct.py +15 -0
  75. _pystachy_source/lib/textwrap.py +520 -0
  76. _pystachy_source/lib/this.py +28 -0
  77. _pystachy_source/lib/threading.py +1674 -0
  78. _pystachy_source/lib/token.py +166 -0
  79. _pystachy_source/lib/tomllib/__init__.py +10 -0
  80. _pystachy_source/lib/tomllib/_parser.py +732 -0
  81. _pystachy_source/lib/tomllib/_re.py +114 -0
  82. _pystachy_source/lib/tomllib/_types.py +13 -0
  83. _pystachy_source/lib/traceback.py +184 -0
  84. _pystachy_source/lib/types.py +49 -0
  85. _pystachy_source/lib/urllib/__init__.py +0 -0
  86. _pystachy_source/lib/urllib/parse.py +1713 -0
  87. _pystachy_source/lib/warnings.py +764 -0
  88. _pystachy_source/lib/weakref.py +539 -0
  89. _pystachy_source/mpz/mpz.c +463 -0
  90. _pystachy_source/pyembed.c +373 -0
  91. _pystachy_source/pyext.c +266 -0
  92. _pystachy_source/pystachy.py +34 -0
  93. _pystachy_source/pystachy_cli.py +31 -0
  94. _pystachy_source/pystachy_toolchain.py +152 -0
  95. _pystachy_source/runtime.c +8564 -0
  96. _pystachy_source/runtime.py +2276 -0
  97. _pystachy_source/ryu/common.h +114 -0
  98. _pystachy_source/ryu/d2s.c +509 -0
  99. _pystachy_source/ryu/d2s_intrinsics.h +357 -0
  100. _pystachy_source/ryu/d2s_small_table.h +186 -0
  101. _pystachy_source/ryu/digit_table.h +35 -0
  102. _pystachy_source/ryu/ryu.h +46 -0
  103. _pystachy_source/src/__init__.py +1 -0
  104. _pystachy_source/src/pystachy_compiler/__init__.py +1 -0
  105. _pystachy_source/src/pystachy_compiler/analysis.py +2105 -0
  106. _pystachy_source/src/pystachy_compiler/argparse_schema.py +393 -0
  107. _pystachy_source/src/pystachy_compiler/bigints.py +1498 -0
  108. _pystachy_source/src/pystachy_compiler/builtins.py +3026 -0
  109. _pystachy_source/src/pystachy_compiler/calls.py +1698 -0
  110. _pystachy_source/src/pystachy_compiler/containers.py +1256 -0
  111. _pystachy_source/src/pystachy_compiler/conversions.py +399 -0
  112. _pystachy_source/src/pystachy_compiler/declarations.py +3000 -0
  113. _pystachy_source/src/pystachy_compiler/diagnostics.py +176 -0
  114. _pystachy_source/src/pystachy_compiler/driver.py +921 -0
  115. _pystachy_source/src/pystachy_compiler/dynamic.py +4513 -0
  116. _pystachy_source/src/pystachy_compiler/emission.py +666 -0
  117. _pystachy_source/src/pystachy_compiler/exceptions.py +1642 -0
  118. _pystachy_source/src/pystachy_compiler/expressions.py +5153 -0
  119. _pystachy_source/src/pystachy_compiler/flow.py +972 -0
  120. _pystachy_source/src/pystachy_compiler/functions.py +523 -0
  121. _pystachy_source/src/pystachy_compiler/gen.py +5142 -0
  122. _pystachy_source/src/pystachy_compiler/generators.py +1361 -0
  123. _pystachy_source/src/pystachy_compiler/ir.py +579 -0
  124. _pystachy_source/src/pystachy_compiler/ircheck.py +503 -0
  125. _pystachy_source/src/pystachy_compiler/lexer.py +788 -0
  126. _pystachy_source/src/pystachy_compiler/llvm_abi.py +19 -0
  127. _pystachy_source/src/pystachy_compiler/loader.py +3787 -0
  128. _pystachy_source/src/pystachy_compiler/local_versions.py +57 -0
  129. _pystachy_source/src/pystachy_compiler/lower_module.py +754 -0
  130. _pystachy_source/src/pystachy_compiler/lowering.py +625 -0
  131. _pystachy_source/src/pystachy_compiler/memory_ir.py +28 -0
  132. _pystachy_source/src/pystachy_compiler/module.py +55 -0
  133. _pystachy_source/src/pystachy_compiler/names.py +55 -0
  134. _pystachy_source/src/pystachy_compiler/optimizations.py +1445 -0
  135. _pystachy_source/src/pystachy_compiler/parser.py +2465 -0
  136. _pystachy_source/src/pystachy_compiler/program.py +1296 -0
  137. _pystachy_source/src/pystachy_compiler/reflection.py +3073 -0
  138. _pystachy_source/src/pystachy_compiler/scalar_ir.py +61 -0
  139. _pystachy_source/src/pystachy_compiler/scope.py +619 -0
  140. _pystachy_source/src/pystachy_compiler/specs.py +1102 -0
  141. _pystachy_source/src/pystachy_compiler/statements.py +2126 -0
  142. _pystachy_source/src/pystachy_compiler/structs.py +271 -0
  143. _pystachy_source/src/pystachy_compiler/syntax.py +245 -0
  144. _pystachy_source/src/pystachy_compiler/templates.py +998 -0
  145. _pystachy_source/src/pystachy_compiler/union_arithmetic.py +161 -0
  146. _pystachy_source/src/pystachy_compiler/union_proof.py +891 -0
  147. _pystachy_source/src/pystachy_compiler/union_state.py +251 -0
  148. _pystachy_source/src/pystachy_compiler/unions.py +591 -0
  149. _pystachy_source/src/pystachy_compiler/utils.py +1124 -0
  150. _pystachy_source/src/pystachy_compiler/variables.py +2323 -0
  151. pystachy-0.2.0.dist-info/METADATA +93 -0
  152. pystachy-0.2.0.dist-info/RECORD +158 -0
  153. pystachy-0.2.0.dist-info/WHEEL +5 -0
  154. pystachy-0.2.0.dist-info/entry_points.txt +2 -0
  155. pystachy-0.2.0.dist-info/licenses/LICENSE +21 -0
  156. pystachy-0.2.0.dist-info/licenses/THIRD_PARTY_NOTICES +1051 -0
  157. pystachy-0.2.0.dist-info/licenses/ryu/LICENSE-Boost +23 -0
  158. pystachy-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,475 @@
1
+ """Python objects for Pystachy code, through CPython's stable ABI (@ffi.extern). In a program,
2
+ CPython runs embedded (PYSTACHY_LIBPYTHON names its libpython); in an extension module (pystachy
3
+ ext) the host interpreter is used. tools/rt_cpython/pyobj.py is this module for CPython itself, and
4
+ a program may use only what both offer (__all__; an Object's fields are this module's own).
5
+
6
+ An Object holds a reference until close(), or until a collection finds the Object dropped (the
7
+ reference is then released by the next operation); a closed Object raises ValueError. A Python
8
+ exception becomes the nearest builtin exception class Pystachy has, with the same str();
9
+ SystemExit and KeyboardInterrupt stay what they are. Embedded, the program holds the GIL only
10
+ inside these calls, so Python threads run while compiled code does, and CPython is finalized
11
+ after the program ends (pyembed.c), as it is when it runs a script."""
12
+ import os
13
+ import sys
14
+ from ffi import extern, c_int, c_int64, c_ssize_t, c_void_p, c_buf, string_at, track, untrack, collected
15
+
16
+ __all__ = ["Object", "import_module", "of_int", "of_float", "of_str", "of_list"]
17
+
18
+
19
+ @extern("python")
20
+ def Py_IsInitialized() -> c_int: ...
21
+ @extern("python")
22
+ def Py_InitializeEx(sigs: c_int) -> None: ...
23
+ @extern("python")
24
+ def PyEval_SaveThread() -> c_void_p: ...
25
+ @extern("python")
26
+ def PyImport_ImportModule(name: str) -> c_void_p: ...
27
+ @extern("python")
28
+ def PyObject_GetAttrString(o: c_void_p, name: str) -> c_void_p: ...
29
+ @extern("python")
30
+ def Py_DecRef(o: c_void_p) -> None: ...
31
+ @extern("python")
32
+ def Py_IncRef(o: c_void_p) -> None: ...
33
+ @extern("python")
34
+ def PyLong_FromLongLong(v: c_int64) -> c_void_p: ...
35
+ @extern("python")
36
+ def PyLong_AsLongLong(o: c_void_p) -> c_int64: ...
37
+ @extern("python")
38
+ def PyFloat_FromDouble(x: float) -> c_void_p: ...
39
+ @extern("python")
40
+ def PyFloat_AsDouble(o: c_void_p) -> float: ...
41
+ @extern("python")
42
+ def PyUnicode_DecodeUTF8(s: c_buf, n: c_ssize_t, errors: str) -> c_void_p: ...
43
+ @extern("python")
44
+ def PyUnicode_AsEncodedString(o: c_void_p, encoding: str, errors: str) -> c_void_p: ...
45
+ @extern("python")
46
+ def PyBytes_Size(o: c_void_p) -> c_ssize_t: ...
47
+ @extern("python")
48
+ def PyBytes_AsString(o: c_void_p) -> c_void_p: ...
49
+ @extern("python")
50
+ def PyObject_Str(o: c_void_p) -> c_void_p: ...
51
+ @extern("python")
52
+ def PyObject_Repr(o: c_void_p) -> c_void_p: ...
53
+ @extern("python")
54
+ def PyTuple_New(n: c_ssize_t) -> c_void_p: ...
55
+ @extern("python")
56
+ def PyTuple_SetItem(t: c_void_p, i: c_ssize_t, o: c_void_p) -> c_int: ...
57
+ @extern("python")
58
+ def PyTuple_GetItem(t: c_void_p, i: c_ssize_t) -> c_void_p: ...
59
+ @extern("python")
60
+ def PyTuple_Size(t: c_void_p) -> c_ssize_t: ...
61
+ @extern("python")
62
+ def PyList_New(n: c_ssize_t) -> c_void_p: ...
63
+ @extern("python")
64
+ def PyList_Append(lst: c_void_p, o: c_void_p) -> c_int: ...
65
+ @extern("python")
66
+ def PyObject_Call(f: c_void_p, args: c_void_p, kwargs: c_void_p) -> c_void_p: ...
67
+ @extern("python")
68
+ def PyObject_GetItem(o: c_void_p, k: c_void_p) -> c_void_p: ...
69
+ @extern("python")
70
+ def PyObject_Length(o: c_void_p) -> c_ssize_t: ...
71
+ @extern("python")
72
+ def PyObject_IsTrue(o: c_void_p) -> c_int: ...
73
+ @extern("python")
74
+ def PyErr_Occurred() -> c_void_p: ...
75
+ @extern("python")
76
+ def PyErr_GetRaisedException() -> c_void_p: ...
77
+ @extern("python")
78
+ def PyObject_Type(o: c_void_p) -> c_void_p: ...
79
+ @extern("python")
80
+ def PySys_GetObject(name: str) -> c_void_p: ...
81
+ @extern("python")
82
+ def PySys_SetObject(name: str, o: c_void_p) -> c_int: ...
83
+ # pyembed.c: CPython set up as its own main sets it up, the GIL with the two runtimes' output in order,
84
+ # and a Python object's size
85
+ @extern("")
86
+ def pye_preinit() -> None: ...
87
+ @extern("")
88
+ def pye_init() -> c_int: ...
89
+ @extern("")
90
+ def pye_enter() -> c_int: ...
91
+ @extern("")
92
+ def pye_leave(g: c_int) -> None: ...
93
+ @extern("")
94
+ def pye_sizeof(o: c_void_p) -> c_int64: ...
95
+ @extern("")
96
+ def pye_flush() -> None: ...
97
+ @extern("")
98
+ def pye_exit(e: c_void_p) -> None: ...
99
+
100
+
101
+ _BUILTIN = ["KeyError", "IndexError", "ZeroDivisionError", "OverflowError", "ValueError", "TypeError",
102
+ "AttributeError", "ModuleNotFoundError", "ImportError", "NotImplementedError", "RecursionError",
103
+ "RuntimeError", "FileNotFoundError", "OSError", "StopIteration", "AssertionError", "LookupError",
104
+ "ArithmeticError", "EOFError", "KeyboardInterrupt", "SystemExit", "GeneratorExit", "Exception",
105
+ "BaseException"]
106
+
107
+
108
+ def _text(o: int, alt: str) -> str:
109
+ # a Python str (a new reference, which this releases) as Pystachy's UTF-8 str; when o is NULL or
110
+ # cannot be encoded, alt (the error dropped)
111
+ if o == 0:
112
+ Py_DecRef(PyErr_GetRaisedException())
113
+ return alt
114
+ b = PyUnicode_AsEncodedString(o, "utf-8", "surrogatepass")
115
+ Py_DecRef(o)
116
+ if b == 0:
117
+ Py_DecRef(PyErr_GetRaisedException())
118
+ return alt
119
+ s = string_at(PyBytes_AsString(b), PyBytes_Size(b))
120
+ Py_DecRef(b)
121
+ return s
122
+
123
+
124
+ def _raise() -> None:
125
+ # the Python exception just raised, as the first builtin class in its type's __mro__ that
126
+ # Pystachy has, with its str() (if str() raises, what it raised instead, once); a SystemExit
127
+ # is raised as it is (pye_exit)
128
+ e = PyErr_GetRaisedException()
129
+ if e == 0:
130
+ raise RuntimeError("error return without exception set")
131
+ k = "Exception"
132
+ m = ""
133
+ again = True
134
+ while True:
135
+ t = PyObject_Type(e)
136
+ mro = PyObject_GetAttrString(t, "__mro__")
137
+ Py_DecRef(t)
138
+ k = "Exception"
139
+ for i in range(PyTuple_Size(mro) if mro != 0 else 0):
140
+ n = _text(PyObject_GetAttrString(PyTuple_GetItem(mro, i), "__name__"), "") # (borrowed item)
141
+ if n in _BUILTIN:
142
+ k = n
143
+ break
144
+ Py_DecRef(mro)
145
+ if k == "SystemExit":
146
+ # the status its code gives CPython's own exit (one beyond 64 bits too), its str() and its repr()
147
+ pye_exit(e) # (which returns only if str(e) or repr(e) raises: then it is raised as any other)
148
+ s = PyObject_Str(e)
149
+ if s != 0 or not again:
150
+ m = _text(s, "<exception str() failed>")
151
+ Py_DecRef(e)
152
+ break
153
+ Py_DecRef(e)
154
+ e = PyErr_GetRaisedException() # (str(e) raised: that is raised instead, as the twin's str(e) raises it)
155
+ again = False
156
+ if e == 0:
157
+ m = "<exception str() failed>"
158
+ break
159
+ if k == "KeyboardInterrupt":
160
+ raise KeyboardInterrupt(m)
161
+ if k == "StopIteration":
162
+ raise StopIteration(m)
163
+ if k == "KeyError":
164
+ raise KeyError(m)
165
+ if k == "IndexError":
166
+ raise IndexError(m)
167
+ if k == "ZeroDivisionError":
168
+ raise ZeroDivisionError(m)
169
+ if k == "OverflowError":
170
+ raise OverflowError(m)
171
+ if k == "ValueError":
172
+ raise ValueError(m)
173
+ if k == "TypeError":
174
+ raise TypeError(m)
175
+ if k == "AttributeError":
176
+ raise AttributeError(m)
177
+ if k == "ModuleNotFoundError":
178
+ raise ModuleNotFoundError(m)
179
+ if k == "ImportError":
180
+ raise ImportError(m)
181
+ if k == "NotImplementedError":
182
+ raise NotImplementedError(m)
183
+ if k == "RuntimeError" or k == "RecursionError":
184
+ raise RuntimeError(m)
185
+ if k == "FileNotFoundError":
186
+ raise FileNotFoundError(m)
187
+ if k == "OSError":
188
+ raise OSError(m)
189
+ if k == "AssertionError":
190
+ raise AssertionError(m)
191
+ if k == "LookupError":
192
+ raise LookupError(m)
193
+ if k == "ArithmeticError":
194
+ raise ArithmeticError(m)
195
+ if k == "EOFError":
196
+ raise EOFError(m)
197
+ if k == "SystemExit":
198
+ raise SystemExit(m)
199
+ if k == "GeneratorExit":
200
+ raise GeneratorExit(m)
201
+ if k == "BaseException":
202
+ raise BaseException(m)
203
+ raise Exception(m)
204
+
205
+
206
+ def _enter() -> int:
207
+ # attach to CPython (the GIL, with Pystachy's pending output written first: pyembed.c) for one
208
+ # operation, after releasing the references of Objects that collections found dropped: here,
209
+ # not inside a collection (Py_DecRef runs Python code). That is safe at any depth (Python code
210
+ # calling back into an extension module), as each operation owns the references it uses (_own)
211
+ g = pye_enter()
212
+ v = collected()
213
+ while v != 0:
214
+ Py_DecRef(v)
215
+ v = collected()
216
+ return g
217
+
218
+
219
+ def _leave(g: int) -> None:
220
+ pye_leave(g)
221
+
222
+
223
+ def _own(p: int) -> int:
224
+ # an operation's own reference to the object an Object holds (released when the operation ends)
225
+ if p == 0:
226
+ raise ValueError("operation on a closed Python object")
227
+ Py_IncRef(p)
228
+ return p
229
+
230
+
231
+ def _ref(p: int, n: int) -> "Object":
232
+ # a new reference (NULL: the error raised) as an Object; n: what it holds (-1: sys.getsizeof)
233
+ if p == 0:
234
+ _raise()
235
+ return Object(p, pye_sizeof(p) if n < 0 else n)
236
+
237
+
238
+ class Object:
239
+ # made by this module only (the twin's Object is another class); every operation owns the
240
+ # references it uses, so a release that runs meanwhile cannot free them
241
+ def __init__(self, p: int, n: int) -> None:
242
+ self._p = p # a strong reference, a PyObject *; 0 once closed
243
+ self._t: int = track(self, p, n) # (released when a collection finds this Object dropped)
244
+
245
+ def close(self) -> None:
246
+ g = _enter()
247
+ try:
248
+ p = self._p
249
+ if p != 0:
250
+ self._p = 0
251
+ untrack(self._t)
252
+ Py_DecRef(p)
253
+ finally:
254
+ _leave(g)
255
+
256
+ def attr(self, name: str) -> "Object":
257
+ g = _enter()
258
+ p = 0
259
+ try:
260
+ p = _own(self._p)
261
+ return _ref(PyObject_GetAttrString(p, name), -1)
262
+ finally:
263
+ Py_DecRef(p)
264
+ _leave(g)
265
+
266
+ def call(self, args: list["Object"]) -> "Object":
267
+ g = _enter()
268
+ p = 0
269
+ t = 0
270
+ try:
271
+ p = _own(self._p)
272
+ t = PyTuple_New(len(args))
273
+ if t == 0:
274
+ _raise()
275
+ for i in range(len(args)):
276
+ PyTuple_SetItem(t, i, _own(args[i]._p)) # (steals the reference _own took)
277
+ pye_flush() # (what was printed reaches stdout's file before the call, as the twin flushes sys.stdout)
278
+ return _ref(PyObject_Call(p, t, 0), -1)
279
+ finally:
280
+ Py_DecRef(t)
281
+ Py_DecRef(p)
282
+ _leave(g)
283
+
284
+ def __getitem__(self, k: "Object") -> "Object":
285
+ g = _enter()
286
+ p = 0
287
+ q = 0
288
+ try:
289
+ p = _own(self._p)
290
+ q = _own(k._p)
291
+ return _ref(PyObject_GetItem(p, q), -1)
292
+ finally:
293
+ Py_DecRef(q)
294
+ Py_DecRef(p)
295
+ _leave(g)
296
+
297
+ def __len__(self) -> int:
298
+ g = _enter()
299
+ p = 0
300
+ try:
301
+ p = _own(self._p)
302
+ n = PyObject_Length(p)
303
+ if n < 0:
304
+ _raise()
305
+ return n
306
+ finally:
307
+ Py_DecRef(p)
308
+ _leave(g)
309
+
310
+ def __bool__(self) -> bool:
311
+ g = _enter()
312
+ p = 0
313
+ try:
314
+ p = _own(self._p)
315
+ r = PyObject_IsTrue(p)
316
+ if r < 0:
317
+ _raise()
318
+ return r == 1
319
+ finally:
320
+ Py_DecRef(p)
321
+ _leave(g)
322
+
323
+ def to_int(self) -> int:
324
+ g = _enter()
325
+ p = 0
326
+ try:
327
+ p = _own(self._p)
328
+ v = PyLong_AsLongLong(p)
329
+ if v == -1 and PyErr_Occurred() != 0:
330
+ _raise()
331
+ return v
332
+ finally:
333
+ Py_DecRef(p)
334
+ _leave(g)
335
+
336
+ def to_float(self) -> float:
337
+ g = _enter()
338
+ p = 0
339
+ try:
340
+ p = _own(self._p)
341
+ v = PyFloat_AsDouble(p)
342
+ if v == -1.0 and PyErr_Occurred() != 0:
343
+ _raise()
344
+ return v
345
+ finally:
346
+ Py_DecRef(p)
347
+ _leave(g)
348
+
349
+ def __str__(self) -> str:
350
+ g = _enter()
351
+ p = 0
352
+ try:
353
+ p = _own(self._p)
354
+ r = PyObject_Str(p)
355
+ if r == 0:
356
+ _raise()
357
+ return _text(r, "")
358
+ finally:
359
+ Py_DecRef(p)
360
+ _leave(g)
361
+
362
+ def __repr__(self) -> str:
363
+ g = _enter()
364
+ p = 0
365
+ try:
366
+ p = _own(self._p)
367
+ r = PyObject_Repr(p)
368
+ if r == 0:
369
+ _raise()
370
+ return _text(r, "")
371
+ finally:
372
+ Py_DecRef(p)
373
+ _leave(g)
374
+
375
+
376
+ def import_module(name: str) -> Object:
377
+ g = _enter()
378
+ try:
379
+ return _ref(PyImport_ImportModule(name), -1)
380
+ finally:
381
+ _leave(g)
382
+
383
+
384
+ def of_int(v: int) -> Object:
385
+ g = _enter()
386
+ try:
387
+ return _ref(PyLong_FromLongLong(v), 0)
388
+ finally:
389
+ _leave(g)
390
+
391
+
392
+ def of_float(x: float) -> Object:
393
+ g = _enter()
394
+ try:
395
+ return _ref(PyFloat_FromDouble(x), 0)
396
+ finally:
397
+ _leave(g)
398
+
399
+
400
+ def of_str(s: str) -> Object:
401
+ g = _enter()
402
+ try:
403
+ # c_buf exposes the UTF-8 payload; CPython and allocation accounting need
404
+ # its byte size, while public len(s) counts Unicode code points.
405
+ n = __pys_text_size(s)
406
+ return _ref(PyUnicode_DecodeUTF8(s, n, "surrogatepass"), n)
407
+ finally:
408
+ _leave(g)
409
+
410
+
411
+ def of_list(xs: list[Object]) -> Object:
412
+ g = _enter()
413
+ r = 0
414
+ try:
415
+ r = PyList_New(0)
416
+ if r == 0:
417
+ _raise()
418
+ for x in xs:
419
+ q = _own(x._p)
420
+ e = PyList_Append(r, q)
421
+ Py_DecRef(q)
422
+ if e < 0:
423
+ _raise()
424
+ o = _ref(r, -1)
425
+ r = 0
426
+ return o
427
+ finally:
428
+ Py_DecRef(r)
429
+ _leave(g)
430
+
431
+
432
+ def _check() -> None:
433
+ # the setup's own Python error, if any, raised in the program
434
+ if PyErr_Occurred() != 0:
435
+ _raise()
436
+
437
+
438
+ if Py_IsInitialized() == 0:
439
+ # embedded: CPython as it runs the script (its locale and UTF-8 mode, sys.executable, sys.argv, the
440
+ # script's directory first on sys.path), without its signal handlers (Pystachy keeps its own); then
441
+ # the GIL is released until an operation
442
+ pye_preinit()
443
+ Py_InitializeEx(0)
444
+ try:
445
+ if pye_init() == 0:
446
+ _raise()
447
+ _argv = PyList_New(0)
448
+ _check()
449
+ for _a in sys.argv:
450
+ _s = PyUnicode_DecodeUTF8(_a, __pys_text_size(_a), "surrogateescape") # (as CPython decodes arguments)
451
+ _check()
452
+ PyList_Append(_argv, _s)
453
+ Py_DecRef(_s)
454
+ _check()
455
+ PySys_SetObject("argv", _argv)
456
+ Py_DecRef(_argv)
457
+ _check()
458
+ _d = os.path.realpath(sys.argv[0])
459
+ _d = _d[: _d.rfind("/")] if _d.rfind("/") > 0 else "/"
460
+ _path = PySys_GetObject("path") # (borrowed)
461
+ _ins = PyObject_GetAttrString(_path, "insert")
462
+ _check()
463
+ _t = PyTuple_New(2)
464
+ _check()
465
+ PyTuple_SetItem(_t, 0, PyLong_FromLongLong(0))
466
+ PyTuple_SetItem(_t, 1, PyUnicode_DecodeUTF8(_d, __pys_text_size(_d), "surrogateescape"))
467
+ _check()
468
+ _r = PyObject_Call(_ins, _t, 0)
469
+ Py_DecRef(_t)
470
+ Py_DecRef(_ins)
471
+ if _r == 0:
472
+ _raise()
473
+ Py_DecRef(_r)
474
+ finally:
475
+ PyEval_SaveThread()