Skip to content

Commit feb5066

Browse files
authored
stdlib_module_names (#6568)
1 parent 45a1940 commit feb5066

File tree

4 files changed

+315
-3
lines changed

4 files changed

+315
-3
lines changed

.cspell.dict/cpython.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ kwonlyarg
3636
kwonlyargs
3737
lasti
3838
linearise
39+
lsprof
3940
maxdepth
4041
mult
42+
multibytecodec
4143
nkwargs
4244
noraise
4345
numer
@@ -49,6 +51,8 @@ posonlyarg
4951
posonlyargs
5052
prec
5153
preinitialized
54+
pydecimal
55+
pyrepl
5256
pythonw
5357
PYTHREAD_NAME
5458
releasebuffer

Lib/test/test_sys.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,6 @@ def test_orig_argv(self):
11241124
self.assertEqual(proc.stdout.rstrip().splitlines(), expected,
11251125
proc)
11261126

1127-
# TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'stdlib_module_names'
1128-
@unittest.expectedFailure
11291127
def test_module_names(self):
11301128
self.assertIsInstance(sys.stdlib_module_names, frozenset)
11311129
for name in sys.stdlib_module_names:

crates/vm/src/builtins/frame.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ impl Frame {
4141
self.globals.clone()
4242
}
4343

44+
#[pygetset]
45+
fn f_builtins(&self) -> PyDictRef {
46+
self.builtins.clone()
47+
}
48+
4449
#[pygetset]
4550
fn f_locals(&self, vm: &VirtualMachine) -> PyResult {
4651
self.locals(vm).map(Into::into)

crates/vm/src/stdlib/sys.rs

Lines changed: 306 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ mod sys {
99
use crate::{
1010
AsObject, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult,
1111
builtins::{
12-
PyBaseExceptionRef, PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
12+
PyBaseExceptionRef, PyDictRef, PyFrozenSet, PyNamespace, PyStr, PyStrRef, PyTupleRef,
13+
PyTypeRef,
1314
},
1415
common::{
1516
ascii,
@@ -148,6 +149,310 @@ mod sys {
148149
)
149150
}
150151

152+
// List from cpython/Python/stdlib_module_names.h
153+
const STDLIB_MODULE_NAMES: &[&str] = &[
154+
"__future__",
155+
"_abc",
156+
"_aix_support",
157+
"_android_support",
158+
"_apple_support",
159+
"_ast",
160+
"_asyncio",
161+
"_bisect",
162+
"_blake2",
163+
"_bz2",
164+
"_codecs",
165+
"_codecs_cn",
166+
"_codecs_hk",
167+
"_codecs_iso2022",
168+
"_codecs_jp",
169+
"_codecs_kr",
170+
"_codecs_tw",
171+
"_collections",
172+
"_collections_abc",
173+
"_colorize",
174+
"_compat_pickle",
175+
"_compression",
176+
"_contextvars",
177+
"_csv",
178+
"_ctypes",
179+
"_curses",
180+
"_curses_panel",
181+
"_datetime",
182+
"_dbm",
183+
"_decimal",
184+
"_elementtree",
185+
"_frozen_importlib",
186+
"_frozen_importlib_external",
187+
"_functools",
188+
"_gdbm",
189+
"_hashlib",
190+
"_heapq",
191+
"_imp",
192+
"_interpchannels",
193+
"_interpqueues",
194+
"_interpreters",
195+
"_io",
196+
"_ios_support",
197+
"_json",
198+
"_locale",
199+
"_lsprof",
200+
"_lzma",
201+
"_markupbase",
202+
"_md5",
203+
"_multibytecodec",
204+
"_multiprocessing",
205+
"_opcode",
206+
"_opcode_metadata",
207+
"_operator",
208+
"_osx_support",
209+
"_overlapped",
210+
"_pickle",
211+
"_posixshmem",
212+
"_posixsubprocess",
213+
"_py_abc",
214+
"_pydatetime",
215+
"_pydecimal",
216+
"_pyio",
217+
"_pylong",
218+
"_pyrepl",
219+
"_queue",
220+
"_random",
221+
"_scproxy",
222+
"_sha1",
223+
"_sha2",
224+
"_sha3",
225+
"_signal",
226+
"_sitebuiltins",
227+
"_socket",
228+
"_sqlite3",
229+
"_sre",
230+
"_ssl",
231+
"_stat",
232+
"_statistics",
233+
"_string",
234+
"_strptime",
235+
"_struct",
236+
"_suggestions",
237+
"_symtable",
238+
"_sysconfig",
239+
"_thread",
240+
"_threading_local",
241+
"_tkinter",
242+
"_tokenize",
243+
"_tracemalloc",
244+
"_typing",
245+
"_uuid",
246+
"_warnings",
247+
"_weakref",
248+
"_weakrefset",
249+
"_winapi",
250+
"_wmi",
251+
"_zoneinfo",
252+
"abc",
253+
"antigravity",
254+
"argparse",
255+
"array",
256+
"ast",
257+
"asyncio",
258+
"atexit",
259+
"base64",
260+
"bdb",
261+
"binascii",
262+
"bisect",
263+
"builtins",
264+
"bz2",
265+
"cProfile",
266+
"calendar",
267+
"cmath",
268+
"cmd",
269+
"code",
270+
"codecs",
271+
"codeop",
272+
"collections",
273+
"colorsys",
274+
"compileall",
275+
"concurrent",
276+
"configparser",
277+
"contextlib",
278+
"contextvars",
279+
"copy",
280+
"copyreg",
281+
"csv",
282+
"ctypes",
283+
"curses",
284+
"dataclasses",
285+
"datetime",
286+
"dbm",
287+
"decimal",
288+
"difflib",
289+
"dis",
290+
"doctest",
291+
"email",
292+
"encodings",
293+
"ensurepip",
294+
"enum",
295+
"errno",
296+
"faulthandler",
297+
"fcntl",
298+
"filecmp",
299+
"fileinput",
300+
"fnmatch",
301+
"fractions",
302+
"ftplib",
303+
"functools",
304+
"gc",
305+
"genericpath",
306+
"getopt",
307+
"getpass",
308+
"gettext",
309+
"glob",
310+
"graphlib",
311+
"grp",
312+
"gzip",
313+
"hashlib",
314+
"heapq",
315+
"hmac",
316+
"html",
317+
"http",
318+
"idlelib",
319+
"imaplib",
320+
"importlib",
321+
"inspect",
322+
"io",
323+
"ipaddress",
324+
"itertools",
325+
"json",
326+
"keyword",
327+
"linecache",
328+
"locale",
329+
"logging",
330+
"lzma",
331+
"mailbox",
332+
"marshal",
333+
"math",
334+
"mimetypes",
335+
"mmap",
336+
"modulefinder",
337+
"msvcrt",
338+
"multiprocessing",
339+
"netrc",
340+
"nt",
341+
"ntpath",
342+
"nturl2path",
343+
"numbers",
344+
"opcode",
345+
"operator",
346+
"optparse",
347+
"os",
348+
"pathlib",
349+
"pdb",
350+
"pickle",
351+
"pickletools",
352+
"pkgutil",
353+
"platform",
354+
"plistlib",
355+
"poplib",
356+
"posix",
357+
"posixpath",
358+
"pprint",
359+
"profile",
360+
"pstats",
361+
"pty",
362+
"pwd",
363+
"py_compile",
364+
"pyclbr",
365+
"pydoc",
366+
"pydoc_data",
367+
"pyexpat",
368+
"queue",
369+
"quopri",
370+
"random",
371+
"re",
372+
"readline",
373+
"reprlib",
374+
"resource",
375+
"rlcompleter",
376+
"runpy",
377+
"sched",
378+
"secrets",
379+
"select",
380+
"selectors",
381+
"shelve",
382+
"shlex",
383+
"shutil",
384+
"signal",
385+
"site",
386+
"smtplib",
387+
"socket",
388+
"socketserver",
389+
"sqlite3",
390+
"sre_compile",
391+
"sre_constants",
392+
"sre_parse",
393+
"ssl",
394+
"stat",
395+
"statistics",
396+
"string",
397+
"stringprep",
398+
"struct",
399+
"subprocess",
400+
"symtable",
401+
"sys",
402+
"sysconfig",
403+
"syslog",
404+
"tabnanny",
405+
"tarfile",
406+
"tempfile",
407+
"termios",
408+
"textwrap",
409+
"this",
410+
"threading",
411+
"time",
412+
"timeit",
413+
"tkinter",
414+
"token",
415+
"tokenize",
416+
"tomllib",
417+
"trace",
418+
"traceback",
419+
"tracemalloc",
420+
"tty",
421+
"turtle",
422+
"turtledemo",
423+
"types",
424+
"typing",
425+
"unicodedata",
426+
"unittest",
427+
"urllib",
428+
"uuid",
429+
"venv",
430+
"warnings",
431+
"wave",
432+
"weakref",
433+
"webbrowser",
434+
"winreg",
435+
"winsound",
436+
"wsgiref",
437+
"xml",
438+
"xmlrpc",
439+
"zipapp",
440+
"zipfile",
441+
"zipimport",
442+
"zlib",
443+
"zoneinfo",
444+
];
445+
446+
#[pyattr(once)]
447+
fn stdlib_module_names(vm: &VirtualMachine) -> PyObjectRef {
448+
let names = STDLIB_MODULE_NAMES
449+
.iter()
450+
.map(|&n| vm.ctx.new_str(n).into());
451+
PyFrozenSet::from_iter(vm, names)
452+
.expect("Creating stdlib_module_names frozen set must succeed")
453+
.to_pyobject(vm)
454+
}
455+
151456
#[pyattr]
152457
fn byteorder(vm: &VirtualMachine) -> PyStrRef {
153458
// https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian

0 commit comments

Comments
 (0)