Skip to content

Commit f43c64b

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Move changes back to load_module_from_name()
1 parent 1974456 commit f43c64b

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
@@ -10,12 +10,9 @@
1010
from __future__ import annotations
1111

1212
import collections
13-
import io
14-
import logging
1513
import os
1614
import types
1715
import zipimport
18-
from contextlib import redirect_stderr, redirect_stdout
1916
from importlib.util import find_spec, module_from_spec
2017
from typing import TYPE_CHECKING, ClassVar
2118

@@ -42,9 +39,6 @@
4239
ZIP_IMPORT_EXTS = (".zip", ".egg", ".whl", ".pyz", ".pyzw")
4340

4441

45-
logger = logging.getLogger(__name__)
46-
47-
4842
def safe_repr(obj):
4943
try:
5044
return repr(obj)
@@ -179,29 +173,13 @@ def ast_from_module_name(self, modname, context_file=None):
179173
):
180174
return self._build_stub_module(modname)
181175
try:
182-
# Capture and log anything emitted during import to avoid
183-
# contaminating JSON reports in pylint
184-
with redirect_stderr(io.StringIO()) as stderr, redirect_stdout(
185-
io.StringIO()
186-
) as stdout:
187-
module = load_module_from_name(modname)
176+
module = load_module_from_name(modname)
188177
except Exception as e:
189178
raise AstroidImportError(
190179
"Loading {modname} failed with:\n{error}",
191180
modname=modname,
192181
path=found_spec.location,
193182
) from e
194-
stderr_value = stderr.getvalue()
195-
if stderr_value:
196-
logger.error(
197-
"Captured stderr from importing %s:\n%s", modname, stderr_value
198-
)
199-
stdout_value = stdout.getvalue()
200-
if stdout_value:
201-
logger.info(
202-
"Captured stdout from importing %s:\n%s", modname, stdout_value
203-
)
204-
205183
return self.ast_from_module(module, modname)
206184

207185
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
@@ -19,17 +19,23 @@
1919
import importlib
2020
import importlib.machinery
2121
import importlib.util
22+
import io
2223
import itertools
24+
import logging
2325
import os
2426
import sys
2527
import sysconfig
2628
import types
29+
from contextlib import redirect_stderr, redirect_stdout
2730
from functools import lru_cache
2831
from pathlib import Path
2932

3033
from astroid.const import IS_JYTHON, IS_PYPY
3134
from astroid.interpreter._import import spec, util
3235

36+
logger = logging.getLogger(__name__)
37+
38+
3339
if sys.platform.startswith("win"):
3440
PY_SOURCE_EXTS = ("py", "pyw")
3541
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)