Python-checkins
Threads by month
- ----- 2024 -----
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
July 2012
- 13 participants
- 553 discussions
http://hg.python.org/cpython/rev/b213894e0859
changeset: 78361:b213894e0859
user: Barry Warsaw <barry(a)python.org>
date: Tue Jul 31 17:52:32 2012 -0400
summary:
abc fixes.
files:
Lib/test/test_importlib/source/test_abc_loader.py | 11 ++++++++++
Lib/test/test_importlib/source/test_file_loader.py | 2 +
2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_importlib/source/test_abc_loader.py b/Lib/test/test_importlib/source/test_abc_loader.py
--- a/Lib/test/test_importlib/source/test_abc_loader.py
+++ b/Lib/test/test_importlib/source/test_abc_loader.py
@@ -32,6 +32,9 @@
def get_filename(self, fullname):
return self.path
+ def module_repr(self, module):
+ return '<module>'
+
class SourceLoaderMock(SourceOnlyLoaderMock):
@@ -107,6 +110,9 @@
assert issubclass(w[0].category, DeprecationWarning)
return path
+ def module_repr(self):
+ return '<module>'
+
class PyLoaderCompatMock(PyLoaderMock):
@@ -779,11 +785,16 @@
class Loader(abc.Loader):
def load_module(self, fullname):
super().load_module(fullname)
+ def module_repr(self, module):
+ super().module_repr(module)
class Finder(abc.Finder):
def find_module(self, _):
super().find_module(_)
+ def find_loader(self, _):
+ super().find_loader(_)
+
class ResourceLoader(Loader, abc.ResourceLoader):
def get_data(self, _):
super().get_data(_)
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -29,6 +29,7 @@
# If fullname is not specified that assume self.name is desired.
class TesterMixin(importlib.abc.Loader):
def load_module(self, fullname): return fullname
+ def module_repr(self, module): return '<module>'
class Tester(importlib.abc.FileLoader, TesterMixin):
def get_code(self, _): pass
@@ -49,6 +50,7 @@
def get_code(self, _): pass
def get_source(self, _): pass
def is_package(self, _): pass
+ def module_repr(self, _): pass
path = 'some_path'
name = 'some_name'
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/564c2a8c975b
changeset: 78360:564c2a8c975b
user: Barry Warsaw <barry(a)python.org>
date: Tue Jul 31 16:39:43 2012 -0400
summary:
Typo.
files:
Lib/importlib/abc.py | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -33,7 +33,7 @@
The fullname is a str."""
raise NotImplementedError
- @abs.abstractmethod
+ @abc.abstractmethod
def module_repr(self, module):
"""Abstract method which when implemented calculates and returns the
given module's repr."""
@@ -44,7 +44,7 @@
"""Abstract base class for import finders."""
- @abs.abstractmethod
+ @abc.abstractmethod
def find_loader(self, fullname):
"""Abstract method which when implemented returns a module loader.
The fullname is a str. Returns a 2-tuple of (Loader, portion) where
--
Repository URL: http://hg.python.org/cpython
1
0
cpython: - Issue #15295: Reorganize and rewrite the documentation on the import system.
by barry.warsaw 31 Jul '12
by barry.warsaw 31 Jul '12
31 Jul '12
http://hg.python.org/cpython/rev/d5317b8f455a
changeset: 78359:d5317b8f455a
user: Barry Warsaw <barry(a)python.org>
date: Tue Jul 31 16:10:12 2012 -0400
summary:
- Issue #15295: Reorganize and rewrite the documentation on the import system.
files:
Misc/NEWS | 3 +--
1 files changed, 1 insertions(+), 2 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -225,8 +225,7 @@
Documentation
-------------
-- Issue #15295: Reorganize and rewrite the documentation on the import
- machinery.
+- Issue #15295: Reorganize and rewrite the documentation on the import system.
- Issue #15230: Clearly document some of the limitations of the runpy
module and nudge readers towards importlib when appropriate.
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/8c0745ffbef5
changeset: 78358:8c0745ffbef5
parent: 78357:1cf1df6f62f5
parent: 78349:28c935ded243
user: Barry Warsaw <barry(a)python.org>
date: Tue Jul 31 16:03:25 2012 -0400
summary:
merge
files:
Doc/distutils/uploading.rst | 6 +-
Doc/howto/ipaddress.rst | 8 +-
Doc/library/faulthandler.rst | 2 +-
Doc/library/functions.rst | 3 +
Doc/library/ipaddress.rst | 248 +-
Lib/idlelib/PyShell.py | 4 +-
Lib/importlib/_bootstrap.py | 65 +-
Lib/test/test_faulthandler.py | 15 +
Lib/test/test_import.py | 18 +-
Misc/NEWS | 7 +
Python/bltinmodule.c | 5 +-
Python/import.c | 28 +-
Python/importlib.h | 7034 ++++++++++----------
Python/pythonrun.c | 8 +-
14 files changed, 3749 insertions(+), 3702 deletions(-)
diff --git a/Doc/distutils/uploading.rst b/Doc/distutils/uploading.rst
--- a/Doc/distutils/uploading.rst
+++ b/Doc/distutils/uploading.rst
@@ -74,5 +74,7 @@
:mod:`docutils` will display a warning if there's something wrong with your
syntax. Because PyPI applies additional checks (e.g. by passing ``--no-raw``
-to ``rst2html.py`` in the command above), running the command above without
-warnings is not sufficient for PyPI to convert the content successfully.
+to ``rst2html.py`` in the command above), being able to run the command above
+without warnings does not guarantee that PyPI will convert the content
+successfully.
+
diff --git a/Doc/howto/ipaddress.rst b/Doc/howto/ipaddress.rst
--- a/Doc/howto/ipaddress.rst
+++ b/Doc/howto/ipaddress.rst
@@ -19,7 +19,7 @@
Creating Address/Network/Interface objects
==========================================
-Since :mod:`ipaddress` is a module for inspecting and manipulating IP address,
+Since :mod:`ipaddress` is a module for inspecting and manipulating IP addresses,
the first thing you'll want to do is create some objects. You can use
:mod:`ipaddress` to create objects from strings and integers.
@@ -183,10 +183,10 @@
>>> net6.numhosts
4294967296
-Iterating through the 'usable' addresses on a network::
+Iterating through the "usable" addresses on a network::
>>> net4 = ipaddress.ip_network('192.0.2.0/24')
- >>> for x in net4.iterhosts():
+ >>> for x in net4.hosts():
print(x)
192.0.2.1
192.0.2.2
@@ -294,7 +294,7 @@
When creating address/network/interface objects using the version-agnostic
factory functions, any errors will be reported as :exc:`ValueError` with
a generic error message that simply says the passed in value was not
-recognised as an object of that type. The lack of a specific error is
+recognized as an object of that type. The lack of a specific error is
because it's necessary to know whether the value is *supposed* to be IPv4
or IPv6 in order to provide more detail on why it has been rejected.
diff --git a/Doc/library/faulthandler.rst b/Doc/library/faulthandler.rst
--- a/Doc/library/faulthandler.rst
+++ b/Doc/library/faulthandler.rst
@@ -23,7 +23,7 @@
* Only ASCII is supported. The ``backslashreplace`` error handler is used on
encoding.
-* Each string is limited to 100 characters.
+* Each string is limited to 500 characters.
* Only the filename, the function name and the line number are
displayed. (no source code)
* It is limited to 100 frames and 100 threads.
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -1506,6 +1506,9 @@
If you simply want to import a module (potentially within a package) by name,
use :func:`importlib.import_module`.
+ .. versionchanged:: 3.3
+ Negative values for *level* are no longer supported.
+
.. rubric:: Footnotes
diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst
--- a/Doc/library/ipaddress.rst
+++ b/Doc/library/ipaddress.rst
@@ -22,19 +22,18 @@
IP address or network definition.
-Defining IP Addresses and Interfaces
-------------------------------------
+Convenience factory functions
+-----------------------------
-The :mod:`ipaddress` module provides factory functions to define IP addresses
-and networks:
+The :mod:`ipaddress` module provides factory functions to conveniently create
+IP addresses, networks and interfaces:
.. function:: ip_address(address)
Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
- the IP address passed as argument. *address* is a string or integer
- representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
- integers less than 2**32 will be considered to be IPv4 by default. A
- :exc:`ValueError` is raised if the *address* passed is neither an IPv4 nor
+ the IP address passed as argument. Either IPv4 or IPv6 addresses may be
+ supplied; integers less than 2**32 will be considered to be IPv4 by default.
+ A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
IPv6 address.
>>> ipaddress.ip_address('192.168.0.1')
@@ -50,8 +49,8 @@
representing the IP network. Either IPv4 or IPv6 networks may be supplied;
integers less than 2**32 will be considered to be IPv4 by default. *strict*
is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A
- :exc:`ValueError` is raised if the string passed isn't either an IPv4 or IPv6
- address, or if the network has host bits set.
+ :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
+ IPv6 address, or if the network has host bits set.
>>> ipaddress.ip_network('192.168.0.0/28')
IPv4Network('192.168.0.0/28')
@@ -62,45 +61,174 @@
Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending
on the IP address passed as argument. *address* is a string or integer
representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
- integers less than 2**32 will be considered to be IPv4 by default.. A
- :exc:`ValueError` is raised if the *address* passed isn't either an IPv4 or
+ integers less than 2**32 will be considered to be IPv4 by default. A
+ :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
IPv6 address.
-Representing IP Addresses and Networks
---------------------------------------
+Address objects
+---------------
-The module defines the following and classes to represent IP addresses
-and networks:
-
-.. todo: list the properties and methods
+The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
+attributes. Some attributes that are only meaningful for IPv6 addresses are
+also implemented by :class:`IPv4Address` objects, in order to make it easier to
+write code that handles both IP versions correctly. To avoid duplication, all
+common attributes will only be documented for :class:`IPv4Address`.
.. class:: IPv4Address(address)
- Construct an IPv4 address. *address* is a string or integer representing the
- IP address. An :exc:`AddressValueError` is raised if *address* is not a
- valid IPv4 address.
+ Construct an IPv4 address. An :exc:`AddressValueError` is raised if
+ *address* is not a valid IPv4 address.
+
+ The following constitutes a valid IPv4 address:
+
+ 1. A string in decimal-dot notation, consisting of four decimal integers in
+ the inclusive range 0-255, separated by dots (e.g. ``192.168.0.1``). Each
+ integer represents an octet (byte) in the address, big-endian.
+ 2. An integer that fits into 32 bits.
+ 3. An integer packed into a :class:`bytes` object of length 4, big-endian.
>>> ipaddress.IPv4Address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.IPv4Address('192.0.2.1') == ipaddress.IPv4Address(3221225985)
True
+ .. attribute:: exploded
-.. class:: IPv4Interface(address)
+ The longhand version of the address as a string. Note: the
+ exploded/compressed distinction is meaningful only for IPv6 addresses.
+ For IPv4 addresses it is the same.
- Construct an IPv4 interface. *address* is a string or integer representing
- the IP interface. An :exc:`AddressValueError` is raised if *address* is not
- a valid IPv4 address.
+ .. attribute:: compressed
- The network address for the interface is determined by calling
- ``IPv4Network(address, strict=False)``.
+ The shorthand version of the address as a string.
- >>> ipaddress.IPv4Interface('192.168.0.0/24')
- IPv4Interface('192.168.0.0/24')
- >>> ipaddress.IPv4Interface('192.168.0.0/24').network
- IPv4Network('192.168.0.0/24')
+ .. attribute:: packed
+ The binary representation of this address - a :class:`bytes` object.
+
+ .. attribute:: version
+
+ A numeric version number.
+
+ .. attribute:: max_prefixlen
+
+ Maximal length of the prefix (in bits). The prefix defines the number of
+ leading bits in an address that are compared to determine whether or not an
+ address is part of a network.
+
+ .. attribute:: is_multicast
+
+ ``True`` if the address is reserved for multicast use. See :RFC:`3171` (for
+ IPv4) or :RFC:`2373` (for IPv6).
+
+ .. attribute:: is_private
+
+ ``True`` if the address is allocated for private networks. See :RFC:`1918`
+ (for IPv4) or :RFC:`4193` (for IPv6).
+
+ .. attribute:: is_unspecified
+
+ ``True`` if the address is unspecified. See :RFC:`5375` (for IPv4) or
+ :RFC:`2373` (for IPv6).
+
+ .. attribute:: is_reserved
+
+ ``True`` if the address is otherwise IETF reserved.
+
+ .. attribute:: is_loopback
+
+ ``True`` if this is a loopback address. See :RFC:`3330` (for IPv4) or
+ :RFC:`2373` (for IPv6).
+
+ .. attribute:: is_link_local
+
+ ``True`` if the address is reserved for link-local. See :RFC:`3927`.
+
+.. class:: IPv6Address(address)
+
+ Construct an IPv6 address. An :exc:`AddressValueError` is raised if
+ *address* is not a valid IPv6 address.
+
+ The following constitutes a valid IPv6 address:
+
+ 1. A string consisting of eight groups of four hexadecimal digits, each
+ group representing 16 bits. The groups are separated by colons.
+ This describes an *exploded* (longhand) notation. The string can
+ also be *compressed* (shorthand notation) by various means. See
+ :RFC:`4291` for details. For example,
+ ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
+ ``"::abc:7:def"``.
+ 2. An integer that fits into 128 bits.
+ 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
+
+ >>> ipaddress.IPv6Address('2001:db8::1000')
+ IPv6Address('2001:db8::1000')
+
+ All the attributes exposed by :class:`IPv4Address` are supported. In
+ addition, the following attributs are exposed only by :class:`IPv6Address`.
+
+ .. attribute:: is_site_local
+
+ ``True`` if the address is reserved for site-local. Note that the site-local
+ address space has been deprecated by :RFC:`3879`. Use
+ :attr:`~IPv4Address.is_private` to test if this address is in the space of
+ unique local addresses as defined by :RFC:`4193`.
+
+ .. attribute:: ipv4_mapped
+
+ If this address represents a IPv4 mapped address, return the IPv4 mapped
+ address. Otherwise return ``None``.
+
+ .. attribute:: teredo
+
+ If this address appears to be a teredo address (starts with ``2001::/32``),
+ return a tuple of embedded teredo IPs ``(server, client)`` pairs. Otherwise
+ return ``None``.
+
+ .. attribute:: sixtofour
+
+ If this address appears to contain a 6to4 embedded address, return the
+ embedded IPv4 address. Otherwise return ``None``.
+
+
+Operators
+^^^^^^^^^
+
+Address objects support some operators. Unless stated otherwise, operators can
+only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
+IPv6).
+
+Logical operators
+"""""""""""""""""
+
+Address objects can be compared with the usual set of logical operators. Some
+examples::
+
+ >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
+ True
+ >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
+ False
+ >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
+ True
+
+Arithmetic operators
+""""""""""""""""""""
+
+Integers can be added to or subtracted from address objects. Some examples::
+
+ >>> IPv4Address('127.0.0.2') + 3
+ IPv4Address('127.0.0.5')
+ >>> IPv4Address('127.0.0.2') - 3
+ IPv4Address('126.255.255.255')
+ >>> IPv4Address('255.255.255.255') + 1
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
+
+
+Network objects
+---------------
.. class:: IPv4Network(address, strict=True)
@@ -121,31 +249,6 @@
IPv4Network('192.0.2.0/27')
-.. class:: IPv6Address(address)
-
- Construct an IPv6 address. *address* is a string or integer representing the
- IP address. An :exc:`AddressValueError` is raised if *address* is not a
- valid IPv6 address.
-
- >>> ipaddress.IPv6Address('2001:db8::1000')
- IPv6Address('2001:db8::1000')
-
-
-.. class:: IPv6Interface(address)
-
- Construct an IPv6 interface. *address* is a string or integer representing
- the IP interface. An :exc:`AddressValueError` is raised if *address* is not
- a valid IPv6 address.
-
- The network address for the interface is determined by calling
- ``IPv6Network(address, strict=False)``.
-
- >>> ipaddress.IPv6Interface('2001:db8::1000/96')
- IPv6Interface('2001:db8::1000/96')
- >>> ipaddress.IPv6Interface('2001:db8::1000/96').network
- IPv6Network('2001:db8::/96')
-
-
.. class:: IPv6Network(address, strict=True)
Construct an IPv6 network. *address* is a string or integer representing the
@@ -165,6 +268,39 @@
IPv6Network('2001:db8::/96')
+Interface objects
+-----------------
+
+.. class:: IPv4Interface(address)
+
+ Construct an IPv4 interface. *address* is a string or integer representing
+ the IP interface. An :exc:`AddressValueError` is raised if *address* is not
+ a valid IPv4 address.
+
+ The network address for the interface is determined by calling
+ ``IPv4Network(address, strict=False)``.
+
+ >>> ipaddress.IPv4Interface('192.168.0.0/24')
+ IPv4Interface('192.168.0.0/24')
+ >>> ipaddress.IPv4Interface('192.168.0.0/24').network
+ IPv4Network('192.168.0.0/24')
+
+
+.. class:: IPv6Interface(address)
+
+ Construct an IPv6 interface. *address* is a string or integer representing
+ the IP interface. An :exc:`AddressValueError` is raised if *address* is not
+ a valid IPv6 address.
+
+ The network address for the interface is determined by calling
+ ``IPv6Network(address, strict=False)``.
+
+ >>> ipaddress.IPv6Interface('2001:db8::1000/96')
+ IPv6Interface('2001:db8::1000/96')
+ >>> ipaddress.IPv6Interface('2001:db8::1000/96').network
+ IPv6Network('2001:db8::/96')
+
+
Other Module Level Functions
----------------------------
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -248,8 +248,8 @@
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
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -297,8 +297,20 @@
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 @@
"""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 @@
"""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 @@
"""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 @@
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 @@
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 @@
"""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 @@
"""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 @@
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 @@
_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 @@
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
--- a/Lib/test/test_faulthandler.py
+++ b/Lib/test/test_faulthandler.py
@@ -7,6 +7,7 @@
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 @@
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
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -785,11 +785,13 @@
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 @@
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()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
Core and Builtins
-----------------
+- Issue #15508: Fix the docstring for __import__ to have the proper default
+ value of 0 for 'level' and to not mention negative levels since they are
+ not supported.
+
- Issue #15425: Eliminated traceback noise from more situations involving
importlib
@@ -342,6 +346,9 @@
Library
-------
+- Issue #9803: Don't close IDLE on saving if breakpoint is open.
+ Patch by Roger Serwy.
+
- Issue #12288: Consider '0' and '0.0' as valid initialvalue
for tkinter SimpleDialog.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -195,7 +195,7 @@
}
PyDoc_STRVAR(import_doc,
-"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
+"__import__(name, globals={}, locals={}, fromlist=[], level=0) -> module\n\
\n\
Import a module. Because this function is meant for use by the Python\n\
interpreter and not for general use it is better to use\n\
@@ -208,8 +208,7 @@
When importing a module from a package, note that __import__('A.B', ...)\n\
returns package A when fromlist is empty, but its submodule B when\n\
fromlist is not empty. Level is used to determine whether to perform \n\
-absolute or relative imports. -1 is the original strategy of attempting\n\
-both absolute and relative imports, 0 is absolute, a positive number\n\
+absolute or relative imports. 0 is absolute while a positive number\n\
is the number of parent directories to search relative to the current module.");
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1153,9 +1153,7 @@
remove_importlib_frames(void)
{
const char *importlib_filename = "<frozen importlib._bootstrap>";
- const char *exec_funcname = "_exec_module";
- const char *get_code_funcname = "get_code";
- const char *recursive_import = "_recursive_import";
+ const char *remove_frames = "_call_with_frames_removed";
int always_trim = 0;
int trim_get_code = 0;
int in_importlib = 0;
@@ -1163,18 +1161,8 @@
PyObject **prev_link, **outer_link = NULL;
/* Synopsis: if it's an ImportError, we trim all importlib chunks
- from the traceback. If it's a SyntaxError, we trim any chunks that
- end with a call to "get_code", We always trim chunks
- which end with a call to "_exec_module". */
-
- /* Thanks to issue 15425, we also strip any chunk ending with
- * _recursive_import. This is used when making a recursive call to the
- * full import machinery which means the inner stack gets stripped early
- * and the normal heuristics won't fire properly for outer frames. A
- * more elegant mechanism would be nice, as this one can misfire if
- * builtins.__import__ has been replaced with a custom implementation.
- * However, the current approach at least gets the job done.
- */
+ from the traceback. We always trim chunks
+ which end with a call to "_call_with_frames_removed". */
PyErr_Fetch(&exception, &value, &base_tb);
if (!exception || Py_VerboseFlag)
@@ -1207,14 +1195,8 @@
if (in_importlib &&
(always_trim ||
- (PyUnicode_CompareWithASCIIString(code->co_name,
- exec_funcname) == 0) ||
- (PyUnicode_CompareWithASCIIString(code->co_name,
- recursive_import) == 0) ||
- (trim_get_code &&
- PyUnicode_CompareWithASCIIString(code->co_name,
- get_code_funcname) == 0)
- )) {
+ PyUnicode_CompareWithASCIIString(code->co_name,
+ remove_frames) == 0)) {
PyObject *tmp = *outer_link;
*outer_link = next;
Py_XINCREF(next);
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -356,10 +356,6 @@
_PyImportHooks_Init();
- /* initialize the faulthandler module */
- if (_PyFaulthandler_Init())
- Py_FatalError("Py_Initialize: can't initialize faulthandler");
-
/* Initialize _warnings. */
_PyWarnings_Init();
@@ -368,6 +364,10 @@
import_init(interp, sysmod);
+ /* initialize the faulthandler module */
+ if (_PyFaulthandler_Init())
+ Py_FatalError("Py_Initialize: can't initialize faulthandler");
+
_PyTime_Init();
if (initfsencoding(interp) < 0)
--
Repository URL: http://hg.python.org/cpython
1
0
cpython: Finally, a coherent set of terminology for all the lil' beasties involved.
by barry.warsaw 31 Jul '12
by barry.warsaw 31 Jul '12
31 Jul '12
http://hg.python.org/cpython/rev/1cf1df6f62f5
changeset: 78357:1cf1df6f62f5
user: Barry Warsaw <barry(a)python.org>
date: Tue Jul 31 16:03:09 2012 -0400
summary:
Finally, a coherent set of terminology for all the lil' beasties involved.
files:
Doc/glossary.rst | 34 +-
Doc/reference/datamodel.rst | 19 +-
Doc/reference/import_machinery.rst | 307 ++++++++--------
Doc/reference/index.rst | 2 +-
Doc/reference/simple_stmts.rst | 15 +-
5 files changed, 205 insertions(+), 172 deletions(-)
diff --git a/Doc/glossary.rst b/Doc/glossary.rst
--- a/Doc/glossary.rst
+++ b/Doc/glossary.rst
@@ -315,6 +315,13 @@
role in places where a constant hash value is needed, for example as a key
in a dictionary.
+ import path
+ A list of locations (or :term:`path entries <path entry>`) that are
+ searched by the :term:`path importer` for modules to import. During
+ import, this list of locations usually comes from :data:`sys.path`, but
+ for subpackages it may also come from the parent package's ``__path__``
+ attribute.
+
importing
The process by which Python code in one module is made available to
Python code in another module.
@@ -446,8 +453,8 @@
meta path finder
A finder returned by a search of :data:`sys.meta_path`. Meta path
- finders are related to, but different from :term:`sys path finders <sys
- path finder>`.
+ finders are related to, but different from :term:`path entry finders
+ <path entry finder>`.
metaclass
The class of a class. Class definitions create a class name, a class
@@ -541,9 +548,23 @@
subpackages. Technically, a package is a Python module with an
``__path__`` attribute.
+ path entry
+ A single location on the :term:`import path` which the :term:`path
+ importer` consults to find modules for importing.
+
+ path entry finder
+ A :term:`finder` returned by a callable on :data:`sys.path_hooks`
+ (i.e. a :term:`path entry hook`) which knows how to locate modules given
+ a :term:`path entry`.
+
+ path entry hook
+ A callable on the :data:`sys.path_hook` list which returns a :term:`path
+ entry finder` if it knows how to find modules on a specific :term:`path
+ entry`.
+
path importer
- A built-in :term:`finder` / :term:`loader` that knows how to find and
- load modules from the file system.
+ One of the default :term:`meta path finders <meta path finder>` which
+ searches an :term:`import path` for modules.
portion
A set of files in a single directory (possibly stored in a zip file)
@@ -671,11 +692,6 @@
:meth:`~collections.somenamedtuple._asdict`. Examples of struct sequences
include :data:`sys.float_info` and the return value of :func:`os.stat`.
- sys path finder
- A finder returned by a search of :data:`sys.path` by the :term:`path
- importer`. Sys path finders are related to, but different from
- :term:`meta path finders <meta path finder>`.
-
triple-quoted string
A string which is bound by three instances of either a quotation mark
(") or an apostrophe ('). While they don't provide any functionality
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -652,15 +652,16 @@
object: module
Modules are a basic organizational unit of Python code, and are created by
- the :ref:`importmachinery` as invoked either by the :keyword:`import`
- statement (see section :ref:`import`) or by calling the built in
- :func:`__import__` function. A module object has a namespace implemented
- by a dictionary object (this is the dictionary referenced by the
- ``__globals__`` attribute of functions defined in the module). Attribute
- references are translated to lookups in this dictionary, e.g., ``m.x`` is
- equivalent to ``m.__dict__["x"]``. A module object does not contain the
- code object used to initialize the module (since it isn't needed once the
- initialization is done).
+ the :ref:`import system <importsystem>` as invoked either by the
+ :keyword:`import` statement (see :keyword:`import`), or by calling
+ functions such as :func:`importlib.import_module` and built-in
+ :func:`__import__`. A module object has a namespace implemented by a
+ dictionary object (this is the dictionary referenced by the ``__globals__``
+ attribute of functions defined in the module). Attribute references are
+ translated to lookups in this dictionary, e.g., ``m.x`` is equivalent to
+ ``m.__dict__["x"]``. A module object does not contain the code object used
+ to initialize the module (since it isn't needed once the initialization is
+ done).
Attribute assignment updates the module's namespace dictionary, e.g.,
``m.x = 1`` is equivalent to ``m.__dict__["x"] = 1``.
diff --git a/Doc/reference/import_machinery.rst b/Doc/reference/import.rst
rename from Doc/reference/import_machinery.rst
rename to Doc/reference/import.rst
--- a/Doc/reference/import_machinery.rst
+++ b/Doc/reference/import.rst
@@ -1,9 +1,9 @@
-.. _importmachinery:
+.. _importsystem:
-****************
-Import machinery
-****************
+*****************
+The import system
+*****************
.. index:: single: import machinery
@@ -33,8 +33,8 @@
cannot be found, an :exc:`ImportError` is raised. Python implements various
strategies to search for the named module when the import machinery is
invoked. These strategies can be modified and extended by using various hooks
-described in the sections below. The entire import machinery itself can be
-overridden by replacing built-in :func:`__import__`.
+described in the sections below. More coarse-grained overriding of the import
+system can be accomplished by replacing built-in :func:`__import__`.
:mod:`importlib`
@@ -189,25 +189,25 @@
single: finder
single: loader
-If the named module is not found in :data:`sys.modules` then Python's import
-protocol is invoked to find and load the module. As this implies, the import
-protocol consists of two conceptual objects, :term:`finders <finder>` and
-:term:`loaders <loader>`. A finder's job is to determine whether it can find
-the named module using whatever strategy it knows about. For example, there
-is a file system finder which know how to search the file system for the named
-module. Other finders may know how to search a zip file, a web page, or a
-database to find the named module. The import machinery is extensible, so new
-finders can be added to extend the range and scope of module searching.
+If the named module is not found in :data:`sys.modules`, then Python's import
+protocol is invoked to find and load the module. This protocol consists of
+two conceptual objects, :term:`finders <finder>` and :term:`loaders <loader>`.
+A finder's job is to determine whether it can find the named module using
+whatever strategy it knows about.
+
+By default, Python comes with several default finders. One knows how to
+locate frozen modules, and another knows how to locate built-in modules. A
+third default finder searches an :term:`import path` for modules. The
+:term:`import path` is a list of locations that may name file system paths or
+zip files. It can also be extended to search for any locatable resource, such
+as those identified by URLs.
+
+The import machinery is extensible, so new finders can be added to extend the
+range and scope of module searching.
Finders do not actually load modules. If they can find the named module, they
-return a loader, which the import machinery later invokes to load the module
-and create the corresponding module object.
-
-There are actually two types of finders, and two different but related APIs
-for finders, depending on whether it is a :term:`meta path finder` or a
-:term:`sys path finder`. Meta path processing occurs at the beginning of
-import processing, while sys path processing happens later, by the :term:`path
-importer`.
+return a :term:`loader`, which the import machinery then invokes to load the
+module and create the corresponding module object.
The following sections describe the protocol for finders and loaders in more
detail, including how you can create and register new ones to extend the
@@ -227,18 +227,18 @@
The import machinery is designed to be extensible; the primary mechanism for
this are the *import hooks*. There are two types of import hooks: *meta
-hooks* and *path hooks*.
+hooks* and *import path hooks*.
Meta hooks are called at the start of import processing, before any other
-import processing has occurred. This allows meta hooks to override
-:data:`sys.path` processing, frozen modules, or even built-in modules. Meta
-hooks are registered by adding new finder objects to :data:`sys.meta_path`, as
-described below.
+import processing has occurred, other than :data:`sys.modules` cache look up.
+This allows meta hooks to override :data:`sys.path` processing, frozen
+modules, or even built-in modules. Meta hooks are registered by adding new
+finder objects to :data:`sys.meta_path`, as described below.
-Path hooks are called as part of :data:`sys.path` (or ``package.__path__``)
-processing, at the point where their associated path item is encountered.
-Path hooks are registered by adding new callables to :data:`sys.path_hooks` as
-described below.
+Import path hooks are called as part of :data:`sys.path` (or
+``package.__path__``) processing, at the point where their associated path
+item is encountered. Import path hooks are registered by adding new callables
+to :data:`sys.path_hooks` as described below.
The meta path
@@ -253,9 +253,9 @@
searches :data:`sys.meta_path`, which contains a list of meta path finder
objects. These finders are queried in order to see if they know how to handle
the named module. Meta path finders must implement a method called
-:meth:`find_module()` which takes two arguments, a name and a path. The meta
-path finder can use any strategy it wants to determine whether it can handle
-the named module or not.
+:meth:`find_module()` which takes two arguments, a name and an import path.
+The meta path finder can use any strategy it wants to determine whether it can
+handle the named module or not.
If the meta path finder knows how to handle the named module, it returns a
loader object. If it cannot handle the named module, it returns ``None``. If
@@ -266,21 +266,21 @@
The :meth:`find_module()` method of meta path finders is called with two
arguments. The first is the fully qualified name of the module being
imported, for example ``foo.bar.baz``. The second argument is the relative
-path for the module search. For top-level modules, the second argument is
-``None``, but for submodules or subpackages, the second argument is the value
-of the parent package's ``__path__`` attribute, which must exist or an
-:exc:`ImportError` is raised.
+import path for the module search. For top-level modules, this second
+argument will always be ``None``, but for submodules or subpackages, the
+second argument is the value of the parent package's ``__path__`` attribute,
+which must exist on the parent module or an :exc:`ImportError` is raised.
Python's default :data:`sys.meta_path` has three meta path finders, one that
knows how to import built-in modules, one that knows how to import frozen
-modules, and one that knows how to import modules from the file system
+modules, and one that knows how to import modules from an :term:`import path`
(i.e. the :term:`path importer`).
-Meta path loaders
------------------
+Loaders
+=======
-Once a loader is found via a meta path finder, the loader's
+If and when a module loader is found its
:meth:`~importlib.abc.Loader.load_module` method is called, with a single
argument, the fully qualified name of the module being imported. This method
has several responsibilities, and should return the module object it has
@@ -288,8 +288,8 @@
:exc:`ImportError`, although any other exception raised during
:meth:`load_module()` will be propagated.
-In many cases, the meta path finder and loader can be the same object,
-e.g. :meth:`finder.find_module()` would just return ``self``.
+In many cases, the finder and loader can be the same object; in such cases the
+:meth:`finder.find_module()` would just return ``self``.
Loaders must satisfy the following requirements:
@@ -305,9 +305,11 @@
beforehand prevents unbounded recursion in the worst case and multiple
loading in the best.
- If the load fails, the loader needs to remove any modules it may have
- inserted into ``sys.modules``. If the module was already in
- ``sys.modules`` then the loader should leave it alone.
+ If loading fails, the loader must remove any modules it has inserted into
+ :data:`sys.modules`, but it must remove **only** the failing module, and
+ only if the loader itself has loaded it explicitly. Any module already in
+ the :data:`sys.modules` cache, and any module that was successfully loaded
+ as a side-effect, must remain in the cache.
* The loader may set the ``__file__`` attribute of the module. If set, this
attribute's value must be a string. The loader may opt to leave
@@ -329,10 +331,13 @@
data associated with an importer.
* The module's ``__package__`` attribute should be set. Its value must be a
- string, but it can be the same value as its ``__name__``. This is the
- recommendation when the module is a package. When the module is not a
- package, ``__package__`` should be set to the parent package's
- name [#fnpk]_.
+ string, but it can be the same value as its ``__name__``. If the attribute
+ is set to ``None`` or is missing, the import system will fill it in with a
+ more appropriate value. When the module is a package, its ``__package__``
+ value should be set to its ``__name__``. When the module is not a package,
+ ``__package__`` should be set to the empty string for top-level modules, or
+ for submodules, to the parent package's name. See :pep:`366` for further
+ details.
This attribute is used instead of ``__name__`` to calculate explicit
relative imports for main modules, as defined in :pep:`366`.
@@ -421,32 +426,44 @@
single: path importer
As mentioned previously, Python comes with several default meta path finders.
-One of these, called the :term:`path importer`, knows how to provide
-traditional file system imports. It implements all the semantics for finding
-modules on the file system, handling special file types such as Python source
-code (``.py`` files), Python byte code (``.pyc`` and ``.pyo`` files) and
-shared libraries (e.g. ``.so`` files).
+One of these, called the :term:`path importer`, searches an :term:`import
+path`, which contains a list of :term:`path entries <path entry>`. Each path
+entry names a location to search for modules.
-In addition to being able to find such modules, there is built-in support for
-loading these modules. To accomplish these two related tasks, additional
-hooks and protocols are provided so that you can extend and customize the path
-importer semantics.
+Path entries may name file system locations, and by default the :term:`path
+importer` knows how to provide traditional file system imports. It implements
+all the semantics for finding modules on the file system, handling special
+file types such as Python source code (``.py`` files), Python byte code
+(``.pyc`` and ``.pyo`` files) and shared libraries (e.g. ``.so`` files).
+
+Path entries need not be limited to file system locations. They can refer to
+the contents of zip files, URLs, database queries, or any other location that
+can be specified as a string.
+
+The :term:`path importer` provides additional hooks and protocols so that you
+can extend and customize the types of searchable path entries. For example,
+if you wanted to support path entries as network URLs, you could write a hook
+that implements HTTP semantics to find modules on the web. This hook (a
+callable) would return a :term:`path entry finder` supporting the protocol
+described below, which was then used to get a loader for the module from the
+web.
A word of warning: this section and the previous both use the term *finder*,
distinguishing between them by using the terms :term:`meta path finder` and
-:term:`sys path finder`. Meta path finders and sys path finders are very
-similar, support similar protocols, and function in similar ways during the
-import process, but it's important to keep in mind that they are subtly
-different. In particular, meta path finders operate at the beginning of the
-import process, as keyed off the :data:`sys.meta_path` traversal.
+:term:`path entry finder`. These two types of finders are very similar,
+support similar protocols, and function in similar ways during the import
+process, but it's important to keep in mind that they are subtly different.
+In particular, meta path finders operate at the beginning of the import
+process, as keyed off the :data:`sys.meta_path` traversal.
-On the other hand, sys path finders are in a sense an implementation detail of
-the path importer, and in fact, if the path importer were to be removed from
-:data:`sys.meta_path`, none of the sys path finder semantics would be invoked.
+On the other hand, path entry finders are in a sense an implementation detail
+of the :term:`path importer`, and in fact, if the path importer were to be
+removed from :data:`sys.meta_path`, none of the path entry finder semantics
+would be invoked.
-sys path finders
-----------------
+Path entry finders
+------------------
.. index::
single: sys.path
@@ -454,112 +471,107 @@
single: sys.path_importer_cache
single: PYTHONPATH
-The path importer is responsible for finding and loading Python modules and
-packages from the file system. As a meta path finder, it implements the
+The :term:`path importer` is responsible for finding and loading Python
+modules and packages whose location is specified with a string :term:`path
+entry`. Most path entries name locations in the file system, but they need
+not be limited to this.
+
+As a meta path finder, the :term:`path importer` implements the
:meth:`find_module()` protocol previously described, however it exposes
additional hooks that can be used to customize how modules are found and
-loaded from the file system.
+loaded from the :term:`import path`.
-Three variables are used during file system import, :data:`sys.path`,
-:data:`sys.path_hooks` and :data:`sys.path_importer_cache`. These provide
-additional ways that the import machinery can be customized, in this case
-specifically during file system path import.
+Three variables are used by the :term:`path importer`, :data:`sys.path`,
+:data:`sys.path_hooks` and :data:`sys.path_importer_cache`. The ``__path__``
+attribute on package objects is also used. These provide additional ways that
+the import machinery can be customized.
:data:`sys.path` contains a list of strings providing search locations for
modules and packages. It is initialized from the :data:`PYTHONPATH`
environment variable and various other installation- and
implementation-specific defaults. Entries in :data:`sys.path` can name
directories on the file system, zip files, and potentially other "locations"
-(see the :mod:`site` module) that should be searched for modules.
+(see the :mod:`site` module) that should be searched for modules, such as
+URLs, or database queries.
-The path importer is a meta path finder, so the import machinery begins file
-system search by calling the path importer's :meth:`find_module()` method as
-described previously. When the ``path`` argument to :meth:`find_module()` is
-given, it will be a list of string paths to traverse. If not,
+The :term:`path importer` is a :term:`meta path finder`, so the import
+machinery begins :term:`import path` search by calling the path importer's
+:meth:`find_module()` method as described previously. When the ``path``
+argument to :meth:`find_module()` is given, it will be a list of string paths
+to traverse. If the ``path`` argument is not given or is ``None``,
:data:`sys.path` is used.
-The path importer iterates over every entry in the search path, and for each
-of these, searches for an appropriate sys path finder for the path entry.
-Because this can be an expensive operation (e.g. there are `stat()` call
-overheads for this search), the path importer maintains a cache mapping path
-entries to sys path finders. This cache is maintained in
-:data:`sys.path_importer_cache`. In this way, the expensive search for a
-particular path location's sys path finder need only be done once. User code
-is free to remove cache entries from :data:`sys.path_importer_cache` forcing
-the path importer to perform the path search again [#fnpic]_.
+The :term:`path importer` iterates over every entry in the search path, and
+for each of these, looks for an appropriate :term:`path entry finder` for the
+path entry. Because this can be an expensive operation (e.g. there may be
+`stat()` call overheads for this search), the :term:`path importer` maintains
+a cache mapping path entries to path entry finders. This cache is maintained
+in :data:`sys.path_importer_cache`. In this way, the expensive search for a
+particular :term:`path entry` location's :term:`path entry finder` need only
+be done once. User code is free to remove cache entries from
+:data:`sys.path_importer_cache` forcing the :term:`path importer` to perform
+the path entry search again [#fnpic]_.
If the path entry is not present in the cache, the path importer iterates over
-every callable in :data:`sys.path_hooks`. Each entry in this list is called
-with a single argument, the path entry being searched. This callable may
-either return a sys path finder that can handle the path entry, or it may
-raise :exc:`ImportError`. An :exc:`ImportError` is used by the path importer
-to signal that the hook cannot find a sys path finder for that path entry.
-The exception is ignored and :data:`sys.path_hooks` iteration continues.
+every callable in :data:`sys.path_hooks`. Each of the :term:`path entry hooks
+<path entry hook>` in this list is called with a single argument, the path
+entry being searched. This callable may either return a :term:`path entry
+finder` that can handle the path entry, or it may raise :exc:`ImportError`.
+An :exc:`ImportError` is used by the path importer to signal that the hook
+cannot find a :term:`path entry finder` for that :term:`path entry`. The
+exception is ignored and :term:`import path` iteration continues.
-If :data:`sys.path_hooks` iteration ends with no sys path finder being
-returned then the path importer's :meth:`find_module()` method will return
-``None`` and an :exc:`ImportError` will be raised.
+If :data:`sys.path_hooks` iteration ends with no :term:`path entry finder`
+being returned, then the path importer's :meth:`find_module()` method will
+return ``None``, indicating that this :term:`meta path finder` could not find
+the module.
-If a sys path finder *is* returned by one of the callables on
-:data:`sys.path_hooks`, then the following protocol is used to ask the sys
-path finder for a module loader, which is then used to load the module as
-previously described (i.e. its :meth:`load_module()` method is called).
+If a :term:`path entry finder` *is* returned by one of the :term:`path entry
+hook` callables on :data:`sys.path_hooks`, then the following protocol is used
+to ask the finder for a module loader, which is then used to load the module.
-sys path finder protocol
-------------------------
+Path entry finder protocol
+--------------------------
-sys path finders support the same, traditional :meth:`find_module()` method
-that meta path finders support, however sys path finder :meth:`find_module()`
+Path entry finders support the same :meth:`find_module()` method that meta
+path finders support, however path entry finder's :meth:`find_module()`
methods are never called with a ``path`` argument.
-The :meth:`find_module()` method on sys path finders is deprecated though, and
-instead sys path finders should implement the :meth:`find_loader()` method.
-If it exists on the sys path finder, :meth:`find_loader()` will always be
-called instead of :meth:`find_module()`.
+The :meth:`find_module()` method on path entry finders is deprecated though,
+and instead path entry finders should implement the :meth:`find_loader()`
+method. If it exists on the path entry finder, :meth:`find_loader()` will
+always be called instead of :meth:`find_module()`.
:meth:`find_loader()` takes one argument, the fully qualified name of the
module being imported. :meth:`find_loader()` returns a 2-tuple where the
first item is the loader and the second item is a namespace :term:`portion`.
When the first item (i.e. the loader) is ``None``, this means that while the
-sys path finder does not have a loader for the named module, it knows that the
-path entry contributes to a namespace portion for the named module. This will
-almost always be the case where Python is asked to import a namespace package
-that has no physical presence on the file system. When a sys path finder
-returns ``None`` for the loader, the second item of the 2-tuple return value
-must be a sequence, although it can be empty.
+path entry finder does not have a loader for the named module, it knows that
+the :term:`path entry` contributes to a namespace portion for the named
+module. This will almost always be the case where Python is asked to import a
+:term:`namespace package` that has no physical presence on the file system.
+When a path entry finder returns ``None`` for the loader, the second item of
+the 2-tuple return value must be a sequence, although it can be empty.
If :meth:`find_loader()` returns a non-``None`` loader value, the portion is
-ignored and the loader is returned from the path importer, terminating the
-:data:`sys.path` search.
+ignored and the loader is returned from the :term:`path importer`, terminating
+the :term:`import path` search.
Open issues
===========
-XXX Find a better term than "path importer" for class PathFinder and update
-the glossary.
-
-XXX In the glossary, "though I'd change ":term:`finder` / :term:`loader`" to
-"metapath importer".
-
-XXX Find a better term than "sys path finder".
-
XXX It would be really nice to have a diagram.
XXX * (import_machinery.rst) how about a section devoted just to the
attributes of modules and packages, perhaps expanding upon or supplanting the
related entries in the data model reference page?
-XXX * (import_machinery.rst) Meta path loaders, end of paragraph 2: "The
-finder could also be a classmethod that returns an instance of the class."
+XXX Module reprs: how does module.__qualname__ fit in?
-XXX * (import_machinery.rst) Meta path loaders: "If the load fails, the loader
-needs to remove any modules..." is a pretty exceptional case, since the
-modules is not in charge of its parent or children, nor of import statements
-executed for it. Is this a new requirement?
-
-XXX Module reprs: how does module.__qualname__ fit in?
+XXX runpy, pkgutil, et al in the library manual should all get "See Also"
+links at the top pointing to the new import system section.
References
@@ -571,13 +583,21 @@
although some details have changed since the writing of that document.
The original specification for :data:`sys.meta_path` was :pep:`302`, with
-subsequent extension in :pep:`420`, which also introduced namespace packages
-without ``__init__.py`` files in Python 3.3. :pep:`420` also introduced the
-:meth:`find_loader` protocol as an alternative to :meth:`find_module`.
+subsequent extension in :pep:`420`.
+
+:pep:`420` introduced :term:`namespace packages <namespace package>` for
+Python 3.3. :pep:`420` also introduced the :meth:`find_loader` protocol as an
+alternative to :meth:`find_module`.
:pep:`366` describes the addition of the ``__package__`` attribute for
explicit relative imports in main modules.
+:pep:`328` introduced absolute and relative imports and initially proposed
+``__name__`` for semantics :pep:`366` would eventually specify for
+``__package__``.
+
+:pep:`338` defines executing modules as scripts.
+
Footnotes
=========
@@ -591,13 +611,6 @@
implementation-specific behavior that is not guaranteed to work in other
Python implementations.
-.. [#fnpk] In practice, within CPython there is little consistency in the
- values of ``__package__`` for top-level modules. In some, such as in the
- :mod:`email` package, both the ``__name__`` and ``__package__`` are set to
- "email". In other top-level modules (non-packages), ``__package__`` may be
- set to ``None`` or the empty string. The recommendation for top-level
- non-package modules is to set ``__package__`` to the empty string.
-
.. [#fnpic] In legacy code, it is possible to find instances of
:class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
recommended that code be changed to use ``None`` instead. See
diff --git a/Doc/reference/index.rst b/Doc/reference/index.rst
--- a/Doc/reference/index.rst
+++ b/Doc/reference/index.rst
@@ -24,7 +24,7 @@
lexical_analysis.rst
datamodel.rst
executionmodel.rst
- import_machinery.rst
+ import.rst
expressions.rst
simple_stmts.rst
compound_stmts.rst
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -662,14 +662,17 @@
Import statements are executed in two steps: (1) find a module, loading and
initializing it if necessary; (2) define a name or names in the local
-namespace (of the scope where the :keyword:`import` statement occurs). The
-statement comes in two forms differing on whether it uses the :keyword:`from`
-keyword. The first form (without :keyword:`from`) repeats these steps for each
-identifier in the list. The form with :keyword:`from` performs step (1) once,
-and then performs step (2) repeatedly.
+namespace (of the scope where the :keyword:`import` statement occurs).
+Step (1) may be performed recursively if the named module is a submodule or
+subpackage of a parent package.
+
+The :keyword:`import` statement comes in two forms differing on whether it
+uses the :keyword:`from` keyword. The first form (without :keyword:`from`)
+repeats these steps for each identifier in the list. The form with
+:keyword:`from` performs step (1), and then performs step (2) repeatedly.
The details of step (1), finding and loading modules is described in greater
-detail in the section on the :ref:`import machinery <importmachinery>`, which
+detail in the section on the :ref:`import system <importsystem>`, which
also describes the various types of packages and modules that can be imported,
as well as all the hooks that can be used to customize Python's import.
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/9adcc021cbc2
changeset: 78356:9adcc021cbc2
user: Barry Warsaw <barry(a)python.org>
date: Mon Jul 30 16:57:20 2012 -0400
summary:
Another XXX.
files:
Doc/reference/import_machinery.rst | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/Doc/reference/import_machinery.rst b/Doc/reference/import_machinery.rst
--- a/Doc/reference/import_machinery.rst
+++ b/Doc/reference/import_machinery.rst
@@ -540,6 +540,11 @@
XXX Find a better term than "path importer" for class PathFinder and update
the glossary.
+XXX In the glossary, "though I'd change ":term:`finder` / :term:`loader`" to
+"metapath importer".
+
+XXX Find a better term than "sys path finder".
+
XXX It would be really nice to have a diagram.
XXX * (import_machinery.rst) how about a section devoted just to the
--
Repository URL: http://hg.python.org/cpython
1
0
cpython: Address substantially all of Eric Snow's comments in issue #15295, except for
by barry.warsaw 31 Jul '12
by barry.warsaw 31 Jul '12
31 Jul '12
http://hg.python.org/cpython/rev/c933ec7cafcf
changeset: 78355:c933ec7cafcf
user: Barry Warsaw <barry(a)python.org>
date: Mon Jul 30 16:24:12 2012 -0400
summary:
Address substantially all of Eric Snow's comments in issue #15295, except for
those which now have additional XXX's here. I'll get to those later. :)
files:
Doc/glossary.rst | 2 +-
Doc/reference/import_machinery.rst | 210 ++++++++++------
Doc/whatsnew/3.3.rst | 2 +
3 files changed, 134 insertions(+), 80 deletions(-)
diff --git a/Doc/glossary.rst b/Doc/glossary.rst
--- a/Doc/glossary.rst
+++ b/Doc/glossary.rst
@@ -475,7 +475,7 @@
module
An object that serves as an organizational unit of Python code. Modules
- have a namespace contain arbitrary Python objects. Modules are loaded
+ have a namespace containing arbitrary Python objects. Modules are loaded
into Python by the process of :term:`importing`.
MRO
diff --git a/Doc/reference/import_machinery.rst b/Doc/reference/import_machinery.rst
--- a/Doc/reference/import_machinery.rst
+++ b/Doc/reference/import_machinery.rst
@@ -8,30 +8,44 @@
.. index:: single: import machinery
Python code in one :term:`module` gains access to the code in another module
-by the process of :term:`importing` it. Most commonly, the :keyword:`import`
-statement is used to invoke the import machinery, but it can also be invoked
-by calling the built-in :func:`__import__` function.
+by the process of :term:`importing` it. The :keyword:`import` statement is
+the most common way of invoking the import machinery, but it is not the only
+way. Functions such as :func:`importlib.import_module` and built-in
+:func:`__import__` can also be used to invoke the import machinery.
The :keyword:`import` statement combines two operations; it searches for the
named module, then it binds the results of that search to a name in the local
scope. The search operation of the :keyword:`import` statement is defined as
-a call to the :func:`__import__` function, with the appropriate arguments.
-The return value of :func:`__import__` is used to perform the name binding
-operation of the :keyword:`import` statement. See the :keyword:`import`
-statement for the exact details of that name binding operation.
+a call to the built-in :func:`__import__` function, with the appropriate
+arguments. The return value of :func:`__import__` is used to perform the name
+binding operation of the :keyword:`import` statement. See the
+:keyword:`import` statement for the exact details of that name binding
+operation.
-A direct call to :func:`__import__` performs only the search for the module.
-The function's return value is used like any other function call in Python;
-there is no special side-effects (e.g. name binding) associated with
-:func:`__import__`.
+A direct call to :func:`__import__` performs only the module search and, if
+found, the module creation operation. While certain side-effects may occur,
+such as the importing of parent packages, and the updating of various caches
+(including :data:`sys.modules`), only the :keyword:`import` statement performs
+a name binding operation.
When a module is first imported, Python searches for the module and if found,
-it creates a module object, initializing it. If the named module cannot be
-found, an :exc:`ImportError` is raised. Python implements various strategies
-to search for the named module when the import machinery is invoked. These
-strategies can be modified and extended by using various hooks described in
-the sections below. The entire import machinery itself can be overridden by
-replacing built-in :func:`__import__`.
+it creates a module object [#fnmo]_, initializing it. If the named module
+cannot be found, an :exc:`ImportError` is raised. Python implements various
+strategies to search for the named module when the import machinery is
+invoked. These strategies can be modified and extended by using various hooks
+described in the sections below. The entire import machinery itself can be
+overridden by replacing built-in :func:`__import__`.
+
+
+:mod:`importlib`
+================
+
+The :mod:`importlib` module provides a rich API for interacting with the
+import system. For example :func:`importlib.import_module` provides a
+recommended, simpler API than built-in :func:`__import__` for invoking the
+import machinery. Refer to the :mod:`importlib` library documentation for
+additional detail.
+
Packages
@@ -43,25 +57,26 @@
Python has only one type of module object, and all modules are of this type,
regardless of whether the module is implemented in Python, C, or something
else. To help organize modules and provide a naming hierarchy, Python has a
-concept of :term:`packages <package>`. It's important to keep in mind that
-all packages are modules, but not all modules are packages. Or put another
-way, packages are just a special kind of module. Although usually
-unnecessary, introspection of various module object attributes can determine
-whether a module is a package or not.
+concept of :term:`packages <package>`.
-Packages can contain other packages and modules, while modules generally do
-not contain other modules or packages. You can think of packages as the
-directories on a file system and modules as files within directories, but
-don't take this analogy too literally since packages and modules need not
-originate from the file system. For the purposes of this documentation, we'll
-use this convenient analogy of directories and files.
+You can think of packages as the directories on a file system and modules as
+files within directories, but don't take this analogy too literally since
+packages and modules need not originate from the file system. For the
+purposes of this documentation, we'll use this convenient analogy of
+directories and files. Like file system directories, packages are organized
+hierarchically, and packages may themselves contain subpackages, as well as
+regular modules.
-All modules have a name. Packages also have names, and subpackages can be
-nested arbitrarily deeply. Subpackage names are separated from their parent
-package by dots, akin to Python's standard attribute access syntax. Thus you
-might have a module called :mod:`sys` and a package called :mod:`email`, which
-in turn has a subpackage called :mod:`email.mime` and a module within that
-subpackage called :mod:`email.mime.text`.
+It's important to keep in mind that all packages are modules, but not all
+modules are packages. Or put another way, packages are just a special kind of
+module. Specifically, any module that contains an ``__path__`` attribute is
+considered a package.
+
+All modules have a name. Subpackage names are separated from their parent
+package name by dots, akin to Python's standard attribute access syntax. Thus
+you might have a module called :mod:`sys` and a package called :mod:`email`,
+which in turn has a subpackage called :mod:`email.mime` and a module within
+that subpackage called :mod:`email.mime.text`.
Regular packages
@@ -80,22 +95,6 @@
contain the same Python code that any other module can contain, and Python
will add some additional attributes to the module when it is imported.
-
-Namespace packages
-------------------
-
-.. index::
- pair:: package; namespace
- pair:: package; portion
-
-A namespace package is a composite of various :term:`portions <portion>`,
-where each portion contributes a subpackage to the parent package. Portions
-may reside in different locations on the file system. Portions may also be
-found in zip files, on the network, or anywhere else that Python searches
-during import. Namespace packages may or may not correspond directly to
-objects on the file system; they may be virtual modules that have no concrete
-representation.
-
For example, the following file system layout defines a top level ``parent``
package with three subpackages::
@@ -113,14 +112,31 @@
``parent.three`` will import ``parent/two/__init__.py`` and
``parent/three/__init__.py`` respectively.
+
+Namespace packages
+------------------
+
+.. index::
+ pair:: package; namespace
+ pair:: package; portion
+
+A namespace package is a composite of various :term:`portions <portion>`,
+where each portion contributes a subpackage to the parent package. Portions
+may reside in different locations on the file system. Portions may also be
+found in zip files, on the network, or anywhere else that Python searches
+during import. Namespace packages may or may not correspond directly to
+objects on the file system; they may be virtual modules that have no concrete
+representation.
+
With namespace packages, there is no ``parent/__init__.py`` file. In fact,
there may be multiple ``parent`` directories found during import search, where
-each one is provided by a separate vendor installed container, and none of
-them contain an ``__init__.py`` file. Thus ``parent/one`` may not be
+each one is provided by a different portion. Thus ``parent/one`` may not be
physically located next to ``parent/two``. In this case, Python will create a
namespace package for the top-level ``parent`` package whenever it or one of
its subpackages is imported.
+See also :pep:`420` for the namespace package specification.
+
Searching
=========
@@ -129,7 +145,7 @@
name of the module (or package, but for the purposes of this discussion, the
difference is immaterial) being imported. This name may come from various
arguments to the :keyword:`import` statement, or from the parameters to the
-:func:`__import__` function.
+:func:`importlib.import_module` or :func:`__import__` functions.
This name will be used in various phases of the import search, and it may be
the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python
@@ -156,8 +172,8 @@
:exc:`ImportError` is raised. If the module name is missing, Python will
continue searching for the module.
-:data:`sys.modules` is writable. Deleting a key will generally not destroy
-the associated module, but it will invalidate the cache entry for the named
+:data:`sys.modules` is writable. Deleting a key will not destroy the
+associated module, but it will invalidate the cache entry for the named
module, causing Python to search anew for the named module upon its next
import. Beware though, because if you keep a reference to the module object,
invalidate its cache entry in :data:`sys.modules`, and then re-import the
@@ -265,11 +281,12 @@
-----------------
Once a loader is found via a meta path finder, the loader's
-:meth:`load_module()` method is called, with a single argument, the fully
-qualified name of the module being imported. This method has several
-responsibilities, and should return the module object it has loaded [#fn1]_.
-If it cannot load the module, it should raise an :exc:`ImportError`, although
-any other exception raised during :meth:`load_module()` will be propagated.
+:meth:`~importlib.abc.Loader.load_module` method is called, with a single
+argument, the fully qualified name of the module being imported. This method
+has several responsibilities, and should return the module object it has
+loaded [#fnlo]_. If it cannot load the module, it should raise an
+:exc:`ImportError`, although any other exception raised during
+:meth:`load_module()` will be propagated.
In many cases, the meta path finder and loader can be the same object,
e.g. :meth:`finder.find_module()` would just return ``self``.
@@ -278,8 +295,8 @@
* If there is an existing module object with the given name in
:data:`sys.modules`, the loader must use that existing module. (Otherwise,
- the :func:`reload()` builtin will not work correctly.) If the named module
- does not exist in :data:`sys.modules`, the loader must create a new module
+ the :func:`imp.reload` will not work correctly.) If the named module does
+ not exist in :data:`sys.modules`, the loader must create a new module
object and add it to :data:`sys.modules`.
Note that the module *must* exist in :data:`sys.modules` before the loader
@@ -314,28 +331,29 @@
* The module's ``__package__`` attribute should be set. Its value must be a
string, but it can be the same value as its ``__name__``. This is the
recommendation when the module is a package. When the module is not a
- package, ``__package__`` should be set to the parent package's name.
+ package, ``__package__`` should be set to the parent package's
+ name [#fnpk]_.
This attribute is used instead of ``__name__`` to calculate explicit
relative imports for main modules, as defined in :pep:`366`.
* If the module is a Python module (as opposed to a built-in module or a
- dynamically loaded extension), it should execute the module's code in the
- module's global name space (``module.__dict__``).
+ dynamically loaded extension), the loader should execute the module's code
+ in the module's global name space (``module.__dict__``).
Module reprs
------------
By default, all modules have a usable repr, however depending on the
-attributes set above, and hooks in the loader, you can more tightly control
+attributes set above, and hooks in the loader, you can more explicitly control
the repr of module objects.
Loaders may implement a :meth:`module_repr()` method which takes a single
argument, the module object. When ``repr(module)`` is called for a module
with a loader supporting this protocol, whatever is returned from
-``loader.module_repr(module)`` is returned as the module's repr without
-further processing. This return value must be a string.
+``module.__loader__.module_repr(module)`` is returned as the module's repr
+without further processing. This return value must be a string.
If the module has no ``__loader__`` attribute, or the loader has no
:meth:`module_repr()` method, then the module object implementation itself
@@ -385,7 +403,7 @@
``__path__`` must be a list, but it may be empty. The same rules used for
:data:`sys.path` also apply to a package's ``__path__``, and
-:data:`sys.path_hooks` (described below) are consulted when traversing a
+:data:`sys.path_hooks` (described below) is consulted when traversing a
package's ``__path__``.
A package's ``__init__.py`` file may set or alter the package's ``__path__``
@@ -452,7 +470,7 @@
environment variable and various other installation- and
implementation-specific defaults. Entries in :data:`sys.path` can name
directories on the file system, zip files, and potentially other "locations"
-that should be searched for modules.
+(see the :mod:`site` module) that should be searched for modules.
The path importer is a meta path finder, so the import machinery begins file
system search by calling the path importer's :meth:`find_module()` method as
@@ -468,7 +486,7 @@
:data:`sys.path_importer_cache`. In this way, the expensive search for a
particular path location's sys path finder need only be done once. User code
is free to remove cache entries from :data:`sys.path_importer_cache` forcing
-the path importer to perform the path search again.
+the path importer to perform the path search again [#fnpic]_.
If the path entry is not present in the cache, the path importer iterates over
every callable in :data:`sys.path_hooks`. Each entry in this list is called
@@ -484,9 +502,8 @@
If a sys path finder *is* returned by one of the callables on
:data:`sys.path_hooks`, then the following protocol is used to ask the sys
-path finder for a module loader. If a loader results from this step, it is
-used to load the module as previously described (i.e. its
-:meth:`load_module()` method is called).
+path finder for a module loader, which is then used to load the module as
+previously described (i.e. its :meth:`load_module()` method is called).
sys path finder protocol
@@ -520,14 +537,24 @@
Open issues
===========
-XXX What to say about `imp.NullImporter` when it's found in
-:data:`sys.path_importer_cache`?
+XXX Find a better term than "path importer" for class PathFinder and update
+the glossary.
XXX It would be really nice to have a diagram.
-.. [#fn1] The importlib implementation appears not to use the return value
- directly. Instead, it gets the module object by looking the module name up
- in ``sys.modules``.)
+XXX * (import_machinery.rst) how about a section devoted just to the
+attributes of modules and packages, perhaps expanding upon or supplanting the
+related entries in the data model reference page?
+
+XXX * (import_machinery.rst) Meta path loaders, end of paragraph 2: "The
+finder could also be a classmethod that returns an instance of the class."
+
+XXX * (import_machinery.rst) Meta path loaders: "If the load fails, the loader
+needs to remove any modules..." is a pretty exceptional case, since the
+modules is not in charge of its parent or children, nor of import statements
+executed for it. Is this a new requirement?
+
+XXX Module reprs: how does module.__qualname__ fit in?
References
@@ -545,3 +572,28 @@
:pep:`366` describes the addition of the ``__package__`` attribute for
explicit relative imports in main modules.
+
+
+Footnotes
+=========
+
+.. [#fnmo] See :class:`types.ModuleType`.
+
+.. [#fnlo] The importlib implementation appears not to use the return value
+ directly. Instead, it gets the module object by looking the module name up
+ in :data:`sys.modules`.) The indirect effect of this is that an imported
+ module may replace itself in :data:`sys.modules`. This is
+ implementation-specific behavior that is not guaranteed to work in other
+ Python implementations.
+
+.. [#fnpk] In practice, within CPython there is little consistency in the
+ values of ``__package__`` for top-level modules. In some, such as in the
+ :mod:`email` package, both the ``__name__`` and ``__package__`` are set to
+ "email". In other top-level modules (non-packages), ``__package__`` may be
+ set to ``None`` or the empty string. The recommendation for top-level
+ non-package modules is to set ``__package__`` to the empty string.
+
+.. [#fnpic] In legacy code, it is possible to find instances of
+ :class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
+ recommended that code be changed to use ``None`` instead. See
+ :ref:`portingpythoncode` for more details.
diff --git a/Doc/whatsnew/3.3.rst b/Doc/whatsnew/3.3.rst
--- a/Doc/whatsnew/3.3.rst
+++ b/Doc/whatsnew/3.3.rst
@@ -1677,6 +1677,8 @@
This section lists previously described changes and other bugfixes
that may require changes to your code.
+.. _portingpythoncode:
+
Porting Python code
-------------------
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/deed2a674a1c
changeset: 78354:deed2a674a1c
parent: 78353:d406e1392bbc
parent: 78336:2f3ccf4ec193
user: Barry Warsaw <barry(a)python.org>
date: Mon Jul 30 14:34:43 2012 -0400
summary:
merge
files:
Lib/idlelib/NEWS.txt | 3 +
Lib/idlelib/macosxSupport.py | 16 +++-
Lib/test/test_memoryio.py | 11 +++
Lib/tkinter/simpledialog.py | 2 +-
Mac/BuildScript/build-installer.py | 57 ++++++++++++++---
Misc/ACKS | 1 +
Misc/NEWS | 13 +++-
Modules/_io/bytesio.c | 12 +++
Python/traceback.c | 2 +-
setup.py | 4 +-
10 files changed, 99 insertions(+), 22 deletions(-)
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -34,6 +34,9 @@
- Issue #3573: IDLE hangs when passing invalid command line args
(directory(ies) instead of file(s)).
+- Issue #14018: Update checks for unstable system Tcl/Tk versions on OS X
+ to include versions shipped with OS X 10.7 and 10.8 in addition to 10.6.
+
What's New in IDLE 3.2.1?
=========================
diff --git a/Lib/idlelib/macosxSupport.py b/Lib/idlelib/macosxSupport.py
--- a/Lib/idlelib/macosxSupport.py
+++ b/Lib/idlelib/macosxSupport.py
@@ -37,17 +37,21 @@
def tkVersionWarning(root):
"""
Returns a string warning message if the Tk version in use appears to
- be one known to cause problems with IDLE. The Apple Cocoa-based Tk 8.5
- that was shipped with Mac OS X 10.6.
+ be one known to cause problems with IDLE.
+ 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable.
+ 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but
+ can still crash unexpectedly.
"""
if (runningAsOSXApp() and
- ('AppKit' in root.tk.call('winfo', 'server', '.')) and
- (root.tk.call('info', 'patchlevel') == '8.5.7') ):
- return (r"WARNING: The version of Tcl/Tk (8.5.7) in use may"
+ ('AppKit' in root.tk.call('winfo', 'server', '.')) ):
+ patchlevel = root.tk.call('info', 'patchlevel')
+ if patchlevel not in ('8.5.7', '8.5.9'):
+ return False
+ return (r"WARNING: The version of Tcl/Tk ({0}) in use may"
r" be unstable.\n"
r"Visit http://www.python.org/download/mac/tcltk/"
- r" for current information.")
+ r" for current information.".format(patchlevel))
else:
return False
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -654,6 +654,17 @@
memio.close()
self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None))
+ check_sizeof = support.check_sizeof
+
+ @support.cpython_only
+ def test_sizeof(self):
+ basesize = support.calcobjsize('P2nN2Pn')
+ check = self.check_sizeof
+ self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
+ check(io.BytesIO(), basesize )
+ check(io.BytesIO(b'a'), basesize + 1 + 1 )
+ check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
+
class CStringIOTest(PyStringIOTest):
ioclass = io.StringIO
diff --git a/Lib/tkinter/simpledialog.py b/Lib/tkinter/simpledialog.py
--- a/Lib/tkinter/simpledialog.py
+++ b/Lib/tkinter/simpledialog.py
@@ -282,7 +282,7 @@
self.entry = Entry(master, name="entry")
self.entry.grid(row=1, padx=5, sticky=W+E)
- if self.initialvalue:
+ if self.initialvalue is not None:
self.entry.insert(0, self.initialvalue)
self.entry.select_range(0, END)
diff --git a/Mac/BuildScript/build-installer.py b/Mac/BuildScript/build-installer.py
--- a/Mac/BuildScript/build-installer.py
+++ b/Mac/BuildScript/build-installer.py
@@ -161,6 +161,13 @@
--universal-archs=x universal architectures (options: %(UNIVERSALOPTS)r, default: %(UNIVERSALARCHS)r)
""")% globals()
+# Dict of object file names with shared library names to check after building.
+# This is to ensure that we ended up dynamically linking with the shared
+# library paths and versions we expected. For example:
+# EXPECTED_SHARED_LIBS['_tkinter.so'] = [
+# '/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl',
+# '/Library/Frameworks/Tk.framework/Versions/8.5/Tk']
+EXPECTED_SHARED_LIBS = {}
# Instructions for building libraries that are necessary for building a
# batteries included python.
@@ -460,27 +467,40 @@
# Because we only support dynamic load of only one major/minor version of
# Tcl/Tk, ensure:
# 1. there are no user-installed frameworks of Tcl/Tk with version
- # higher than the Apple-supplied system version
- # 2. there is a user-installed framework in /Library/Frameworks with the
- # same version as the system version. This allows users to choose
- # to install a newer patch level.
+ # higher than the Apple-supplied system version in
+ # SDKROOT/System/Library/Frameworks
+ # 2. there is a user-installed framework (usually ActiveTcl) in (or linked
+ # in) SDKROOT/Library/Frameworks with the same version as the system
+ # version. This allows users to choose to install a newer patch level.
+ frameworks = {}
for framework in ['Tcl', 'Tk']:
- #fw = dict(lower=framework.lower(),
- # upper=framework.upper(),
- # cap=framework.capitalize())
- #fwpth = "Library/Frameworks/%(cap)s.framework/%(lower)sConfig.sh" % fw
- fwpth = 'Library/Frameworks/Tcl.framework/Versions/Current'
+ fwpth = 'Library/Frameworks/%s.framework/Versions/Current' % framework
sysfw = os.path.join(SDKPATH, 'System', fwpth)
- libfw = os.path.join('/', fwpth)
+ libfw = os.path.join(SDKPATH, fwpth)
usrfw = os.path.join(os.getenv('HOME'), fwpth)
- #version = "%(upper)s_VERSION" % fw
+ frameworks[framework] = os.readlink(sysfw)
+ if not os.path.exists(libfw):
+ fatal("Please install a link to a current %s %s as %s so "
+ "the user can override the system framework."
+ % (framework, frameworks[framework], libfw))
if os.readlink(libfw) != os.readlink(sysfw):
fatal("Version of %s must match %s" % (libfw, sysfw) )
if os.path.exists(usrfw):
fatal("Please rename %s to avoid possible dynamic load issues."
% usrfw)
+ if frameworks['Tcl'] != frameworks['Tk']:
+ fatal("The Tcl and Tk frameworks are not the same version.")
+
+ # add files to check after build
+ EXPECTED_SHARED_LIBS['_tkinter.so'] = [
+ "/Library/Frameworks/Tcl.framework/Versions/%s/Tcl"
+ % frameworks['Tcl'],
+ "/Library/Frameworks/Tk.framework/Versions/%s/Tk"
+ % frameworks['Tk'],
+ ]
+
# Remove inherited environment variables which might influence build
environ_var_prefixes = ['CPATH', 'C_INCLUDE_', 'DYLD_', 'LANG', 'LC_',
'LD_', 'LIBRARY_', 'PATH', 'PYTHON']
@@ -861,12 +881,12 @@
frmDir = os.path.join(rootDir, 'Library', 'Frameworks', 'Python.framework')
gid = grp.getgrnam('admin').gr_gid
+ shared_lib_error = False
for dirpath, dirnames, filenames in os.walk(frmDir):
for dn in dirnames:
os.chmod(os.path.join(dirpath, dn), STAT_0o775)
os.chown(os.path.join(dirpath, dn), -1, gid)
-
for fn in filenames:
if os.path.islink(fn):
continue
@@ -877,6 +897,19 @@
os.chmod(p, stat.S_IMODE(st.st_mode) | stat.S_IWGRP)
os.chown(p, -1, gid)
+ if fn in EXPECTED_SHARED_LIBS:
+ # check to see that this file was linked with the
+ # expected library path and version
+ data = captureCommand("otool -L %s" % shellQuote(p))
+ for sl in EXPECTED_SHARED_LIBS[fn]:
+ if ("\t%s " % sl) not in data:
+ print("Expected shared lib %s was not linked with %s"
+ % (sl, p))
+ shared_lib_error = True
+
+ if shared_lib_error:
+ fatal("Unexpected shared library errors.")
+
if PYTHON_3:
LDVERSION=None
VERSION=None
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -700,6 +700,7 @@
Alexis Métaireau
Steven Miale
Trent Mick
+Tom Middleton
Stan Mihai
Stefan Mihaila
Aristotelis Mikropoulos
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,9 @@
Library
-------
+- Issue #15463: the faulthandler module truncates strings to 500 characters,
+ instead of 100, to be able to display long file paths
+
- Issue #6056: Make multiprocessing use setblocking(True) on the
sockets it uses. Original patch by J Derek Wilson.
@@ -267,8 +270,10 @@
Build
-----
+- Issue #14018: Fix OS X Tcl/Tk framework checking when using OS X SDKs.
+
- Issue #15431: Add _freeze_importlib project to regenerate importlib.h
- on Windows. Patch by Kristj�n Valur J�nsson.
+ on Windows. Patch by Kristján Valur Jónsson.
- Issue #14197: For OS X framework builds, ensure links to the shared
library are created with the proper ABI suffix.
@@ -337,6 +342,12 @@
Library
-------
+- Issue #12288: Consider '0' and '0.0' as valid initialvalue
+ for tkinter SimpleDialog.
+
+- Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
+ Patch by Serhiy Storchaka.
+
- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Patch by Serhiy Storchaka.
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -834,6 +834,17 @@
return 0;
}
+static PyObject *
+bytesio_sizeof(bytesio *self, void *unused)
+{
+ Py_ssize_t res;
+
+ res = sizeof(bytesio);
+ if (self->buf)
+ res += self->buf_size;
+ return PyLong_FromSsize_t(res);
+}
+
static int
bytesio_traverse(bytesio *self, visitproc visit, void *arg)
{
@@ -876,6 +887,7 @@
{"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
{"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
{"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
+ {"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
{NULL, NULL} /* sentinel */
};
diff --git a/Python/traceback.c b/Python/traceback.c
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -14,7 +14,7 @@
#define OFF(x) offsetof(PyTracebackObject, x)
#define PUTS(fd, str) write(fd, str, strlen(str))
-#define MAX_STRING_LENGTH 100
+#define MAX_STRING_LENGTH 500
#define MAX_FRAME_DEPTH 100
#define MAX_NTHREADS 100
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -69,7 +69,9 @@
"""
Returns True if 'path' can be located in an OSX SDK
"""
- return (path.startswith('/usr/') and not path.startswith('/usr/local')) or path.startswith('/System/')
+ return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
+ or path.startswith('/System/')
+ or path.startswith('/Library/') )
def find_file(filename, std_dirs, paths):
"""Searches for the directory where a given file is located,
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/d406e1392bbc
changeset: 78353:d406e1392bbc
parent: 78352:a391185ab5b2
parent: 78319:1d811e1097ed
user: Barry Warsaw <barry(a)python.org>
date: Sun Jul 29 16:40:04 2012 -0400
summary:
merged
files:
Lib/importlib/_bootstrap.py | 89 +-
Lib/test/support.py | 28 +
Lib/test/test_import.py | 68 +
Lib/test/test_io.py | 20 +-
Lib/test/test_struct.py | 33 +-
Lib/test/test_sys.py | 214 +-
Lib/test/test_xml_etree_c.py | 24 +-
Misc/NEWS | 11 +
Modules/_freeze_importlib.c | 11 +-
Modules/_io/bufferedio.c | 14 +
Modules/_struct.c | 8 +-
PCbuild/pcbuild.sln | 6 -
Python/import.c | 30 +-
Python/importlib.h | 8060 +++++++++++----------
14 files changed, 4511 insertions(+), 4105 deletions(-)
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -722,6 +722,56 @@
return _imp.init_frozen(fullname)
+class WindowsRegistryImporter:
+
+ """Meta path import for modules declared in the Windows registry.
+ """
+
+ REGISTRY_KEY = (
+ "Software\\Python\\PythonCore\\{sys_version}"
+ "\\Modules\\{fullname}")
+ REGISTRY_KEY_DEBUG = (
+ "Software\\Python\\PythonCore\\{sys_version}"
+ "\\Modules\\{fullname}\\Debug")
+ DEBUG_BUILD = False # Changed in _setup()
+
+ @classmethod
+ def _open_registry(cls, key):
+ try:
+ return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
+ except WindowsError:
+ return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
+
+ @classmethod
+ def _search_registry(cls, fullname):
+ if cls.DEBUG_BUILD:
+ registry_key = cls.REGISTRY_KEY_DEBUG
+ else:
+ registry_key = cls.REGISTRY_KEY
+ key = registry_key.format(fullname=fullname,
+ sys_version=sys.version[:3])
+ try:
+ with cls._open_registry(key) as hkey:
+ filepath = _winreg.QueryValue(hkey, "")
+ except WindowsError:
+ return None
+ return filepath
+
+ @classmethod
+ def find_module(cls, fullname, path=None):
+ """Find module named in the registry."""
+ filepath = cls._search_registry(fullname)
+ if filepath is None:
+ return None
+ try:
+ _os.stat(filepath)
+ except OSError:
+ return None
+ for loader, suffixes, _ in _get_supported_file_loaders():
+ if filepath.endswith(tuple(suffixes)):
+ return loader(fullname, filepath)
+
+
class _LoaderBasics:
"""Base class of common code needed by both SourceLoader and
@@ -1422,7 +1472,7 @@
parent = name.rpartition('.')[0]
if parent:
if parent not in sys.modules:
- import_(parent)
+ _recursive_import(import_, parent)
# Crazy side-effects!
if name in sys.modules:
return sys.modules[name]
@@ -1500,6 +1550,12 @@
_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.
@@ -1519,7 +1575,8 @@
fromlist.extend(module.__all__)
for x in fromlist:
if not hasattr(module, x):
- import_('{}.{}'.format(module.__name__, x))
+ _recursive_import(import_,
+ '{}.{}'.format(module.__name__, x))
return module
@@ -1538,6 +1595,17 @@
return package
+def _get_supported_file_loaders():
+ """Returns a list of file-based module loaders.
+
+ Each item is a tuple (loader, suffixes, allow_packages).
+ """
+ extensions = ExtensionFileLoader, _imp.extension_suffixes(), False
+ source = SourceFileLoader, SOURCE_SUFFIXES, True
+ bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES, True
+ return [extensions, source, bytecode]
+
+
def __import__(name, globals={}, locals={}, fromlist=[], level=0):
"""Import a module.
@@ -1620,6 +1688,10 @@
thread_module = None
weakref_module = BuiltinImporter.load_module('_weakref')
+ if builtin_os == 'nt':
+ winreg_module = BuiltinImporter.load_module('winreg')
+ setattr(self_module, '_winreg', winreg_module)
+
setattr(self_module, '_os', os_module)
setattr(self_module, '_thread', thread_module)
setattr(self_module, '_weakref', weakref_module)
@@ -1629,14 +1701,17 @@
setattr(self_module, '_relax_case', _make_relax_case())
if builtin_os == 'nt':
SOURCE_SUFFIXES.append('.pyw')
+ if '_d.pyd' in _imp.extension_suffixes():
+ WindowsRegistryImporter.DEBUG_BUILD = True
def _install(sys_module, _imp_module):
"""Install importlib as the implementation of import."""
_setup(sys_module, _imp_module)
- extensions = ExtensionFileLoader, _imp_module.extension_suffixes(), False
- source = SourceFileLoader, SOURCE_SUFFIXES, True
- bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES, True
- supported_loaders = [extensions, source, bytecode]
+ supported_loaders = _get_supported_file_loaders()
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
- sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])
+ sys.meta_path.append(BuiltinImporter)
+ sys.meta_path.append(FrozenImporter)
+ if _os.__name__ == 'nt':
+ sys.meta_path.append(WindowsRegistryImporter)
+ sys.meta_path.append(PathFinder)
diff --git a/Lib/test/support.py b/Lib/test/support.py
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -25,6 +25,7 @@
import logging.handlers
import struct
import tempfile
+import _testcapi
try:
import _thread, threading
@@ -1082,6 +1083,33 @@
return final_opt != '' and final_opt != '-O0'
+_header = 'nP'
+_align = '0n'
+if hasattr(sys, "gettotalrefcount"):
+ _header = '2P' + _header
+ _align = '0P'
+_vheader = _header + 'n'
+
+def calcobjsize(fmt):
+ return struct.calcsize(_header + fmt + _align)
+
+def calcvobjsize(fmt):
+ return struct.calcsize(_vheader + fmt + _align)
+
+
+_TPFLAGS_HAVE_GC = 1<<14
+_TPFLAGS_HEAPTYPE = 1<<9
+
+def check_sizeof(test, o, size):
+ result = sys.getsizeof(o)
+ # add GC header size
+ if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\
+ ((type(o) != type) and (type(o).__flags__ & _TPFLAGS_HAVE_GC))):
+ size += _testcapi.SIZEOF_PYGC_HEAD
+ msg = 'wrong size for %s: got %d, expected %d' \
+ % (type(o), result, size)
+ test.assertEqual(result, size, msg)
+
#=======================================================================
# Decorator for running a function in a different locale, correctly resetting
# it afterwards.
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -844,6 +844,74 @@
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py'])
+ # A few more examples from issue #15425
+ def test_syntax_error(self):
+ self.create_module("foo", "invalid syntax is invalid")
+ try:
+ import foo
+ except SyntaxError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("SyntaxError should have been raised")
+ self.assert_traceback(tb, [__file__])
+
+ 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)
+ # Touch the __init__.py
+ init_path = os.path.join(pkg_name, '__init__.py')
+ with open(init_path, 'w') as f:
+ f.write(parent)
+ bar_path = os.path.join(pkg_name, 'bar.py')
+ with open(bar_path, 'w') as f:
+ f.write(child)
+ importlib.invalidate_caches()
+ return init_path, bar_path
+
+ def test_broken_submodule(self):
+ init_path, bar_path = self._setup_broken_package("", "1/0")
+ try:
+ import _parent_foo.bar
+ except ZeroDivisionError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("ZeroDivisionError should have been raised")
+ self.assert_traceback(tb, [__file__, bar_path])
+
+ def test_broken_from(self):
+ init_path, bar_path = self._setup_broken_package("", "1/0")
+ try:
+ from _parent_foo import bar
+ except ZeroDivisionError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("ImportError should have been raised")
+ self.assert_traceback(tb, [__file__, bar_path])
+
+ def test_broken_parent(self):
+ init_path, bar_path = self._setup_broken_package("1/0", "")
+ try:
+ import _parent_foo.bar
+ except ZeroDivisionError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("ZeroDivisionError should have been raised")
+ self.assert_traceback(tb, [__file__, init_path])
+
+ def test_broken_parent_from(self):
+ init_path, bar_path = self._setup_broken_package("1/0", "")
+ try:
+ from _parent_foo import bar
+ except ZeroDivisionError as e:
+ tb = e.__traceback__
+ else:
+ self.fail("ZeroDivisionError should have been raised")
+ self.assert_traceback(tb, [__file__, init_path])
+
@cpython_only
def test_import_bug(self):
# We simulate a bug in importlib and check that it's not stripped
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -802,6 +802,20 @@
buf.raw = x
+class SizeofTest:
+
+ @support.cpython_only
+ def test_sizeof(self):
+ bufsize1 = 4096
+ bufsize2 = 8192
+ rawio = self.MockRawIO()
+ bufio = self.tp(rawio, buffer_size=bufsize1)
+ size = sys.getsizeof(bufio) - bufsize1
+ rawio = self.MockRawIO()
+ bufio = self.tp(rawio, buffer_size=bufsize2)
+ self.assertEqual(sys.getsizeof(bufio), size + bufsize2)
+
+
class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
read_mode = "rb"
@@ -999,7 +1013,7 @@
"failed for {}: {} != 0".format(n, rawio._extraneous_reads))
-class CBufferedReaderTest(BufferedReaderTest):
+class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
tp = io.BufferedReader
def test_constructor(self):
@@ -1260,7 +1274,7 @@
self.tp(self.MockRawIO(), 8, 12)
-class CBufferedWriterTest(BufferedWriterTest):
+class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
tp = io.BufferedWriter
def test_constructor(self):
@@ -1650,7 +1664,7 @@
# You can't construct a BufferedRandom over a non-seekable stream.
test_unseekable = None
-class CBufferedRandomTest(BufferedRandomTest):
+class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
tp = io.BufferedRandom
def test_constructor(self):
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -3,7 +3,7 @@
import struct
import sys
-from test.support import run_unittest
+from test import support
ISBIGENDIAN = sys.byteorder == "big"
IS32BIT = sys.maxsize == 0x7fffffff
@@ -572,18 +572,29 @@
s = struct.Struct('i')
s.__init__('ii')
- def test_sizeof(self):
- self.assertGreater(sys.getsizeof(struct.Struct('BHILfdspP')),
- sys.getsizeof(struct.Struct('B')))
- self.assertGreater(sys.getsizeof(struct.Struct('123B')),
- sys.getsizeof(struct.Struct('B')))
- self.assertGreater(sys.getsizeof(struct.Struct('B' * 1234)),
- sys.getsizeof(struct.Struct('123B')))
- self.assertGreater(sys.getsizeof(struct.Struct('1234B')),
- sys.getsizeof(struct.Struct('123B')))
+ def check_sizeof(self, format_str, number_of_codes):
+ # The size of 'PyStructObject'
+ totalsize = support.calcobjsize('2n3P')
+ # The size taken up by the 'formatcode' dynamic array
+ totalsize += struct.calcsize('P2n0P') * (number_of_codes + 1)
+ support.check_sizeof(self, struct.Struct(format_str), totalsize)
+
+ @support.cpython_only
+ def test__sizeof__(self):
+ for code in integer_codes:
+ self.check_sizeof(code, 1)
+ self.check_sizeof('BHILfdspP', 9)
+ self.check_sizeof('B' * 1234, 1234)
+ self.check_sizeof('fd', 2)
+ self.check_sizeof('xxxxxxxxxxxxxx', 0)
+ self.check_sizeof('100H', 100)
+ self.check_sizeof('187s', 1)
+ self.check_sizeof('20p', 1)
+ self.check_sizeof('0s', 1)
+ self.check_sizeof('0c', 0)
def test_main():
- run_unittest(StructTest)
+ support.run_unittest(StructTest)
if __name__ == '__main__':
test_main()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -612,22 +612,8 @@
class SizeofTest(unittest.TestCase):
- TPFLAGS_HAVE_GC = 1<<14
- TPFLAGS_HEAPTYPE = 1<<9
-
def setUp(self):
- self.c = len(struct.pack('c', b' '))
- self.H = len(struct.pack('H', 0))
- self.i = len(struct.pack('i', 0))
- self.l = len(struct.pack('l', 0))
- self.P = len(struct.pack('P', 0))
- # due to missing size_t information from struct, it is assumed that
- # sizeof(Py_ssize_t) = sizeof(void*)
- self.header = 'PP'
- self.vheader = self.header + 'P'
- if hasattr(sys, "gettotalrefcount"):
- self.header += '2P'
- self.vheader += '2P'
+ self.P = struct.calcsize('P')
self.longdigit = sys.int_info.sizeof_digit
import _testcapi
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
@@ -637,129 +623,108 @@
self.file.close()
test.support.unlink(test.support.TESTFN)
- def check_sizeof(self, o, size):
- result = sys.getsizeof(o)
- # add GC header size
- if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
- ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
- size += self.gc_headsize
- msg = 'wrong size for %s: got %d, expected %d' \
- % (type(o), result, size)
- self.assertEqual(result, size, msg)
-
- def calcsize(self, fmt):
- """Wrapper around struct.calcsize which enforces the alignment of the
- end of a structure to the alignment requirement of pointer.
-
- Note: This wrapper should only be used if a pointer member is included
- and no member with a size larger than a pointer exists.
- """
- return struct.calcsize(fmt + '0P')
+ 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.
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ vsize = test.support.calcvobjsize
gc_header_size = self.gc_headsize
# bool objects are not gc tracked
- self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
+ self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
# but lists are
- self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)
+ self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)
def test_default(self):
- h = self.header
- vh = self.vheader
- size = self.calcsize
- self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
- self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)
+ 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/
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# bool
- check(True, size(vh) + self.longdigit)
+ check(True, vsize('') + self.longdigit)
# buffer
# XXX
# builtin_function_or_method
- check(len, size(h + '3P'))
+ check(len, size('3P')) # XXX check layout
# bytearray
samples = [b'', b'u'*100000]
for sample in samples:
x = bytearray(sample)
- check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
+ check(x, vsize('inP') + x.__alloc__())
# bytearray_iterator
- check(iter(bytearray()), size(h + 'PP'))
+ check(iter(bytearray()), size('nP'))
# cell
def get_cell():
x = 42
def inner():
return x
return inner
- check(get_cell().__closure__[0], size(h + 'P'))
+ check(get_cell().__closure__[0], size('P'))
# code
- check(get_cell().__code__, size(h + '5i9Pi3P'))
- check(get_cell.__code__, size(h + '5i9Pi3P'))
+ check(get_cell().__code__, size('5i9Pi3P'))
+ check(get_cell.__code__, size('5i9Pi3P'))
def get_cell2(x):
def inner():
return x
return inner
- check(get_cell2.__code__, size(h + '5i9Pi3P') + 1)
+ check(get_cell2.__code__, size('5i9Pi3P') + 1)
# complex
- check(complex(0,1), size(h + '2d'))
+ check(complex(0,1), size('2d'))
# method_descriptor (descriptor object)
- check(str.lower, size(h + '3PP'))
+ check(str.lower, size('3PP'))
# classmethod_descriptor (descriptor object)
# XXX
# member_descriptor (descriptor object)
import datetime
- check(datetime.timedelta.days, size(h + '3PP'))
+ check(datetime.timedelta.days, size('3PP'))
# getset_descriptor (descriptor object)
import collections
- check(collections.defaultdict.default_factory, size(h + '3PP'))
+ check(collections.defaultdict.default_factory, size('3PP'))
# wrapper_descriptor (descriptor object)
- check(int.__add__, size(h + '3P2P'))
+ check(int.__add__, size('3P2P'))
# method-wrapper (descriptor object)
- check({}.__iter__, size(h + '2P'))
+ check({}.__iter__, size('2P'))
# dict
- check({}, size(h + '3P' + '4P' + 8*'P2P'))
+ check({}, size('n2P' + '2nPn' + 8*'n2P'))
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
- check(longdict, size(h + '3P' + '4P') + 16*size('P2P'))
+ check(longdict, size('n2P' + '2nPn') + 16*struct.calcsize('n2P'))
# dictionary-keyiterator
- check({}.keys(), size(h + 'P'))
+ check({}.keys(), size('P'))
# dictionary-valueiterator
- check({}.values(), size(h + 'P'))
+ check({}.values(), size('P'))
# dictionary-itemiterator
- check({}.items(), size(h + 'P'))
+ check({}.items(), size('P'))
+ # dictionary iterator
+ check(iter({}), size('P2nPn'))
# dictproxy
class C(object): pass
- check(C.__dict__, size(h + 'P'))
+ check(C.__dict__, size('P'))
# BaseException
- check(BaseException(), size(h + '5Pi'))
+ check(BaseException(), size('5Pi'))
# UnicodeEncodeError
- check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5Pi 2P2PP'))
+ check(UnicodeEncodeError("", "", 0, 0, ""), size('5Pi 2P2nP'))
# UnicodeDecodeError
- # XXX
-# check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
+ check(UnicodeDecodeError("", b"", 0, 0, ""), size('5Pi 2P2nP'))
# UnicodeTranslateError
- check(UnicodeTranslateError("", 0, 1, ""), size(h + '5Pi 2P2PP'))
+ check(UnicodeTranslateError("", 0, 1, ""), size('5Pi 2P2nP'))
# ellipses
- check(Ellipsis, size(h + ''))
+ check(Ellipsis, size(''))
# EncodingMap
import codecs, encodings.iso8859_3
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
- check(x, size(h + '32B2iB'))
+ check(x, size('32B2iB'))
# enumerate
- check(enumerate([]), size(h + 'l3P'))
+ check(enumerate([]), size('n3P'))
# reverse
- check(reversed(''), size(h + 'PP'))
+ check(reversed(''), size('nP'))
# float
- check(float(0), size(h + 'd'))
+ check(float(0), size('d'))
# sys.floatinfo
- check(sys.float_info, size(vh) + self.P * len(sys.float_info))
+ check(sys.float_info, vsize('') + self.P * len(sys.float_info))
# frame
import inspect
CO_MAXBLOCKS = 20
@@ -768,10 +733,10 @@
nfrees = len(x.f_code.co_freevars)
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
ncells + nfrees - 1
- check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
+ check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
# function
def func(): pass
- check(func, size(h + '12P'))
+ check(func, size('12P'))
class c():
@staticmethod
def foo():
@@ -780,68 +745,68 @@
def bar(cls):
pass
# staticmethod
- check(foo, size(h + 'PP'))
+ check(foo, size('PP'))
# classmethod
- check(bar, size(h + 'PP'))
+ check(bar, size('PP'))
# generator
def get_gen(): yield 1
- check(get_gen(), size(h + 'Pi2P'))
+ check(get_gen(), size('Pb2P'))
# iterator
- check(iter('abc'), size(h + 'lP'))
+ check(iter('abc'), size('lP'))
# callable-iterator
import re
- check(re.finditer('',''), size(h + '2P'))
+ check(re.finditer('',''), size('2P'))
# list
samples = [[], [1,2,3], ['1', '2', '3']]
for sample in samples:
- check(sample, size(vh + 'PP') + len(sample)*self.P)
+ check(sample, vsize('Pn') + len(sample)*self.P)
# sortwrapper (list)
# XXX
# cmpwrapper (list)
# XXX
# listiterator (list)
- check(iter([]), size(h + 'lP'))
+ check(iter([]), size('lP'))
# listreverseiterator (list)
- check(reversed([]), size(h + 'lP'))
+ check(reversed([]), size('nP'))
# long
- check(0, size(vh))
- check(1, size(vh) + self.longdigit)
- check(-1, size(vh) + self.longdigit)
+ 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), size(vh) + 2*self.longdigit)
- check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
- check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
+ 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)
# memoryview
- check(memoryview(b''), size(h + 'PPiP4P2i5P3c2P'))
+ check(memoryview(b''), size('Pnin 2P2n2i5P 3cPn'))
# module
- check(unittest, size(h + '3P'))
+ check(unittest, size('PnP'))
# None
- check(None, size(h + ''))
+ check(None, size(''))
# NotImplementedType
- check(NotImplemented, size(h))
+ check(NotImplemented, size(''))
# object
- check(object(), size(h + ''))
+ 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(h + '4Pi'))
+ check(x, size('4Pi'))
# PyCapsule
# XXX
# rangeiterator
- check(iter(range(1)), size(h + '4l'))
+ check(iter(range(1)), size('4l'))
# reverse
- check(reversed(''), size(h + 'PP'))
+ check(reversed(''), size('nP'))
# range
- check(range(1), size(h + '4P'))
- check(range(66000), size(h + '4P'))
+ check(range(1), size('4P'))
+ check(range(66000), size('4P'))
# set
# frozenset
PySet_MINSIZE = 8
samples = [[], range(10), range(50)]
- s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
+ s = size('3n2P' + PySet_MINSIZE*'nP' + 'nP')
for sample in samples:
minused = len(sample)
if minused == 0: tmp = 1
@@ -855,31 +820,31 @@
check(set(sample), s)
check(frozenset(sample), s)
else:
- check(set(sample), s + newsize*struct.calcsize('lP'))
- check(frozenset(sample), s + newsize*struct.calcsize('lP'))
+ check(set(sample), s + newsize*struct.calcsize('nP'))
+ check(frozenset(sample), s + newsize*struct.calcsize('nP'))
# setiterator
- check(iter(set()), size(h + 'P3P'))
+ check(iter(set()), size('P3n'))
# slice
- check(slice(0), size(h + '3P'))
+ check(slice(0), size('3P'))
# super
- check(super(int), size(h + '3P'))
+ check(super(int), size('3P'))
# tuple
- check((), size(vh))
- check((1,2,3), size(vh) + 3*self.P)
+ check((), vsize(''))
+ check((1,2,3), vsize('') + 3*self.P)
# type
# static type: PyTypeObject
- s = size(vh + 'P2P15Pl4PP9PP11PI')
+ s = vsize('P2n15Pl4Pn9Pn11PI')
check(int, s)
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
# PySequenceMethods + PyBufferProcs + 4P)
- s = size(vh + 'P2P15Pl4PP9PP11PI') + size('34P 3P 10P 2P 4P')
+ s = vsize('P2n15Pl4Pn9Pn11PI') + struct.calcsize('34P 3P 10P 2P 4P')
# Separate block for PyDictKeysObject with 4 entries
- s += size("PPPP") + 4*size("PPP")
+ s += struct.calcsize("2nPn") + 4*struct.calcsize("n2P")
# class
class newstyleclass(object): pass
check(newstyleclass, s)
# dict with shared keys
- check(newstyleclass().__dict__, size(h+"PPP4P"))
+ check(newstyleclass().__dict__, size('n2P' + '2nPn'))
# unicode
# each tuple contains a string and its expected character size
# don't put any static strings here, as they may contain
@@ -887,8 +852,8 @@
samples = ['1'*100, '\xff'*50,
'\u0100'*40, '\uffff'*100,
'\U00010000'*30, '\U0010ffff'*100]
- asciifields = h + "PPiP"
- compactfields = asciifields + "PPP"
+ asciifields = "nniP"
+ compactfields = asciifields + "nPn"
unicodefields = compactfields + "P"
for s in samples:
maxchar = ord(max(s))
@@ -912,32 +877,31 @@
# TODO: add check that forces layout of unicodefields
# weakref
import weakref
- check(weakref.ref(int), size(h + '2Pl2P'))
+ check(weakref.ref(int), size('2Pn2P'))
# weakproxy
# XXX
# weakcallableproxy
- check(weakref.proxy(int), size(h + '2Pl2P'))
+ check(weakref.proxy(int), size('2Pn2P'))
def test_pythontypes(self):
# check all types defined in Python/
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
- check(_ast.AST(), size(h + 'P'))
+ check(_ast.AST(), size('P'))
try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
# traceback
if tb != None:
- check(tb, size(h + '2P2i'))
+ check(tb, size('2P2i'))
# symtable entry
# XXX
# sys.flags
- check(sys.flags, size(vh) + self.P * len(sys.flags))
+ check(sys.flags, vsize('') + self.P * len(sys.flags))
def test_main():
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
--- a/Lib/test/test_xml_etree_c.py
+++ b/Lib/test/test_xml_etree_c.py
@@ -41,38 +41,30 @@
@unittest.skipUnless(cET, 'requires _elementtree')
+(a)support.cpython_only
class SizeofTest(unittest.TestCase):
def setUp(self):
- import _testcapi
- gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
- # object header
- header = 'PP'
- if hasattr(sys, "gettotalrefcount"):
- # debug header
- header = 'PP' + header
- # fields
- element = header + '5P'
- self.elementsize = gc_headsize + struct.calcsize(element)
+ self.elementsize = support.calcobjsize('5P')
# extra
self.extra = struct.calcsize('PiiP4P')
+ check_sizeof = support.check_sizeof
+
def test_element(self):
e = cET.Element('a')
- self.assertEqual(sys.getsizeof(e), self.elementsize)
+ self.check_sizeof(e, self.elementsize)
def test_element_with_attrib(self):
e = cET.Element('a', href='about:')
- self.assertEqual(sys.getsizeof(e),
- self.elementsize + self.extra)
+ self.check_sizeof(e, self.elementsize + self.extra)
def test_element_with_children(self):
e = cET.Element('a')
for i in range(5):
cET.SubElement(e, 'span')
# should have space for 8 children now
- self.assertEqual(sys.getsizeof(e),
- self.elementsize + self.extra +
- struct.calcsize('8P'))
+ self.check_sizeof(e, self.elementsize + self.extra +
+ struct.calcsize('8P'))
def test_main():
from test import test_xml_etree, test_xml_etree_c
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@
Core and Builtins
-----------------
+- Issue #15425: Eliminated traceback noise from more situations involving
+ importlib
+
+- Issue #14578: Support modules registered in the Windows registry again.
+
- Issue #15466: Stop using TYPE_INT64 in marshal, to make importlib.h
(and other byte code files) equal between 32-bit and 64-bit systems.
@@ -236,6 +241,9 @@
Tests
-----
+- Issue #15467: Move helpers for __sizeof__ tests into test_support.
+ Patch by Serhiy Storchaka.
+
- Issue #15320: Make iterating the list of tests thread-safe when running
tests in multiprocess mode. Patch by Chris Jerdonek.
@@ -329,6 +337,9 @@
Library
-------
+- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
+ Patch by Serhiy Storchaka.
+
- Issue #15187: Bugfix: remove temporary directories test_shutil was leaving
behind.
diff --git a/Modules/_freeze_importlib.c b/Modules/_freeze_importlib.c
--- a/Modules/_freeze_importlib.c
+++ b/Modules/_freeze_importlib.c
@@ -21,6 +21,13 @@
{0, 0, 0} /* sentinel */
};
+#ifndef MS_WINDOWS
+/* On Windows, this links with the regular pythonXY.dll, so this variable comes
+ from frozen.obj. In the Makefile, frozen.o is not linked into this executable,
+ so we define the variable here. */
+struct _frozen *PyImport_FrozenModules;
+#endif
+
const char header[] = "/* Auto-generated by Modules/_freeze_importlib.c */";
int
@@ -91,8 +98,8 @@
data_size = PyBytes_GET_SIZE(marshalled);
/* Open the file in text mode. The hg checkout should be using the eol extension,
- which in turn should cause the existing file to use CRLF */
- outfile = fopen(outpath, "wt");
+ which in turn should cause the EOL style match the C library's text mode */
+ outfile = fopen(outpath, "w");
if (outfile == NULL) {
fprintf(stderr, "cannot open '%s' for writing\n", outpath);
return 1;
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -398,6 +398,17 @@
Py_TYPE(self)->tp_free((PyObject *)self);
}
+static PyObject *
+buffered_sizeof(buffered *self, void *unused)
+{
+ Py_ssize_t res;
+
+ res = sizeof(buffered);
+ if (self->buffer)
+ res += self->buffer_size;
+ return PyLong_FromSsize_t(res);
+}
+
static int
buffered_traverse(buffered *self, visitproc visit, void *arg)
{
@@ -1699,6 +1710,7 @@
{"seek", (PyCFunction)buffered_seek, METH_VARARGS},
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
{"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
+ {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
{NULL, NULL}
};
@@ -2079,6 +2091,7 @@
{"flush", (PyCFunction)buffered_flush, METH_NOARGS},
{"seek", (PyCFunction)buffered_seek, METH_VARARGS},
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
+ {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
{NULL, NULL}
};
@@ -2470,6 +2483,7 @@
{"readline", (PyCFunction)buffered_readline, METH_VARARGS},
{"peek", (PyCFunction)buffered_peek, METH_VARARGS},
{"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
+ {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
{NULL, NULL}
};
diff --git a/Modules/_struct.c b/Modules/_struct.c
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1756,15 +1756,11 @@
"S.__sizeof__() -> size of S in memory, in bytes");
static PyObject *
-s_sizeof(PyStructObject *self)
+s_sizeof(PyStructObject *self, void *unused)
{
Py_ssize_t size;
- formatcode *code;
- size = sizeof(PyStructObject) + sizeof(formatcode);
- for (code = self->s_codes; code->fmtdef != NULL; code++) {
- size += sizeof(formatcode);
- }
+ size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1);
return PyLong_FromSsize_t(size);
}
diff --git a/PCbuild/pcbuild.sln b/PCbuild/pcbuild.sln
--- a/PCbuild/pcbuild.sln
+++ b/PCbuild/pcbuild.sln
@@ -603,19 +603,13 @@
{1D4B18D3-7C12-4ECB-9179-8531FF876CE6}.Release|x64.ActiveCfg = Release|Win32
{1D4B18D3-7C12-4ECB-9179-8531FF876CE6}.Release|x64.Build.0 = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Debug|Win32.ActiveCfg = Debug|Win32
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Debug|Win32.Build.0 = Debug|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Debug|x64.ActiveCfg = Debug|x64
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Debug|x64.Build.0 = Debug|x64
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGInstrument|Win32.ActiveCfg = Release|Win32
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGInstrument|Win32.Build.0 = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGInstrument|x64.ActiveCfg = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGUpdate|Win32.ActiveCfg = Release|Win32
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGUpdate|Win32.Build.0 = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.PGUpdate|x64.ActiveCfg = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Release|Win32.ActiveCfg = Release|Win32
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Release|Win32.Build.0 = Release|Win32
{19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Release|x64.ActiveCfg = Release|x64
- {19C0C13F-47CA-4432-AFF3-799A296A4DDC}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1154,14 +1154,27 @@
{
const char *importlib_filename = "<frozen importlib._bootstrap>";
const char *exec_funcname = "_exec_module";
+ const char *get_code_funcname = "get_code";
+ const char *recursive_import = "_recursive_import";
int always_trim = 0;
+ int trim_get_code = 0;
int in_importlib = 0;
PyObject *exception, *value, *base_tb, *tb;
PyObject **prev_link, **outer_link = NULL;
/* Synopsis: if it's an ImportError, we trim all importlib chunks
- from the traceback. Otherwise, we trim only those chunks which
- end with a call to "_exec_module". */
+ from the traceback. If it's a SyntaxError, we trim any chunks that
+ end with a call to "get_code", We always trim chunks
+ which end with a call to "_exec_module". */
+
+ /* Thanks to issue 15425, we also strip any chunk ending with
+ * _recursive_import. This is used when making a recursive call to the
+ * full import machinery which means the inner stack gets stripped early
+ * and the normal heuristics won't fire properly for outer frames. A
+ * more elegant mechanism would be nice, as this one can misfire if
+ * builtins.__import__ has been replaced with a custom implementation.
+ * However, the current approach at least gets the job done.
+ */
PyErr_Fetch(&exception, &value, &base_tb);
if (!exception || Py_VerboseFlag)
@@ -1169,6 +1182,9 @@
if (PyType_IsSubtype((PyTypeObject *) exception,
(PyTypeObject *) PyExc_ImportError))
always_trim = 1;
+ if (PyType_IsSubtype((PyTypeObject *) exception,
+ (PyTypeObject *) PyExc_SyntaxError))
+ trim_get_code = 1;
prev_link = &base_tb;
tb = base_tb;
@@ -1191,8 +1207,14 @@
if (in_importlib &&
(always_trim ||
- PyUnicode_CompareWithASCIIString(code->co_name,
- exec_funcname) == 0)) {
+ (PyUnicode_CompareWithASCIIString(code->co_name,
+ exec_funcname) == 0) ||
+ (PyUnicode_CompareWithASCIIString(code->co_name,
+ recursive_import) == 0) ||
+ (trim_get_code &&
+ PyUnicode_CompareWithASCIIString(code->co_name,
+ get_code_funcname) == 0)
+ )) {
PyObject *tmp = *outer_link;
*outer_link = next;
Py_XINCREF(next);
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/a391185ab5b2
changeset: 78352:a391185ab5b2
user: Barry Warsaw <barry(a)python.org>
date: Sun Jul 29 16:37:33 2012 -0400
summary:
Add NEWS
files:
Misc/NEWS | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -213,6 +213,9 @@
Documentation
-------------
+- Issue #15295: Reorganize and rewrite the documentation on the import
+ machinery.
+
- Issue #15230: Clearly document some of the limitations of the runpy
module and nudge readers towards importlib when appropriate.
--
Repository URL: http://hg.python.org/cpython
1
0