Skip to content

Commit 5e29dc0

Browse files
Move changes back to load_module_from_name()
1 parent 8610725 commit 5e29dc0

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

astroid/manager.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
from various source and using a cache of built modules)
88
"""
99

10-
import io
11-
import logging
1210
import os
1311
import types
1412
import zipimport
15-
from contextlib import redirect_stderr, redirect_stdout
1613
from typing import TYPE_CHECKING, ClassVar, List, Optional
1714

1815
from astroid.exceptions import AstroidBuildingError, AstroidImportError
@@ -36,9 +33,6 @@
3633
ZIP_IMPORT_EXTS = (".zip", ".egg", ".whl", ".pyz", ".pyzw")
3734

3835

39-
logger = logging.getLogger(__name__)
40-
41-
4236
def safe_repr(obj):
4337
try:
4438
return repr(obj)
@@ -173,29 +167,13 @@ def ast_from_module_name(self, modname, context_file=None):
173167
):
174168
return self._build_stub_module(modname)
175169
try:
176-
# Capture and log anything emitted during import to avoid
177-
# contaminating JSON reports in pylint
178-
with redirect_stderr(io.StringIO()) as stderr, redirect_stdout(
179-
io.StringIO()
180-
) as stdout:
181-
module = load_module_from_name(modname)
170+
module = load_module_from_name(modname)
182171
except Exception as e:
183172
raise AstroidImportError(
184173
"Loading {modname} failed with:\n{error}",
185174
modname=modname,
186175
path=found_spec.location,
187176
) from e
188-
stderr_value = stderr.getvalue()
189-
if stderr_value:
190-
logger.error(
191-
"Captured stderr from importing %s:\n%s", modname, stderr_value
192-
)
193-
stdout_value = stdout.getvalue()
194-
if stdout_value:
195-
logger.info(
196-
"Captured stdout from importing %s:\n%s", modname, stdout_value
197-
)
198-
199177
return self.ast_from_module(module, modname)
200178

201179
elif found_spec.type == spec.ModuleType.PY_COMPILED:

astroid/modutils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
import importlib
1818
import importlib.machinery
1919
import importlib.util
20+
import io
2021
import itertools
22+
import logging
2123
import os
2224
import sys
2325
import sysconfig
2426
import types
27+
from contextlib import redirect_stderr, redirect_stdout
2528
from pathlib import Path
2629
from typing import Dict, Set
2730

2831
from astroid.const import IS_JYTHON, IS_PYPY
2932
from astroid.interpreter._import import spec, util
3033

34+
logger = logging.getLogger(__name__)
35+
36+
3137
if sys.platform.startswith("win"):
3238
PY_SOURCE_EXTS = ("py", "pyw")
3339
PY_COMPILED_EXTS = ("dll", "pyd")
@@ -171,7 +177,23 @@ def load_module_from_name(dotted_name: str) -> types.ModuleType:
171177
except KeyError:
172178
pass
173179

174-
return importlib.import_module(dotted_name)
180+
# Capture and log anything emitted during import to avoid
181+
# contaminating JSON reports in pylint
182+
with redirect_stderr(io.StringIO()) as stderr, redirect_stdout(
183+
io.StringIO()
184+
) as stdout:
185+
module = importlib.import_module(dotted_name)
186+
187+
stderr_value = stderr.getvalue()
188+
if stderr_value:
189+
logger.error(
190+
"Captured stderr from importing %s:\n%s", dotted_name, stderr_value
191+
)
192+
stdout_value = stdout.getvalue()
193+
if stdout_value:
194+
logger.info("Captured stdout from importing %s:\n%s", dotted_name, stdout_value)
195+
196+
return module
175197

176198

177199
def load_module_from_modpath(parts):

0 commit comments

Comments
 (0)