Skip to content

Commit 55afe5d

Browse files
authored
Replace and deprecate modutils.is_standard_module() (#2015)
1 parent 3540bae commit 55afe5d

File tree

6 files changed

+541
-28
lines changed

6 files changed

+541
-28
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Release date: TBA
1818

1919
Closes #1680
2020

21+
* Deprecate ``modutils.is_standard_module()``. It will be removed in the next minor release.
22+
Functionality has been replaced by two new functions,
23+
``modutils.is_stdlib_module()`` and ``modutils.module_in_path()``.
24+
25+
Closes #2012
26+
2127

2228

2329
What's New in astroid 2.14.2?

astroid/_backport_stdlib_names.py

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2+
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
3+
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4+
5+
"""
6+
Shim to support Python versions < 3.10 that don't have sys.stdlib_module_names
7+
8+
These values were created by cherry-picking the commits from
9+
https://bugs.python.org/issue42955 into each version, but may be updated
10+
manually if changes are needed.
11+
"""
12+
13+
import sys
14+
15+
# TODO: Remove this file when Python 3.9 is no longer supported
16+
17+
PY_3_7 = frozenset(
18+
{
19+
"__future__",
20+
"_abc",
21+
"_ast",
22+
"_asyncio",
23+
"_bisect",
24+
"_blake2",
25+
"_bootlocale",
26+
"_bz2",
27+
"_codecs",
28+
"_codecs_cn",
29+
"_codecs_hk",
30+
"_codecs_iso2022",
31+
"_codecs_jp",
32+
"_codecs_kr",
33+
"_codecs_tw",
34+
"_collections",
35+
"_collections_abc",
36+
"_compat_pickle",
37+
"_compression",
38+
"_contextvars",
39+
"_crypt",
40+
"_csv",
41+
"_ctypes",
42+
"_curses",
43+
"_curses_panel",
44+
"_datetime",
45+
"_dbm",
46+
"_decimal",
47+
"_dummy_thread",
48+
"_elementtree",
49+
"_functools",
50+
"_gdbm",
51+
"_hashlib",
52+
"_heapq",
53+
"_imp",
54+
"_io",
55+
"_json",
56+
"_locale",
57+
"_lsprof",
58+
"_lzma",
59+
"_markupbase",
60+
"_md5",
61+
"_msi",
62+
"_multibytecodec",
63+
"_multiprocessing",
64+
"_opcode",
65+
"_operator",
66+
"_osx_support",
67+
"_pickle",
68+
"_posixsubprocess",
69+
"_py_abc",
70+
"_pydecimal",
71+
"_pyio",
72+
"_queue",
73+
"_random",
74+
"_sha1",
75+
"_sha256",
76+
"_sha3",
77+
"_sha512",
78+
"_signal",
79+
"_sitebuiltins",
80+
"_socket",
81+
"_sqlite3",
82+
"_sre",
83+
"_ssl",
84+
"_stat",
85+
"_string",
86+
"_strptime",
87+
"_struct",
88+
"_symtable",
89+
"_thread",
90+
"_threading_local",
91+
"_tkinter",
92+
"_tracemalloc",
93+
"_uuid",
94+
"_warnings",
95+
"_weakref",
96+
"_weakrefset",
97+
"_winapi",
98+
"abc",
99+
"aifc",
100+
"antigravity",
101+
"argparse",
102+
"array",
103+
"ast",
104+
"asynchat",
105+
"asyncio",
106+
"asyncore",
107+
"atexit",
108+
"audioop",
109+
"base64",
110+
"bdb",
111+
"binascii",
112+
"binhex",
113+
"bisect",
114+
"builtins",
115+
"bz2",
116+
"cProfile",
117+
"calendar",
118+
"cgi",
119+
"cgitb",
120+
"chunk",
121+
"cmath",
122+
"cmd",
123+
"code",
124+
"codecs",
125+
"codeop",
126+
"collections",
127+
"colorsys",
128+
"compileall",
129+
"concurrent",
130+
"configparser",
131+
"contextlib",
132+
"contextvars",
133+
"copy",
134+
"copyreg",
135+
"crypt",
136+
"csv",
137+
"ctypes",
138+
"curses",
139+
"dataclasses",
140+
"datetime",
141+
"dbm",
142+
"decimal",
143+
"difflib",
144+
"dis",
145+
"distutils",
146+
"doctest",
147+
"dummy_threading",
148+
"email",
149+
"encodings",
150+
"ensurepip",
151+
"enum",
152+
"errno",
153+
"faulthandler",
154+
"fcntl",
155+
"filecmp",
156+
"fileinput",
157+
"fnmatch",
158+
"formatter",
159+
"fractions",
160+
"ftplib",
161+
"functools",
162+
"gc",
163+
"genericpath",
164+
"getopt",
165+
"getpass",
166+
"gettext",
167+
"glob",
168+
"grp",
169+
"gzip",
170+
"hashlib",
171+
"heapq",
172+
"hmac",
173+
"html",
174+
"http",
175+
"idlelib",
176+
"imaplib",
177+
"imghdr",
178+
"imp",
179+
"importlib",
180+
"inspect",
181+
"io",
182+
"ipaddress",
183+
"itertools",
184+
"json",
185+
"keyword",
186+
"lib2to3",
187+
"linecache",
188+
"locale",
189+
"logging",
190+
"lzma",
191+
"macpath",
192+
"mailbox",
193+
"mailcap",
194+
"marshal",
195+
"math",
196+
"mimetypes",
197+
"mmap",
198+
"modulefinder",
199+
"msilib",
200+
"msvcrt",
201+
"multiprocessing",
202+
"netrc",
203+
"nis",
204+
"nntplib",
205+
"nt",
206+
"ntpath",
207+
"nturl2path",
208+
"numbers",
209+
"opcode",
210+
"operator",
211+
"optparse",
212+
"os",
213+
"ossaudiodev",
214+
"parser",
215+
"pathlib",
216+
"pdb",
217+
"pickle",
218+
"pickletools",
219+
"pipes",
220+
"pkgutil",
221+
"platform",
222+
"plistlib",
223+
"poplib",
224+
"posix",
225+
"posixpath",
226+
"pprint",
227+
"profile",
228+
"pstats",
229+
"pty",
230+
"pwd",
231+
"py_compile",
232+
"pyclbr",
233+
"pydoc",
234+
"pydoc_data",
235+
"pyexpat",
236+
"queue",
237+
"quopri",
238+
"random",
239+
"re",
240+
"readline",
241+
"reprlib",
242+
"resource",
243+
"rlcompleter",
244+
"runpy",
245+
"sched",
246+
"secrets",
247+
"select",
248+
"selectors",
249+
"shelve",
250+
"shlex",
251+
"shutil",
252+
"signal",
253+
"site",
254+
"smtpd",
255+
"smtplib",
256+
"sndhdr",
257+
"socket",
258+
"socketserver",
259+
"spwd",
260+
"sqlite3",
261+
"sre_compile",
262+
"sre_constants",
263+
"sre_parse",
264+
"ssl",
265+
"stat",
266+
"statistics",
267+
"string",
268+
"stringprep",
269+
"struct",
270+
"subprocess",
271+
"sunau",
272+
"symbol",
273+
"symtable",
274+
"sys",
275+
"sysconfig",
276+
"syslog",
277+
"tabnanny",
278+
"tarfile",
279+
"telnetlib",
280+
"tempfile",
281+
"termios",
282+
"textwrap",
283+
"this",
284+
"threading",
285+
"time",
286+
"timeit",
287+
"tkinter",
288+
"token",
289+
"tokenize",
290+
"trace",
291+
"traceback",
292+
"tracemalloc",
293+
"tty",
294+
"turtle",
295+
"turtledemo",
296+
"types",
297+
"typing",
298+
"unicodedata",
299+
"unittest",
300+
"urllib",
301+
"uu",
302+
"uuid",
303+
"venv",
304+
"warnings",
305+
"wave",
306+
"weakref",
307+
"webbrowser",
308+
"winreg",
309+
"winsound",
310+
"wsgiref",
311+
"xdrlib",
312+
"xml",
313+
"xmlrpc",
314+
"zipapp",
315+
"zipfile",
316+
"zipimport",
317+
"zlib",
318+
}
319+
)
320+
321+
PY_3_8 = frozenset(
322+
PY_3_7
323+
- {
324+
"macpath",
325+
}
326+
| {
327+
"_posixshmem",
328+
"_statistics",
329+
"_xxsubinterpreters",
330+
}
331+
)
332+
333+
PY_3_9 = frozenset(
334+
PY_3_8
335+
- {
336+
"_dummy_thread",
337+
"dummy_threading",
338+
}
339+
| {
340+
"_aix_support",
341+
"_bootsubprocess",
342+
"_peg_parser",
343+
"_zoneinfo",
344+
"graphlib",
345+
"zoneinfo",
346+
}
347+
)
348+
349+
if sys.version_info[:2] == (3, 7):
350+
stdlib_module_names = PY_3_7
351+
elif sys.version_info[:2] == (3, 8):
352+
stdlib_module_names = PY_3_8
353+
elif sys.version_info[:2] == (3, 9):
354+
stdlib_module_names = PY_3_9
355+
else:
356+
raise AssertionError("This module is only intended as a backport for Python <= 3.9")

astroid/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
get_source_file,
3131
is_module_name_part_of_extension_package_whitelist,
3232
is_python_source,
33-
is_standard_module,
33+
is_stdlib_module,
3434
load_module_from_name,
3535
modpath_from_file,
3636
)
@@ -154,7 +154,7 @@ def _build_namespace_module(
154154
def _can_load_extension(self, modname: str) -> bool:
155155
if self.always_load_extensions:
156156
return True
157-
if is_standard_module(modname):
157+
if is_stdlib_module(modname):
158158
return True
159159
return is_module_name_part_of_extension_package_whitelist(
160160
modname, self.extension_package_whitelist

0 commit comments

Comments
 (0)