xref: /aosp_15_r20/external/pytorch/torch/package/_stdlib.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# mypy: allow-untyped-defs
2"""List of Python standard library modules.
3
4Sadly, there is no reliable way to tell whether a module is part of the
5standard library except by comparing to a canonical list.
6
7This is taken from https://github.com/PyCQA/isort/tree/develop/isort/stdlibs,
8which itself is sourced from the Python documentation.
9"""
10
11import sys
12
13
14def is_stdlib_module(module: str) -> bool:
15    base_module = module.partition(".")[0]
16    return base_module in _get_stdlib_modules()
17
18
19def _get_stdlib_modules():
20    if sys.version_info.major == 3:
21        if sys.version_info.minor == 8:
22            return stdlib3_8
23        if sys.version_info.minor == 9:
24            return stdlib3_9
25        if sys.version_info.minor >= 10:
26            return sys.stdlib_module_names  # type: ignore[attr-defined]
27    elif sys.version_info.major > 3:
28        return sys.stdlib_module_names  # type: ignore[attr-defined]
29
30    raise RuntimeError(f"Unsupported Python version: {sys.version_info}")
31
32
33stdlib3_8 = {
34    "_dummy_thread",
35    "_thread",
36    "abc",
37    "aifc",
38    "argparse",
39    "array",
40    "ast",
41    "asynchat",
42    "asyncio",
43    "asyncore",
44    "atexit",
45    "audioop",
46    "base64",
47    "bdb",
48    "binascii",
49    "binhex",
50    "bisect",
51    "builtins",
52    "bz2",
53    "cProfile",
54    "calendar",
55    "cgi",
56    "cgitb",
57    "chunk",
58    "cmath",
59    "cmd",
60    "code",
61    "codecs",
62    "codeop",
63    "collections",
64    "colorsys",
65    "compileall",
66    "concurrent",
67    "configparser",
68    "contextlib",
69    "contextvars",
70    "copy",
71    "copyreg",
72    "crypt",
73    "csv",
74    "ctypes",
75    "curses",
76    "dataclasses",
77    "datetime",
78    "dbm",
79    "decimal",
80    "difflib",
81    "dis",
82    "distutils",
83    "doctest",
84    "dummy_threading",
85    "email",
86    "encodings",
87    "ensurepip",
88    "enum",
89    "errno",
90    "faulthandler",
91    "fcntl",
92    "filecmp",
93    "fileinput",
94    "fnmatch",
95    "formatter",
96    "fractions",
97    "ftplib",
98    "functools",
99    "gc",
100    "getopt",
101    "getpass",
102    "gettext",
103    "glob",
104    "grp",
105    "gzip",
106    "hashlib",
107    "heapq",
108    "hmac",
109    "html",
110    "http",
111    "imaplib",
112    "imghdr",
113    "imp",
114    "importlib",
115    "inspect",
116    "io",
117    "ipaddress",
118    "itertools",
119    "json",
120    "keyword",
121    "lib2to3",
122    "linecache",
123    "locale",
124    "logging",
125    "lzma",
126    "mailbox",
127    "mailcap",
128    "marshal",
129    "math",
130    "mimetypes",
131    "mmap",
132    "modulefinder",
133    "msilib",
134    "msvcrt",
135    "multiprocessing",
136    "netrc",
137    "nis",
138    "nntplib",
139    "ntpath",
140    "numbers",
141    "operator",
142    "optparse",
143    "os",
144    "ossaudiodev",
145    "parser",
146    "pathlib",
147    "pdb",
148    "pickle",
149    "pickletools",
150    "pipes",
151    "pkgutil",
152    "platform",
153    "plistlib",
154    "poplib",
155    "posix",
156    "posixpath",
157    "pprint",
158    "profile",
159    "pstats",
160    "pty",
161    "pwd",
162    "py_compile",
163    "pyclbr",
164    "pydoc",
165    "queue",
166    "quopri",
167    "random",
168    "re",
169    "readline",
170    "reprlib",
171    "resource",
172    "rlcompleter",
173    "runpy",
174    "sched",
175    "secrets",
176    "select",
177    "selectors",
178    "shelve",
179    "shlex",
180    "shutil",
181    "signal",
182    "site",
183    "smtpd",
184    "smtplib",
185    "sndhdr",
186    "socket",
187    "socketserver",
188    "spwd",
189    "sqlite3",
190    "sre",
191    "sre_compile",
192    "sre_constants",
193    "sre_parse",
194    "ssl",
195    "stat",
196    "statistics",
197    "string",
198    "stringprep",
199    "struct",
200    "subprocess",
201    "sunau",
202    "symbol",
203    "symtable",
204    "sys",
205    "sysconfig",
206    "syslog",
207    "tabnanny",
208    "tarfile",
209    "telnetlib",
210    "tempfile",
211    "termios",
212    "test",
213    "textwrap",
214    "threading",
215    "time",
216    "timeit",
217    "tkinter",
218    "token",
219    "tokenize",
220    "trace",
221    "traceback",
222    "tracemalloc",
223    "tty",
224    "turtle",
225    "turtledemo",
226    "types",
227    "typing",
228    "unicodedata",
229    "unittest",
230    "urllib",
231    "uu",
232    "uuid",
233    "venv",
234    "warnings",
235    "wave",
236    "weakref",
237    "webbrowser",
238    "winreg",
239    "winsound",
240    "wsgiref",
241    "xdrlib",
242    "xml",
243    "xmlrpc",
244    "zipapp",
245    "zipfile",
246    "zipimport",
247    "zlib",
248}
249
250stdlib3_9 = {
251    "_thread",
252    "abc",
253    "aifc",
254    "argparse",
255    "array",
256    "ast",
257    "asynchat",
258    "asyncio",
259    "asyncore",
260    "atexit",
261    "audioop",
262    "base64",
263    "bdb",
264    "binascii",
265    "binhex",
266    "bisect",
267    "builtins",
268    "bz2",
269    "cProfile",
270    "calendar",
271    "cgi",
272    "cgitb",
273    "chunk",
274    "cmath",
275    "cmd",
276    "code",
277    "codecs",
278    "codeop",
279    "collections",
280    "colorsys",
281    "compileall",
282    "concurrent",
283    "configparser",
284    "contextlib",
285    "contextvars",
286    "copy",
287    "copyreg",
288    "crypt",
289    "csv",
290    "ctypes",
291    "curses",
292    "dataclasses",
293    "datetime",
294    "dbm",
295    "decimal",
296    "difflib",
297    "dis",
298    "distutils",
299    "doctest",
300    "email",
301    "encodings",
302    "ensurepip",
303    "enum",
304    "errno",
305    "faulthandler",
306    "fcntl",
307    "filecmp",
308    "fileinput",
309    "fnmatch",
310    "formatter",
311    "fractions",
312    "ftplib",
313    "functools",
314    "gc",
315    "getopt",
316    "getpass",
317    "gettext",
318    "glob",
319    "graphlib",
320    "grp",
321    "gzip",
322    "hashlib",
323    "heapq",
324    "hmac",
325    "html",
326    "http",
327    "imaplib",
328    "imghdr",
329    "imp",
330    "importlib",
331    "inspect",
332    "io",
333    "ipaddress",
334    "itertools",
335    "json",
336    "keyword",
337    "lib2to3",
338    "linecache",
339    "locale",
340    "logging",
341    "lzma",
342    "mailbox",
343    "mailcap",
344    "marshal",
345    "math",
346    "mimetypes",
347    "mmap",
348    "modulefinder",
349    "msilib",
350    "msvcrt",
351    "multiprocessing",
352    "netrc",
353    "nis",
354    "nntplib",
355    "ntpath",
356    "numbers",
357    "operator",
358    "optparse",
359    "os",
360    "ossaudiodev",
361    "parser",
362    "pathlib",
363    "pdb",
364    "pickle",
365    "pickletools",
366    "pipes",
367    "pkgutil",
368    "platform",
369    "plistlib",
370    "poplib",
371    "posix",
372    "posixpath",
373    "pprint",
374    "profile",
375    "pstats",
376    "pty",
377    "pwd",
378    "py_compile",
379    "pyclbr",
380    "pydoc",
381    "queue",
382    "quopri",
383    "random",
384    "re",
385    "readline",
386    "reprlib",
387    "resource",
388    "rlcompleter",
389    "runpy",
390    "sched",
391    "secrets",
392    "select",
393    "selectors",
394    "shelve",
395    "shlex",
396    "shutil",
397    "signal",
398    "site",
399    "smtpd",
400    "smtplib",
401    "sndhdr",
402    "socket",
403    "socketserver",
404    "spwd",
405    "sqlite3",
406    "sre",
407    "sre_compile",
408    "sre_constants",
409    "sre_parse",
410    "ssl",
411    "stat",
412    "statistics",
413    "string",
414    "stringprep",
415    "struct",
416    "subprocess",
417    "sunau",
418    "symbol",
419    "symtable",
420    "sys",
421    "sysconfig",
422    "syslog",
423    "tabnanny",
424    "tarfile",
425    "telnetlib",
426    "tempfile",
427    "termios",
428    "test",
429    "textwrap",
430    "threading",
431    "time",
432    "timeit",
433    "tkinter",
434    "token",
435    "tokenize",
436    "trace",
437    "traceback",
438    "tracemalloc",
439    "tty",
440    "turtle",
441    "turtledemo",
442    "types",
443    "typing",
444    "unicodedata",
445    "unittest",
446    "urllib",
447    "uu",
448    "uuid",
449    "venv",
450    "warnings",
451    "wave",
452    "weakref",
453    "webbrowser",
454    "winreg",
455    "winsound",
456    "wsgiref",
457    "xdrlib",
458    "xml",
459    "xmlrpc",
460    "zipapp",
461    "zipfile",
462    "zipimport",
463    "zlib",
464    "zoneinfo",
465}
466