Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .cspell.dict/cpython.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ kwonlyarg
kwonlyargs
lasti
linearise
lsprof
maxdepth
mult
multibytecodec
nkwargs
noraise
numer
Expand All @@ -49,6 +51,8 @@ posonlyarg
posonlyargs
prec
preinitialized
pydecimal
pyrepl
pythonw
PYTHREAD_NAME
releasebuffer
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,6 @@ def test_orig_argv(self):
self.assertEqual(proc.stdout.rstrip().splitlines(), expected,
proc)

# TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'stdlib_module_names'
@unittest.expectedFailure
def test_module_names(self):
self.assertIsInstance(sys.stdlib_module_names, frozenset)
for name in sys.stdlib_module_names:
Expand Down
5 changes: 5 additions & 0 deletions crates/vm/src/builtins/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl Frame {
self.globals.clone()
}

#[pygetset]
fn f_builtins(&self) -> PyDictRef {
self.builtins.clone()
}

#[pygetset]
fn f_locals(&self, vm: &VirtualMachine) -> PyResult {
self.locals(vm).map(Into::into)
Expand Down
307 changes: 306 additions & 1 deletion crates/vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ mod sys {
use crate::{
AsObject, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult,
builtins::{
PyBaseExceptionRef, PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
PyBaseExceptionRef, PyDictRef, PyFrozenSet, PyNamespace, PyStr, PyStrRef, PyTupleRef,
PyTypeRef,
},
common::{
ascii,
Expand Down Expand Up @@ -148,6 +149,310 @@ mod sys {
)
}

// List from cpython/Python/stdlib_module_names.h
const STDLIB_MODULE_NAMES: &[&str] = &[
"__future__",
"_abc",
"_aix_support",
"_android_support",
"_apple_support",
"_ast",
"_asyncio",
"_bisect",
"_blake2",
"_bz2",
"_codecs",
"_codecs_cn",
"_codecs_hk",
"_codecs_iso2022",
"_codecs_jp",
"_codecs_kr",
"_codecs_tw",
"_collections",
"_collections_abc",
"_colorize",
"_compat_pickle",
"_compression",
"_contextvars",
"_csv",
"_ctypes",
"_curses",
"_curses_panel",
"_datetime",
"_dbm",
"_decimal",
"_elementtree",
"_frozen_importlib",
"_frozen_importlib_external",
"_functools",
"_gdbm",
"_hashlib",
"_heapq",
"_imp",
"_interpchannels",
"_interpqueues",
"_interpreters",
"_io",
"_ios_support",
"_json",
"_locale",
"_lsprof",
"_lzma",
"_markupbase",
"_md5",
"_multibytecodec",
"_multiprocessing",
"_opcode",
"_opcode_metadata",
"_operator",
"_osx_support",
"_overlapped",
"_pickle",
"_posixshmem",
"_posixsubprocess",
"_py_abc",
"_pydatetime",
"_pydecimal",
"_pyio",
"_pylong",
"_pyrepl",
"_queue",
"_random",
"_scproxy",
"_sha1",
"_sha2",
"_sha3",
"_signal",
"_sitebuiltins",
"_socket",
"_sqlite3",
"_sre",
"_ssl",
"_stat",
"_statistics",
"_string",
"_strptime",
"_struct",
"_suggestions",
"_symtable",
"_sysconfig",
"_thread",
"_threading_local",
"_tkinter",
"_tokenize",
"_tracemalloc",
"_typing",
"_uuid",
"_warnings",
"_weakref",
"_weakrefset",
"_winapi",
"_wmi",
"_zoneinfo",
"abc",
"antigravity",
"argparse",
"array",
"ast",
"asyncio",
"atexit",
"base64",
"bdb",
"binascii",
"bisect",
"builtins",
"bz2",
"cProfile",
"calendar",
"cmath",
"cmd",
"code",
"codecs",
"codeop",
"collections",
"colorsys",
"compileall",
"concurrent",
"configparser",
"contextlib",
"contextvars",
"copy",
"copyreg",
"csv",
"ctypes",
"curses",
"dataclasses",
"datetime",
"dbm",
"decimal",
"difflib",
"dis",
"doctest",
"email",
"encodings",
"ensurepip",
"enum",
"errno",
"faulthandler",
"fcntl",
"filecmp",
"fileinput",
"fnmatch",
"fractions",
"ftplib",
"functools",
"gc",
"genericpath",
"getopt",
"getpass",
"gettext",
"glob",
"graphlib",
"grp",
"gzip",
"hashlib",
"heapq",
"hmac",
"html",
"http",
"idlelib",
"imaplib",
"importlib",
"inspect",
"io",
"ipaddress",
"itertools",
"json",
"keyword",
"linecache",
"locale",
"logging",
"lzma",
"mailbox",
"marshal",
"math",
"mimetypes",
"mmap",
"modulefinder",
"msvcrt",
"multiprocessing",
"netrc",
"nt",
"ntpath",
"nturl2path",
"numbers",
"opcode",
"operator",
"optparse",
"os",
"pathlib",
"pdb",
"pickle",
"pickletools",
"pkgutil",
"platform",
"plistlib",
"poplib",
"posix",
"posixpath",
"pprint",
"profile",
"pstats",
"pty",
"pwd",
"py_compile",
"pyclbr",
"pydoc",
"pydoc_data",
"pyexpat",
"queue",
"quopri",
"random",
"re",
"readline",
"reprlib",
"resource",
"rlcompleter",
"runpy",
"sched",
"secrets",
"select",
"selectors",
"shelve",
"shlex",
"shutil",
"signal",
"site",
"smtplib",
"socket",
"socketserver",
"sqlite3",
"sre_compile",
"sre_constants",
"sre_parse",
"ssl",
"stat",
"statistics",
"string",
"stringprep",
"struct",
"subprocess",
"symtable",
"sys",
"sysconfig",
"syslog",
"tabnanny",
"tarfile",
"tempfile",
"termios",
"textwrap",
"this",
"threading",
"time",
"timeit",
"tkinter",
"token",
"tokenize",
"tomllib",
"trace",
"traceback",
"tracemalloc",
"tty",
"turtle",
"turtledemo",
"types",
"typing",
"unicodedata",
"unittest",
"urllib",
"uuid",
"venv",
"warnings",
"wave",
"weakref",
"webbrowser",
"winreg",
"winsound",
"wsgiref",
"xml",
"xmlrpc",
"zipapp",
"zipfile",
"zipimport",
"zlib",
"zoneinfo",
];

#[pyattr(once)]
fn stdlib_module_names(vm: &VirtualMachine) -> PyObjectRef {
let names = STDLIB_MODULE_NAMES
.iter()
.map(|&n| vm.ctx.new_str(n).into());
PyFrozenSet::from_iter(vm, names)
.expect("Creating stdlib_module_names frozen set must succeed")
.to_pyobject(vm)
}

#[pyattr]
fn byteorder(vm: &VirtualMachine) -> PyStrRef {
// https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian
Expand Down
Loading