diff options
author | Barry Warsaw <barry@python.org> | 2012-07-31 20:03:25 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2012-07-31 20:03:25 (GMT) |
commit | 9a5af1288deb62d975c044a45711cc179b5a2ad1 (patch) | |
tree | 767b3a8dc1a144e66cc1e476ddd50a0a8e5cfb92 /Lib | |
parent | dadebab42c87e29342de67501a6b280e547fb633 (diff) | |
parent | 233f6845b3f3498d800b429c4e8d84abcb5726b7 (diff) | |
download | cpython-9a5af1288deb62d975c044a45711cc179b5a2ad1.zip cpython-9a5af1288deb62d975c044a45711cc179b5a2ad1.tar.gz cpython-9a5af1288deb62d975c044a45711cc179b5a2ad1.tar.bz2 |
merge
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/idlelib/PyShell.py | 4 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap.py | 65 | ||||
-rw-r--r-- | Lib/test/test_faulthandler.py | 15 | ||||
-rw-r--r-- | Lib/test/test_import.py | 18 |
4 files changed, 49 insertions, 53 deletions
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py index e0b8c29..50d6182 100644 --- a/Lib/idlelib/PyShell.py +++ b/Lib/idlelib/PyShell.py @@ -248,8 +248,8 @@ class PyShellEditorWindow(EditorWindow): def ranges_to_linenumbers(self, ranges): lines = [] for index in range(0, len(ranges), 2): - lineno = int(float(ranges[index])) - end = int(float(ranges[index+1])) + lineno = int(float(ranges[index].string)) + end = int(float(ranges[index+1].string)) while lineno < end: lines.append(lineno) lineno += 1 diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 6a869f7..5980141 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -297,8 +297,20 @@ def _lock_unlock_module(name): else: lock.release() +# Frame stripping magic ############################################### -# Finder/loader utility code ################################################## +def _call_with_frames_removed(f, *args, **kwds): + """remove_importlib_frames in import.c will always remove sequences + of importlib frames that end with a call to this function + + Use it instead of a normal call in places where including the importlib + frames introduces unwanted noise into the traceback (e.g. when executing + module code) + """ + return f(*args, **kwds) + + +# Finder/loader utility code ############################################### """Magic word to reject .pyc files generated by other Python versions. It should change for each incompatible change to the bytecode. @@ -629,20 +641,13 @@ class BuiltinImporter: """Load a built-in module.""" is_reload = fullname in sys.modules try: - return cls._exec_module(fullname) + return _call_with_frames_removed(_imp.init_builtin, fullname) except: if not is_reload and fullname in sys.modules: del sys.modules[fullname] raise @classmethod - def _exec_module(cls, fullname): - """Helper for load_module, allowing to isolate easily (when - looking at a traceback) whether an error comes from executing - an imported module's code.""" - return _imp.init_builtin(fullname) - - @classmethod @_requires_builtin def get_code(cls, fullname): """Return None as built-in modules do not have code objects.""" @@ -687,7 +692,7 @@ class FrozenImporter: """Load a frozen module.""" is_reload = fullname in sys.modules try: - m = cls._exec_module(fullname) + m = _call_with_frames_removed(_imp.init_frozen, fullname) # Let our own module_repr() method produce a suitable repr. del m.__file__ return m @@ -714,13 +719,6 @@ class FrozenImporter: """Return if the frozen module is a package.""" return _imp.is_frozen_package(fullname) - @classmethod - def _exec_module(cls, fullname): - """Helper for load_module, allowing to isolate easily (when - looking at a traceback) whether an error comes from executing - an imported module's code.""" - return _imp.init_frozen(fullname) - class WindowsRegistryImporter: @@ -850,15 +848,9 @@ class _LoaderBasics: else: module.__package__ = module.__package__.rpartition('.')[0] module.__loader__ = self - self._exec_module(code_object, module.__dict__) + _call_with_frames_removed(exec, code_object, module.__dict__) return module - def _exec_module(self, code_object, module_dict): - """Helper for _load_module, allowing to isolate easily (when - looking at a traceback) whether an error comes from executing - an imported module's code.""" - exec(code_object, module_dict) - class SourceLoader(_LoaderBasics): @@ -956,8 +948,9 @@ class SourceLoader(_LoaderBasics): raise ImportError(msg.format(bytecode_path), name=fullname, path=bytecode_path) source_bytes = self.get_data(source_path) - code_object = compile(source_bytes, source_path, 'exec', - dont_inherit=True) + code_object = _call_with_frames_removed(compile, + source_bytes, source_path, 'exec', + dont_inherit=True) _verbose_message('code object from {}', source_path) if (not sys.dont_write_bytecode and bytecode_path is not None and source_mtime is not None): @@ -1093,7 +1086,8 @@ class ExtensionFileLoader: """Load an extension module.""" is_reload = fullname in sys.modules try: - module = self._exec_module(fullname, self.path) + module = _call_with_frames_removed(_imp.load_dynamic, + fullname, self.path) _verbose_message('extension module loaded from {!r}', self.path) return module except: @@ -1113,12 +1107,6 @@ class ExtensionFileLoader: """Return None as extension modules have no source code.""" return None - def _exec_module(self, fullname, path): - """Helper for load_module, allowing to isolate easily (when - looking at a traceback) whether an error comes from executing - an imported module's code.""" - return _imp.load_dynamic(fullname, path) - class _NamespacePath: """Represents a namespace package's path. It uses the module name @@ -1472,7 +1460,7 @@ def _find_and_load_unlocked(name, import_): parent = name.rpartition('.')[0] if parent: if parent not in sys.modules: - _recursive_import(import_, parent) + _call_with_frames_removed(import_, parent) # Crazy side-effects! if name in sys.modules: return sys.modules[name] @@ -1550,13 +1538,6 @@ def _gcd_import(name, package=None, level=0): _lock_unlock_module(name) return module -def _recursive_import(import_, name): - """Common exit point for recursive calls to the import machinery - - This simplifies the process of stripping importlib from tracebacks - """ - return import_(name) - def _handle_fromlist(module, fromlist, import_): """Figure out what __import__ should return. @@ -1575,7 +1556,7 @@ def _handle_fromlist(module, fromlist, import_): fromlist.extend(module.__all__) for x in fromlist: if not hasattr(module, x): - _recursive_import(import_, + _call_with_frames_removed(import_, '{}.{}'.format(module.__name__, x)) return module diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 1138f8f..8c12b21 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -7,6 +7,7 @@ import signal import subprocess import sys from test import support, script_helper +from test.script_helper import assert_python_ok import tempfile import unittest @@ -256,6 +257,20 @@ faulthandler._read_null() finally: sys.stderr = orig_stderr + def test_disabled_by_default(self): + # By default, the module should be disabled + code = "import faulthandler; print(faulthandler.is_enabled())" + rc, stdout, stderr = assert_python_ok("-c", code) + stdout = (stdout + stderr).strip() + self.assertEqual(stdout, b"False") + + def test_sys_xoptions(self): + # Test python -X faulthandler + code = "import faulthandler; print(faulthandler.is_enabled())" + rc, stdout, stderr = assert_python_ok("-X", "faulthandler", "-c", code) + stdout = (stdout + stderr).strip() + self.assertEqual(stdout, b"True") + def check_dump_traceback(self, filename): """ Explicitly call dump_traceback() function and check its output. diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 3e61577..2e58199 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -785,11 +785,13 @@ class ImportTracebackTests(unittest.TestCase): sys.path[:] = self.old_path rmtree(TESTFN) - def create_module(self, mod, contents): - with open(os.path.join(TESTFN, mod + ".py"), "w") as f: + def create_module(self, mod, contents, ext=".py"): + fname = os.path.join(TESTFN, mod + ext) + with open(fname, "w") as f: f.write(contents) self.addCleanup(unload, mod) importlib.invalidate_caches() + return fname def assert_traceback(self, tb, files): deduped_files = [] @@ -857,16 +859,14 @@ class ImportTracebackTests(unittest.TestCase): def _setup_broken_package(self, parent, child): pkg_name = "_parent_foo" - def cleanup(): - rmtree(pkg_name) - unload(pkg_name) - os.mkdir(pkg_name) - self.addCleanup(cleanup) + self.addCleanup(unload, pkg_name) + pkg_path = os.path.join(TESTFN, pkg_name) + os.mkdir(pkg_path) # Touch the __init__.py - init_path = os.path.join(pkg_name, '__init__.py') + init_path = os.path.join(pkg_path, '__init__.py') with open(init_path, 'w') as f: f.write(parent) - bar_path = os.path.join(pkg_name, 'bar.py') + bar_path = os.path.join(pkg_path, 'bar.py') with open(bar_path, 'w') as f: f.write(child) importlib.invalidate_caches() |