2012-05-19 13:34:13 -03:00
|
|
|
:mod:`types` --- Dynamic type creation and names for built-in types
|
|
|
|
===================================================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. module:: types
|
|
|
|
:synopsis: Names for built-in types.
|
|
|
|
|
2011-01-26 21:20:32 -04:00
|
|
|
**Source code:** :source:`Lib/types.py`
|
|
|
|
|
|
|
|
--------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
This module defines utility function to assist in dynamic creation of
|
|
|
|
new types.
|
|
|
|
|
|
|
|
It also defines names for some object types that are used by the standard
|
2008-08-23 12:14:57 -03:00
|
|
|
Python interpreter, but not exposed as builtins like :class:`int` or
|
2012-05-19 13:34:13 -03:00
|
|
|
:class:`str` are.
|
|
|
|
|
2014-02-25 17:03:14 -04:00
|
|
|
Finally, it provides some additional type-related utility classes and functions
|
|
|
|
that are not fundamental enough to be builtins.
|
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
|
|
|
|
Dynamic Type Creation
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
.. function:: new_class(name, bases=(), kwds=None, exec_body=None)
|
|
|
|
|
|
|
|
Creates a class object dynamically using the appropriate metaclass.
|
|
|
|
|
2012-05-30 09:17:30 -03:00
|
|
|
The first three arguments are the components that make up a class
|
|
|
|
definition header: the class name, the base classes (in order), the
|
|
|
|
keyword arguments (such as ``metaclass``).
|
2012-05-19 13:34:13 -03:00
|
|
|
|
2012-05-30 09:17:30 -03:00
|
|
|
The *exec_body* argument is a callback that is used to populate the
|
|
|
|
freshly created class namespace. It should accept the class namespace
|
|
|
|
as its sole argument and update the namespace directly with the class
|
|
|
|
contents. If no callback is provided, it has the same effect as passing
|
|
|
|
in ``lambda ns: ns``.
|
2012-05-19 13:34:13 -03:00
|
|
|
|
2012-05-22 10:02:00 -03:00
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
.. function:: prepare_class(name, bases=(), kwds=None)
|
|
|
|
|
|
|
|
Calculates the appropriate metaclass and creates the class namespace.
|
|
|
|
|
2012-05-30 09:17:30 -03:00
|
|
|
The arguments are the components that make up a class definition header:
|
|
|
|
the class name, the base classes (in order) and the keyword arguments
|
|
|
|
(such as ``metaclass``).
|
2012-05-19 13:34:13 -03:00
|
|
|
|
|
|
|
The return value is a 3-tuple: ``metaclass, namespace, kwds``
|
|
|
|
|
2012-05-30 09:17:30 -03:00
|
|
|
*metaclass* is the appropriate metaclass, *namespace* is the
|
|
|
|
prepared class namespace and *kwds* is an updated copy of the passed
|
|
|
|
in *kwds* argument with any ``'metaclass'`` entry removed. If no *kwds*
|
|
|
|
argument is passed in, this will be an empty dict.
|
2012-05-19 13:34:13 -03:00
|
|
|
|
2012-05-22 10:02:00 -03:00
|
|
|
.. versionadded:: 3.3
|
2012-05-19 13:34:13 -03:00
|
|
|
|
2016-09-05 18:50:11 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
|
|
|
|
The default value for the ``namespace`` element of the returned
|
2016-09-08 19:11:11 -03:00
|
|
|
tuple has changed. Now an insertion-order-preserving mapping is
|
|
|
|
used when the metaclass does not have a ``__prepare__`` method,
|
2016-09-05 18:50:11 -03:00
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
.. seealso::
|
|
|
|
|
2012-05-30 09:17:30 -03:00
|
|
|
:ref:`metaclasses`
|
|
|
|
Full details of the class creation process supported by these functions
|
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
:pep:`3115` - Metaclasses in Python 3000
|
|
|
|
Introduced the ``__prepare__`` namespace hook
|
|
|
|
|
|
|
|
|
|
|
|
Standard Interpreter Types
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
This module provides names for many of the types that are required to
|
|
|
|
implement a Python interpreter. It deliberately avoids including some of
|
|
|
|
the types that arise only incidentally during processing such as the
|
|
|
|
``listiterator`` type.
|
2008-08-23 12:14:57 -03:00
|
|
|
|
2012-08-26 01:33:10 -03:00
|
|
|
Typical use of these names is for :func:`isinstance` or
|
2012-05-19 13:34:13 -03:00
|
|
|
:func:`issubclass` checks.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
Standard names are defined for the following types:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. data:: FunctionType
|
2007-09-04 04:23:09 -03:00
|
|
|
LambdaType
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-05-19 13:34:13 -03:00
|
|
|
The type of user-defined functions and functions created by
|
|
|
|
:keyword:`lambda` expressions.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: GeneratorType
|
|
|
|
|
2015-09-10 22:26:54 -03:00
|
|
|
The type of :term:`generator`-iterator objects, created by
|
|
|
|
generator functions.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
.. data:: CoroutineType
|
|
|
|
|
2015-09-10 22:26:54 -03:00
|
|
|
The type of :term:`coroutine` objects, created by
|
|
|
|
:keyword:`async def` functions.
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.5
|
|
|
|
|
|
|
|
|
2016-12-15 18:36:05 -04:00
|
|
|
.. data:: AsyncGeneratorType
|
|
|
|
|
|
|
|
The type of :term:`asynchronous generator`-iterator objects, created by
|
|
|
|
asynchronous generator functions.
|
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. data:: CodeType
|
|
|
|
|
|
|
|
.. index:: builtin: compile
|
|
|
|
|
|
|
|
The type for code objects such as returned by :func:`compile`.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: MethodType
|
|
|
|
|
|
|
|
The type of methods of user-defined class instances.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: BuiltinFunctionType
|
2007-09-04 04:23:09 -03:00
|
|
|
BuiltinMethodType
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-08-23 12:14:57 -03:00
|
|
|
The type of built-in functions like :func:`len` or :func:`sys.exit`, and
|
|
|
|
methods of built-in classes. (Here, the term "built-in" means "written in
|
|
|
|
C".)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2013-06-14 20:19:57 -03:00
|
|
|
.. class:: ModuleType(name, doc=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-06-14 20:19:57 -03:00
|
|
|
The type of :term:`modules <module>`. Constructor takes the name of the
|
|
|
|
module to be created and optionally its :term:`docstring`.
|
|
|
|
|
2014-05-30 15:55:29 -03:00
|
|
|
.. note::
|
|
|
|
Use :func:`importlib.util.module_from_spec` to create a new module if you
|
|
|
|
wish to set the various import-controlled attributes.
|
|
|
|
|
2013-06-14 20:19:57 -03:00
|
|
|
.. attribute:: __doc__
|
|
|
|
|
|
|
|
The :term:`docstring` of the module. Defaults to ``None``.
|
|
|
|
|
|
|
|
.. attribute:: __loader__
|
|
|
|
|
|
|
|
The :term:`loader` which loaded the module. Defaults to ``None``.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Defaults to ``None``. Previously the attribute was optional.
|
|
|
|
|
|
|
|
.. attribute:: __name__
|
|
|
|
|
|
|
|
The name of the module.
|
|
|
|
|
|
|
|
.. attribute:: __package__
|
|
|
|
|
|
|
|
Which :term:`package` a module belongs to. If the module is top-level
|
|
|
|
(i.e. not a part of any specific package) then the attribute should be set
|
|
|
|
to ``''``, else it should be set to the name of the package (which can be
|
|
|
|
:attr:`__name__` if the module is a package itself). Defaults to ``None``.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Defaults to ``None``. Previously the attribute was optional.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: TracebackType
|
|
|
|
|
|
|
|
The type of traceback objects such as found in ``sys.exc_info()[2]``.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: FrameType
|
|
|
|
|
|
|
|
The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
|
|
|
|
traceback object.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: GetSetDescriptorType
|
|
|
|
|
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62194 | jeffrey.yasskin | 2008-04-07 01:04:28 +0200 (Mon, 07 Apr 2008) | 7 lines
Add enough debugging information to diagnose failures where the
HandlerBException is ignored, and fix one such problem, where it was thrown
during the __del__ method of the previous Popen object.
We may want to find a better way of printing verbose information so it's not
spammy when the test passes.
........
r62197 | mark.hammond | 2008-04-07 03:53:39 +0200 (Mon, 07 Apr 2008) | 2 lines
Issue #2513: enable 64bit cross compilation on windows.
........
r62198 | mark.hammond | 2008-04-07 03:59:40 +0200 (Mon, 07 Apr 2008) | 2 lines
correct heading underline for new "Cross-compiling on Windows" section
........
r62204 | gregory.p.smith | 2008-04-07 08:33:21 +0200 (Mon, 07 Apr 2008) | 4 lines
Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly
within the standard library. These modules use PyFile_AsFile and later
release the GIL while operating on the previously returned FILE*.
........
r62205 | mark.summerfield | 2008-04-07 09:39:23 +0200 (Mon, 07 Apr 2008) | 4 lines
changed "2500 components" to "several thousand" since the number keeps
growning:-)
........
r62214 | georg.brandl | 2008-04-07 20:51:59 +0200 (Mon, 07 Apr 2008) | 2 lines
#2525: update timezone info examples in the docs.
........
r62219 | andrew.kuchling | 2008-04-08 01:57:07 +0200 (Tue, 08 Apr 2008) | 1 line
Write PEP 3127 section; add items
........
r62220 | andrew.kuchling | 2008-04-08 01:57:21 +0200 (Tue, 08 Apr 2008) | 1 line
Typo fix
........
r62221 | andrew.kuchling | 2008-04-08 03:33:10 +0200 (Tue, 08 Apr 2008) | 1 line
Typographical fix: 32bit -> 32-bit, 64bit -> 64-bit
........
r62227 | andrew.kuchling | 2008-04-08 23:22:53 +0200 (Tue, 08 Apr 2008) | 1 line
Add items
........
r62229 | amaury.forgeotdarc | 2008-04-08 23:27:42 +0200 (Tue, 08 Apr 2008) | 7 lines
Issue2564: Prevent a hang in "import test.autotest", which runs the entire test
suite as a side-effect of importing the module.
- in test_capi, a thread tried to import other modules
- re.compile() imported sre_parse again on every call.
........
r62230 | amaury.forgeotdarc | 2008-04-08 23:51:57 +0200 (Tue, 08 Apr 2008) | 2 lines
Prevent an error when inspect.isabstract() is called with something else than a new-style class.
........
r62231 | amaury.forgeotdarc | 2008-04-09 00:07:05 +0200 (Wed, 09 Apr 2008) | 8 lines
Issue 2408: remove the _types module
It was only used as a helper in types.py to access types (GetSetDescriptorType and MemberDescriptorType),
when they can easily be obtained with python code.
These expressions even work with Jython.
I don't know what the future of the types module is; (cf. discussion in http://bugs.python.org/issue1605 )
at least this change makes it simpler.
........
r62233 | amaury.forgeotdarc | 2008-04-09 01:10:07 +0200 (Wed, 09 Apr 2008) | 2 lines
Add a NEWS entry for previous checkin
........
r62234 | trent.nelson | 2008-04-09 01:47:30 +0200 (Wed, 09 Apr 2008) | 37 lines
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
........
r62235 | gregory.p.smith | 2008-04-09 02:25:17 +0200 (Wed, 09 Apr 2008) | 3 lines
Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
It tried to allocate negative or zero memory. That fails.
........
r62237 | trent.nelson | 2008-04-09 02:34:53 +0200 (Wed, 09 Apr 2008) | 1 line
Fix typo with regards to self.PORT shadowing class variables with the same name.
........
r62238 | andrew.kuchling | 2008-04-09 03:08:32 +0200 (Wed, 09 Apr 2008) | 1 line
Add items
........
r62239 | jerry.seutter | 2008-04-09 07:07:58 +0200 (Wed, 09 Apr 2008) | 1 line
Changed test so it no longer runs as a side effect of importing.
........
2008-04-09 05:37:03 -03:00
|
|
|
The type of objects defined in extension modules with ``PyGetSetDef``, such
|
|
|
|
as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
|
|
|
|
descriptor for object attributes; it has the same purpose as the
|
|
|
|
:class:`property` type, but for classes defined in extension modules.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: MemberDescriptorType
|
|
|
|
|
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62194 | jeffrey.yasskin | 2008-04-07 01:04:28 +0200 (Mon, 07 Apr 2008) | 7 lines
Add enough debugging information to diagnose failures where the
HandlerBException is ignored, and fix one such problem, where it was thrown
during the __del__ method of the previous Popen object.
We may want to find a better way of printing verbose information so it's not
spammy when the test passes.
........
r62197 | mark.hammond | 2008-04-07 03:53:39 +0200 (Mon, 07 Apr 2008) | 2 lines
Issue #2513: enable 64bit cross compilation on windows.
........
r62198 | mark.hammond | 2008-04-07 03:59:40 +0200 (Mon, 07 Apr 2008) | 2 lines
correct heading underline for new "Cross-compiling on Windows" section
........
r62204 | gregory.p.smith | 2008-04-07 08:33:21 +0200 (Mon, 07 Apr 2008) | 4 lines
Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly
within the standard library. These modules use PyFile_AsFile and later
release the GIL while operating on the previously returned FILE*.
........
r62205 | mark.summerfield | 2008-04-07 09:39:23 +0200 (Mon, 07 Apr 2008) | 4 lines
changed "2500 components" to "several thousand" since the number keeps
growning:-)
........
r62214 | georg.brandl | 2008-04-07 20:51:59 +0200 (Mon, 07 Apr 2008) | 2 lines
#2525: update timezone info examples in the docs.
........
r62219 | andrew.kuchling | 2008-04-08 01:57:07 +0200 (Tue, 08 Apr 2008) | 1 line
Write PEP 3127 section; add items
........
r62220 | andrew.kuchling | 2008-04-08 01:57:21 +0200 (Tue, 08 Apr 2008) | 1 line
Typo fix
........
r62221 | andrew.kuchling | 2008-04-08 03:33:10 +0200 (Tue, 08 Apr 2008) | 1 line
Typographical fix: 32bit -> 32-bit, 64bit -> 64-bit
........
r62227 | andrew.kuchling | 2008-04-08 23:22:53 +0200 (Tue, 08 Apr 2008) | 1 line
Add items
........
r62229 | amaury.forgeotdarc | 2008-04-08 23:27:42 +0200 (Tue, 08 Apr 2008) | 7 lines
Issue2564: Prevent a hang in "import test.autotest", which runs the entire test
suite as a side-effect of importing the module.
- in test_capi, a thread tried to import other modules
- re.compile() imported sre_parse again on every call.
........
r62230 | amaury.forgeotdarc | 2008-04-08 23:51:57 +0200 (Tue, 08 Apr 2008) | 2 lines
Prevent an error when inspect.isabstract() is called with something else than a new-style class.
........
r62231 | amaury.forgeotdarc | 2008-04-09 00:07:05 +0200 (Wed, 09 Apr 2008) | 8 lines
Issue 2408: remove the _types module
It was only used as a helper in types.py to access types (GetSetDescriptorType and MemberDescriptorType),
when they can easily be obtained with python code.
These expressions even work with Jython.
I don't know what the future of the types module is; (cf. discussion in http://bugs.python.org/issue1605 )
at least this change makes it simpler.
........
r62233 | amaury.forgeotdarc | 2008-04-09 01:10:07 +0200 (Wed, 09 Apr 2008) | 2 lines
Add a NEWS entry for previous checkin
........
r62234 | trent.nelson | 2008-04-09 01:47:30 +0200 (Wed, 09 Apr 2008) | 37 lines
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
........
r62235 | gregory.p.smith | 2008-04-09 02:25:17 +0200 (Wed, 09 Apr 2008) | 3 lines
Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
It tried to allocate negative or zero memory. That fails.
........
r62237 | trent.nelson | 2008-04-09 02:34:53 +0200 (Wed, 09 Apr 2008) | 1 line
Fix typo with regards to self.PORT shadowing class variables with the same name.
........
r62238 | andrew.kuchling | 2008-04-09 03:08:32 +0200 (Wed, 09 Apr 2008) | 1 line
Add items
........
r62239 | jerry.seutter | 2008-04-09 07:07:58 +0200 (Wed, 09 Apr 2008) | 1 line
Changed test so it no longer runs as a side effect of importing.
........
2008-04-09 05:37:03 -03:00
|
|
|
The type of objects defined in extension modules with ``PyMemberDef``, such
|
|
|
|
as ``datetime.timedelta.days``. This type is used as descriptor for simple C
|
|
|
|
data members which use standard conversion functions; it has the same purpose
|
|
|
|
as the :class:`property` type, but for classes defined in extension modules.
|
Merged revisions 75365,75394,75402-75403,75418,75459,75484,75592-75596,75600,75602-75607,75610-75613,75616-75617,75623,75627,75640,75647,75696,75795 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r75365 | georg.brandl | 2009-10-11 22:16:16 +0200 (So, 11 Okt 2009) | 1 line
Fix broken links found by "make linkcheck". scipy.org seems to be done right now, so I could not verify links going there.
........
r75394 | georg.brandl | 2009-10-13 20:10:59 +0200 (Di, 13 Okt 2009) | 1 line
Fix markup.
........
r75402 | georg.brandl | 2009-10-14 17:51:48 +0200 (Mi, 14 Okt 2009) | 1 line
#7125: fix typo.
........
r75403 | georg.brandl | 2009-10-14 17:57:46 +0200 (Mi, 14 Okt 2009) | 1 line
#7126: os.environ changes *do* take effect in subprocesses started with os.system().
........
r75418 | georg.brandl | 2009-10-14 20:48:32 +0200 (Mi, 14 Okt 2009) | 1 line
#7116: str.join() takes an iterable.
........
r75459 | georg.brandl | 2009-10-17 10:57:43 +0200 (Sa, 17 Okt 2009) | 1 line
Fix refleaks in _ctypes PyCSimpleType_New, which fixes the refleak seen in test___all__.
........
r75484 | georg.brandl | 2009-10-18 09:58:12 +0200 (So, 18 Okt 2009) | 1 line
Fix missing word.
........
r75592 | georg.brandl | 2009-10-22 09:05:48 +0200 (Do, 22 Okt 2009) | 1 line
Fix punctuation.
........
r75593 | georg.brandl | 2009-10-22 09:06:49 +0200 (Do, 22 Okt 2009) | 1 line
Revert unintended change.
........
r75594 | georg.brandl | 2009-10-22 09:56:02 +0200 (Do, 22 Okt 2009) | 1 line
Fix markup.
........
r75595 | georg.brandl | 2009-10-22 09:56:56 +0200 (Do, 22 Okt 2009) | 1 line
Fix duplicate target.
........
r75596 | georg.brandl | 2009-10-22 10:05:04 +0200 (Do, 22 Okt 2009) | 1 line
Add a new directive marking up implementation details and start using it.
........
r75600 | georg.brandl | 2009-10-22 13:01:46 +0200 (Do, 22 Okt 2009) | 1 line
Make it more robust.
........
r75602 | georg.brandl | 2009-10-22 13:28:06 +0200 (Do, 22 Okt 2009) | 1 line
Document new directive.
........
r75603 | georg.brandl | 2009-10-22 13:28:23 +0200 (Do, 22 Okt 2009) | 1 line
Allow short form with text as argument.
........
r75604 | georg.brandl | 2009-10-22 13:36:50 +0200 (Do, 22 Okt 2009) | 1 line
Fix stylesheet for multi-paragraph impl-details.
........
r75605 | georg.brandl | 2009-10-22 13:48:10 +0200 (Do, 22 Okt 2009) | 1 line
Use "impl-detail" directive where applicable.
........
r75606 | georg.brandl | 2009-10-22 17:00:06 +0200 (Do, 22 Okt 2009) | 1 line
#6324: membership test tries iteration via __iter__.
........
r75607 | georg.brandl | 2009-10-22 17:04:09 +0200 (Do, 22 Okt 2009) | 1 line
#7088: document new functions in signal as Unix-only.
........
r75610 | georg.brandl | 2009-10-22 17:27:24 +0200 (Do, 22 Okt 2009) | 1 line
Reorder __slots__ fine print and add a clarification.
........
r75611 | georg.brandl | 2009-10-22 17:42:32 +0200 (Do, 22 Okt 2009) | 1 line
#7035: improve docs of the various <method>_errors() functions, and give them docstrings.
........
r75612 | georg.brandl | 2009-10-22 17:52:15 +0200 (Do, 22 Okt 2009) | 1 line
#7156: document curses as Unix-only.
........
r75613 | georg.brandl | 2009-10-22 17:54:35 +0200 (Do, 22 Okt 2009) | 1 line
#6977: getopt does not support optional option arguments.
........
r75616 | georg.brandl | 2009-10-22 18:17:05 +0200 (Do, 22 Okt 2009) | 1 line
Add proper references.
........
r75617 | georg.brandl | 2009-10-22 18:20:55 +0200 (Do, 22 Okt 2009) | 1 line
Make printout margin important.
........
r75623 | georg.brandl | 2009-10-23 10:14:44 +0200 (Fr, 23 Okt 2009) | 1 line
#7188: fix optionxform() docs.
........
r75627 | fred.drake | 2009-10-23 15:04:51 +0200 (Fr, 23 Okt 2009) | 2 lines
add further note about what's passed to optionxform
........
r75640 | neil.schemenauer | 2009-10-23 21:58:17 +0200 (Fr, 23 Okt 2009) | 2 lines
Improve some docstrings in the 'warnings' module.
........
r75647 | georg.brandl | 2009-10-24 12:04:19 +0200 (Sa, 24 Okt 2009) | 1 line
Fix markup.
........
r75696 | georg.brandl | 2009-10-25 21:25:43 +0100 (So, 25 Okt 2009) | 1 line
Fix a demo.
........
r75795 | georg.brandl | 2009-10-27 16:10:22 +0100 (Di, 27 Okt 2009) | 1 line
Fix a strange mis-edit.
........
2009-10-27 12:28:25 -03:00
|
|
|
|
|
|
|
.. impl-detail::
|
|
|
|
|
|
|
|
In other implementations of Python, this type may be identical to
|
|
|
|
``GetSetDescriptorType``.
|
2012-04-15 19:16:30 -03:00
|
|
|
|
|
|
|
.. class:: MappingProxyType(mapping)
|
|
|
|
|
|
|
|
Read-only proxy of a mapping. It provides a dynamic view on the mapping's
|
|
|
|
entries, which means that when the mapping changes, the view reflects these
|
|
|
|
changes.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
|
|
.. describe:: key in proxy
|
|
|
|
|
|
|
|
Return ``True`` if the underlying mapping has a key *key*, else
|
|
|
|
``False``.
|
|
|
|
|
|
|
|
.. describe:: proxy[key]
|
|
|
|
|
|
|
|
Return the item of the underlying mapping with key *key*. Raises a
|
|
|
|
:exc:`KeyError` if *key* is not in the underlying mapping.
|
|
|
|
|
|
|
|
.. describe:: iter(proxy)
|
|
|
|
|
|
|
|
Return an iterator over the keys of the underlying mapping. This is a
|
|
|
|
shortcut for ``iter(proxy.keys())``.
|
|
|
|
|
|
|
|
.. describe:: len(proxy)
|
|
|
|
|
|
|
|
Return the number of items in the underlying mapping.
|
|
|
|
|
|
|
|
.. method:: copy()
|
|
|
|
|
|
|
|
Return a shallow copy of the underlying mapping.
|
|
|
|
|
|
|
|
.. method:: get(key[, default])
|
|
|
|
|
|
|
|
Return the value for *key* if *key* is in the underlying mapping, else
|
|
|
|
*default*. If *default* is not given, it defaults to ``None``, so that
|
|
|
|
this method never raises a :exc:`KeyError`.
|
|
|
|
|
|
|
|
.. method:: items()
|
|
|
|
|
|
|
|
Return a new view of the underlying mapping's items (``(key, value)``
|
|
|
|
pairs).
|
|
|
|
|
|
|
|
.. method:: keys()
|
|
|
|
|
|
|
|
Return a new view of the underlying mapping's keys.
|
|
|
|
|
|
|
|
.. method:: values()
|
|
|
|
|
|
|
|
Return a new view of the underlying mapping's values.
|
|
|
|
|
|
|
|
|
2014-02-25 17:03:14 -04:00
|
|
|
Additional Utility Classes and Functions
|
|
|
|
----------------------------------------
|
|
|
|
|
2012-06-03 17:18:47 -03:00
|
|
|
.. class:: SimpleNamespace
|
|
|
|
|
|
|
|
A simple :class:`object` subclass that provides attribute access to its
|
|
|
|
namespace, as well as a meaningful repr.
|
|
|
|
|
|
|
|
Unlike :class:`object`, with ``SimpleNamespace`` you can add and remove
|
|
|
|
attributes. If a ``SimpleNamespace`` object is initialized with keyword
|
|
|
|
arguments, those are directly added to the underlying namespace.
|
|
|
|
|
|
|
|
The type is roughly equivalent to the following code::
|
|
|
|
|
|
|
|
class SimpleNamespace:
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
2016-05-10 06:01:23 -03:00
|
|
|
|
2012-06-03 17:18:47 -03:00
|
|
|
def __repr__(self):
|
|
|
|
keys = sorted(self.__dict__)
|
|
|
|
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
|
|
|
|
return "{}({})".format(type(self).__name__, ", ".join(items))
|
2016-05-10 06:01:23 -03:00
|
|
|
|
2013-02-16 19:32:39 -04:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
2012-06-03 17:18:47 -03:00
|
|
|
|
|
|
|
``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``.
|
|
|
|
However, for a structured record type use :func:`~collections.namedtuple`
|
|
|
|
instead.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
2014-02-25 17:03:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)
|
|
|
|
|
|
|
|
Route attribute access on a class to __getattr__.
|
|
|
|
|
|
|
|
This is a descriptor, used to define attributes that act differently when
|
|
|
|
accessed through an instance and through a class. Instance access remains
|
|
|
|
normal, but access to an attribute through a class will be routed to the
|
|
|
|
class's __getattr__ method; this is done by raising AttributeError.
|
|
|
|
|
|
|
|
This allows one to have properties active on an instance, and have virtual
|
|
|
|
attributes on the class with the same name (see Enum for an example).
|
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
2015-05-21 12:50:30 -03:00
|
|
|
|
|
|
|
|
2015-06-24 12:04:15 -03:00
|
|
|
Coroutine Utility Functions
|
|
|
|
---------------------------
|
2015-05-21 12:50:30 -03:00
|
|
|
|
|
|
|
.. function:: coroutine(gen_func)
|
|
|
|
|
2015-06-24 12:04:15 -03:00
|
|
|
This function transforms a :term:`generator` function into a
|
|
|
|
:term:`coroutine function` which returns a generator-based coroutine.
|
|
|
|
The generator-based coroutine is still a :term:`generator iterator`,
|
|
|
|
but is also considered to be a :term:`coroutine` object and is
|
|
|
|
:term:`awaitable`. However, it may not necessarily implement
|
|
|
|
the :meth:`__await__` method.
|
2015-05-21 12:50:30 -03:00
|
|
|
|
2015-06-24 12:04:15 -03:00
|
|
|
If *gen_func* is a generator function, it will be modified in-place.
|
|
|
|
|
|
|
|
If *gen_func* is not a generator function, it will be wrapped. If it
|
|
|
|
returns an instance of :class:`collections.abc.Generator`, the instance
|
|
|
|
will be wrapped in an *awaitable* proxy object. All other types
|
|
|
|
of objects will be returned as is.
|
2015-05-21 12:50:30 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.5
|