diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index e89e84bc081127..1e12420ab6c241 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -7,7 +7,7 @@ """ from abc import ABCMeta, abstractmethod -import sys +import _sys as sys # We use the low-level module during interpreter init. __all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator", "AsyncGenerator", diff --git a/Lib/_compat_pickle.py b/Lib/_compat_pickle.py index f68496ae639f5f..cb88615a9f0a63 100644 --- a/Lib/_compat_pickle.py +++ b/Lib/_compat_pickle.py @@ -57,7 +57,7 @@ NAME_MAPPING = { ('__builtin__', 'xrange'): ('builtins', 'range'), ('__builtin__', 'reduce'): ('functools', 'reduce'), - ('__builtin__', 'intern'): ('sys', 'intern'), + ('__builtin__', 'intern'): ('_sys', 'intern'), ('__builtin__', 'unichr'): ('builtins', 'chr'), ('__builtin__', 'unicode'): ('builtins', 'str'), ('__builtin__', 'long'): ('builtins', 'int'), diff --git a/Lib/_sitebuiltins.py b/Lib/_sitebuiltins.py index c29cf4bf8fe488..1ba697365023f2 100644 --- a/Lib/_sitebuiltins.py +++ b/Lib/_sitebuiltins.py @@ -8,7 +8,7 @@ # Note this means this module should also avoid keep things alive in its # globals. -import sys +import _sys as sys # We use the low-level module during interpreter init. class Quitter(object): def __init__(self, name, eof): diff --git a/Lib/codecs.py b/Lib/codecs.py index 44618cbd2c4a36..958e2c56fac4b7 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -8,7 +8,7 @@ """ import builtins -import sys +import _sys as sys # We use the low-level module during interpreter init. ### Registry and builtin stateless codec functions diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 50cf8141731183..55846174b1e4f8 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -25,7 +25,7 @@ from operator import itemgetter as _itemgetter, eq as _eq from keyword import iskeyword as _iskeyword -import sys as _sys +import _sys # We use the low-level module during interpreter init. import heapq as _heapq from _weakref import proxy as _proxy from itertools import repeat as _repeat, chain as _chain, starmap as _starmap diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 962cedab490eb2..e4f08915479267 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -1,6 +1,6 @@ """Utilities for with-statement contexts. See PEP 343.""" import abc -import sys +import _sys as sys import _collections_abc from collections import deque from functools import wraps diff --git a/Lib/doctest.py b/Lib/doctest.py index 5e5bc21a038670..d3d861b6adf936 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -209,7 +209,7 @@ def _normalize_module(module, depth=2): elif module is None: return sys.modules[sys._getframe(depth).f_globals['__name__']] else: - raise TypeError("Expected a module, string, or None") + raise TypeError("Expected a module, string, or None, got {}".format(type(module))) def _load_testfile(filename, package, module_relative, encoding): if module_relative: diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index aa2fb7c2b93d83..48162c4671437c 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -29,7 +29,7 @@ """#" import codecs -import sys +import _sys as sys from . import aliases _cache = {} diff --git a/Lib/enum.py b/Lib/enum.py index 6d90f7dd65f06e..be2db7229db23f 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1,4 +1,4 @@ -import sys +import _sys as sys from types import MappingProxyType, DynamicClassAttribute from functools import reduce from operator import or_ as _or_ diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index cb37d246a7071b..f8334aab203d47 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -10,7 +10,7 @@ # of a fully initialised version (either the frozen one or the one # initialised below if the frozen one is not available). import _imp # Just the builtin component, NOT the full Python module -import sys +import _sys as sys try: import _frozen_importlib as _bootstrap diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 32354042f6f233..3348a362dfd794 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -968,8 +968,8 @@ def _find_parent_path_names(self): """Returns a tuple of (parent-module-name, parent-path-attr-name)""" parent, dot, me = self._name.rpartition('.') if dot == '': - # This is a top-level module. sys.path contains the parent path. - return 'sys', 'path' + # This is a top-level module. _sys.path contains the parent path. + return '_sys', 'path' # Not a top-level module. parent-module.__path__ contains the # parent path. return parent, '__path__' diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 41c74d4cc6c2a6..5be4c90fc91c81 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -12,7 +12,7 @@ from contextlib import contextmanager import functools -import sys +import _sys as sys import types import warnings diff --git a/Lib/locale.py b/Lib/locale.py index 569fe854dbb69c..35bdd69bdc2eed 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -10,7 +10,7 @@ """ -import sys +import _sys as sys import encodings import encodings.aliases import re diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 10d3f2dc35ba88..46a029d6d48033 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -6,7 +6,7 @@ """ import os -import sys +import _sys as sys # We use the low-level module during interpreter init. import stat import genericpath from genericpath import * diff --git a/Lib/optparse.py b/Lib/optparse.py index e8ac1e156a2b29..22409826613db2 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -73,7 +73,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -import sys, os +import os +import _sys as sys import textwrap def _repr(self): diff --git a/Lib/os.py b/Lib/os.py index 807ddb56c06558..2accbf7fc22fb5 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -23,7 +23,8 @@ #' import abc -import sys, errno +import errno +import _sys as sys # We use the low-level module during interpreter init. import stat as st _names = sys.builtin_module_names diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 9180eaed84dfd6..28189f230bc114 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -7,7 +7,7 @@ import importlib.machinery import os import os.path -import sys +import _sys as sys from types import ModuleType import warnings diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 6dbdab27497f2b..3130965dc67e19 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -11,7 +11,7 @@ """ import os -import sys +import _sys as sys # We use the low-level module during interpreter init. import stat import genericpath from genericpath import * diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 5edc77005a8714..87d81fccc2eb8d 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -410,7 +410,7 @@ def getdocloc(self, object, basedir = os.path.normcase(basedir) if (isinstance(object, type(os)) and (object.__name__ in ('errno', 'exceptions', 'gc', 'imp', - 'marshal', 'posix', 'signal', 'sys', + 'marshal', 'posix', 'signal', '_sys', '_thread', 'zipimport') or (file.startswith(basedir) and not file.startswith(os.path.join(basedir, 'site-packages')))) and diff --git a/Lib/runpy.py b/Lib/runpy.py index d86f0e4a3c49ba..82cec274dfad51 100644 --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -10,7 +10,7 @@ # to implement PEP 338 (Executing Modules as Scripts) -import sys +import _sys as sys # the low-level module import importlib.machinery # importlib first so we can test #15386 via -m import importlib.util import types diff --git a/Lib/site.py b/Lib/site.py index 7dc1b041c192c8..78b5bc319ea6c7 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -69,7 +69,7 @@ ImportError exception, it is silently ignored. """ -import sys +import _sys as sys # We use the low-level module during interpreter init. import os import builtins import _sitebuiltins diff --git a/Lib/sys.py b/Lib/sys.py new file mode 100644 index 00000000000000..996a547f64c79b --- /dev/null +++ b/Lib/sys.py @@ -0,0 +1,51 @@ +"""This module provides access to some objects used or maintained by the +interpreter and to functions that interact strongly with the interpreter. + +This is a wrapper around the low-level _sys module. +""" + +import _sys + + +MODULE_ATTRS = [ + "__name__", + "__doc__", + "__loader__", + "__spec__", + "__package__", + # not in _sys: + "__file__", + "__cached__", + "__builtins__", + ] + + +# For now we strictly proxy _sys. + +class SysModule(type(_sys)): + + # XXX What about cases of "moduletype = type(sys)"? + + def __init__(self, name, *, _ns=None): + super().__init__(name) + + if _ns is not None: + for attr in MODULE_ATTRS: + object.__setattr__(self, name, _ns[attr]) + + def __dir__(self): + return sorted(dir(_sys) + MODULE_ATTRS) + + def __getattr__(self, name): + return getattr(_sys, name) + + def __setattr__(self, name, value): + # XXX Only wrap existing attrs? + setattr(_sys, name, value) + + def __delattr__(self, name): + # XXX Only wrap existing attrs? + delattr(_sys, name) + + +_sys.modules[__name__] = SysModule(__name__, _ns=vars()) diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 8dfe1a714d5e62..d214c9ce2da3d2 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -1,7 +1,7 @@ """Access to Python's configuration information.""" import os -import sys +import _sys as sys # We use the low-level module during interpreter init. from os.path import pardir, realpath __all__ = [ diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index f57b25177920aa..ee23c99726ccc7 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -22,7 +22,7 @@ import stat import struct import subprocess -import sys +import _sys as sys import sysconfig import tempfile import _thread diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py index b3ac848f08252b..5ce3c56af7bcd5 100644 --- a/Lib/test/support/script_helper.py +++ b/Lib/test/support/script_helper.py @@ -3,7 +3,7 @@ import collections import importlib -import sys +import _sys as sys import os import os.path import subprocess diff --git a/Lib/test/test__sys.py b/Lib/test/test__sys.py new file mode 100644 index 00000000000000..d41d20879ae66b --- /dev/null +++ b/Lib/test/test__sys.py @@ -0,0 +1,1239 @@ +import unittest, test.support +from test.support.script_helper import assert_python_ok, assert_python_failure +import io, os +import struct +import subprocess +import _sys as sys +import textwrap +import warnings +import operator +import codecs +import gc +import sysconfig +import locale + +# count the number of test runs, used to create unique +# strings to intern in test_intern() +numruns = 0 + +try: + import threading +except ImportError: + threading = None + +class SysModuleTest(unittest.TestCase): + + def setUp(self): + self.orig_stdout = sys.stdout + self.orig_stderr = sys.stderr + self.orig_displayhook = sys.displayhook + + def tearDown(self): + sys.stdout = self.orig_stdout + sys.stderr = self.orig_stderr + sys.displayhook = self.orig_displayhook + test.support.reap_children() + + def test_original_displayhook(self): + import builtins + out = io.StringIO() + sys.stdout = out + + dh = sys.__displayhook__ + + self.assertRaises(TypeError, dh) + if hasattr(builtins, "_"): + del builtins._ + + dh(None) + self.assertEqual(out.getvalue(), "") + self.assertTrue(not hasattr(builtins, "_")) + dh(42) + self.assertEqual(out.getvalue(), "42\n") + self.assertEqual(builtins._, 42) + + del sys.stdout + self.assertRaises(RuntimeError, dh, 42) + + def test_lost_displayhook(self): + del sys.displayhook + code = compile("42", "", "single") + self.assertRaises(RuntimeError, eval, code) + + def test_custom_displayhook(self): + def baddisplayhook(obj): + raise ValueError + sys.displayhook = baddisplayhook + code = compile("42", "", "single") + self.assertRaises(ValueError, eval, code) + + def test_original_excepthook(self): + err = io.StringIO() + sys.stderr = err + + eh = sys.__excepthook__ + + self.assertRaises(TypeError, eh) + try: + raise ValueError(42) + except ValueError as exc: + eh(*sys.exc_info()) + + self.assertTrue(err.getvalue().endswith("ValueError: 42\n")) + + def test_excepthook(self): + with test.support.captured_output("stderr") as stderr: + sys.excepthook(1, '1', 1) + self.assertTrue("TypeError: print_exception(): Exception expected for " \ + "value, str found" in stderr.getvalue()) + + # FIXME: testing the code for a lost or replaced excepthook in + # Python/pythonrun.c::PyErr_PrintEx() is tricky. + + def test_exit(self): + # call with two arguments + self.assertRaises(TypeError, sys.exit, 42, 42) + + # call without argument + with self.assertRaises(SystemExit) as cm: + sys.exit() + self.assertIsNone(cm.exception.code) + + rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()') + self.assertEqual(rc, 0) + self.assertEqual(out, b'') + self.assertEqual(err, b'') + + # call with integer argument + with self.assertRaises(SystemExit) as cm: + sys.exit(42) + self.assertEqual(cm.exception.code, 42) + + # call with tuple argument with one entry + # entry will be unpacked + with self.assertRaises(SystemExit) as cm: + sys.exit((42,)) + self.assertEqual(cm.exception.code, 42) + + # call with string argument + with self.assertRaises(SystemExit) as cm: + sys.exit("exit") + self.assertEqual(cm.exception.code, "exit") + + # call with tuple argument with two entries + with self.assertRaises(SystemExit) as cm: + sys.exit((17, 23)) + self.assertEqual(cm.exception.code, (17, 23)) + + # test that the exit machinery handles SystemExits properly + rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)') + self.assertEqual(rc, 47) + self.assertEqual(out, b'') + self.assertEqual(err, b'') + + def check_exit_message(code, expected, **env_vars): + rc, out, err = assert_python_failure('-c', code, **env_vars) + self.assertEqual(rc, 1) + self.assertEqual(out, b'') + self.assertTrue(err.startswith(expected), + "%s doesn't start with %s" % (ascii(err), ascii(expected))) + + # test that stderr buffer is flushed before the exit message is written + # into stderr + check_exit_message( + r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', + b"unflushed,message") + + # test that the exit message is written with backslashreplace error + # handler to stderr + check_exit_message( + r'import sys; sys.exit("surrogates:\uDCFF")', + b"surrogates:\\udcff") + + # test that the unicode message is encoded to the stderr encoding + # instead of the default encoding (utf8) + check_exit_message( + r'import sys; sys.exit("h\xe9")', + b"h\xe9", PYTHONIOENCODING='latin-1') + + def test_getdefaultencoding(self): + self.assertRaises(TypeError, sys.getdefaultencoding, 42) + # can't check more than the type, as the user might have changed it + self.assertIsInstance(sys.getdefaultencoding(), str) + + # testing sys.settrace() is done in test_sys_settrace.py + # testing sys.setprofile() is done in test_sys_setprofile.py + + def test_setcheckinterval(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + self.assertRaises(TypeError, sys.setcheckinterval) + orig = sys.getcheckinterval() + for n in 0, 100, 120, orig: # orig last to restore starting state + sys.setcheckinterval(n) + self.assertEqual(sys.getcheckinterval(), n) + + @unittest.skipUnless(threading, 'Threading required for this test.') + def test_switchinterval(self): + self.assertRaises(TypeError, sys.setswitchinterval) + self.assertRaises(TypeError, sys.setswitchinterval, "a") + self.assertRaises(ValueError, sys.setswitchinterval, -1.0) + self.assertRaises(ValueError, sys.setswitchinterval, 0.0) + orig = sys.getswitchinterval() + # sanity check + self.assertTrue(orig < 0.5, orig) + try: + for n in 0.00001, 0.05, 3.0, orig: + sys.setswitchinterval(n) + self.assertAlmostEqual(sys.getswitchinterval(), n) + finally: + sys.setswitchinterval(orig) + + def test_recursionlimit(self): + self.assertRaises(TypeError, sys.getrecursionlimit, 42) + oldlimit = sys.getrecursionlimit() + self.assertRaises(TypeError, sys.setrecursionlimit) + self.assertRaises(ValueError, sys.setrecursionlimit, -42) + sys.setrecursionlimit(10000) + self.assertEqual(sys.getrecursionlimit(), 10000) + sys.setrecursionlimit(oldlimit) + + def test_recursionlimit_recovery(self): + if hasattr(sys, 'gettrace') and sys.gettrace(): + self.skipTest('fatal error if run with a trace function') + + oldlimit = sys.getrecursionlimit() + def f(): + f() + try: + for depth in (10, 25, 50, 75, 100, 250, 1000): + try: + sys.setrecursionlimit(depth) + except RecursionError: + # Issue #25274: The recursion limit is too low at the + # current recursion depth + continue + + # Issue #5392: test stack overflow after hitting recursion + # limit twice + self.assertRaises(RecursionError, f) + self.assertRaises(RecursionError, f) + finally: + sys.setrecursionlimit(oldlimit) + + @test.support.cpython_only + def test_setrecursionlimit_recursion_depth(self): + # Issue #25274: Setting a low recursion limit must be blocked if the + # current recursion depth is already higher than the "lower-water + # mark". Otherwise, it may not be possible anymore to + # reset the overflowed flag to 0. + + from _testcapi import get_recursion_depth + + def set_recursion_limit_at_depth(depth, limit): + recursion_depth = get_recursion_depth() + if recursion_depth >= depth: + with self.assertRaises(RecursionError) as cm: + sys.setrecursionlimit(limit) + self.assertRegex(str(cm.exception), + "cannot set the recursion limit to [0-9]+ " + "at the recursion depth [0-9]+: " + "the limit is too low") + else: + set_recursion_limit_at_depth(depth, limit) + + oldlimit = sys.getrecursionlimit() + try: + sys.setrecursionlimit(1000) + + for limit in (10, 25, 50, 75, 100, 150, 200): + # formula extracted from _Py_RecursionLimitLowerWaterMark() + if limit > 200: + depth = limit - 50 + else: + depth = limit * 3 // 4 + set_recursion_limit_at_depth(depth, limit) + finally: + sys.setrecursionlimit(oldlimit) + + def test_recursionlimit_fatalerror(self): + # A fatal error occurs if a second recursion limit is hit when recovering + # from a first one. + code = textwrap.dedent(""" + import sys + + def f(): + try: + f() + except RecursionError: + f() + + sys.setrecursionlimit(%d) + f()""") + with test.support.SuppressCrashReport(): + for i in (50, 1000): + sub = subprocess.Popen([sys.executable, '-c', code % i], + stderr=subprocess.PIPE) + err = sub.communicate()[1] + self.assertTrue(sub.returncode, sub.returncode) + self.assertIn( + b"Fatal Python error: Cannot recover from stack overflow", + err) + + def test_getwindowsversion(self): + # Raise SkipTest if sys doesn't have getwindowsversion attribute + test.support.get_attribute(sys, "getwindowsversion") + v = sys.getwindowsversion() + self.assertEqual(len(v), 5) + self.assertIsInstance(v[0], int) + self.assertIsInstance(v[1], int) + self.assertIsInstance(v[2], int) + self.assertIsInstance(v[3], int) + self.assertIsInstance(v[4], str) + self.assertRaises(IndexError, operator.getitem, v, 5) + self.assertIsInstance(v.major, int) + self.assertIsInstance(v.minor, int) + self.assertIsInstance(v.build, int) + self.assertIsInstance(v.platform, int) + self.assertIsInstance(v.service_pack, str) + self.assertIsInstance(v.service_pack_minor, int) + self.assertIsInstance(v.service_pack_major, int) + self.assertIsInstance(v.suite_mask, int) + self.assertIsInstance(v.product_type, int) + self.assertEqual(v[0], v.major) + self.assertEqual(v[1], v.minor) + self.assertEqual(v[2], v.build) + self.assertEqual(v[3], v.platform) + self.assertEqual(v[4], v.service_pack) + + # This is how platform.py calls it. Make sure tuple + # still has 5 elements + maj, min, buildno, plat, csd = sys.getwindowsversion() + + def test_call_tracing(self): + self.assertRaises(TypeError, sys.call_tracing, type, 2) + + @unittest.skipUnless(hasattr(sys, "setdlopenflags"), + 'test needs sys.setdlopenflags()') + def test_dlopenflags(self): + self.assertTrue(hasattr(sys, "getdlopenflags")) + self.assertRaises(TypeError, sys.getdlopenflags, 42) + oldflags = sys.getdlopenflags() + self.assertRaises(TypeError, sys.setdlopenflags) + sys.setdlopenflags(oldflags+1) + self.assertEqual(sys.getdlopenflags(), oldflags+1) + sys.setdlopenflags(oldflags) + + @test.support.refcount_test + def test_refcount(self): + # n here must be a global in order for this test to pass while + # tracing with a python function. Tracing calls PyFrame_FastToLocals + # which will add a copy of any locals to the frame object, causing + # the reference count to increase by 2 instead of 1. + global n + self.assertRaises(TypeError, sys.getrefcount) + c = sys.getrefcount(None) + n = None + self.assertEqual(sys.getrefcount(None), c+1) + del n + self.assertEqual(sys.getrefcount(None), c) + if hasattr(sys, "gettotalrefcount"): + self.assertIsInstance(sys.gettotalrefcount(), int) + + def test_getframe(self): + self.assertRaises(TypeError, sys._getframe, 42, 42) + self.assertRaises(ValueError, sys._getframe, 2000000000) + self.assertTrue( + SysModuleTest.test_getframe.__code__ \ + is sys._getframe().f_code + ) + + # sys._current_frames() is a CPython-only gimmick. + def test_current_frames(self): + have_threads = True + try: + import _thread + except ImportError: + have_threads = False + + if have_threads: + self.current_frames_with_threads() + else: + self.current_frames_without_threads() + + # Test sys._current_frames() in a WITH_THREADS build. + @test.support.reap_threads + def current_frames_with_threads(self): + import threading + import traceback + + # Spawn a thread that blocks at a known place. Then the main + # thread does sys._current_frames(), and verifies that the frames + # returned make sense. + entered_g = threading.Event() + leave_g = threading.Event() + thread_info = [] # the thread's id + + def f123(): + g456() + + def g456(): + thread_info.append(threading.get_ident()) + entered_g.set() + leave_g.wait() + + t = threading.Thread(target=f123) + t.start() + entered_g.wait() + + # At this point, t has finished its entered_g.set(), although it's + # impossible to guess whether it's still on that line or has moved on + # to its leave_g.wait(). + self.assertEqual(len(thread_info), 1) + thread_id = thread_info[0] + + d = sys._current_frames() + for tid in d: + self.assertIsInstance(tid, int) + self.assertGreater(tid, 0) + + main_id = threading.get_ident() + self.assertIn(main_id, d) + self.assertIn(thread_id, d) + + # Verify that the captured main-thread frame is _this_ frame. + frame = d.pop(main_id) + self.assertTrue(frame is sys._getframe()) + + # Verify that the captured thread frame is blocked in g456, called + # from f123. This is a litte tricky, since various bits of + # threading.py are also in the thread's call stack. + frame = d.pop(thread_id) + stack = traceback.extract_stack(frame) + for i, (filename, lineno, funcname, sourceline) in enumerate(stack): + if funcname == "f123": + break + else: + self.fail("didn't find f123() on thread's call stack") + + self.assertEqual(sourceline, "g456()") + + # And the next record must be for g456(). + filename, lineno, funcname, sourceline = stack[i+1] + self.assertEqual(funcname, "g456") + self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"]) + + # Reap the spawned thread. + leave_g.set() + t.join() + + # Test sys._current_frames() when thread support doesn't exist. + def current_frames_without_threads(self): + # Not much happens here: there is only one thread, with artificial + # "thread id" 0. + d = sys._current_frames() + self.assertEqual(len(d), 1) + self.assertIn(0, d) + self.assertTrue(d[0] is sys._getframe()) + + def test_attributes(self): + self.assertIsInstance(sys.api_version, int) + self.assertIsInstance(sys.argv, list) + self.assertIn(sys.byteorder, ("little", "big")) + self.assertIsInstance(sys.builtin_module_names, tuple) + self.assertIsInstance(sys.copyright, str) + self.assertIsInstance(sys.exec_prefix, str) + self.assertIsInstance(sys.base_exec_prefix, str) + self.assertIsInstance(sys.executable, str) + self.assertEqual(len(sys.float_info), 11) + self.assertEqual(sys.float_info.radix, 2) + self.assertEqual(len(sys.int_info), 2) + self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) + self.assertTrue(sys.int_info.sizeof_digit >= 1) + self.assertEqual(type(sys.int_info.bits_per_digit), int) + self.assertEqual(type(sys.int_info.sizeof_digit), int) + self.assertIsInstance(sys.hexversion, int) + + self.assertEqual(len(sys.hash_info), 9) + self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) + # sys.hash_info.modulus should be a prime; we do a quick + # probable primality test (doesn't exclude the possibility of + # a Carmichael number) + for x in range(1, 100): + self.assertEqual( + pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus), + 1, + "sys.hash_info.modulus {} is a non-prime".format( + sys.hash_info.modulus) + ) + self.assertIsInstance(sys.hash_info.inf, int) + self.assertIsInstance(sys.hash_info.nan, int) + self.assertIsInstance(sys.hash_info.imag, int) + algo = sysconfig.get_config_var("Py_HASH_ALGORITHM") + if sys.hash_info.algorithm in {"fnv", "siphash24"}: + self.assertIn(sys.hash_info.hash_bits, {32, 64}) + self.assertIn(sys.hash_info.seed_bits, {32, 64, 128}) + + if algo == 1: + self.assertEqual(sys.hash_info.algorithm, "siphash24") + elif algo == 2: + self.assertEqual(sys.hash_info.algorithm, "fnv") + else: + self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash24"}) + else: + # PY_HASH_EXTERNAL + self.assertEqual(algo, 0) + self.assertGreaterEqual(sys.hash_info.cutoff, 0) + self.assertLess(sys.hash_info.cutoff, 8) + + self.assertIsInstance(sys.maxsize, int) + self.assertIsInstance(sys.maxunicode, int) + self.assertEqual(sys.maxunicode, 0x10FFFF) + self.assertIsInstance(sys.platform, str) + self.assertIsInstance(sys.prefix, str) + self.assertIsInstance(sys.base_prefix, str) + self.assertIsInstance(sys.version, str) + vi = sys.version_info + self.assertIsInstance(vi[:], tuple) + self.assertEqual(len(vi), 5) + self.assertIsInstance(vi[0], int) + self.assertIsInstance(vi[1], int) + self.assertIsInstance(vi[2], int) + self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) + self.assertIsInstance(vi[4], int) + self.assertIsInstance(vi.major, int) + self.assertIsInstance(vi.minor, int) + self.assertIsInstance(vi.micro, int) + self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) + self.assertIsInstance(vi.serial, int) + self.assertEqual(vi[0], vi.major) + self.assertEqual(vi[1], vi.minor) + self.assertEqual(vi[2], vi.micro) + self.assertEqual(vi[3], vi.releaselevel) + self.assertEqual(vi[4], vi.serial) + self.assertTrue(vi > (1,0,0)) + self.assertIsInstance(sys.float_repr_style, str) + self.assertIn(sys.float_repr_style, ('short', 'legacy')) + if not sys.platform.startswith('win'): + self.assertIsInstance(sys.abiflags, str) + + @unittest.skipUnless(hasattr(sys, 'thread_info'), + 'Threading required for this test.') + def test_thread_info(self): + info = sys.thread_info + self.assertEqual(len(info), 3) + self.assertIn(info.name, ('nt', 'pthread', 'solaris', None)) + self.assertIn(info.lock, ('semaphore', 'mutex+cond', None)) + + def test_43581(self): + # Can't use sys.stdout, as this is a StringIO object when + # the test runs under regrtest. + self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) + + def test_intern(self): + global numruns + numruns += 1 + self.assertRaises(TypeError, sys.intern) + s = "never interned before" + str(numruns) + self.assertTrue(sys.intern(s) is s) + s2 = s.swapcase().swapcase() + self.assertTrue(sys.intern(s2) is s) + + # Subclasses of string can't be interned, because they + # provide too much opportunity for insane things to happen. + # We don't want them in the interned dict and if they aren't + # actually interned, we don't want to create the appearance + # that they are by allowing intern() to succeed. + class S(str): + def __hash__(self): + return 123 + + self.assertRaises(TypeError, sys.intern, S("abc")) + + def test_sys_flags(self): + self.assertTrue(sys.flags) + attrs = ("debug", + "inspect", "interactive", "optimize", "dont_write_bytecode", + "no_user_site", "no_site", "ignore_environment", "verbose", + "bytes_warning", "quiet", "hash_randomization", "isolated") + for attr in attrs: + self.assertTrue(hasattr(sys.flags, attr), attr) + self.assertEqual(type(getattr(sys.flags, attr)), int, attr) + self.assertTrue(repr(sys.flags)) + self.assertEqual(len(sys.flags), len(attrs)) + + def assert_raise_on_new_sys_type(self, sys_attr): + # Users are intentionally prevented from creating new instances of + # sys.flags, sys.version_info, and sys.getwindowsversion. + attr_type = type(sys_attr) + with self.assertRaises(TypeError): + attr_type() + with self.assertRaises(TypeError): + attr_type.__new__(attr_type) + + def test_sys_flags_no_instantiation(self): + self.assert_raise_on_new_sys_type(sys.flags) + + def test_sys_version_info_no_instantiation(self): + self.assert_raise_on_new_sys_type(sys.version_info) + + def test_sys_getwindowsversion_no_instantiation(self): + # Skip if not being run on Windows. + test.support.get_attribute(sys, "getwindowsversion") + self.assert_raise_on_new_sys_type(sys.getwindowsversion()) + + @test.support.cpython_only + def test_clear_type_cache(self): + sys._clear_type_cache() + + def test_ioencoding(self): + env = dict(os.environ) + + # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, + # not representable in ASCII. + + env["PYTHONIOENCODING"] = "cp424" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout = subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + expected = ("\xa2" + os.linesep).encode("cp424") + self.assertEqual(out, expected) + + env["PYTHONIOENCODING"] = "ascii:replace" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout = subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + self.assertEqual(out, b'?') + + env["PYTHONIOENCODING"] = "ascii" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env) + out, err = p.communicate() + self.assertEqual(out, b'') + self.assertIn(b'UnicodeEncodeError:', err) + self.assertIn(rb"'\xa2'", err) + + env["PYTHONIOENCODING"] = "ascii:" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env) + out, err = p.communicate() + self.assertEqual(out, b'') + self.assertIn(b'UnicodeEncodeError:', err) + self.assertIn(rb"'\xa2'", err) + + env["PYTHONIOENCODING"] = ":surrogateescape" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], + stdout=subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + self.assertEqual(out, b'\xbd') + + @unittest.skipUnless(test.support.FS_NONASCII, + 'requires OS support of non-ASCII encodings') + @unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False), + 'requires FS encoding to match locale') + def test_ioencoding_nonascii(self): + env = dict(os.environ) + + env["PYTHONIOENCODING"] = "" + p = subprocess.Popen([sys.executable, "-c", + 'print(%a)' % test.support.FS_NONASCII], + stdout=subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) + + @unittest.skipIf(sys.base_prefix != sys.prefix, + 'Test is not venv-compatible') + def test_executable(self): + # sys.executable should be absolute + self.assertEqual(os.path.abspath(sys.executable), sys.executable) + + # Issue #7774: Ensure that sys.executable is an empty string if argv[0] + # has been set to a non existent program name and Python is unable to + # retrieve the real program name + + # For a normal installation, it should work without 'cwd' + # argument. For test runs in the build directory, see #7774. + python_dir = os.path.dirname(os.path.realpath(sys.executable)) + p = subprocess.Popen( + ["nonexistent", "-c", + 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], + executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) + stdout = p.communicate()[0] + executable = stdout.strip().decode("ASCII") + p.wait() + self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) + + def check_fsencoding(self, fs_encoding, expected=None): + self.assertIsNotNone(fs_encoding) + codecs.lookup(fs_encoding) + if expected: + self.assertEqual(fs_encoding, expected) + + def test_getfilesystemencoding(self): + fs_encoding = sys.getfilesystemencoding() + if sys.platform == 'darwin': + expected = 'utf-8' + else: + expected = None + self.check_fsencoding(fs_encoding, expected) + + def c_locale_get_error_handler(self, isolated=False, encoding=None): + # Force the POSIX locale + env = os.environ.copy() + env["LC_ALL"] = "C" + env["PYTHONCOERCECLOCALE"] = "0" + code = '\n'.join(( + 'import sys', + 'def dump(name):', + ' std = getattr(sys, name)', + ' print("%s: %s" % (name, std.errors))', + 'dump("stdin")', + 'dump("stdout")', + 'dump("stderr")', + )) + args = [sys.executable, "-c", code] + if isolated: + args.append("-I") + if encoding is not None: + env['PYTHONIOENCODING'] = encoding + else: + env.pop('PYTHONIOENCODING', None) + p = subprocess.Popen(args, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + env=env, + universal_newlines=True) + stdout, stderr = p.communicate() + return stdout + + def test_c_locale_surrogateescape(self): + out = self.c_locale_get_error_handler(isolated=True) + self.assertEqual(out, + 'stdin: surrogateescape\n' + 'stdout: surrogateescape\n' + 'stderr: backslashreplace\n') + + # replace the default error handler + out = self.c_locale_get_error_handler(encoding=':ignore') + self.assertEqual(out, + 'stdin: ignore\n' + 'stdout: ignore\n' + 'stderr: backslashreplace\n') + + # force the encoding + out = self.c_locale_get_error_handler(encoding='iso8859-1') + self.assertEqual(out, + 'stdin: strict\n' + 'stdout: strict\n' + 'stderr: backslashreplace\n') + out = self.c_locale_get_error_handler(encoding='iso8859-1:') + self.assertEqual(out, + 'stdin: strict\n' + 'stdout: strict\n' + 'stderr: backslashreplace\n') + + # have no any effect + out = self.c_locale_get_error_handler(encoding=':') + self.assertEqual(out, + 'stdin: surrogateescape\n' + 'stdout: surrogateescape\n' + 'stderr: backslashreplace\n') + out = self.c_locale_get_error_handler(encoding='') + self.assertEqual(out, + 'stdin: surrogateescape\n' + 'stdout: surrogateescape\n' + 'stderr: backslashreplace\n') + + def test_implementation(self): + # This test applies to all implementations equally. + + levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} + + self.assertTrue(hasattr(sys.implementation, 'name')) + self.assertTrue(hasattr(sys.implementation, 'version')) + self.assertTrue(hasattr(sys.implementation, 'hexversion')) + self.assertTrue(hasattr(sys.implementation, 'cache_tag')) + + version = sys.implementation.version + self.assertEqual(version[:2], (version.major, version.minor)) + + hexversion = (version.major << 24 | version.minor << 16 | + version.micro << 8 | levels[version.releaselevel] << 4 | + version.serial << 0) + self.assertEqual(sys.implementation.hexversion, hexversion) + + # PEP 421 requires that .name be lower case. + self.assertEqual(sys.implementation.name, + sys.implementation.name.lower()) + + @test.support.cpython_only + def test_debugmallocstats(self): + # Test sys._debugmallocstats() + from test.support.script_helper import assert_python_ok + args = ['-c', 'import sys; sys._debugmallocstats()'] + ret, out, err = assert_python_ok(*args) + self.assertIn(b"free PyDictObjects", err) + + # The function has no parameter + self.assertRaises(TypeError, sys._debugmallocstats, True) + + @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), + "sys.getallocatedblocks unavailable on this build") + def test_getallocatedblocks(self): + # Some sanity checks + with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC') + a = sys.getallocatedblocks() + self.assertIs(type(a), int) + if with_pymalloc: + self.assertGreater(a, 0) + else: + # When WITH_PYMALLOC isn't available, we don't know anything + # about the underlying implementation: the function might + # return 0 or something greater. + self.assertGreaterEqual(a, 0) + try: + # While we could imagine a Python session where the number of + # multiple buffer objects would exceed the sharing of references, + # it is unlikely to happen in a normal test run. + self.assertLess(a, sys.gettotalrefcount()) + except AttributeError: + # gettotalrefcount() not available + pass + gc.collect() + b = sys.getallocatedblocks() + self.assertLessEqual(b, a) + gc.collect() + c = sys.getallocatedblocks() + self.assertIn(c, range(b - 50, b + 50)) + + @test.support.requires_type_collecting + def test_is_finalizing(self): + self.assertIs(sys.is_finalizing(), False) + # Don't use the atexit module because _Py_Finalizing is only set + # after calling atexit callbacks + code = """if 1: + import sys + + class AtExit: + is_finalizing = sys.is_finalizing + print = print + + def __del__(self): + self.print(self.is_finalizing(), flush=True) + + # Keep a reference in the __main__ module namespace, so the + # AtExit destructor will be called at Python exit + ref = AtExit() + """ + rc, stdout, stderr = assert_python_ok('-c', code) + self.assertEqual(stdout.rstrip(), b'True') + + @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'), + 'need sys.getandroidapilevel()') + def test_getandroidapilevel(self): + level = sys.getandroidapilevel() + self.assertIsInstance(level, int) + self.assertGreater(level, 0) + + +@test.support.cpython_only +class SizeofTest(unittest.TestCase): + + def setUp(self): + self.P = struct.calcsize('P') + self.longdigit = sys.int_info.sizeof_digit + import _testcapi + self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD + + check_sizeof = test.support.check_sizeof + + def test_gc_head_size(self): + # Check that the gc header size is added to objects tracked by the gc. + vsize = test.support.calcvobjsize + gc_header_size = self.gc_headsize + # bool objects are not gc tracked + self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit) + # but lists are + self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size) + + def test_errors(self): + class BadSizeof: + def __sizeof__(self): + raise ValueError + self.assertRaises(ValueError, sys.getsizeof, BadSizeof()) + + class InvalidSizeof: + def __sizeof__(self): + return None + self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof()) + sentinel = ["sentinel"] + self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel) + + class FloatSizeof: + def __sizeof__(self): + return 4.5 + self.assertRaises(TypeError, sys.getsizeof, FloatSizeof()) + self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel) + + class OverflowSizeof(int): + def __sizeof__(self): + return int(self) + self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)), + sys.maxsize + self.gc_headsize) + with self.assertRaises(OverflowError): + sys.getsizeof(OverflowSizeof(sys.maxsize + 1)) + with self.assertRaises(ValueError): + sys.getsizeof(OverflowSizeof(-1)) + with self.assertRaises((ValueError, OverflowError)): + sys.getsizeof(OverflowSizeof(-sys.maxsize - 1)) + + def test_default(self): + size = test.support.calcvobjsize + self.assertEqual(sys.getsizeof(True), size('') + self.longdigit) + self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit) + + def test_objecttypes(self): + # check all types defined in Objects/ + calcsize = struct.calcsize + size = test.support.calcobjsize + vsize = test.support.calcvobjsize + check = self.check_sizeof + # bool + check(True, vsize('') + self.longdigit) + # buffer + # XXX + # builtin_function_or_method + check(len, size('4P')) # XXX check layout + # bytearray + samples = [b'', b'u'*100000] + for sample in samples: + x = bytearray(sample) + check(x, vsize('n2Pi') + x.__alloc__()) + # bytearray_iterator + check(iter(bytearray()), size('nP')) + # bytes + check(b'', vsize('n') + 1) + check(b'x' * 10, vsize('n') + 11) + # cell + def get_cell(): + x = 42 + def inner(): + return x + return inner + check(get_cell().__closure__[0], size('P')) + # code + def check_code_size(a, expected_size): + self.assertGreaterEqual(sys.getsizeof(a), expected_size) + check_code_size(get_cell().__code__, size('6i13P')) + check_code_size(get_cell.__code__, size('6i13P')) + def get_cell2(x): + def inner(): + return x + return inner + check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n')) + # complex + check(complex(0,1), size('2d')) + # method_descriptor (descriptor object) + check(str.lower, size('3PP')) + # classmethod_descriptor (descriptor object) + # XXX + # member_descriptor (descriptor object) + import datetime + check(datetime.timedelta.days, size('3PP')) + # getset_descriptor (descriptor object) + import collections + check(collections.defaultdict.default_factory, size('3PP')) + # wrapper_descriptor (descriptor object) + check(int.__add__, size('3P2P')) + # method-wrapper (descriptor object) + check({}.__iter__, size('2P')) + # dict + check({}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P')) + longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} + check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P')) + # dictionary-keyview + check({}.keys(), size('P')) + # dictionary-valueview + check({}.values(), size('P')) + # dictionary-itemview + check({}.items(), size('P')) + # dictionary iterator + check(iter({}), size('P2nPn')) + # dictionary-keyiterator + check(iter({}.keys()), size('P2nPn')) + # dictionary-valueiterator + check(iter({}.values()), size('P2nPn')) + # dictionary-itemiterator + check(iter({}.items()), size('P2nPn')) + # dictproxy + class C(object): pass + check(C.__dict__, size('P')) + # BaseException + check(BaseException(), size('5Pb')) + # UnicodeEncodeError + check(UnicodeEncodeError("", "", 0, 0, ""), size('5Pb 2P2nP')) + # UnicodeDecodeError + check(UnicodeDecodeError("", b"", 0, 0, ""), size('5Pb 2P2nP')) + # UnicodeTranslateError + check(UnicodeTranslateError("", 0, 1, ""), size('5Pb 2P2nP')) + # ellipses + check(Ellipsis, size('')) + # EncodingMap + import codecs, encodings.iso8859_3 + x = codecs.charmap_build(encodings.iso8859_3.decoding_table) + check(x, size('32B2iB')) + # enumerate + check(enumerate([]), size('n3P')) + # reverse + check(reversed(''), size('nP')) + # float + check(float(0), size('d')) + # sys.floatinfo + check(sys.float_info, vsize('') + self.P * len(sys.float_info)) + # frame + import inspect + CO_MAXBLOCKS = 20 + x = inspect.currentframe() + ncells = len(x.f_code.co_cellvars) + nfrees = len(x.f_code.co_freevars) + extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ + ncells + nfrees - 1 + check(x, vsize('12P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) + # function + def func(): pass + check(func, size('12P')) + class c(): + @staticmethod + def foo(): + pass + @classmethod + def bar(cls): + pass + # staticmethod + check(foo, size('PP')) + # classmethod + check(bar, size('PP')) + # generator + def get_gen(): yield 1 + check(get_gen(), size('Pb2PPP')) + # iterator + check(iter('abc'), size('lP')) + # callable-iterator + import re + check(re.finditer('',''), size('2P')) + # list + samples = [[], [1,2,3], ['1', '2', '3']] + for sample in samples: + check(sample, vsize('Pn') + len(sample)*self.P) + # sortwrapper (list) + # XXX + # cmpwrapper (list) + # XXX + # listiterator (list) + check(iter([]), size('lP')) + # listreverseiterator (list) + check(reversed([]), size('nP')) + # int + check(0, vsize('')) + check(1, vsize('') + self.longdigit) + check(-1, vsize('') + self.longdigit) + PyLong_BASE = 2**sys.int_info.bits_per_digit + check(int(PyLong_BASE), vsize('') + 2*self.longdigit) + check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) + check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) + # module + check(unittest, size('PnPPP')) + # None + check(None, size('')) + # NotImplementedType + check(NotImplemented, size('')) + # object + check(object(), size('')) + # property (descriptor object) + class C(object): + def getx(self): return self.__x + def setx(self, value): self.__x = value + def delx(self): del self.__x + x = property(getx, setx, delx, "") + check(x, size('4Pi')) + # PyCapsule + # XXX + # rangeiterator + check(iter(range(1)), size('4l')) + # reverse + check(reversed(''), size('nP')) + # range + check(range(1), size('4P')) + check(range(66000), size('4P')) + # set + # frozenset + PySet_MINSIZE = 8 + samples = [[], range(10), range(50)] + s = size('3nP' + PySet_MINSIZE*'nP' + '2nP') + for sample in samples: + minused = len(sample) + if minused == 0: tmp = 1 + # the computation of minused is actually a bit more complicated + # but this suffices for the sizeof test + minused = minused*2 + newsize = PySet_MINSIZE + while newsize <= minused: + newsize = newsize << 1 + if newsize <= 8: + check(set(sample), s) + check(frozenset(sample), s) + else: + check(set(sample), s + newsize*calcsize('nP')) + check(frozenset(sample), s + newsize*calcsize('nP')) + # setiterator + check(iter(set()), size('P3n')) + # slice + check(slice(0), size('3P')) + # super + check(super(int), size('3P')) + # tuple + check((), vsize('')) + check((1,2,3), vsize('') + 3*self.P) + # type + # static type: PyTypeObject + fmt = 'P2n15Pl4Pn9Pn11PIP' + if hasattr(sys, 'getcounts'): + fmt += '3n2P' + s = vsize(fmt) + check(int, s) + s = vsize(fmt + # PyTypeObject + '3P' # PyAsyncMethods + '36P' # PyNumberMethods + '3P' # PyMappingMethods + '10P' # PySequenceMethods + '2P' # PyBufferProcs + '4P') + # Separate block for PyDictKeysObject with 8 keys and 5 entries + s += calcsize("2nP2n") + 8 + 5*calcsize("n2P") + # class + class newstyleclass(object): pass + check(newstyleclass, s) + # dict with shared keys + check(newstyleclass().__dict__, size('nQ2P' + '2nP2n')) + # unicode + # each tuple contains a string and its expected character size + # don't put any static strings here, as they may contain + # wchar_t or UTF-8 representations + samples = ['1'*100, '\xff'*50, + '\u0100'*40, '\uffff'*100, + '\U00010000'*30, '\U0010ffff'*100] + asciifields = "nnbP" + compactfields = asciifields + "nPn" + unicodefields = compactfields + "P" + for s in samples: + maxchar = ord(max(s)) + if maxchar < 128: + L = size(asciifields) + len(s) + 1 + elif maxchar < 256: + L = size(compactfields) + len(s) + 1 + elif maxchar < 65536: + L = size(compactfields) + 2*(len(s) + 1) + else: + L = size(compactfields) + 4*(len(s) + 1) + check(s, L) + # verify that the UTF-8 size is accounted for + s = chr(0x4000) # 4 bytes canonical representation + check(s, size(compactfields) + 4) + # compile() will trigger the generation of the UTF-8 + # representation as a side effect + compile(s, "", "eval") + check(s, size(compactfields) + 4 + 4) + # TODO: add check that forces the presence of wchar_t representation + # TODO: add check that forces layout of unicodefields + # weakref + import weakref + check(weakref.ref(int), size('2Pn2P')) + # weakproxy + # XXX + # weakcallableproxy + check(weakref.proxy(int), size('2Pn2P')) + + def check_slots(self, obj, base, extra): + expected = sys.getsizeof(base) + struct.calcsize(extra) + if gc.is_tracked(obj) and not gc.is_tracked(base): + expected += self.gc_headsize + self.assertEqual(sys.getsizeof(obj), expected) + + def test_slots(self): + # check all subclassable types defined in Objects/ that allow + # non-empty __slots__ + check = self.check_slots + class BA(bytearray): + __slots__ = 'a', 'b', 'c' + check(BA(), bytearray(), '3P') + class D(dict): + __slots__ = 'a', 'b', 'c' + check(D(x=[]), {'x': []}, '3P') + class L(list): + __slots__ = 'a', 'b', 'c' + check(L(), [], '3P') + class S(set): + __slots__ = 'a', 'b', 'c' + check(S(), set(), '3P') + class FS(frozenset): + __slots__ = 'a', 'b', 'c' + check(FS(), frozenset(), '3P') + from collections import OrderedDict + class OD(OrderedDict): + __slots__ = 'a', 'b', 'c' + check(OD(x=[]), OrderedDict(x=[]), '3P') + + def test_pythontypes(self): + # check all types defined in Python/ + size = test.support.calcobjsize + vsize = test.support.calcvobjsize + check = self.check_sizeof + # _ast.AST + import _ast + check(_ast.AST(), size('P')) + try: + raise TypeError + except TypeError: + tb = sys.exc_info()[2] + # traceback + if tb is not None: + check(tb, size('2P2i')) + # symtable entry + # XXX + # sys.flags + check(sys.flags, vsize('') + self.P * len(sys.flags)) + + def test_asyncgen_hooks(self): + old = sys.get_asyncgen_hooks() + self.assertIsNone(old.firstiter) + self.assertIsNone(old.finalizer) + + firstiter = lambda *a: None + sys.set_asyncgen_hooks(firstiter=firstiter) + hooks = sys.get_asyncgen_hooks() + self.assertIs(hooks.firstiter, firstiter) + self.assertIs(hooks[0], firstiter) + self.assertIs(hooks.finalizer, None) + self.assertIs(hooks[1], None) + + finalizer = lambda *a: None + sys.set_asyncgen_hooks(finalizer=finalizer) + hooks = sys.get_asyncgen_hooks() + self.assertIs(hooks.firstiter, firstiter) + self.assertIs(hooks[0], firstiter) + self.assertIs(hooks.finalizer, finalizer) + self.assertIs(hooks[1], finalizer) + + sys.set_asyncgen_hooks(*old) + cur = sys.get_asyncgen_hooks() + self.assertIsNone(cur.firstiter) + self.assertIsNone(cur.finalizer) + + +def test_main(): + test.support.run_unittest(SysModuleTest, SizeofTest) + +if __name__ == "__main__": + test_main() diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 9d949b74cb8d0b..8e53cffa9eb6e1 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -12,7 +12,7 @@ import platform import random import re -import sys +import _sys as sys # ...the *builtin* module. import traceback import types import unittest @@ -142,15 +142,15 @@ def check_iter_pickle(self, it, seq, proto): self.assertEqual(list(it), seq[1:]) def test_import(self): - __import__('sys') + __import__('_sys') __import__('time') __import__('string') - __import__(name='sys') + __import__(name='_sys') __import__(name='time', level=0) self.assertRaises(ImportError, __import__, 'spamspam') self.assertRaises(TypeError, __import__, 1, 2, 3, 4) self.assertRaises(ValueError, __import__, '') - self.assertRaises(TypeError, __import__, 'sys', name='sys') + self.assertRaises(TypeError, __import__, '_sys', name='_sys') # embedded null character self.assertRaises(ModuleNotFoundError, __import__, 'string\x00') diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index ced25f3fc44244..01ed7472474880 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4,7 +4,7 @@ import itertools import math import pickle -import sys +import _sys as sys import types import unittest import warnings diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index 50dbfe1eda4547..17c9d50d4d2b2a 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -5,7 +5,7 @@ machinery = test_util.import_importlib('importlib.machinery') import os.path -import sys +import _sys as sys from test import support import types import unittest @@ -206,8 +206,8 @@ def test_reload_modules(self): def test_module_replaced(self): def code(): - import sys - module = type(sys)('top_level') + import _sys + module = type(_sys)('top_level') module.spam = 3 sys.modules['top_level'] = module mock = test_util.mock_modules('top_level', diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py index 64e039e00feaae..bf0fc957c2a8c7 100644 --- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -120,7 +120,7 @@ def uncache(*names): """ for name in names: - if name in ('sys', 'marshal', 'imp'): + if name in ('_sys', 'marshal', 'imp'): raise ValueError( "cannot uncache {0}".format(name)) try: diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 350d5dbd776ac2..f78ac87f172bfe 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -11,7 +11,7 @@ import _pickle import pickle import shutil -import sys +import _sys as sys import types import textwrap import unicodedata @@ -3658,7 +3658,7 @@ def test_qualname_source(self): def test_builtins(self): module = importlib.import_module('unittest') _, out, err = assert_python_failure('-m', 'inspect', - 'sys') + '_sys') lines = err.decode().splitlines() self.assertEqual(lines, ["Can't get info for builtin modules."]) diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py index 6d0d59407ed240..dac26db86ea09d 100644 --- a/Lib/test/test_module.py +++ b/Lib/test/test_module.py @@ -4,8 +4,8 @@ from test.support import gc_collect, requires_type_collecting from test.support.script_helper import assert_python_ok -import sys -ModuleType = type(sys) +import _sys +ModuleType = type(_sys) class FullLoader: @classmethod @@ -204,7 +204,7 @@ def test_module_repr_with_full_loader_and_filename(self): self.assertEqual(repr(m), "") def test_module_repr_builtin(self): - self.assertEqual(repr(sys), "") + self.assertEqual(repr(_sys), "") def test_module_repr_source(self): r = repr(unittest) diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py index e4df2a90d4a4d0..deaf5e088ea59c 100644 --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -30,7 +30,7 @@ maybe_test = [ "a.module", - ["a", "a.module", "sys", + ["a", "a.module", "_sys", "b"], ["c"], ["b.something"], """\ @@ -39,12 +39,12 @@ from b import something from c import something b/__init__.py - from sys import * + from _sys import * """] maybe_test_new = [ "a.module", - ["a", "a.module", "sys", + ["a", "a.module", "_sys", "b", "__future__"], ["c"], ["b.something"], """\ @@ -54,12 +54,12 @@ from c import something b/__init__.py from __future__ import absolute_import - from sys import * + from _sys import * """] package_test = [ "a.module", - ["a", "a.b", "a.c", "a.module", "mymodule", "sys"], + ["a", "a.b", "a.c", "a.module", "mymodule", "_sys"], ["blahblah", "c"], [], """\ mymodule.py @@ -68,35 +68,35 @@ from a import b import c a/module.py - import sys + import _sys from a import b as x from a.c import sillyname a/b.py a/c.py from a.module import x import mymodule as sillyname - from sys import version_info + from _sys import version_info """] absolute_import_test = [ "a.module", ["a", "a.module", "b", "b.x", "b.y", "b.z", - "__future__", "sys", "gc"], + "__future__", "_sys", "gc"], ["blahblah", "z"], [], """\ mymodule.py a/__init__.py a/module.py from __future__ import absolute_import - import sys # sys + import _sys # _sys import blahblah # fails import gc # gc import b.x # b.x from b import y # b.y from b.z import * # b.z.* a/gc.py -a/sys.py +a/_sys.py import mymodule a/b/__init__.py a/b/x.py @@ -128,7 +128,7 @@ from __future__ import absolute_import # __future__ import gc # gc a/gc.py -a/sys.py +a/_sys.py a/b/__init__.py from ..b import x # a.b.x #from a.b.c import moduleC @@ -149,7 +149,7 @@ relative_import_test_2 = [ "a.module", ["a", "a.module", - "a.sys", + "a._sys", "a.b", "a.b.y", "a.b.z", "a.b.c", "a.b.c.d", "a.b.c.e", @@ -161,12 +161,12 @@ """\ mymodule.py a/__init__.py - from . import sys # a.sys + from . import _sys # a._sys a/another.py a/module.py from .b import y, z # a.b.y, a.b.z a/gc.py -a/sys.py +a/_sys.py a/b/__init__.py from .c import moduleC # a.b.c.moduleC from .c import d # a.b.c.d @@ -255,13 +255,13 @@ def _do_test(self, info, report=False, debug=0, replace_paths=[]): if report: mf.report() ## # This wouldn't work in general when executed several times: -## opath = sys.path[:] -## sys.path = TEST_PATH +## opath = _sys.path[:] +## _sys.path = TEST_PATH ## try: ## __import__(import_this) ## except: ## import traceback; traceback.print_exc() -## sys.path = opath +## _sys.path = opath ## return modules = sorted(set(modules)) found = sorted(mf.modules) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 2887ce6cc055da..1005efbdafc75e 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -1,6 +1,6 @@ from test.support import run_unittest, unload, check_warnings, CleanImport import unittest -import sys +import _sys as sys import importlib from importlib.util import spec_from_file_location import pkgutil @@ -418,7 +418,7 @@ def test_loader_deprecated(self): def test_get_loader_avoids_emulation(self): with check_warnings() as w: - self.assertIsNotNone(pkgutil.get_loader("sys")) + self.assertIsNotNone(pkgutil.get_loader("_sys")) self.assertIsNotNone(pkgutil.get_loader("os")) self.assertIsNotNone(pkgutil.get_loader("test.support")) self.assertEqual(len(w.warnings), 0) @@ -469,7 +469,7 @@ def test_find_loader_missing_module(self): def test_find_loader_avoids_emulation(self): with check_warnings() as w: - self.assertIsNotNone(pkgutil.find_loader("sys")) + self.assertIsNotNone(pkgutil.find_loader("_sys")) self.assertIsNotNone(pkgutil.find_loader("os")) self.assertIsNotNone(pkgutil.find_loader("test.support")) self.assertEqual(len(w.warnings), 0) diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py index 4bf91945ea4f4a..744e591d70037f 100644 --- a/Lib/test/test_reprlib.py +++ b/Lib/test/test_reprlib.py @@ -3,7 +3,7 @@ Nick Mathewson """ -import sys +import _sys import os import shutil import importlib @@ -235,7 +235,7 @@ def setUp(self): create_empty_file(os.path.join(self.subpkgname, '__init__.py')) # Remember where we are self.here = os.getcwd() - sys.path.insert(0, self.here) + _sys.path.insert(0, self.here) # When regrtest is run with its -j option, this command alone is not # enough. importlib.invalidate_caches() @@ -253,7 +253,7 @@ def tearDown(self): os.rmdir(p) else: os.remove(p) - del sys.path[0] + del _sys.path[0] def _check_path_limitations(self, module_name): # base directory @@ -281,7 +281,7 @@ def test_module(self): from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation module = areallylongpackageandmodulenametotestreprtruncation self.assertEqual(repr(module), "" % (module.__name__, module.__file__)) - self.assertEqual(repr(sys), "") + self.assertEqual(repr(_sys), "") def test_type(self): self._check_path_limitations('foo') diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index fd45abee67e5a5..0e342fdfff1a53 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1,6 +1,9 @@ import unittest, test.support from test.support.script_helper import assert_python_ok, assert_python_failure -import sys, io, os +import io +import os +import _sys +import sys import struct import subprocess import textwrap @@ -12,23 +15,74 @@ import locale import threading + # count the number of test runs, used to create unique # strings to intern in test_intern() numruns = 0 +MODULE_ATTRS = { + "__name__", + "__doc__", + "__loader__", + "__spec__", + "__package__", + "__file__", + "__cached__", + "__builtins__", + } + class SysModuleTest(unittest.TestCase): def setUp(self): - self.orig_stdout = sys.stdout - self.orig_stderr = sys.stderr - self.orig_displayhook = sys.displayhook + self.orig_stdout = _sys.stdout + self.orig_stderr = _sys.stderr + self.orig_displayhook = _sys.displayhook + self.orig_excepthook = _sys.excepthook def tearDown(self): - sys.stdout = self.orig_stdout - sys.stderr = self.orig_stderr - sys.displayhook = self.orig_displayhook - test.support.reap_children() + _sys.stdout = self.orig_stdout + _sys.stderr = self.orig_stderr + _sys.displayhook = self.orig_displayhook + _sys.excepthook = self.orig_excepthook + + def test_get_existing_attr_backward_compatible(self): + for name, expected in vars(_sys).items(): + if name in MODULE_ATTRS: + continue + with self.subTest(name): + value = getattr(sys, name) + self.assertIs(value, expected) + + def test_get_missing_attr_backward_compatible(self): + with self.assertRaises(AttributeError): + sys.spamspamspam + + def test_set_new_attr_backward_compatible(self): + value = 'eggs' + sys.spamspamspam = value + self.assertIs(sys.spamspamspam, value) + self.assertIs(_sys.spamspamspam, value) + + def test_set_existing_attr_backward_compatible(self): + out = io.StringIO() + def hook(obj): + raise RuntimeError + sys.displayhook = hook + sys.excepthook = hook + sys.stdout = out + + self.assertIs(_sys.displayhook, hook) + self.assertIs(_sys.excepthook, hook) + self.assertIs(_sys.stdout, out) + + def test_del_new_attr_backward_compatible(self): + _sys.spamspamspam = 'eggs' + del sys.spamspamspam + with self.assertRaises(AttributeError): + sys.spamspamspam + with self.assertRaises(AttributeError): + _sys.spamspamspam def test_original_displayhook(self): import builtins diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py index a3e1d31fbebe94..ec8371a9d49601 100644 --- a/Lib/test/test_sys_setprofile.py +++ b/Lib/test/test_sys_setprofile.py @@ -1,6 +1,6 @@ import gc import pprint -import sys +import _sys as sys import unittest diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index ed9e6d4f492fec..b83f968954d7de 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -2,7 +2,7 @@ from test import support import unittest -import sys +import _sys as sys import difflib import gc diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 75539cf1c87f8a..822009b0e71294 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -403,7 +403,7 @@ def test_stacklevel(self): warning_tests.inner("spam7", stacklevel=9999) self.assertEqual(os.path.basename(w[-1].filename), - "sys") + "_sys") def test_stacklevel_import(self): # Issue #24305: With stacklevel=2, module-level warnings should work. @@ -1131,7 +1131,7 @@ def test_late_resource_warning(self): # Issue #21925: Emitting a ResourceWarning late during the Python # shutdown must be logged. - expected = b"sys:1: ResourceWarning: unclosed file " + expected = b"_sys:1: ResourceWarning: unclosed file " # don't import the warnings module # (_warnings will try to import it) diff --git a/Lib/trace.py b/Lib/trace.py index 48a1d1b6a91402..932c919d1bb70a 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -52,7 +52,7 @@ import linecache import os import re -import sys +import _sys as sys import token import tokenize import inspect diff --git a/Lib/types.py b/Lib/types.py index 929cba223aa905..5854761cf8653f 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -1,7 +1,7 @@ """ Define names for built-in types that aren't directly accessible as a builtin. """ -import sys +import _sys as sys # We use the low-level module (closer to the interpreter). # Iterators in Python aren't a matter of type but of protocol. A large # and changing number of builtin types implement *some* flavor of diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py index 48d8fe95bce261..c3069d27d5accc 100644 --- a/Lib/unittest/test/test_discovery.py +++ b/Lib/unittest/test/test_discovery.py @@ -1,7 +1,7 @@ import os.path from os.path import abspath import re -import sys +import _sys as sys import types import pickle import builtins @@ -812,7 +812,7 @@ def restore(): self.addCleanup(restore) with self.assertRaises(TypeError) as cm: - loader.discover('sys') + loader.discover('_sys') self.assertEqual(str(cm.exception), 'Can not use builtin modules ' 'as dotted module names') diff --git a/Lib/warnings.py b/Lib/warnings.py index a1f774637a24f9..9ae69915afd247 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -1,6 +1,6 @@ """Python part of the warnings subsystem.""" -import sys +import _sys as sys # We use the low-level module to avoid a circular dep. __all__ = ["warn", "warn_explicit", "showwarning", diff --git a/Lib/weakref.py b/Lib/weakref.py index 1802f32a20633b..6878a390e1c3dc 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -22,7 +22,7 @@ from _weakrefset import WeakSet, _IterationGuard import collections.abc # Import after _weakref to avoid circular import. -import sys +import _sys as sys import itertools ProxyTypes = (ProxyType, CallableProxyType) diff --git a/Makefile.pre.in b/Makefile.pre.in index 5a001ecbe7303b..6fa0f2417bf15f 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -534,11 +534,11 @@ $(BUILDPYTHON): Programs/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY) $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST) platform: $(BUILDPYTHON) pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform + $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import _sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *_sys.version_info[:2]))' >platform # Create build directory and generate the sysconfig build-time data there. # pybuilddir.txt contains the name of the build dir and is used for -# sys.path fixup -- see Modules/getpath.c. +# _sys.path fixup -- see Modules/getpath.c. # Since this step runs before shared modules are built, try to avoid bootstrap # problems by creating a dummy pybuilddir.txt just to allow interpreter # initialization to succeed. It will be overwritten by generate-posix-vars diff --git a/Modules/config.c.in b/Modules/config.c.in index 7b77199c2e93ca..5d67896056eb13 100644 --- a/Modules/config.c.in +++ b/Modules/config.c.in @@ -46,7 +46,7 @@ struct _inittab _PyImport_Inittab[] = { /* These entries are here for sys.builtin_module_names */ {"builtins", NULL}, - {"sys", NULL}, + {"_sys", NULL}, /* This lives in gcmodule.c */ {"gc", PyInit_gc}, diff --git a/Modules/main.c b/Modules/main.c index 3e347dc8e243c6..15a06f24bbb91f 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -155,7 +155,7 @@ static void RunStartupFile(PyCompilerFlags *cf) static void RunInteractiveHook(void) { PyObject *sys, *hook, *result; - sys = PyImport_ImportModule("sys"); + sys = PyImport_ImportModule("_sys"); if (sys == NULL) goto error; hook = PyObject_GetAttrString(sys, "__interactivehook__"); diff --git a/PC/config.c b/PC/config.c index 699e1d07077bb7..290c0b305b94bd 100644 --- a/PC/config.c +++ b/PC/config.c @@ -150,9 +150,9 @@ struct _inittab _PyImport_Inittab[] = { /* This lives it with import.c */ {"_imp", PyInit_imp}, - /* These entries are here for sys.builtin_module_names */ + /* These entries are here for _sys.builtin_module_names */ {"builtins", NULL}, - {"sys", NULL}, + {"_sys", NULL}, {"_warnings", _PyWarnings_Init}, {"_string", PyInit__string}, diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index ef5189068e0c9b..55f9a284aabdae 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -36,7 +36,7 @@ opcodes can take an arbitrary time to execute. The `interval` value is available for the user to read and modify - using the Python API `sys.{get,set}switchinterval()`. + using the Python API `_sys.{get,set}switchinterval()`. - When a thread releases the GIL and gil_drop_request is set, that thread ensures that another GIL-awaiting thread gets scheduled. diff --git a/Python/import.c b/Python/import.c index 6b2634c3497afb..65d8b510db8f35 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1175,7 +1175,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec) PyModuleDef *def; if (_PyUnicode_EqualToASCIIString(name, p->name)) { if (p->initfunc == NULL) { - /* Cannot re-init internal module ("sys" or "builtins") */ + /* Cannot re-init internal module ("_sys" or "builtins") */ mod = PyImport_AddModule(namestr); Py_DECREF(name); return mod; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index caa324e3afa250..eddc25fb127d7b 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -296,7 +296,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod) importlib = PyImport_AddModule("_frozen_importlib"); if (importlib == NULL) { Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " - "sys.modules"); + "_sys.modules"); } interp->importlib = importlib; Py_INCREF(interp->importlib); @@ -315,7 +315,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod) PySys_FormatStderr("import _imp # builtin\n"); } if (_PyImport_SetModuleString("_imp", impmod) < 0) { - Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); + Py_FatalError("Py_Initialize: can't save _imp to _sys.modules"); } /* Install importlib as the implementation of import */ @@ -683,7 +683,7 @@ void _Py_InitializeCore(const _PyCoreConfig *config) Py_FatalError("Py_InitializeCore: can't initialize sys dict"); Py_INCREF(interp->sysdict); PyDict_SetItemString(interp->sysdict, "modules", modules); - _PyImport_FixupBuiltin(sysmod, "sys", modules); + _PyImport_FixupBuiltin(sysmod, "_sys", modules); /* Init Unicode implementation; relies on the codec registry */ if (_PyUnicode_Init() < 0) @@ -1202,7 +1202,7 @@ Py_NewInterpreter(void) if (modules == NULL) Py_FatalError("Py_NewInterpreter: can't make modules dictionary"); - sysmod = _PyImport_FindBuiltin("sys", modules); + sysmod = _PyImport_FindBuiltin("_sys", modules); if (sysmod != NULL) { interp->sysdict = PyModule_GetDict(sysmod); if (interp->sysdict == NULL) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 3ecd7fca54ab8d..d13f954530ccb6 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3,7 +3,7 @@ /* Various bits of information used by the interpreter are collected in -module 'sys'. +module '_sys'. Function member: - exit(sts): raise SystemExit Data members: @@ -1898,7 +1898,7 @@ make_impl_info(PyObject *version_info) static struct PyModuleDef sysmodule = { PyModuleDef_HEAD_INIT, - "sys", + "_sys", sys_doc, -1, /* multiple "initialization" just copies the module dict. */ sys_methods, diff --git a/Tools/freeze/makeconfig.py b/Tools/freeze/makeconfig.py index fabaace07a83e0..1a4f4e2041f8d6 100644 --- a/Tools/freeze/makeconfig.py +++ b/Tools/freeze/makeconfig.py @@ -4,7 +4,7 @@ # Write the config.c file never = ['marshal', '_imp', '_ast', '__main__', 'builtins', - 'sys', 'gc', '_warnings'] + '_sys', 'gc', '_warnings'] def makeconfig(infp, outfp, modules, with_ifdef=0): m1 = re.compile('-- ADDMODULE MARKER 1 --') diff --git a/setup.py b/setup.py index d5c58e0686981f..a5f8c22e30ac64 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,11 @@ # Autodetecting setup.py script for building the Python extensions # -import sys, os, importlib.machinery, re, optparse +import os, importlib.machinery, re, optparse from glob import glob import importlib._bootstrap import importlib.util +import _sys as sys import sysconfig from distutils import log