this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3# $builtin-init-module$
4
5from builtins import SimpleNamespace as _SimpleNamespace
6
7from _builtins import (
8 _builtin,
9 _get_asyncgen_hooks,
10 _int_check,
11 _structseq_new_type,
12 _Unbound,
13 _unimplemented,
14)
15from _builtins import maxunicode # noqa: F401
16from _io import TextIOWrapper
17from _path import dirname as _dirname, join as _join
18
19
20# These values are all injected by our boot process. flake8 has no knowledge
21# about their definitions and will complain without these lines.
22_stderr_fd = _stderr_fd # noqa: F821
23_stdin_fd = _stdin_fd # noqa: F821
24_stdout_fd = _stdout_fd # noqa: F821
25_version_releaselevel = _version_releaselevel # noqa: F821
26hexversion = hexversion # noqa: F821
27_use_buffered_stdio = _use_buffered_stdio # noqa: F821
28
29
30_Flags = _structseq_new_type(
31 "sys.flags",
32 (
33 "debug",
34 "inspect",
35 "interactive",
36 "optimize",
37 "dont_write_bytecode",
38 "no_user_site",
39 "no_site",
40 "ignore_environment",
41 "verbose",
42 "bytes_warning",
43 "quiet",
44 "hash_randomization",
45 "isolated",
46 "dev_mode",
47 "utf8_mode",
48 ),
49 is_heaptype=False,
50)
51
52
53_FloatInfo = _structseq_new_type(
54 "sys.float_info",
55 (
56 "max",
57 "max_exp",
58 "max_10_exp",
59 "min",
60 "min_exp",
61 "min_10_exp",
62 "dig",
63 "mant_dig",
64 "epsilon",
65 "radix",
66 "rounds",
67 ),
68 is_heaptype=False,
69)
70
71
72_HashInfo = _structseq_new_type(
73 "sys.hash_info",
74 (
75 "width",
76 "modulus",
77 "inf",
78 "nan",
79 "imag",
80 "algorithm",
81 "hash_bits",
82 "seed_bits",
83 "cutoff",
84 ),
85 is_heaptype=False,
86)
87
88_AsyncGenHooks = _structseq_new_type(
89 "sys.asyncgen_hooks", ("firstiter", "finalizer"), is_heaptype=False
90)
91
92
93_VersionInfo = _structseq_new_type(
94 "sys.version_info",
95 ("major", "minor", "micro", "releaselevel", "serial"),
96 is_heaptype=False,
97)
98
99
100def _init(
101 _executable,
102 _python_path,
103 _flags_data,
104 _warnoptions,
105 extend_python_path_with_stdlib,
106):
107 global executable
108 executable = _executable
109 global _base_executable
110 _base_executable = _executable
111
112 executable_dir = _dirname(executable)
113 cfg = None
114 try:
115 cfg = open(_join(executable_dir, "pyvenv.cfg"), "r", encoding="utf-8")
116 except IOError as e:
117 try:
118 cfg = open(_join(executable_dir, "..", "pyvenv.cfg"), "r", encoding="utf-8")
119 except IOError as e:
120 pass
121
122 if cfg is not None:
123 with cfg:
124 for line in cfg:
125 if line[0] == "#":
126 continue
127 tokens = line.split()
128 if len(tokens) >= 3 and tokens[0] == "home" and tokens[1] == "=":
129 executable_dir = tokens[2]
130 break
131
132 _prefix = _join(executable_dir, "..")
133
134 global prefix
135 prefix = _prefix
136 global base_exec_prefix
137 base_exec_prefix = _prefix
138 global base_prefix
139 base_prefix = _prefix
140 global exec_prefix
141 exec_prefix = _prefix
142
143 global path
144 path = _python_path
145 if extend_python_path_with_stdlib:
146 stdlib_dir = _join(
147 _prefix, "lib", f"{implementation.name}{_version.major}.{_version.minor}"
148 )
149 path.append(stdlib_dir)
150
151 global flags
152 flags = _Flags(_flags_data)
153
154 global warnoptions
155 warnoptions = _warnoptions
156
157
158if _use_buffered_stdio:
159 __stderr__ = open(_stderr_fd, "w", buffering=-1, closefd=False, encoding="utf-8")
160
161 __stdin__ = open(_stdin_fd, "r", buffering=-1, closefd=False, encoding="utf-8")
162
163 __stdout__ = open(_stdout_fd, "w", buffering=-1, closefd=False, encoding="utf-8")
164else:
165 __stderr__ = open(_stderr_fd, "wb", buffering=False, closefd=False)
166 __stderr__ = TextIOWrapper(__stderr__, encoding="utf-8", line_buffering=False)
167
168 __stdin__ = open(_stdin_fd, "rb", buffering=False, closefd=False)
169 __stdin__ = TextIOWrapper(__stdin__, encoding="utf-8", line_buffering=False)
170
171 __stdout__ = open(_stdout_fd, "wb", buffering=False, closefd=False)
172 __stdout__ = TextIOWrapper(__stdout__, encoding="utf-8", line_buffering=False)
173
174
175_base_executable = None # will be set by _init
176
177
178_framework = ""
179
180
181_version = _VersionInfo(
182 (
183 (hexversion >> 24) & 0xFF, # major
184 (hexversion >> 16) & 0xFF, # minor
185 (hexversion >> 8) & 0xFF, # micro
186 _version_releaselevel, # releaselevel
187 hexversion & 0x0F, # serial
188 )
189)
190
191
192def _getframe(depth=0):
193 _builtin()
194
195
196abiflags = ""
197
198
199# TODO(T86943617): Add `addaudithook`.
200
201
202def audit(event, *args):
203 pass # TODO(T86943617): implement
204
205
206base_exec_prefix = None # will be set by _init
207
208
209base_prefix = None # will be set by _init
210
211
212copyright = ""
213
214
215def displayhook(value):
216 if value is None:
217 return
218 # Set '_' to None to avoid recursion
219 import builtins
220
221 builtins._ = None
222 text = repr(value)
223 try:
224 stdout.write(text)
225 except UnicodeEncodeError:
226 bytes = text.encode(stdout.encoding, "backslashreplace")
227 if hasattr(stdout, "buffer"):
228 stdout.buffer.write(bytes)
229 else:
230 text = bytes.decode(stdout.encoding, "strict")
231 stdout.write(text)
232 stdout.write("\n")
233 builtins._ = value
234
235
236__displayhook__ = displayhook
237
238
239dont_write_bytecode = False
240
241
242def exc_info():
243 _builtin()
244
245
246def excepthook(exc, value, tb):
247 _builtin()
248
249
250__excepthook__ = excepthook
251
252
253exec_prefix = None # will be set by _init
254
255
256executable = None # will be set by _init
257
258
259def exit(code=_Unbound):
260 if code is _Unbound:
261 raise SystemExit()
262 raise SystemExit(code)
263
264
265flags = None # will be set by _init
266
267
268float_info = _FloatInfo(
269 (
270 1.79769313486231570814527423731704357e308,
271 1024,
272 308,
273 2.22507385850720138309023271733240406e-308,
274 -1021,
275 -307,
276 15,
277 53,
278 2.22044604925031308084726333618164062e-16,
279 2,
280 1,
281 )
282)
283
284
285def getdefaultencoding():
286 return "utf-8"
287
288
289def getfilesystemencodeerrors():
290 # TODO(T40363016): Allow arbitrary encodings and error handlings.
291 return "surrogatepass"
292
293
294def getfilesystemencoding():
295 # TODO(T40363016): Allow arbitrary encodings instead of defaulting to utf-8.
296 return "utf-8"
297
298
299# TODO(T62600497): Enforce the recursion limit
300def getrecursionlimit():
301 _builtin()
302
303
304def getsizeof(object, default=_Unbound):
305 # It is possible (albeit difficult) to define a class without __sizeof__
306 try:
307 cls = type(object)
308 size = cls.__sizeof__
309 except AttributeError:
310 if default is _Unbound:
311 raise TypeError(f"Type {cls.__name__} doesn't define __sizeof__")
312 return default
313 result = size(object)
314 if not _int_check(result):
315 if default is _Unbound:
316 raise TypeError("an integer is required")
317 return default
318 if result < 0:
319 raise ValueError("__sizeof__() should return >= 0")
320 return int(result)
321
322
323def gettrace():
324 return None
325
326
327implementation = _SimpleNamespace(
328 cache_tag=f"skybison-{_version.major}{_version.minor}", name="skybison", version=_version
329)
330
331
332def intern(string):
333 _builtin()
334
335
336def is_finalizing():
337 _builtin()
338
339
340meta_path = []
341
342
343path = None # will be set by _init
344
345
346path_hooks = []
347
348
349path_importer_cache = {}
350
351
352platlibdir = "lib"
353
354
355prefix = None # will be set by _init
356
357
358ps1 = "ypyp> "
359
360
361ps2 = "..... "
362
363
364pycache_prefix = None
365
366
367# TODO(T62600497): Enforce the recursion limit
368def setrecursionlimit(limit):
369 _builtin()
370
371
372def settrace(function):
373 if function is None:
374 return
375 _unimplemented()
376
377
378stderr = __stderr__
379
380
381stdin = __stdin__
382
383
384stdout = __stdout__
385
386
387def unraisablehook(unraisable):
388 _unimplemented()
389
390
391__unraisablehook__ = unraisablehook
392
393
394version_info = _version
395
396
397warnoptions = None # will be set by _init
398
399
400def _program_name():
401 _builtin()
402
403
404def _calculate_path():
405 """Returns a tuple representing (prefix, exec_prefix, module_search_path)"""
406 # TODO(T61328507): Implement the path lookup algorithm. In the meantime, return
407 # the compiled-in defaults that CPython returns when run out of a build directory.
408 return "/usr/local", "/usr/local", ""
409
410
411def get_asyncgen_hooks():
412 return _AsyncGenHooks(_get_asyncgen_hooks())
413
414
415def set_asyncgen_hooks(firstiter=_Unbound, finalizer=_Unbound):
416 _builtin()