2007-08-15 11:28:01 -03:00
|
|
|
****************************
|
2008-05-24 15:31:28 -03:00
|
|
|
What's New in Python 2.6
|
2007-08-15 11:28:01 -03:00
|
|
|
****************************
|
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
.. XXX add trademark info for Apple, Microsoft, SourceForge.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-09-02 10:08:11 -03:00
|
|
|
:Author: A.M. Kuchling (amk at amk.ca)
|
2007-08-15 11:28:01 -03:00
|
|
|
:Release: |release|
|
|
|
|
:Date: |today|
|
|
|
|
|
2008-10-04 13:52:01 -03:00
|
|
|
.. $Id$
|
2007-12-29 06:57:00 -04:00
|
|
|
Rules for maintenance:
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* Anyone can add text to this document. Do not spend very much time
|
|
|
|
on the wording of your changes, because your text will probably
|
|
|
|
get rewritten to some degree.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* The maintainer will go through Misc/NEWS periodically and add
|
|
|
|
changes; it's therefore more important to add your changes to
|
|
|
|
Misc/NEWS than to this file.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* This is not a complete list of every single change; completeness
|
|
|
|
is the purpose of Misc/NEWS. Some changes I consider too small
|
|
|
|
or esoteric to include. If such a change is added to the text,
|
|
|
|
I'll just remove it. (This is another reason you shouldn't spend
|
|
|
|
too much time on writing your addition.)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* If you want to draw your new text to the attention of the
|
|
|
|
maintainer, add 'XXX' to the beginning of the paragraph or
|
|
|
|
section.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* It's OK to just add a fragmentary note about a change. For
|
|
|
|
example: "XXX Describe the transmogrify() function added to the
|
|
|
|
socket module." The maintainer will research the change and
|
|
|
|
write the necessary text.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* You can comment out your additions if you like, but it's not
|
|
|
|
necessary (especially when a final release is some months away).
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
* Credit the author of a patch or bugfix. Just the name is
|
|
|
|
sufficient; the e-mail address isn't necessary.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
* It's helpful to add the bug/patch number in a parenthetical comment.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
XXX Describe the transmogrify() function added to the socket
|
|
|
|
module.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by P.Y. Developer; :issue:`12345`.)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
This saves the maintainer some effort going through the SVN logs
|
2007-12-29 06:57:00 -04:00
|
|
|
when researching a change.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-10-01 19:05:43 -03:00
|
|
|
This article explains the new features in Python 2.6, released on October 1
|
|
|
|
2008. The release schedule is described in :pep:`361`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The major theme of Python 2.6 is preparing the migration path to
|
|
|
|
Python 3.0, a major redesign of the language. Whenever possible,
|
|
|
|
Python 2.6 incorporates new features and syntax from 3.0 while
|
|
|
|
remaining compatible with existing code by not removing older features
|
|
|
|
or syntax. When it's not possible to do that, Python 2.6 tries to do
|
|
|
|
what it can, adding compatibility functions in a
|
|
|
|
:mod:`future_builtins` module and a :option:`-3` switch to warn about
|
|
|
|
usages that will become unsupported in 3.0.
|
|
|
|
|
|
|
|
Some significant new packages have been added to the standard library,
|
2008-09-06 09:50:05 -03:00
|
|
|
such as the :mod:`multiprocessing` and :mod:`json` modules, but
|
2008-08-30 12:19:57 -03:00
|
|
|
there aren't many new features that aren't related to Python 3.0 in
|
|
|
|
some way.
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
Python 2.6 also sees a number of improvements and bugfixes throughout
|
2008-08-31 12:48:44 -03:00
|
|
|
the source. A search through the change logs finds there were 259
|
|
|
|
patches applied and 612 bugs fixed between Python 2.5 and 2.6. Both
|
2008-08-31 12:41:48 -03:00
|
|
|
figures are likely to be underestimates.
|
|
|
|
|
2008-04-10 18:29:01 -03:00
|
|
|
This article doesn't attempt to provide a complete specification of
|
|
|
|
the new features, but instead provides a convenient overview. For
|
|
|
|
full details, you should refer to the documentation for Python 2.6. If
|
2008-05-09 08:46:05 -03:00
|
|
|
you want to understand the rationale for the design and
|
|
|
|
implementation, refer to the PEP for a particular new feature.
|
|
|
|
Whenever possible, "What's New in Python" links to the bug/patch item
|
|
|
|
for each change.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. Compare with previous release in 2 - 3 sentences here.
|
|
|
|
add hyperlink when the documentation becomes available online.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ========================================================================
|
|
|
|
.. Large, PEP-level features and changes should be described here.
|
|
|
|
.. ========================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
Python 3.0
|
|
|
|
================
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The development cycle for Python versions 2.6 and 3.0 was
|
|
|
|
synchronized, with the alpha and beta releases for both versions being
|
|
|
|
made on the same days. The development of 3.0 has influenced many
|
|
|
|
features in 2.6.
|
2007-09-24 21:09:42 -03:00
|
|
|
|
|
|
|
Python 3.0 is a far-ranging redesign of Python that breaks
|
|
|
|
compatibility with the 2.x series. This means that existing Python
|
2008-08-30 23:24:08 -03:00
|
|
|
code will need some conversion in order to run on
|
2007-09-24 21:09:42 -03:00
|
|
|
Python 3.0. However, not all the changes in 3.0 necessarily break
|
|
|
|
compatibility. In cases where new features won't cause existing code
|
|
|
|
to break, they've been backported to 2.6 and are described in this
|
2008-05-24 15:31:28 -03:00
|
|
|
document in the appropriate place. Some of the 3.0-derived features
|
2007-09-24 21:09:42 -03:00
|
|
|
are:
|
|
|
|
|
|
|
|
* A :meth:`__complex__` method for converting objects to a complex number.
|
|
|
|
* Alternate syntax for catching exceptions: ``except TypeError as exc``.
|
|
|
|
* The addition of :func:`functools.reduce` as a synonym for the built-in
|
|
|
|
:func:`reduce` function.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
Python 3.0 adds several new built-in functions and changes the
|
2010-03-07 11:23:59 -04:00
|
|
|
semantics of some existing builtins. Functions that are new in 3.0
|
2008-08-30 12:19:57 -03:00
|
|
|
such as :func:`bin` have simply been added to Python 2.6, but existing
|
2010-03-07 11:23:59 -04:00
|
|
|
builtins haven't been changed; instead, the :mod:`future_builtins`
|
2008-08-30 12:19:57 -03:00
|
|
|
module has versions with the new 3.0 semantics. Code written to be
|
|
|
|
compatible with 3.0 can do ``from future_builtins import hex, map`` as
|
|
|
|
necessary.
|
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
A new command-line switch, :option:`-3`, enables warnings
|
|
|
|
about features that will be removed in Python 3.0. You can run code
|
|
|
|
with this switch to see how much work will be necessary to port
|
2008-05-24 15:31:28 -03:00
|
|
|
code to 3.0. The value of this switch is available
|
2008-03-25 05:29:14 -03:00
|
|
|
to Python code as the boolean variable :data:`sys.py3kwarning`,
|
2012-01-14 11:42:02 -04:00
|
|
|
and to C extension code as :c:data:`Py_Py3kWarningFlag`.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The 3xxx series of PEPs, which contains proposals for Python 3.0.
|
|
|
|
:pep:`3000` describes the development process for Python 3.0.
|
|
|
|
Start with :pep:`3100` that describes the general goals for Python
|
|
|
|
3.0, and then explore the higher-numbered PEPS that propose
|
|
|
|
specific features.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
Changes to the Development Process
|
2007-12-18 22:02:04 -04:00
|
|
|
==================================================
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
While 2.6 was being developed, the Python development process
|
2008-08-30 12:19:57 -03:00
|
|
|
underwent two significant changes: we switched from SourceForge's
|
|
|
|
issue tracker to a customized Roundup installation, and the
|
|
|
|
documentation was converted from LaTeX to reStructuredText.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
New Issue Tracker: Roundup
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
For a long time, the Python developers had been growing increasingly
|
2008-03-20 19:49:26 -03:00
|
|
|
annoyed by SourceForge's bug tracker. SourceForge's hosted solution
|
|
|
|
doesn't permit much customization; for example, it wasn't possible to
|
|
|
|
customize the life cycle of issues.
|
|
|
|
|
|
|
|
The infrastructure committee of the Python Software Foundation
|
|
|
|
therefore posted a call for issue trackers, asking volunteers to set
|
|
|
|
up different products and import some of the bugs and patches from
|
2008-08-30 12:19:57 -03:00
|
|
|
SourceForge. Four different trackers were examined: `Jira
|
2008-05-24 15:31:28 -03:00
|
|
|
<http://www.atlassian.com/software/jira/>`__,
|
|
|
|
`Launchpad <http://www.launchpad.net>`__,
|
|
|
|
`Roundup <http://roundup.sourceforge.net/>`__, and
|
|
|
|
`Trac <http://trac.edgewall.org/>`__.
|
2008-04-05 00:38:39 -03:00
|
|
|
The committee eventually settled on Jira
|
2008-03-20 19:49:26 -03:00
|
|
|
and Roundup as the two candidates. Jira is a commercial product that
|
2008-08-30 12:19:57 -03:00
|
|
|
offers no-cost hosted instances to free-software projects; Roundup
|
2008-03-20 19:49:26 -03:00
|
|
|
is an open-source project that requires volunteers
|
|
|
|
to administer it and a server to host it.
|
|
|
|
|
|
|
|
After posting a call for volunteers, a new Roundup installation was
|
|
|
|
set up at http://bugs.python.org. One installation of Roundup can
|
|
|
|
host multiple trackers, and this server now also hosts issue trackers
|
2008-05-24 15:31:28 -03:00
|
|
|
for Jython and for the Python web site. It will surely find
|
2008-04-10 18:29:01 -03:00
|
|
|
other uses in the future. Where possible,
|
|
|
|
this edition of "What's New in Python" links to the bug/patch
|
|
|
|
item for each change.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
Hosting of the Python bug tracker is kindly provided by
|
2008-05-24 15:31:28 -03:00
|
|
|
`Upfront Systems <http://www.upfrontsystems.co.za/>`__
|
2008-04-10 18:29:01 -03:00
|
|
|
of Stellenbosch, South Africa. Martin von Loewis put a
|
2008-04-05 00:38:39 -03:00
|
|
|
lot of effort into importing existing bugs and patches from
|
2008-05-24 15:31:28 -03:00
|
|
|
SourceForge; his scripts for this import operation are at
|
2008-08-30 12:19:57 -03:00
|
|
|
http://svn.python.org/view/tracker/importer/ and may be useful to
|
2008-10-16 17:15:47 -03:00
|
|
|
other projects wishing to move from SourceForge to Roundup.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
http://bugs.python.org
|
2008-04-05 12:57:46 -03:00
|
|
|
The Python bug tracker.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-04-05 12:57:46 -03:00
|
|
|
http://bugs.jython.org:
|
|
|
|
The Jython bug tracker.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-04-05 12:57:46 -03:00
|
|
|
http://roundup.sourceforge.net/
|
|
|
|
Roundup downloads and documentation.
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
http://svn.python.org/view/tracker/importer/
|
|
|
|
Martin von Loewis's conversion scripts.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-05-02 19:12:58 -03:00
|
|
|
New Documentation Format: reStructuredText Using Sphinx
|
2008-04-05 12:57:46 -03:00
|
|
|
-----------------------------------------------------------
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The Python documentation was written using LaTeX since the project
|
|
|
|
started around 1989. In the 1980s and early 1990s, most documentation
|
|
|
|
was printed out for later study, not viewed online. LaTeX was widely
|
|
|
|
used because it provided attractive printed output while remaining
|
2008-09-02 04:23:16 -03:00
|
|
|
straightforward to write once the basic rules of the markup were
|
2008-03-20 19:49:26 -03:00
|
|
|
learned.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
Today LaTeX is still used for writing publications destined for
|
|
|
|
printing, but the landscape for programming tools has shifted. We no
|
|
|
|
longer print out reams of documentation; instead, we browse through it
|
|
|
|
online and HTML has become the most important format to support.
|
|
|
|
Unfortunately, converting LaTeX to HTML is fairly complicated and Fred
|
|
|
|
L. Drake Jr., the long-time Python documentation editor, spent a lot
|
|
|
|
of time maintaining the conversion process. Occasionally people would
|
|
|
|
suggest converting the documentation into SGML and later XML, but
|
|
|
|
performing a good conversion is a major task and no one ever committed
|
|
|
|
the time required to finish the job.
|
|
|
|
|
|
|
|
During the 2.6 development cycle, Georg Brandl put a lot of effort
|
|
|
|
into building a new toolchain for processing the documentation. The
|
|
|
|
resulting package is called Sphinx, and is available from
|
|
|
|
http://sphinx.pocoo.org/.
|
|
|
|
|
|
|
|
Sphinx concentrates on HTML output, producing attractively styled and
|
|
|
|
modern HTML; printed output is still supported through conversion to
|
|
|
|
LaTeX. The input format is reStructuredText, a markup syntax
|
|
|
|
supporting custom extensions and directives that is commonly used in
|
|
|
|
the Python community.
|
|
|
|
|
|
|
|
Sphinx is a standalone package that can be used for writing, and
|
|
|
|
almost two dozen other projects
|
|
|
|
(`listed on the Sphinx web site <http://sphinx.pocoo.org/examples.html>`__)
|
|
|
|
have adopted Sphinx as their documentation tool.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2012-01-14 17:22:54 -04:00
|
|
|
`Documenting Python <http://docs.python.org/devguide/documenting.html>`__
|
2008-04-05 12:57:46 -03:00
|
|
|
Describes how to write for Python's documentation.
|
|
|
|
|
|
|
|
`Sphinx <http://sphinx.pocoo.org/>`__
|
|
|
|
Documentation and code for the Sphinx toolchain.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-04-05 12:57:46 -03:00
|
|
|
`Docutils <http://docutils.sf.net>`__
|
2008-04-21 11:40:22 -03:00
|
|
|
The underlying reStructuredText parser and toolset.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
PEP 343: The 'with' statement
|
|
|
|
=============================
|
|
|
|
|
|
|
|
The previous version, Python 2.5, added the ':keyword:`with`'
|
2008-08-30 12:19:57 -03:00
|
|
|
statement as an optional feature, to be enabled by a ``from __future__
|
2007-12-03 17:28:41 -04:00
|
|
|
import with_statement`` directive. In 2.6 the statement no longer needs to
|
2007-09-13 19:49:34 -03:00
|
|
|
be specially enabled; this means that :keyword:`with` is now always a
|
2008-05-24 15:31:28 -03:00
|
|
|
keyword. The rest of this section is a copy of the corresponding
|
2008-08-30 12:19:57 -03:00
|
|
|
section from the "What's New in Python 2.5" document; if you're
|
|
|
|
familiar with the ':keyword:`with`' statement
|
|
|
|
from Python 2.5, you can skip this section.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
The ':keyword:`with`' statement clarifies code that previously would use
|
|
|
|
``try...finally`` blocks to ensure that clean-up code is executed. In this
|
|
|
|
section, I'll discuss the statement as it will commonly be used. In the next
|
|
|
|
section, I'll examine the implementation details and show how to write objects
|
|
|
|
for use with this statement.
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The ':keyword:`with`' statement is a control-flow structure whose basic
|
2007-09-13 19:49:34 -03:00
|
|
|
structure is::
|
|
|
|
|
|
|
|
with expression [as variable]:
|
|
|
|
with-block
|
|
|
|
|
|
|
|
The expression is evaluated, and it should result in an object that supports the
|
|
|
|
context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
|
2009-02-27 12:52:55 -04:00
|
|
|
methods).
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
The object's :meth:`__enter__` is called before *with-block* is executed and
|
|
|
|
therefore can run set-up code. It also may return a value that is bound to the
|
|
|
|
name *variable*, if given. (Note carefully that *variable* is *not* assigned
|
|
|
|
the result of *expression*.)
|
|
|
|
|
|
|
|
After execution of the *with-block* is finished, the object's :meth:`__exit__`
|
|
|
|
method is called, even if the block raised an exception, and can therefore run
|
|
|
|
clean-up code.
|
|
|
|
|
|
|
|
Some standard Python objects now support the context management protocol and can
|
|
|
|
be used with the ':keyword:`with`' statement. File objects are one example::
|
|
|
|
|
|
|
|
with open('/etc/passwd', 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
print line
|
|
|
|
... more processing code ...
|
|
|
|
|
|
|
|
After this statement has executed, the file object in *f* will have been
|
|
|
|
automatically closed, even if the :keyword:`for` loop raised an exception part-
|
|
|
|
way through the block.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
In this case, *f* is the same object created by :func:`open`, because
|
|
|
|
:meth:`file.__enter__` returns *self*.
|
|
|
|
|
|
|
|
The :mod:`threading` module's locks and condition variables also support the
|
|
|
|
':keyword:`with`' statement::
|
|
|
|
|
|
|
|
lock = threading.Lock()
|
|
|
|
with lock:
|
|
|
|
# Critical section of code
|
|
|
|
...
|
|
|
|
|
|
|
|
The lock is acquired before the block is executed and always released once the
|
|
|
|
block is complete.
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The :func:`localcontext` function in the :mod:`decimal` module makes it easy
|
2007-09-13 19:49:34 -03:00
|
|
|
to save and restore the current decimal context, which encapsulates the desired
|
|
|
|
precision and rounding characteristics for computations::
|
|
|
|
|
|
|
|
from decimal import Decimal, Context, localcontext
|
|
|
|
|
|
|
|
# Displays with default precision of 28 digits
|
|
|
|
v = Decimal('578')
|
|
|
|
print v.sqrt()
|
|
|
|
|
|
|
|
with localcontext(Context(prec=16)):
|
|
|
|
# All code in this block uses a precision of 16 digits.
|
|
|
|
# The original context is restored on exiting the block.
|
|
|
|
print v.sqrt()
|
|
|
|
|
|
|
|
|
|
|
|
.. _new-26-context-managers:
|
|
|
|
|
|
|
|
Writing Context Managers
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
Under the hood, the ':keyword:`with`' statement is fairly complicated. Most
|
|
|
|
people will only use ':keyword:`with`' in company with existing objects and
|
|
|
|
don't need to know these details, so you can skip the rest of this section if
|
|
|
|
you like. Authors of new objects will need to understand the details of the
|
|
|
|
underlying implementation and should keep reading.
|
|
|
|
|
|
|
|
A high-level explanation of the context management protocol is:
|
|
|
|
|
|
|
|
* The expression is evaluated and should result in an object called a "context
|
|
|
|
manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
|
|
|
|
methods.
|
|
|
|
|
|
|
|
* The context manager's :meth:`__enter__` method is called. The value returned
|
2007-12-16 19:15:07 -04:00
|
|
|
is assigned to *VAR*. If no ``as VAR`` clause is present, the value is simply
|
2007-09-13 19:49:34 -03:00
|
|
|
discarded.
|
|
|
|
|
|
|
|
* The code in *BLOCK* is executed.
|
|
|
|
|
2010-02-14 04:18:23 -04:00
|
|
|
* If *BLOCK* raises an exception, the context manager's :meth:`__exit__` method
|
|
|
|
is called with three arguments, the exception details (``type, value, traceback``,
|
|
|
|
the same values returned by :func:`sys.exc_info`, which can also be ``None``
|
|
|
|
if no exception occurred). The method's return value controls whether an exception
|
2007-09-13 19:49:34 -03:00
|
|
|
is re-raised: any false value re-raises the exception, and ``True`` will result
|
|
|
|
in suppressing it. You'll only rarely want to suppress the exception, because
|
|
|
|
if you do the author of the code containing the ':keyword:`with`' statement will
|
|
|
|
never realize anything went wrong.
|
|
|
|
|
|
|
|
* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
|
|
|
|
called, but *type*, *value*, and *traceback* are all ``None``.
|
|
|
|
|
|
|
|
Let's think through an example. I won't present detailed code but will only
|
|
|
|
sketch the methods necessary for a database that supports transactions.
|
|
|
|
|
|
|
|
(For people unfamiliar with database terminology: a set of changes to the
|
|
|
|
database are grouped into a transaction. Transactions can be either committed,
|
|
|
|
meaning that all the changes are written into the database, or rolled back,
|
|
|
|
meaning that the changes are all discarded and the database is unchanged. See
|
|
|
|
any database textbook for more information.)
|
|
|
|
|
|
|
|
Let's assume there's an object representing a database connection. Our goal will
|
|
|
|
be to let the user write code like this::
|
|
|
|
|
|
|
|
db_connection = DatabaseConnection()
|
|
|
|
with db_connection as cursor:
|
|
|
|
cursor.execute('insert into ...')
|
|
|
|
cursor.execute('delete from ...')
|
|
|
|
# ... more operations ...
|
|
|
|
|
|
|
|
The transaction should be committed if the code in the block runs flawlessly or
|
|
|
|
rolled back if there's an exception. Here's the basic interface for
|
|
|
|
:class:`DatabaseConnection` that I'll assume::
|
|
|
|
|
|
|
|
class DatabaseConnection:
|
|
|
|
# Database interface
|
2007-12-16 19:13:29 -04:00
|
|
|
def cursor(self):
|
2007-09-13 19:49:34 -03:00
|
|
|
"Returns a cursor object and starts a new transaction"
|
2007-12-16 19:13:29 -04:00
|
|
|
def commit(self):
|
2007-09-13 19:49:34 -03:00
|
|
|
"Commits current transaction"
|
2007-12-16 19:13:29 -04:00
|
|
|
def rollback(self):
|
2007-09-13 19:49:34 -03:00
|
|
|
"Rolls back current transaction"
|
|
|
|
|
|
|
|
The :meth:`__enter__` method is pretty easy, having only to start a new
|
|
|
|
transaction. For this application the resulting cursor object would be a useful
|
|
|
|
result, so the method will return it. The user can then add ``as cursor`` to
|
|
|
|
their ':keyword:`with`' statement to bind the cursor to a variable name. ::
|
|
|
|
|
|
|
|
class DatabaseConnection:
|
|
|
|
...
|
2007-12-16 19:13:29 -04:00
|
|
|
def __enter__(self):
|
2007-09-13 19:49:34 -03:00
|
|
|
# Code to start a new transaction
|
|
|
|
cursor = self.cursor()
|
|
|
|
return cursor
|
|
|
|
|
|
|
|
The :meth:`__exit__` method is the most complicated because it's where most of
|
|
|
|
the work has to be done. The method has to check if an exception occurred. If
|
|
|
|
there was no exception, the transaction is committed. The transaction is rolled
|
|
|
|
back if there was an exception.
|
|
|
|
|
|
|
|
In the code below, execution will just fall off the end of the function,
|
|
|
|
returning the default value of ``None``. ``None`` is false, so the exception
|
|
|
|
will be re-raised automatically. If you wished, you could be more explicit and
|
|
|
|
add a :keyword:`return` statement at the marked location. ::
|
|
|
|
|
|
|
|
class DatabaseConnection:
|
|
|
|
...
|
2007-12-16 19:13:29 -04:00
|
|
|
def __exit__(self, type, value, tb):
|
2007-09-13 19:49:34 -03:00
|
|
|
if tb is None:
|
|
|
|
# No exception, so commit
|
|
|
|
self.commit()
|
|
|
|
else:
|
|
|
|
# Exception occurred, so rollback.
|
|
|
|
self.rollback()
|
|
|
|
# return False
|
|
|
|
|
|
|
|
|
|
|
|
.. _module-contextlib:
|
|
|
|
|
|
|
|
The contextlib module
|
|
|
|
---------------------
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
The :mod:`contextlib` module provides some functions and a decorator that
|
|
|
|
are useful when writing objects for use with the ':keyword:`with`' statement.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
The decorator is called :func:`contextmanager`, and lets you write a single
|
|
|
|
generator function instead of defining a new class. The generator should yield
|
|
|
|
exactly one value. The code up to the :keyword:`yield` will be executed as the
|
|
|
|
:meth:`__enter__` method, and the value yielded will be the method's return
|
|
|
|
value that will get bound to the variable in the ':keyword:`with`' statement's
|
|
|
|
:keyword:`as` clause, if any. The code after the :keyword:`yield` will be
|
|
|
|
executed in the :meth:`__exit__` method. Any exception raised in the block will
|
|
|
|
be raised by the :keyword:`yield` statement.
|
|
|
|
|
2008-08-30 12:19:57 -03:00
|
|
|
Using this decorator, our database example from the previous section
|
|
|
|
could be written as::
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
@contextmanager
|
2007-12-16 19:13:29 -04:00
|
|
|
def db_transaction(connection):
|
2007-09-13 19:49:34 -03:00
|
|
|
cursor = connection.cursor()
|
|
|
|
try:
|
|
|
|
yield cursor
|
|
|
|
except:
|
|
|
|
connection.rollback()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
connection.commit()
|
|
|
|
|
|
|
|
db = DatabaseConnection()
|
|
|
|
with db_transaction(db) as cursor:
|
|
|
|
...
|
|
|
|
|
2010-02-14 04:18:23 -04:00
|
|
|
The :mod:`contextlib` module also has a ``nested(mgr1, mgr2, ...)`` function
|
2007-09-13 19:49:34 -03:00
|
|
|
that combines a number of context managers so you don't need to write nested
|
|
|
|
':keyword:`with`' statements. In this example, the single ':keyword:`with`'
|
|
|
|
statement both starts a database transaction and acquires a thread lock::
|
|
|
|
|
|
|
|
lock = threading.Lock()
|
|
|
|
with nested (db_transaction(db), lock) as (cursor, locked):
|
|
|
|
...
|
|
|
|
|
2010-02-14 04:18:23 -04:00
|
|
|
Finally, the :func:`closing` function returns its argument so that it can be
|
2010-02-14 09:38:12 -04:00
|
|
|
bound to a variable, and calls the argument's ``.close()`` method at the end
|
|
|
|
of the block. ::
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2009-02-19 23:31:23 -04:00
|
|
|
import urllib, sys
|
2007-09-13 19:49:34 -03:00
|
|
|
from contextlib import closing
|
|
|
|
|
|
|
|
with closing(urllib.urlopen('http://www.yahoo.com')) as f:
|
|
|
|
for line in f:
|
|
|
|
sys.stdout.write(line)
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`343` - The "with" statement
|
|
|
|
PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland,
|
|
|
|
Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a
|
|
|
|
':keyword:`with`' statement, which can be helpful in learning how the statement
|
|
|
|
works.
|
|
|
|
|
|
|
|
The documentation for the :mod:`contextlib` module.
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2007-12-18 22:02:04 -04:00
|
|
|
.. _pep-0366:
|
|
|
|
|
|
|
|
PEP 366: Explicit Relative Imports From a Main Module
|
|
|
|
============================================================
|
|
|
|
|
|
|
|
Python's :option:`-m` switch allows running a module as a script.
|
|
|
|
When you ran a module that was located inside a package, relative
|
|
|
|
imports didn't work correctly.
|
|
|
|
|
2008-08-30 13:44:54 -03:00
|
|
|
The fix for Python 2.6 adds a :attr:`__package__` attribute to
|
|
|
|
modules. When this attribute is present, relative imports will be
|
|
|
|
relative to the value of this attribute instead of the
|
|
|
|
:attr:`__name__` attribute.
|
|
|
|
|
|
|
|
PEP 302-style importers can then set :attr:`__package__` as necessary.
|
|
|
|
The :mod:`runpy` module that implements the :option:`-m` switch now
|
|
|
|
does this, so relative imports will now work correctly in scripts
|
|
|
|
running from inside a package.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-01-14 21:47:32 -04:00
|
|
|
.. ======================================================================
|
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
.. _pep-0370:
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
PEP 370: Per-user ``site-packages`` Directory
|
|
|
|
=====================================================
|
|
|
|
|
2008-08-30 13:44:54 -03:00
|
|
|
When you run Python, the module search path ``sys.path`` usually
|
2008-05-09 08:46:05 -03:00
|
|
|
includes a directory whose path ends in ``"site-packages"``. This
|
|
|
|
directory is intended to hold locally-installed packages available to
|
2008-08-30 13:44:54 -03:00
|
|
|
all users using a machine or a particular site installation.
|
2008-05-09 08:46:05 -03:00
|
|
|
|
|
|
|
Python 2.6 introduces a convention for user-specific site directories.
|
|
|
|
The directory varies depending on the platform:
|
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
* Unix and Mac OS X: :file:`~/.local/`
|
2008-05-09 08:46:05 -03:00
|
|
|
* Windows: :file:`%APPDATA%/Python`
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
Within this directory, there will be version-specific subdirectories,
|
2008-09-13 14:41:16 -03:00
|
|
|
such as :file:`lib/python2.6/site-packages` on Unix/Mac OS and
|
2008-05-09 08:46:05 -03:00
|
|
|
:file:`Python26/site-packages` on Windows.
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
If you don't like the default directory, it can be overridden by an
|
|
|
|
environment variable. :envvar:`PYTHONUSERBASE` sets the root
|
|
|
|
directory used for all Python versions supporting this feature. On
|
|
|
|
Windows, the directory for application-specific data can be changed by
|
|
|
|
setting the :envvar:`APPDATA` environment variable. You can also
|
|
|
|
modify the :file:`site.py` file for your Python installation.
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
The feature can be disabled entirely by running Python with the
|
|
|
|
:option:`-s` option or setting the :envvar:`PYTHONNOUSERSITE`
|
|
|
|
environment variable.
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
.. seealso::
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
:pep:`370` - Per-user ``site-packages`` Directory
|
|
|
|
PEP written and implemented by Christian Heimes.
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
.. ======================================================================
|
|
|
|
|
2008-06-11 09:53:14 -03:00
|
|
|
.. _pep-0371:
|
|
|
|
|
|
|
|
PEP 371: The ``multiprocessing`` Package
|
|
|
|
=====================================================
|
|
|
|
|
2008-07-13 22:18:31 -03:00
|
|
|
The new :mod:`multiprocessing` package lets Python programs create new
|
|
|
|
processes that will perform a computation and return a result to the
|
|
|
|
parent. The parent and child processes can communicate using queues
|
2008-08-26 21:27:18 -03:00
|
|
|
and pipes, synchronize their operations using locks and semaphores,
|
|
|
|
and can share simple arrays of data.
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
The :mod:`multiprocessing` module started out as an exact emulation of
|
|
|
|
the :mod:`threading` module using processes instead of threads. That
|
|
|
|
goal was discarded along the path to Python 2.6, but the general
|
|
|
|
approach of the module is still similar. The fundamental class
|
2008-08-26 21:27:18 -03:00
|
|
|
is the :class:`Process`, which is passed a callable object and
|
|
|
|
a collection of arguments. The :meth:`start` method
|
2008-07-13 22:18:31 -03:00
|
|
|
sets the callable running in a subprocess, after which you can call
|
|
|
|
the :meth:`is_alive` method to check whether the subprocess is still running
|
|
|
|
and the :meth:`join` method to wait for the process to exit.
|
|
|
|
|
|
|
|
Here's a simple example where the subprocess will calculate a
|
2008-08-30 13:44:54 -03:00
|
|
|
factorial. The function doing the calculation is written strangely so
|
|
|
|
that it takes significantly longer when the input argument is a
|
2008-07-13 22:18:31 -03:00
|
|
|
multiple of 4.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
import time
|
|
|
|
from multiprocessing import Process, Queue
|
|
|
|
|
|
|
|
|
|
|
|
def factorial(queue, N):
|
2009-01-03 17:04:55 -04:00
|
|
|
"Compute a factorial."
|
|
|
|
# If N is a multiple of 4, this function will take much longer.
|
|
|
|
if (N % 4) == 0:
|
|
|
|
time.sleep(.05 * N/4)
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2009-01-03 17:04:55 -04:00
|
|
|
# Calculate the result
|
|
|
|
fact = 1L
|
|
|
|
for i in range(1, N+1):
|
|
|
|
fact = fact * i
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2009-01-03 17:04:55 -04:00
|
|
|
# Put the result on the queue
|
|
|
|
queue.put(fact)
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2009-01-03 17:04:55 -04:00
|
|
|
queue = Queue()
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2009-01-03 17:04:55 -04:00
|
|
|
N = 5
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2009-01-03 17:04:55 -04:00
|
|
|
p = Process(target=factorial, args=(queue, N))
|
|
|
|
p.start()
|
|
|
|
p.join()
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2009-01-03 17:04:55 -04:00
|
|
|
result = queue.get()
|
|
|
|
print 'Factorial', N, '=', result
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2012-01-05 14:48:30 -04:00
|
|
|
A :class:`Queue` is used to communicate the result of the factorial.
|
|
|
|
The :class:`Queue` object is stored in a global variable.
|
2008-08-30 13:44:54 -03:00
|
|
|
The child process will use the value of the variable when the child
|
|
|
|
was created; because it's a :class:`Queue`, parent and child can use
|
|
|
|
the object to communicate. (If the parent were to change the value of
|
|
|
|
the global variable, the child's value would be unaffected, and vice
|
|
|
|
versa.)
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
Two other classes, :class:`Pool` and :class:`Manager`, provide
|
|
|
|
higher-level interfaces. :class:`Pool` will create a fixed number of
|
|
|
|
worker processes, and requests can then be distributed to the workers
|
2008-09-04 10:26:24 -03:00
|
|
|
by calling :meth:`apply` or :meth:`apply_async` to add a single request,
|
2008-08-30 13:44:54 -03:00
|
|
|
and :meth:`map` or :meth:`map_async` to add a number of
|
2008-07-13 22:18:31 -03:00
|
|
|
requests. The following code uses a :class:`Pool` to spread requests
|
2008-08-30 13:44:54 -03:00
|
|
|
across 5 worker processes and retrieve a list of results::
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2008-08-30 13:44:54 -03:00
|
|
|
from multiprocessing import Pool
|
2008-07-13 22:18:31 -03:00
|
|
|
|
2008-08-30 13:44:54 -03:00
|
|
|
def factorial(N, dictionary):
|
2009-01-03 17:04:55 -04:00
|
|
|
"Compute a factorial."
|
|
|
|
...
|
2008-07-13 22:18:31 -03:00
|
|
|
p = Pool(5)
|
|
|
|
result = p.map(factorial, range(1, 1000, 10))
|
|
|
|
for v in result:
|
2009-01-03 17:04:55 -04:00
|
|
|
print v
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
This produces the following output::
|
|
|
|
|
|
|
|
1
|
|
|
|
39916800
|
|
|
|
51090942171709440000
|
|
|
|
8222838654177922817725562880000000
|
|
|
|
33452526613163807108170062053440751665152000000000
|
|
|
|
...
|
|
|
|
|
2008-08-30 13:44:54 -03:00
|
|
|
The other high-level interface, the :class:`Manager` class, creates a
|
|
|
|
separate server process that can hold master copies of Python data
|
|
|
|
structures. Other processes can then access and modify these data
|
|
|
|
structures using proxy objects. The following example creates a
|
|
|
|
shared dictionary by calling the :meth:`dict` method; the worker
|
|
|
|
processes then insert values into the dictionary. (Locking is not
|
|
|
|
done for you automatically, which doesn't matter in this example.
|
|
|
|
:class:`Manager`'s methods also include :meth:`Lock`, :meth:`RLock`,
|
|
|
|
and :meth:`Semaphore` to create shared locks.)
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
import time
|
|
|
|
from multiprocessing import Pool, Manager
|
|
|
|
|
|
|
|
def factorial(N, dictionary):
|
2008-10-08 14:30:55 -03:00
|
|
|
"Compute a factorial."
|
|
|
|
# Calculate the result
|
|
|
|
fact = 1L
|
|
|
|
for i in range(1, N+1):
|
|
|
|
fact = fact * i
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
# Store result in dictionary
|
2008-10-08 14:30:55 -03:00
|
|
|
dictionary[N] = fact
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2008-10-08 14:30:55 -03:00
|
|
|
p = Pool(5)
|
|
|
|
mgr = Manager()
|
|
|
|
d = mgr.dict() # Create shared dictionary
|
2008-07-01 16:51:54 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
# Run tasks using the pool
|
|
|
|
for N in range(1, 1000, 10):
|
|
|
|
p.apply_async(factorial, (N, d))
|
2008-07-01 16:51:54 -03:00
|
|
|
|
2009-06-03 18:21:09 -03:00
|
|
|
# Mark pool as closed -- no more tasks can be added.
|
|
|
|
p.close()
|
2008-07-01 16:51:54 -03:00
|
|
|
|
2009-06-03 18:21:09 -03:00
|
|
|
# Wait for tasks to exit
|
|
|
|
p.join()
|
2008-07-01 16:51:54 -03:00
|
|
|
|
2009-06-03 18:21:09 -03:00
|
|
|
# Output results
|
|
|
|
for k, v in sorted(d.items()):
|
|
|
|
print k, v
|
2008-07-13 22:18:31 -03:00
|
|
|
|
|
|
|
This will produce the output::
|
|
|
|
|
|
|
|
1 1
|
|
|
|
11 39916800
|
|
|
|
21 51090942171709440000
|
|
|
|
31 8222838654177922817725562880000000
|
|
|
|
41 33452526613163807108170062053440751665152000000000
|
2008-08-30 13:44:54 -03:00
|
|
|
51 15511187532873822802242430164693032110632597200169861120000...
|
2008-06-11 09:53:14 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2008-07-13 22:18:31 -03:00
|
|
|
The documentation for the :mod:`multiprocessing` module.
|
|
|
|
|
2008-06-23 23:41:08 -03:00
|
|
|
:pep:`371` - Addition of the multiprocessing package
|
2008-08-26 21:27:18 -03:00
|
|
|
PEP written by Jesse Noller and Richard Oudkerk;
|
2008-06-19 23:05:57 -03:00
|
|
|
implemented by Richard Oudkerk and Jesse Noller.
|
2008-06-11 09:53:14 -03:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
|
2008-06-11 09:53:14 -03:00
|
|
|
.. ======================================================================
|
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
.. _pep-3101:
|
|
|
|
|
|
|
|
PEP 3101: Advanced String Formatting
|
|
|
|
=====================================================
|
|
|
|
|
2008-05-26 09:29:46 -03:00
|
|
|
In Python 3.0, the `%` operator is supplemented by a more powerful string
|
|
|
|
formatting method, :meth:`format`. Support for the :meth:`str.format` method
|
|
|
|
has been backported to Python 2.6.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
In 2.6, both 8-bit and Unicode strings have a `.format()` method that
|
|
|
|
treats the string as a template and takes the arguments to be formatted.
|
|
|
|
The formatting template uses curly brackets (`{`, `}`) as special characters::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> # Substitute positional argument 0 into the string.
|
|
|
|
>>> "User ID: {0}".format("root")
|
|
|
|
'User ID: root'
|
|
|
|
>>> # Use the named keyword arguments
|
|
|
|
>>> "User ID: {uid} Last seen: {last_login}".format(
|
|
|
|
... uid="root",
|
|
|
|
... last_login = "5 Mar 2008 07:20")
|
|
|
|
'User ID: root Last seen: 5 Mar 2008 07:20'
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
Curly brackets can be escaped by doubling them::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-12-13 00:02:20 -04:00
|
|
|
>>> "Empty dict: {{}}".format()
|
2008-10-08 14:30:55 -03:00
|
|
|
"Empty dict: {}"
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Field names can be integers indicating positional arguments, such as
|
|
|
|
``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
|
2008-03-20 19:49:26 -03:00
|
|
|
supply compound field names that read attributes or access dictionary keys::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> import sys
|
|
|
|
>>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
|
|
|
|
Platform: darwin
|
2009-01-03 16:55:06 -04:00
|
|
|
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41)
|
2008-10-08 14:30:55 -03:00
|
|
|
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> import mimetypes
|
|
|
|
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
|
|
|
|
'Content-type: video/mp4'
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
Note that when using dictionary-style notation such as ``[.mp4]``, you
|
|
|
|
don't need to put any quotation marks around the string; it will look
|
|
|
|
up the value using ``.mp4`` as the key. Strings beginning with a
|
|
|
|
number will be converted to an integer. You can't write more
|
|
|
|
complicated expressions inside a format string.
|
|
|
|
|
|
|
|
So far we've shown how to specify which field to substitute into the
|
|
|
|
resulting string. The precise formatting used is also controllable by
|
2008-03-21 14:19:29 -03:00
|
|
|
adding a colon followed by a format specifier. For example::
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> # Field 0: left justify, pad to 15 characters
|
|
|
|
>>> # Field 1: right justify, pad to 6 characters
|
|
|
|
>>> fmt = '{0:15} ${1:>6}'
|
|
|
|
>>> fmt.format('Registration', 35)
|
|
|
|
'Registration $ 35'
|
|
|
|
>>> fmt.format('Tutorial', 50)
|
|
|
|
'Tutorial $ 50'
|
|
|
|
>>> fmt.format('Banquet', 125)
|
|
|
|
'Banquet $ 125'
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-03-21 14:19:29 -03:00
|
|
|
Format specifiers can reference other fields through nesting::
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> fmt = '{0:{1}}'
|
|
|
|
>>> width = 15
|
|
|
|
>>> fmt.format('Invoice #1234', width)
|
|
|
|
'Invoice #1234 '
|
|
|
|
>>> width = 35
|
|
|
|
>>> fmt.format('Invoice #1234', width)
|
|
|
|
'Invoice #1234 '
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
The alignment of a field within the desired width can be specified:
|
|
|
|
|
|
|
|
================ ============================================
|
|
|
|
Character Effect
|
|
|
|
================ ============================================
|
|
|
|
< (default) Left-align
|
|
|
|
> Right-align
|
|
|
|
^ Center
|
|
|
|
= (For numeric types only) Pad after the sign.
|
|
|
|
================ ============================================
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Format specifiers can also include a presentation type, which
|
2008-03-25 21:30:02 -03:00
|
|
|
controls how the value is formatted. For example, floating-point numbers
|
2008-10-08 14:30:55 -03:00
|
|
|
can be formatted as a general number or in exponential notation::
|
2008-03-25 21:30:02 -03:00
|
|
|
|
|
|
|
>>> '{0:g}'.format(3.75)
|
|
|
|
'3.75'
|
|
|
|
>>> '{0:e}'.format(3.75)
|
|
|
|
'3.750000e+00'
|
|
|
|
|
|
|
|
A variety of presentation types are available. Consult the 2.6
|
2008-10-08 14:30:55 -03:00
|
|
|
documentation for a :ref:`complete list <formatstrings>`; here's a sample:
|
|
|
|
|
|
|
|
===== ========================================================================
|
|
|
|
``b`` Binary. Outputs the number in base 2.
|
|
|
|
``c`` Character. Converts the integer to the corresponding Unicode character
|
|
|
|
before printing.
|
|
|
|
``d`` Decimal Integer. Outputs the number in base 10.
|
|
|
|
``o`` Octal format. Outputs the number in base 8.
|
|
|
|
``x`` Hex format. Outputs the number in base 16, using lower-case letters for
|
|
|
|
the digits above 9.
|
|
|
|
``e`` Exponent notation. Prints the number in scientific notation using the
|
|
|
|
letter 'e' to indicate the exponent.
|
|
|
|
``g`` General format. This prints the number as a fixed-point number, unless
|
|
|
|
the number is too large, in which case it switches to 'e' exponent
|
|
|
|
notation.
|
|
|
|
``n`` Number. This is the same as 'g' (for floats) or 'd' (for integers),
|
|
|
|
except that it uses the current locale setting to insert the appropriate
|
|
|
|
number separator characters.
|
|
|
|
``%`` Percentage. Multiplies the number by 100 and displays in fixed ('f')
|
|
|
|
format, followed by a percent sign.
|
|
|
|
===== ========================================================================
|
2008-03-25 21:30:02 -03:00
|
|
|
|
2008-08-06 22:47:34 -03:00
|
|
|
Classes and types can define a :meth:`__format__` method to control how they're
|
2008-03-04 20:44:41 -04:00
|
|
|
formatted. It receives a single argument, the format specifier::
|
|
|
|
|
|
|
|
def __format__(self, format_spec):
|
|
|
|
if isinstance(format_spec, unicode):
|
|
|
|
return unicode(str(self))
|
|
|
|
else:
|
|
|
|
return str(self)
|
|
|
|
|
2010-03-07 11:23:59 -04:00
|
|
|
There's also a :func:`format` builtin that will format a single
|
2008-08-30 13:44:54 -03:00
|
|
|
value. It calls the type's :meth:`__format__` method with the
|
|
|
|
provided specifier::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
|
|
|
>>> format(75.6564, '.2f')
|
|
|
|
'75.66'
|
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
.. seealso::
|
|
|
|
|
2008-05-26 09:29:46 -03:00
|
|
|
:ref:`formatstrings`
|
2008-08-30 13:44:54 -03:00
|
|
|
The reference documentation for format fields.
|
2008-05-26 09:29:46 -03:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
:pep:`3101` - Advanced String Formatting
|
2008-05-26 09:29:46 -03:00
|
|
|
PEP written by Talin. Implemented by Eric Smith.
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
.. _pep-3105:
|
|
|
|
|
|
|
|
PEP 3105: ``print`` As a Function
|
|
|
|
=====================================================
|
|
|
|
|
|
|
|
The ``print`` statement becomes the :func:`print` function in Python 3.0.
|
2008-08-30 19:56:54 -03:00
|
|
|
Making :func:`print` a function makes it possible to replace the function
|
|
|
|
by doing ``def print(...)`` or importing a new function from somewhere else.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Python 2.6 has a ``__future__`` import that removes ``print`` as language
|
2008-03-20 19:49:26 -03:00
|
|
|
syntax, letting you use the functional form instead. For example::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> from __future__ import print_function
|
|
|
|
>>> print('# of entries', len(dictionary), file=sys.stderr)
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
The signature of the new function is::
|
|
|
|
|
|
|
|
def print(*args, sep=' ', end='\n', file=None)
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
The parameters are:
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
* *args*: positional arguments whose values will be printed out.
|
|
|
|
* *sep*: the separator, which will be printed between arguments.
|
|
|
|
* *end*: the ending text, which will be printed after all of the
|
2008-03-20 19:49:26 -03:00
|
|
|
arguments have been output.
|
2008-08-30 19:56:54 -03:00
|
|
|
* *file*: the file object to which the output will be sent.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2008-03-20 20:04:04 -03:00
|
|
|
:pep:`3105` - Make print a function
|
2008-03-20 19:49:26 -03:00
|
|
|
PEP written by Georg Brandl.
|
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
.. _pep-3110:
|
|
|
|
|
|
|
|
PEP 3110: Exception-Handling Changes
|
|
|
|
=====================================================
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
One error that Python programmers occasionally make
|
2008-08-30 19:56:54 -03:00
|
|
|
is writing the following code::
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
...
|
2008-08-30 19:56:54 -03:00
|
|
|
except TypeError, ValueError: # Wrong!
|
2007-09-13 19:49:34 -03:00
|
|
|
...
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
The author is probably trying to catch both :exc:`TypeError` and
|
|
|
|
:exc:`ValueError` exceptions, but this code actually does something
|
|
|
|
different: it will catch :exc:`TypeError` and bind the resulting
|
|
|
|
exception object to the local name ``"ValueError"``. The
|
|
|
|
:exc:`ValueError` exception will not be caught at all. The correct
|
|
|
|
code specifies a tuple of exceptions::
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
...
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
...
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
This error happens because the use of the comma here is ambiguous:
|
2007-09-13 19:49:34 -03:00
|
|
|
does it indicate two different nodes in the parse tree, or a single
|
2008-08-30 19:56:54 -03:00
|
|
|
node that's a tuple?
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
Python 3.0 makes this unambiguous by replacing the comma with the word
|
|
|
|
"as". To catch an exception and store the exception object in the
|
|
|
|
variable ``exc``, you must write::
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
...
|
|
|
|
except TypeError as exc:
|
|
|
|
...
|
|
|
|
|
|
|
|
Python 3.0 will only support the use of "as", and therefore interprets
|
|
|
|
the first example as catching two different exceptions. Python 2.6
|
|
|
|
supports both the comma and "as", so existing code will continue to
|
2008-08-30 19:56:54 -03:00
|
|
|
work. We therefore suggest using "as" when writing new Python code
|
|
|
|
that will only be executed with 2.6.
|
2007-09-13 19:49:34 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3110` - Catching Exceptions in Python 3000
|
|
|
|
PEP written and implemented by Collin Winter.
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
.. _pep-3112:
|
|
|
|
|
|
|
|
PEP 3112: Byte Literals
|
|
|
|
=====================================================
|
|
|
|
|
2008-06-20 20:43:12 -03:00
|
|
|
Python 3.0 adopts Unicode as the language's fundamental string type and
|
2008-05-24 15:31:28 -03:00
|
|
|
denotes 8-bit literals differently, either as ``b'string'``
|
|
|
|
or using a :class:`bytes` constructor. For future compatibility,
|
2008-03-04 20:44:41 -04:00
|
|
|
Python 2.6 adds :class:`bytes` as a synonym for the :class:`str` type,
|
|
|
|
and it also supports the ``b''`` notation.
|
|
|
|
|
2008-10-08 10:21:27 -03:00
|
|
|
|
|
|
|
The 2.6 :class:`str` differs from 3.0's :class:`bytes` type in various
|
|
|
|
ways; most notably, the constructor is completely different. In 3.0,
|
|
|
|
``bytes([65, 66, 67])`` is 3 elements long, containing the bytes
|
|
|
|
representing ``ABC``; in 2.6, ``bytes([65, 66, 67])`` returns the
|
|
|
|
12-byte string representing the :func:`str` of the list.
|
|
|
|
|
|
|
|
The primary use of :class:`bytes` in 2.6 will be to write tests of
|
|
|
|
object type such as ``isinstance(x, bytes)``. This will help the 2to3
|
|
|
|
converter, which can't tell whether 2.x code intends strings to
|
2009-01-03 16:55:06 -04:00
|
|
|
contain either characters or 8-bit bytes; you can now
|
|
|
|
use either :class:`bytes` or :class:`str` to represent your intention
|
2008-10-08 10:21:27 -03:00
|
|
|
exactly, and the resulting code will also be correct in Python 3.0.
|
|
|
|
|
2008-04-07 20:57:07 -03:00
|
|
|
There's also a ``__future__`` import that causes all string literals
|
2008-05-24 15:31:28 -03:00
|
|
|
to become Unicode strings. This means that ``\u`` escape sequences
|
2008-05-04 00:05:49 -03:00
|
|
|
can be used to include Unicode characters::
|
|
|
|
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-04-13 19:39:12 -03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
s = ('\u751f\u3080\u304e\u3000\u751f\u3054'
|
|
|
|
'\u3081\u3000\u751f\u305f\u307e\u3054')
|
|
|
|
|
|
|
|
print len(s) # 12 Unicode characters
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
At the C level, Python 3.0 will rename the existing 8-bit
|
2012-01-14 11:42:02 -04:00
|
|
|
string type, called :c:type:`PyStringObject` in Python 2.x,
|
|
|
|
to :c:type:`PyBytesObject`. Python 2.6 uses ``#define``
|
|
|
|
to support using the names :c:func:`PyBytesObject`,
|
|
|
|
:c:func:`PyBytes_Check`, :c:func:`PyBytes_FromStringAndSize`,
|
2008-06-20 20:43:12 -03:00
|
|
|
and all the other functions and macros used with strings.
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
Instances of the :class:`bytes` type are immutable just
|
|
|
|
as strings are. A new :class:`bytearray` type stores a mutable
|
2008-06-20 20:43:12 -03:00
|
|
|
sequence of bytes::
|
|
|
|
|
|
|
|
>>> bytearray([65, 66, 67])
|
|
|
|
bytearray(b'ABC')
|
|
|
|
>>> b = bytearray(u'\u21ef\u3244', 'utf-8')
|
|
|
|
>>> b
|
2008-09-04 10:26:24 -03:00
|
|
|
bytearray(b'\xe2\x87\xaf\xe3\x89\x84')
|
2008-06-20 20:43:12 -03:00
|
|
|
>>> b[0] = '\xe3'
|
|
|
|
>>> b
|
2008-09-04 10:26:24 -03:00
|
|
|
bytearray(b'\xe3\x87\xaf\xe3\x89\x84')
|
2008-06-20 20:43:12 -03:00
|
|
|
>>> unicode(str(b), 'utf-8')
|
|
|
|
u'\u31ef \u3244'
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
Byte arrays support most of the methods of string types, such as
|
2008-06-20 20:43:12 -03:00
|
|
|
:meth:`startswith`/:meth:`endswith`, :meth:`find`/:meth:`rfind`,
|
2008-08-26 21:27:18 -03:00
|
|
|
and some of the methods of lists, such as :meth:`append`,
|
2008-06-20 20:43:12 -03:00
|
|
|
:meth:`pop`, and :meth:`reverse`.
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
::
|
|
|
|
|
2008-06-20 20:43:12 -03:00
|
|
|
>>> b = bytearray('ABC')
|
|
|
|
>>> b.append('d')
|
|
|
|
>>> b.append(ord('e'))
|
|
|
|
>>> b
|
|
|
|
bytearray(b'ABCde')
|
2008-05-04 00:05:49 -03:00
|
|
|
|
2008-08-26 23:12:18 -03:00
|
|
|
There's also a corresponding C API, with
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyByteArray_FromObject`,
|
|
|
|
:c:func:`PyByteArray_FromStringAndSize`,
|
2008-08-26 23:12:18 -03:00
|
|
|
and various other functions.
|
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3112` - Bytes literals in Python 3000
|
|
|
|
PEP written by Jason Orendorff; backported to 2.6 by Christian Heimes.
|
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2008-04-05 00:38:39 -03:00
|
|
|
.. _pep-3116:
|
|
|
|
|
|
|
|
PEP 3116: New I/O Library
|
|
|
|
=====================================================
|
|
|
|
|
2008-04-08 18:22:53 -03:00
|
|
|
Python's built-in file objects support a number of methods, but
|
|
|
|
file-like objects don't necessarily support all of them. Objects that
|
|
|
|
imitate files usually support :meth:`read` and :meth:`write`, but they
|
2008-08-30 19:56:54 -03:00
|
|
|
may not support :meth:`readline`, for example. Python 3.0 introduces
|
|
|
|
a layered I/O library in the :mod:`io` module that separates buffering
|
|
|
|
and text-handling features from the fundamental read and write
|
|
|
|
operations.
|
2008-04-08 18:22:53 -03:00
|
|
|
|
|
|
|
There are three levels of abstract base classes provided by
|
|
|
|
the :mod:`io` module:
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
* :class:`RawIOBase` defines raw I/O operations: :meth:`read`,
|
2008-05-24 15:31:28 -03:00
|
|
|
:meth:`readinto`,
|
2008-04-08 18:22:53 -03:00
|
|
|
:meth:`write`, :meth:`seek`, :meth:`tell`, :meth:`truncate`,
|
|
|
|
and :meth:`close`.
|
|
|
|
Most of the methods of this class will often map to a single system call.
|
|
|
|
There are also :meth:`readable`, :meth:`writable`, and :meth:`seekable`
|
|
|
|
methods for determining what operations a given object will allow.
|
|
|
|
|
|
|
|
Python 3.0 has concrete implementations of this class for files and
|
|
|
|
sockets, but Python 2.6 hasn't restructured its file and socket objects
|
|
|
|
in this way.
|
|
|
|
|
|
|
|
.. XXX should 2.6 register them in io.py?
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
* :class:`BufferedIOBase` is an abstract base class that
|
2008-05-24 15:31:28 -03:00
|
|
|
buffers data in memory to reduce the number of
|
2008-04-08 18:22:53 -03:00
|
|
|
system calls used, making I/O processing more efficient.
|
2008-05-24 15:31:28 -03:00
|
|
|
It supports all of the methods of :class:`RawIOBase`,
|
2008-04-08 18:22:53 -03:00
|
|
|
and adds a :attr:`raw` attribute holding the underlying raw object.
|
|
|
|
|
2008-08-26 23:12:18 -03:00
|
|
|
There are five concrete classes implementing this ABC.
|
|
|
|
:class:`BufferedWriter` and :class:`BufferedReader` are for objects
|
2008-08-30 12:25:47 -03:00
|
|
|
that support write-only or read-only usage that have a :meth:`seek`
|
|
|
|
method for random access. :class:`BufferedRandom` objects support
|
|
|
|
read and write access upon the same underlying stream, and
|
|
|
|
:class:`BufferedRWPair` is for objects such as TTYs that have both
|
|
|
|
read and write operations acting upon unconnected streams of data.
|
|
|
|
The :class:`BytesIO` class supports reading, writing, and seeking
|
|
|
|
over an in-memory buffer.
|
2008-04-08 18:22:53 -03:00
|
|
|
|
2012-08-15 12:15:39 -03:00
|
|
|
.. index::
|
|
|
|
single: universal newlines; What's new
|
|
|
|
|
2008-04-08 18:22:53 -03:00
|
|
|
* :class:`TextIOBase`: Provides functions for reading and writing
|
|
|
|
strings (remember, strings will be Unicode in Python 3.0),
|
2012-08-15 12:15:39 -03:00
|
|
|
and supporting :term:`universal newlines`. :class:`TextIOBase` defines
|
2008-05-24 15:31:28 -03:00
|
|
|
the :meth:`readline` method and supports iteration upon
|
|
|
|
objects.
|
2008-04-08 18:22:53 -03:00
|
|
|
|
|
|
|
There are two concrete implementations. :class:`TextIOWrapper`
|
|
|
|
wraps a buffered I/O object, supporting all of the methods for
|
2008-05-24 15:31:28 -03:00
|
|
|
text I/O and adding a :attr:`buffer` attribute for access
|
2008-04-08 18:22:53 -03:00
|
|
|
to the underlying object. :class:`StringIO` simply buffers
|
|
|
|
everything in memory without ever writing anything to disk.
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
(In Python 2.6, :class:`io.StringIO` is implemented in
|
2008-05-24 15:31:28 -03:00
|
|
|
pure Python, so it's pretty slow. You should therefore stick with the
|
|
|
|
existing :mod:`StringIO` module or :mod:`cStringIO` for now. At some
|
2008-04-08 18:22:53 -03:00
|
|
|
point Python 3.0's :mod:`io` module will be rewritten into C for speed,
|
|
|
|
and perhaps the C implementation will be backported to the 2.x releases.)
|
|
|
|
|
|
|
|
In Python 2.6, the underlying implementations haven't been
|
|
|
|
restructured to build on top of the :mod:`io` module's classes. The
|
2008-05-24 15:31:28 -03:00
|
|
|
module is being provided to make it easier to write code that's
|
2008-04-08 18:22:53 -03:00
|
|
|
forward-compatible with 3.0, and to save developers the effort of writing
|
|
|
|
their own implementations of buffering and text I/O.
|
2008-04-05 00:38:39 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3116` - New I/O
|
|
|
|
PEP written by Daniel Stutzbach, Mike Verdone, and Guido van Rossum.
|
2008-04-14 23:24:15 -03:00
|
|
|
Code by Guido van Rossum, Georg Brandl, Walter Doerwald,
|
|
|
|
Jeremy Hylton, Martin von Loewis, Tony Lownds, and others.
|
2008-04-05 00:38:39 -03:00
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2008-03-25 21:30:02 -03:00
|
|
|
.. _pep-3118:
|
|
|
|
|
|
|
|
PEP 3118: Revised Buffer Protocol
|
|
|
|
=====================================================
|
|
|
|
|
2008-04-05 12:57:46 -03:00
|
|
|
The buffer protocol is a C-level API that lets Python types
|
2008-05-24 15:31:28 -03:00
|
|
|
exchange pointers into their internal representations. A
|
2008-04-05 12:57:46 -03:00
|
|
|
memory-mapped file can be viewed as a buffer of characters, for
|
|
|
|
example, and this lets another module such as :mod:`re`
|
|
|
|
treat memory-mapped files as a string of characters to be searched.
|
|
|
|
|
|
|
|
The primary users of the buffer protocol are numeric-processing
|
2008-08-30 19:56:54 -03:00
|
|
|
packages such as NumPy, which expose the internal representation
|
2008-04-05 12:57:46 -03:00
|
|
|
of arrays so that callers can write data directly into an array instead
|
2008-05-24 15:31:28 -03:00
|
|
|
of going through a slower API. This PEP updates the buffer protocol in light of experience
|
2008-04-05 12:57:46 -03:00
|
|
|
from NumPy development, adding a number of new features
|
2008-08-26 21:45:02 -03:00
|
|
|
such as indicating the shape of an array or locking a memory region.
|
2008-04-05 12:57:46 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
The most important new C API function is
|
2008-04-05 12:57:46 -03:00
|
|
|
``PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)``, which
|
|
|
|
takes an object and a set of flags, and fills in the
|
2008-05-24 15:31:28 -03:00
|
|
|
``Py_buffer`` structure with information
|
2008-04-05 12:57:46 -03:00
|
|
|
about the object's memory representation. Objects
|
2008-05-24 15:31:28 -03:00
|
|
|
can use this operation to lock memory in place
|
2008-04-05 12:57:46 -03:00
|
|
|
while an external caller could be modifying the contents,
|
2008-08-26 21:45:02 -03:00
|
|
|
so there's a corresponding ``PyBuffer_Release(Py_buffer *view)`` to
|
2008-04-05 12:57:46 -03:00
|
|
|
indicate that the external caller is done.
|
|
|
|
|
2008-08-26 21:45:02 -03:00
|
|
|
.. XXX PyObject_GetBuffer not documented in c-api
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The *flags* argument to :c:func:`PyObject_GetBuffer` specifies
|
2008-04-05 12:57:46 -03:00
|
|
|
constraints upon the memory returned. Some examples are:
|
|
|
|
|
|
|
|
* :const:`PyBUF_WRITABLE` indicates that the memory must be writable.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 12:57:46 -03:00
|
|
|
* :const:`PyBUF_LOCK` requests a read-only or exclusive lock on the memory.
|
|
|
|
|
|
|
|
* :const:`PyBUF_C_CONTIGUOUS` and :const:`PyBUF_F_CONTIGUOUS`
|
|
|
|
requests a C-contiguous (last dimension varies the fastest) or
|
2008-08-30 19:56:54 -03:00
|
|
|
Fortran-contiguous (first dimension varies the fastest) array layout.
|
2008-04-05 12:57:46 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Two new argument codes for :c:func:`PyArg_ParseTuple`,
|
2008-08-26 21:45:02 -03:00
|
|
|
``s*`` and ``z*``, return locked buffer objects for a parameter.
|
2008-03-25 21:30:02 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3118` - Revising the buffer protocol
|
2008-04-05 12:57:46 -03:00
|
|
|
PEP written by Travis Oliphant and Carl Banks; implemented by
|
|
|
|
Travis Oliphant.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-03-25 21:30:02 -03:00
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
.. _pep-3119:
|
|
|
|
|
|
|
|
PEP 3119: Abstract Base Classes
|
|
|
|
=====================================================
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
Some object-oriented languages such as Java support interfaces,
|
|
|
|
declaring that a class has a given set of methods or supports a given
|
|
|
|
access protocol. Abstract Base Classes (or ABCs) are an equivalent
|
|
|
|
feature for Python. The ABC support consists of an :mod:`abc` module
|
|
|
|
containing a metaclass called :class:`ABCMeta`, special handling of
|
|
|
|
this metaclass by the :func:`isinstance` and :func:`issubclass`
|
2010-03-07 11:23:59 -04:00
|
|
|
builtins, and a collection of basic ABCs that the Python developers
|
2008-08-30 19:56:54 -03:00
|
|
|
think will be widely useful. Future versions of Python will probably
|
|
|
|
add more ABCs.
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Let's say you have a particular class and wish to know whether it supports
|
2008-04-05 15:15:30 -03:00
|
|
|
dictionary-style access. The phrase "dictionary-style" is vague, however.
|
2008-05-24 15:31:28 -03:00
|
|
|
It probably means that accessing items with ``obj[1]`` works.
|
|
|
|
Does it imply that setting items with ``obj[2] = value`` works?
|
2008-04-05 15:15:30 -03:00
|
|
|
Or that the object will have :meth:`keys`, :meth:`values`, and :meth:`items`
|
|
|
|
methods? What about the iterative variants such as :meth:`iterkeys`? :meth:`copy`
|
|
|
|
and :meth:`update`? Iterating over the object with :func:`iter`?
|
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
The Python 2.6 :mod:`collections` module includes a number of
|
|
|
|
different ABCs that represent these distinctions. :class:`Iterable`
|
|
|
|
indicates that a class defines :meth:`__iter__`, and
|
|
|
|
:class:`Container` means the class defines a :meth:`__contains__`
|
|
|
|
method and therefore supports ``x in y`` expressions. The basic
|
|
|
|
dictionary interface of getting items, setting items, and
|
2008-04-05 15:15:30 -03:00
|
|
|
:meth:`keys`, :meth:`values`, and :meth:`items`, is defined by the
|
|
|
|
:class:`MutableMapping` ABC.
|
|
|
|
|
|
|
|
You can derive your own classes from a particular ABC
|
|
|
|
to indicate they support that ABC's interface::
|
|
|
|
|
|
|
|
import collections
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
class Storage(collections.MutableMapping):
|
|
|
|
...
|
2008-01-03 22:31:40 -04:00
|
|
|
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Alternatively, you could write the class without deriving from
|
2008-04-05 15:15:30 -03:00
|
|
|
the desired ABC and instead register the class by
|
|
|
|
calling the ABC's :meth:`register` method::
|
2008-01-03 22:31:40 -04:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
import collections
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
class Storage:
|
|
|
|
...
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
collections.MutableMapping.register(Storage)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
For classes that you write, deriving from the ABC is probably clearer.
|
|
|
|
The :meth:`register` method is useful when you've written a new
|
|
|
|
ABC that can describe an existing type or class, or if you want
|
|
|
|
to declare that some third-party class implements an ABC.
|
|
|
|
For example, if you defined a :class:`PrintableType` ABC,
|
2008-07-23 23:31:28 -03:00
|
|
|
it's legal to do::
|
2008-04-05 15:15:30 -03:00
|
|
|
|
|
|
|
# Register Python's types
|
|
|
|
PrintableType.register(int)
|
|
|
|
PrintableType.register(float)
|
|
|
|
PrintableType.register(str)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Classes should obey the semantics specified by an ABC, but
|
|
|
|
Python can't check this; it's up to the class author to
|
2008-04-05 15:15:30 -03:00
|
|
|
understand the ABC's requirements and to implement the code accordingly.
|
|
|
|
|
|
|
|
To check whether an object supports a particular interface, you can
|
|
|
|
now write::
|
|
|
|
|
|
|
|
def func(d):
|
2008-10-08 14:30:55 -03:00
|
|
|
if not isinstance(d, collections.MutableMapping):
|
|
|
|
raise ValueError("Mapping object expected, not %r" % d)
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
Don't feel that you must now begin writing lots of checks as in the
|
2008-05-24 15:31:28 -03:00
|
|
|
above example. Python has a strong tradition of duck-typing, where
|
2008-08-30 19:56:54 -03:00
|
|
|
explicit type-checking is never done and code simply calls methods on
|
2008-04-05 15:15:30 -03:00
|
|
|
an object, trusting that those methods will be there and raising an
|
2008-08-30 19:56:54 -03:00
|
|
|
exception if they aren't. Be judicious in checking for ABCs and only
|
|
|
|
do it where it's absolutely necessary.
|
2008-04-05 15:15:30 -03:00
|
|
|
|
|
|
|
You can write your own ABCs by using ``abc.ABCMeta`` as the
|
|
|
|
metaclass in a class definition::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
class Drawable():
|
|
|
|
__metaclass__ = ABCMeta
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
@abstractmethod
|
|
|
|
def draw(self, x, y, scale=1.0):
|
|
|
|
pass
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
def draw_doubled(self, x, y):
|
|
|
|
self.draw(x, y, scale=2.0)
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
class Square(Drawable):
|
|
|
|
def draw(self, x, y, scale):
|
|
|
|
...
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-04-05 15:15:30 -03:00
|
|
|
In the :class:`Drawable` ABC above, the :meth:`draw_doubled` method
|
|
|
|
renders the object at twice its size and can be implemented in terms
|
|
|
|
of other methods described in :class:`Drawable`. Classes implementing
|
2008-05-24 15:31:28 -03:00
|
|
|
this ABC therefore don't need to provide their own implementation
|
2008-04-05 15:15:30 -03:00
|
|
|
of :meth:`draw_doubled`, though they can do so. An implementation
|
2008-05-24 15:31:28 -03:00
|
|
|
of :meth:`draw` is necessary, though; the ABC can't provide
|
2008-08-30 19:56:54 -03:00
|
|
|
a useful generic implementation.
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
You can apply the ``@abstractmethod`` decorator to methods such as
|
|
|
|
:meth:`draw` that must be implemented; Python will then raise an
|
|
|
|
exception for classes that don't define the method.
|
2008-05-24 15:31:28 -03:00
|
|
|
Note that the exception is only raised when you actually
|
2008-08-30 19:56:54 -03:00
|
|
|
try to create an instance of a subclass lacking the method::
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-09-02 10:06:00 -03:00
|
|
|
>>> class Circle(Drawable):
|
|
|
|
... pass
|
|
|
|
...
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> c = Circle()
|
2008-04-05 15:15:30 -03:00
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
2008-09-02 10:06:00 -03:00
|
|
|
TypeError: Can't instantiate abstract class Circle with abstract methods draw
|
2008-05-24 15:31:28 -03:00
|
|
|
>>>
|
2008-04-05 15:15:30 -03:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
Abstract data attributes can be declared using the
|
|
|
|
``@abstractproperty`` decorator::
|
2008-01-04 14:24:41 -04:00
|
|
|
|
2008-09-02 10:06:00 -03:00
|
|
|
from abc import abstractproperty
|
|
|
|
...
|
|
|
|
|
2008-01-04 14:24:41 -04:00
|
|
|
@abstractproperty
|
|
|
|
def readonly(self):
|
|
|
|
return self._x
|
2008-01-03 22:31:40 -04:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
Subclasses must then define a :meth:`readonly` property.
|
2008-01-03 22:31:40 -04:00
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3119` - Introducing Abstract Base Classes
|
|
|
|
PEP written by Guido van Rossum and Talin.
|
2008-04-05 15:15:30 -03:00
|
|
|
Implemented by Guido van Rossum.
|
2007-12-18 22:02:04 -04:00
|
|
|
Backported to 2.6 by Benjamin Aranguren, with Alex Martelli.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-01-15 23:17:25 -04:00
|
|
|
.. ======================================================================
|
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
.. _pep-3127:
|
|
|
|
|
|
|
|
PEP 3127: Integer Literal Support and Syntax
|
|
|
|
=====================================================
|
|
|
|
|
2008-04-07 20:57:07 -03:00
|
|
|
Python 3.0 changes the syntax for octal (base-8) integer literals,
|
2008-08-30 19:56:54 -03:00
|
|
|
prefixing them with "0o" or "0O" instead of a leading zero, and adds
|
|
|
|
support for binary (base-2) integer literals, signalled by a "0b" or
|
|
|
|
"0B" prefix.
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Python 2.6 doesn't drop support for a leading 0 signalling
|
2008-04-07 20:57:07 -03:00
|
|
|
an octal number, but it does add support for "0o" and "0b"::
|
|
|
|
|
|
|
|
>>> 0o21, 2*8 + 1
|
|
|
|
(17, 17)
|
|
|
|
>>> 0b101111
|
|
|
|
47
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2010-03-07 11:23:59 -04:00
|
|
|
The :func:`oct` builtin still returns numbers
|
2008-05-24 15:31:28 -03:00
|
|
|
prefixed with a leading zero, and a new :func:`bin`
|
2010-03-07 11:23:59 -04:00
|
|
|
builtin returns the binary representation for a number::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-04-07 20:57:07 -03:00
|
|
|
>>> oct(42)
|
|
|
|
'052'
|
2008-08-30 19:56:54 -03:00
|
|
|
>>> future_builtins.oct(42)
|
|
|
|
'0o52'
|
2008-04-07 20:57:07 -03:00
|
|
|
>>> bin(173)
|
|
|
|
'0b10101101'
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2010-03-07 11:23:59 -04:00
|
|
|
The :func:`int` and :func:`long` builtins will now accept the "0o"
|
2008-04-07 20:57:07 -03:00
|
|
|
and "0b" prefixes when base-8 or base-2 are requested, or when the
|
2008-08-30 19:56:54 -03:00
|
|
|
*base* argument is zero (signalling that the base used should be
|
2008-10-08 14:30:55 -03:00
|
|
|
determined from the string)::
|
2008-04-07 20:57:07 -03:00
|
|
|
|
|
|
|
>>> int ('0o52', 0)
|
|
|
|
42
|
|
|
|
>>> int('1101', 2)
|
|
|
|
13
|
|
|
|
>>> int('0b1101', 2)
|
|
|
|
13
|
|
|
|
>>> int('0b1101', 0)
|
|
|
|
13
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3127` - Integer Literal Support and Syntax
|
2008-04-07 20:57:07 -03:00
|
|
|
PEP written by Patrick Maupin; backported to 2.6 by
|
|
|
|
Eric Smith.
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
.. ======================================================================
|
|
|
|
|
|
|
|
.. _pep-3129:
|
|
|
|
|
|
|
|
PEP 3129: Class Decorators
|
|
|
|
=====================================================
|
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
Decorators have been extended from functions to classes. It's now legal to
|
|
|
|
write::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
@foo
|
|
|
|
@bar
|
|
|
|
class A:
|
|
|
|
pass
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
This is equivalent to::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
|
|
|
class A:
|
|
|
|
pass
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
A = foo(bar(A))
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3129` - Class Decorators
|
|
|
|
PEP written by Collin Winter.
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2008-01-15 23:17:25 -04:00
|
|
|
.. _pep-3141:
|
|
|
|
|
|
|
|
PEP 3141: A Type Hierarchy for Numbers
|
|
|
|
=====================================================
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
Python 3.0 adds several abstract base classes for numeric types
|
|
|
|
inspired by Scheme's numeric tower. These classes were backported to
|
|
|
|
2.6 as the :mod:`numbers` module.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
|
|
|
The most general ABC is :class:`Number`. It defines no operations at
|
|
|
|
all, and only exists to allow checking if an object is a number by
|
|
|
|
doing ``isinstance(obj, Number)``.
|
|
|
|
|
|
|
|
:class:`Complex` is a subclass of :class:`Number`. Complex numbers
|
|
|
|
can undergo the basic operations of addition, subtraction,
|
|
|
|
multiplication, division, and exponentiation, and you can retrieve the
|
2008-05-24 15:31:28 -03:00
|
|
|
real and imaginary parts and obtain a number's conjugate. Python's built-in
|
2008-01-15 23:17:25 -04:00
|
|
|
complex type is an implementation of :class:`Complex`.
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
:class:`Real` further derives from :class:`Complex`, and adds
|
|
|
|
operations that only work on real numbers: :func:`floor`, :func:`trunc`,
|
|
|
|
rounding, taking the remainder mod N, floor division,
|
|
|
|
and comparisons.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
|
|
|
:class:`Rational` numbers derive from :class:`Real`, have
|
|
|
|
:attr:`numerator` and :attr:`denominator` properties, and can be
|
2008-02-10 17:29:51 -04:00
|
|
|
converted to floats. Python 2.6 adds a simple rational-number class,
|
2008-05-24 15:31:28 -03:00
|
|
|
:class:`Fraction`, in the :mod:`fractions` module. (It's called
|
|
|
|
:class:`Fraction` instead of :class:`Rational` to avoid
|
2008-03-03 21:50:32 -04:00
|
|
|
a name clash with :class:`numbers.Rational`.)
|
2008-01-15 23:17:25 -04:00
|
|
|
|
|
|
|
:class:`Integral` numbers derive from :class:`Rational`, and
|
2008-05-24 15:31:28 -03:00
|
|
|
can be shifted left and right with ``<<`` and ``>>``,
|
|
|
|
combined using bitwise operations such as ``&`` and ``|``,
|
2008-01-15 23:17:25 -04:00
|
|
|
and can be used as array indexes and slice boundaries.
|
|
|
|
|
2010-03-07 11:23:59 -04:00
|
|
|
In Python 3.0, the PEP slightly redefines the existing builtins
|
2008-03-03 21:50:32 -04:00
|
|
|
:func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new
|
2008-05-24 15:31:28 -03:00
|
|
|
one, :func:`math.trunc`, that's been backported to Python 2.6.
|
|
|
|
:func:`math.trunc` rounds toward zero, returning the closest
|
2008-01-17 08:00:15 -04:00
|
|
|
:class:`Integral` that's between the function's argument and zero.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-01-17 08:00:15 -04:00
|
|
|
.. seealso::
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
:pep:`3141` - A Type Hierarchy for Numbers
|
|
|
|
PEP written by Jeffrey Yasskin.
|
|
|
|
|
2008-04-05 00:38:39 -03:00
|
|
|
`Scheme's numerical tower <http://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower>`__, from the Guile manual.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-04-05 00:38:39 -03:00
|
|
|
`Scheme's number datatypes <http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2>`__ from the R5RS Scheme specification.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
The :mod:`fractions` Module
|
2008-01-15 23:17:25 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
To fill out the hierarchy of numeric types, the :mod:`fractions`
|
|
|
|
module provides a rational-number class. Rational numbers store their
|
2008-06-19 23:05:57 -03:00
|
|
|
values as a numerator and denominator forming a fraction, and can
|
|
|
|
exactly represent numbers such as ``2/3`` that floating-point numbers
|
|
|
|
can only approximate.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-02-10 17:29:51 -04:00
|
|
|
The :class:`Fraction` constructor takes two :class:`Integral` values
|
2008-01-15 23:17:25 -04:00
|
|
|
that will be the numerator and denominator of the resulting fraction. ::
|
|
|
|
|
2008-02-10 17:29:51 -04:00
|
|
|
>>> from fractions import Fraction
|
|
|
|
>>> a = Fraction(2, 3)
|
|
|
|
>>> b = Fraction(2, 5)
|
2008-01-15 23:17:25 -04:00
|
|
|
>>> float(a), float(b)
|
|
|
|
(0.66666666666666663, 0.40000000000000002)
|
|
|
|
>>> a+b
|
2008-02-10 23:11:55 -04:00
|
|
|
Fraction(16, 15)
|
2008-01-15 23:17:25 -04:00
|
|
|
>>> a/b
|
2008-02-10 23:11:55 -04:00
|
|
|
Fraction(5, 3)
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
For converting floating-point numbers to rationals,
|
|
|
|
the float type now has an :meth:`as_integer_ratio()` method that returns
|
2008-03-04 20:44:41 -04:00
|
|
|
the numerator and denominator for a fraction that evaluates to the same
|
|
|
|
floating-point value::
|
|
|
|
|
|
|
|
>>> (2.5) .as_integer_ratio()
|
|
|
|
(5, 2)
|
|
|
|
>>> (3.1415) .as_integer_ratio()
|
|
|
|
(7074029114692207L, 2251799813685248L)
|
|
|
|
>>> (1./3) .as_integer_ratio()
|
|
|
|
(6004799503160661L, 18014398509481984L)
|
|
|
|
|
|
|
|
Note that values that can only be approximated by floating-point
|
|
|
|
numbers, such as 1./3, are not simplified to the number being
|
|
|
|
approximated; the fraction attempts to match the floating-point value
|
|
|
|
**exactly**.
|
|
|
|
|
2008-02-10 17:29:51 -04:00
|
|
|
The :mod:`fractions` module is based upon an implementation by Sjoerd
|
2008-01-15 23:17:25 -04:00
|
|
|
Mullender that was in Python's :file:`Demo/classes/` directory for a
|
|
|
|
long time. This implementation was significantly updated by Jeffrey
|
2008-03-04 20:44:41 -04:00
|
|
|
Yasskin.
|
2008-01-15 23:17:25 -04:00
|
|
|
|
2008-05-05 17:21:38 -03:00
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
Other Language Changes
|
|
|
|
======================
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
Some smaller changes made to the core Python language are:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-02-22 10:53:17 -04:00
|
|
|
* Directories and zip archives containing a :file:`__main__.py` file
|
|
|
|
can now be executed directly by passing their name to the
|
|
|
|
interpreter. The directory or zip archive is automatically inserted
|
|
|
|
as the first entry in sys.path. (Suggestion and initial patch by
|
|
|
|
Andy Chu, subsequently revised by Phillip J. Eby and Nick Coghlan;
|
|
|
|
:issue:`1739468`.)
|
|
|
|
|
2008-06-20 20:43:12 -03:00
|
|
|
* The :func:`hasattr` function was catching and ignoring all errors,
|
2008-06-28 10:18:14 -03:00
|
|
|
under the assumption that they meant a :meth:`__getattr__` method
|
2008-08-30 23:24:08 -03:00
|
|
|
was failing somehow and the return value of :func:`hasattr` would
|
2008-06-28 10:18:14 -03:00
|
|
|
therefore be ``False``. This logic shouldn't be applied to
|
|
|
|
:exc:`KeyboardInterrupt` and :exc:`SystemExit`, however; Python 2.6
|
|
|
|
will no longer discard such exceptions when :func:`hasattr`
|
|
|
|
encounters them. (Fixed by Benjamin Peterson; :issue:`2196`.)
|
2008-06-20 20:43:12 -03:00
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
* When calling a function using the ``**`` syntax to provide keyword
|
|
|
|
arguments, you are no longer required to use a Python dictionary;
|
|
|
|
any mapping will now work::
|
|
|
|
|
|
|
|
>>> def f(**kw):
|
|
|
|
... print sorted(kw)
|
2008-05-24 15:31:28 -03:00
|
|
|
...
|
2007-09-13 19:49:34 -03:00
|
|
|
>>> ud=UserDict.UserDict()
|
|
|
|
>>> ud['a'] = 1
|
|
|
|
>>> ud['b'] = 'string'
|
|
|
|
>>> f(**ud)
|
|
|
|
['a', 'b']
|
|
|
|
|
2008-04-09 19:28:43 -03:00
|
|
|
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
It's also become legal to provide keyword arguments after a ``*args`` argument
|
2008-10-08 14:30:55 -03:00
|
|
|
to a function call. ::
|
2008-08-26 21:27:18 -03:00
|
|
|
|
|
|
|
>>> def f(*args, **kw):
|
|
|
|
... print args, kw
|
|
|
|
...
|
|
|
|
>>> f(1,2,3, *(4,5,6), keyword=13)
|
|
|
|
(1, 2, 3, 4, 5, 6) {'keyword': 13}
|
|
|
|
|
|
|
|
Previously this would have been a syntax error.
|
|
|
|
(Contributed by Amaury Forgeot d'Arc; :issue:`3473`.)
|
|
|
|
|
2010-03-07 11:23:59 -04:00
|
|
|
* A new builtin, ``next(iterator, [default])`` returns the next item
|
2008-06-05 20:35:31 -03:00
|
|
|
from the specified iterator. If the *default* argument is supplied,
|
|
|
|
it will be returned if *iterator* has been exhausted; otherwise,
|
2008-08-30 23:24:08 -03:00
|
|
|
the :exc:`StopIteration` exception will be raised. (Backported
|
|
|
|
in :issue:`2719`.)
|
2008-06-05 20:35:31 -03:00
|
|
|
|
2008-07-22 16:00:47 -03:00
|
|
|
* Tuples now have :meth:`index` and :meth:`count` methods matching the
|
|
|
|
list type's :meth:`index` and :meth:`count` methods::
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
>>> t = (0,1,2,3,4,0,1,2)
|
2008-04-07 20:57:07 -03:00
|
|
|
>>> t.index(3)
|
|
|
|
3
|
2008-08-30 23:24:08 -03:00
|
|
|
>>> t.count(0)
|
|
|
|
2
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-07-22 16:00:47 -03:00
|
|
|
(Contributed by Raymond Hettinger)
|
|
|
|
|
2007-09-24 21:09:42 -03:00
|
|
|
* The built-in types now have improved support for extended slicing syntax,
|
2008-08-30 23:24:08 -03:00
|
|
|
accepting various combinations of ``(start, stop, step)``.
|
2007-09-24 21:09:42 -03:00
|
|
|
Previously, the support was partial and certain corner cases wouldn't work.
|
|
|
|
(Implemented by Thomas Wouters.)
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. Revision 57619
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
* Properties now have three attributes, :attr:`getter`, :attr:`setter`
|
|
|
|
and :attr:`deleter`, that are decorators providing useful shortcuts
|
|
|
|
for adding a getter, setter or deleter function to an existing
|
|
|
|
property. You would use them like this::
|
2007-12-18 22:02:04 -04:00
|
|
|
|
|
|
|
class C(object):
|
2008-10-08 14:30:55 -03:00
|
|
|
@property
|
|
|
|
def x(self):
|
|
|
|
return self._x
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
@x.setter
|
|
|
|
def x(self, value):
|
|
|
|
self._x = value
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
@x.deleter
|
|
|
|
def x(self):
|
|
|
|
del self._x
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-01-17 19:01:44 -04:00
|
|
|
class D(C):
|
|
|
|
@C.x.getter
|
|
|
|
def x(self):
|
|
|
|
return self._x * 2
|
|
|
|
|
|
|
|
@x.setter
|
|
|
|
def x(self, value):
|
|
|
|
self._x = value / 2
|
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
* Several methods of the built-in set types now accept multiple iterables:
|
2008-08-26 21:27:18 -03:00
|
|
|
:meth:`intersection`,
|
|
|
|
:meth:`intersection_update`,
|
2008-06-19 23:05:57 -03:00
|
|
|
:meth:`union`, :meth:`update`,
|
|
|
|
:meth:`difference` and :meth:`difference_update`.
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
::
|
|
|
|
|
|
|
|
>>> s=set('1234567890')
|
|
|
|
>>> s.intersection('abc123', 'cdf246') # Intersection between all inputs
|
|
|
|
set(['2'])
|
|
|
|
>>> s.difference('246', '789')
|
|
|
|
set(['1', '0', '3', '5'])
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
* Many floating-point features were added. The :func:`float` function
|
2008-06-24 08:08:58 -03:00
|
|
|
will now turn the string ``nan`` into an
|
|
|
|
IEEE 754 Not A Number value, and ``+inf`` and ``-inf`` into
|
2008-05-24 15:31:28 -03:00
|
|
|
positive or negative infinity. This works on any platform with
|
2008-04-10 18:29:01 -03:00
|
|
|
IEEE 754 semantics. (Contributed by Christian Heimes; :issue:`1635`.)
|
2008-01-03 21:16:12 -04:00
|
|
|
|
2008-01-03 22:31:40 -04:00
|
|
|
Other functions in the :mod:`math` module, :func:`isinf` and
|
|
|
|
:func:`isnan`, return true if their floating-point argument is
|
2008-04-10 18:29:01 -03:00
|
|
|
infinite or Not A Number. (:issue:`1640`)
|
2008-02-20 15:12:36 -04:00
|
|
|
|
2008-08-06 22:47:34 -03:00
|
|
|
Conversion functions were added to convert floating-point numbers
|
2008-08-30 23:24:08 -03:00
|
|
|
into hexadecimal strings (:issue:`3008`). These functions
|
2008-08-06 22:47:34 -03:00
|
|
|
convert floats to and from a string representation without
|
|
|
|
introducing rounding errors from the conversion between decimal and
|
|
|
|
binary. Floats have a :meth:`hex` method that returns a string
|
|
|
|
representation, and the ``float.fromhex()`` method converts a string
|
|
|
|
back into a number::
|
|
|
|
|
|
|
|
>>> a = 3.75
|
|
|
|
>>> a.hex()
|
|
|
|
'0x1.e000000000000p+1'
|
|
|
|
>>> float.fromhex('0x1.e000000000000p+1')
|
|
|
|
3.75
|
|
|
|
>>> b=1./3
|
|
|
|
>>> b.hex()
|
|
|
|
'0x1.5555555555555p-2'
|
2008-07-15 16:08:33 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* A numerical nicety: when creating a complex number from two floats
|
2008-08-30 23:24:08 -03:00
|
|
|
on systems that support signed zeros (-0 and +0), the
|
|
|
|
:func:`complex` constructor will now preserve the sign
|
|
|
|
of the zero. (Fixed by Mark T. Dickinson; :issue:`1507`.)
|
|
|
|
|
2008-09-01 22:13:42 -03:00
|
|
|
* Classes that inherit a :meth:`__hash__` method from a parent class
|
|
|
|
can set ``__hash__ = None`` to indicate that the class isn't
|
|
|
|
hashable. This will make ``hash(obj)`` raise a :exc:`TypeError`
|
|
|
|
and the class will not be indicated as implementing the
|
|
|
|
:class:`Hashable` ABC.
|
|
|
|
|
|
|
|
You should do this when you've defined a :meth:`__cmp__` or
|
|
|
|
:meth:`__eq__` method that compares objects by their value rather
|
|
|
|
than by identity. All objects have a default hash method that uses
|
|
|
|
``id(obj)`` as the hash value. There's no tidy way to remove the
|
|
|
|
:meth:`__hash__` method inherited from a parent class, so
|
|
|
|
assigning ``None`` was implemented as an override. At the
|
|
|
|
C level, extensions can set ``tp_hash`` to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyObject_HashNotImplemented`.
|
2008-09-01 22:13:42 -03:00
|
|
|
(Fixed by Nick Coghlan and Amaury Forgeot d'Arc; :issue:`2235`.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :exc:`GeneratorExit` exception now subclasses
|
|
|
|
:exc:`BaseException` instead of :exc:`Exception`. This means
|
2007-12-18 22:02:04 -04:00
|
|
|
that an exception handler that does ``except Exception:``
|
2008-05-24 15:31:28 -03:00
|
|
|
will not inadvertently catch :exc:`GeneratorExit`.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Chad Austin; :issue:`1537`.)
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Generator objects now have a :attr:`gi_code` attribute that refers to
|
|
|
|
the original code object backing the generator.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Collin Winter; :issue:`1473257`.)
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
* The :func:`compile` built-in function now accepts keyword arguments
|
2008-04-10 18:29:01 -03:00
|
|
|
as well as positional parameters. (Contributed by Thomas Wouters;
|
|
|
|
:issue:`1444529`.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :func:`complex` constructor now accepts strings containing
|
2008-08-30 23:24:08 -03:00
|
|
|
parenthesized complex numbers, meaning that ``complex(repr(cplx))``
|
2007-09-01 17:43:36 -03:00
|
|
|
will now round-trip values. For example, ``complex('(3+4j)')``
|
2008-04-10 18:29:01 -03:00
|
|
|
now returns the value (3+4j). (:issue:`1491866`)
|
2007-09-01 17:43:36 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The string :meth:`translate` method now accepts ``None`` as the
|
|
|
|
translation table parameter, which is treated as the identity
|
2007-09-01 18:18:31 -03:00
|
|
|
transformation. This makes it easier to carry out operations
|
2008-07-22 16:18:50 -03:00
|
|
|
that only delete characters. (Contributed by Bengt Richter and
|
|
|
|
implemented by Raymond Hettinger; :issue:`1193128`.)
|
2007-09-01 18:18:31 -03:00
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
* The built-in :func:`dir` function now checks for a :meth:`__dir__`
|
|
|
|
method on the objects it receives. This method must return a list
|
|
|
|
of strings containing the names of valid attributes for the object,
|
|
|
|
and lets the object control the value that :func:`dir` produces.
|
2008-05-24 15:31:28 -03:00
|
|
|
Objects that have :meth:`__getattr__` or :meth:`__getattribute__`
|
2007-12-03 15:49:54 -04:00
|
|
|
methods can use this to advertise pseudo-attributes they will honor.
|
2008-04-10 18:29:01 -03:00
|
|
|
(:issue:`1591665`)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
* Instance method objects have new attributes for the object and function
|
|
|
|
comprising the method; the new synonym for :attr:`im_self` is
|
|
|
|
:attr:`__self__`, and :attr:`im_func` is also available as :attr:`__func__`.
|
2008-08-30 23:24:08 -03:00
|
|
|
The old names are still supported in Python 2.6, but are gone in 3.0.
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2009-04-03 18:45:29 -03:00
|
|
|
* An obscure change: when you use the :func:`locals` function inside a
|
2007-08-15 11:28:01 -03:00
|
|
|
:keyword:`class` statement, the resulting dictionary no longer returns free
|
2008-08-30 23:24:08 -03:00
|
|
|
variables. (Free variables, in this case, are variables referenced in the
|
2007-08-15 11:28:01 -03:00
|
|
|
:keyword:`class` statement that aren't attributes of the class.)
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
Optimizations
|
|
|
|
-------------
|
|
|
|
|
2008-04-20 23:14:24 -03:00
|
|
|
* The :mod:`warnings` module has been rewritten in C. This makes
|
|
|
|
it possible to invoke warnings from the parser, and may also
|
|
|
|
make the interpreter's startup faster.
|
|
|
|
(Contributed by Neal Norwitz and Brett Cannon; :issue:`1631171`.)
|
|
|
|
|
2008-01-15 02:55:56 -04:00
|
|
|
* Type objects now have a cache of methods that can reduce
|
2008-08-30 23:24:08 -03:00
|
|
|
the work required to find the correct method implementation
|
2008-01-14 21:55:32 -04:00
|
|
|
for a particular class; once cached, the interpreter doesn't need to
|
2008-05-24 15:31:28 -03:00
|
|
|
traverse base classes to figure out the right method to call.
|
|
|
|
The cache is cleared if a base class or the class itself is modified,
|
|
|
|
so the cache should remain correct even in the face of Python's dynamic
|
2008-01-14 21:55:32 -04:00
|
|
|
nature.
|
2008-05-24 15:31:28 -03:00
|
|
|
(Original optimization implemented by Armin Rigo, updated for
|
|
|
|
Python 2.6 by Kevin Jacobs; :issue:`1700288`.)
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
By default, this change is only applied to types that are included with
|
|
|
|
the Python core. Extension modules may not necessarily be compatible with
|
|
|
|
this cache,
|
2012-01-14 11:42:02 -04:00
|
|
|
so they must explicitly add :c:macro:`Py_TPFLAGS_HAVE_VERSION_TAG`
|
2008-08-26 21:27:18 -03:00
|
|
|
to the module's ``tp_flags`` field to enable the method cache.
|
|
|
|
(To be compatible with the method cache, the extension module's code
|
|
|
|
must not directly access and modify the ``tp_dict`` member of
|
|
|
|
any of the types it implements. Most modules don't do this,
|
|
|
|
but it's impossible for the Python interpreter to determine that.
|
|
|
|
See :issue:`1878` for some discussion.)
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
* Function calls that use keyword arguments are significantly faster
|
|
|
|
by doing a quick pointer comparison, usually saving the time of a
|
|
|
|
full string comparison. (Contributed by Raymond Hettinger, after an
|
|
|
|
initial implementation by Antoine Pitrou; :issue:`1819`.)
|
2008-08-06 22:47:34 -03:00
|
|
|
|
2008-01-03 22:31:40 -04:00
|
|
|
* All of the functions in the :mod:`struct` module have been rewritten in
|
|
|
|
C, thanks to work at the Need For Speed sprint.
|
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-08-30 23:24:08 -03:00
|
|
|
* Some of the standard built-in types now set a bit in their type
|
|
|
|
objects. This speeds up checking whether an object is a subclass of
|
|
|
|
one of these types. (Contributed by Neal Norwitz.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-04-20 23:14:24 -03:00
|
|
|
* Unicode strings now use faster code for detecting
|
2008-05-24 15:31:28 -03:00
|
|
|
whitespace and line breaks; this speeds up the :meth:`split` method
|
2008-03-03 21:50:32 -04:00
|
|
|
by about 25% and :meth:`splitlines` by 35%.
|
2008-03-20 19:49:26 -03:00
|
|
|
(Contributed by Antoine Pitrou.) Memory usage is reduced
|
|
|
|
by using pymalloc for the Unicode string's data.
|
|
|
|
|
|
|
|
* The ``with`` statement now stores the :meth:`__exit__` method on the stack,
|
2008-03-25 21:30:02 -03:00
|
|
|
producing a small speedup. (Implemented by Jeffrey Yasskin.)
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
* To reduce memory usage, the garbage collector will now clear internal
|
|
|
|
free lists when garbage-collecting the highest generation of objects.
|
2008-08-31 11:29:31 -03:00
|
|
|
This may return memory to the operating system sooner.
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
.. _new-26-interpreter:
|
2008-04-12 22:05:59 -03:00
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
Interpreter Changes
|
2008-04-12 22:05:59 -03:00
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
Two command-line options have been reserved for use by other Python
|
|
|
|
implementations. The :option:`-J` switch has been reserved for use by
|
2008-08-31 11:29:31 -03:00
|
|
|
Jython for Jython-specific options, such as switches that are passed to
|
2008-04-12 22:05:59 -03:00
|
|
|
the underlying JVM. :option:`-X` has been reserved for options
|
|
|
|
specific to a particular implementation of Python such as CPython,
|
|
|
|
Jython, or IronPython. If either option is used with Python 2.6, the
|
|
|
|
interpreter will report that the option isn't currently used.
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
Python can now be prevented from writing :file:`.pyc` or :file:`.pyo`
|
|
|
|
files by supplying the :option:`-B` switch to the Python interpreter,
|
|
|
|
or by setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment
|
|
|
|
variable before running the interpreter. This setting is available to
|
|
|
|
Python programs as the ``sys.dont_write_bytecode`` variable, and
|
|
|
|
Python code can change the value to modify the interpreter's
|
|
|
|
behaviour. (Contributed by Neal Norwitz and Georg Brandl.)
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
The encoding used for standard input, output, and standard error can
|
|
|
|
be specified by setting the :envvar:`PYTHONIOENCODING` environment
|
2008-08-26 21:27:18 -03:00
|
|
|
variable before running the interpreter. The value should be a string
|
2008-08-31 11:29:31 -03:00
|
|
|
in the form ``<encoding>`` or ``<encoding>:<errorhandler>``.
|
2008-08-30 19:56:54 -03:00
|
|
|
The *encoding* part specifies the encoding's name, e.g. ``utf-8`` or
|
|
|
|
``latin-1``; the optional *errorhandler* part specifies
|
2008-06-20 08:39:54 -03:00
|
|
|
what to do with characters that can't be handled by the encoding,
|
|
|
|
and should be one of "error", "ignore", or "replace". (Contributed
|
|
|
|
by Martin von Loewis.)
|
|
|
|
|
2008-04-12 22:05:59 -03:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-11-18 14:52:23 -04:00
|
|
|
New and Improved Modules
|
|
|
|
========================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
As in every release, Python's standard library received a number of
|
|
|
|
enhancements and bug fixes. Here's a partial list of the most notable
|
|
|
|
changes, sorted alphabetically by module name. Consult the
|
|
|
|
:file:`Misc/NEWS` file in the source tree for a more complete list of
|
|
|
|
changes, or look through the Subversion logs for all the details.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
* The :mod:`asyncore` and :mod:`asynchat` modules are
|
|
|
|
being actively maintained again, and a number of patches and bugfixes
|
|
|
|
were applied. (Maintained by Josiah Carlson; see :issue:`1736190` for
|
2008-06-19 23:05:57 -03:00
|
|
|
one patch.)
|
|
|
|
|
2010-04-10 22:40:49 -03:00
|
|
|
* The :mod:`bsddb` module also has a new maintainer, Jesús Cea Avion, and the package
|
2008-09-13 19:54:43 -03:00
|
|
|
is now available as a standalone package. The web page for the package is
|
|
|
|
`www.jcea.es/programacion/pybsddb.htm
|
|
|
|
<http://www.jcea.es/programacion/pybsddb.htm>`__.
|
2008-09-27 11:12:33 -03:00
|
|
|
The plan is to remove the package from the standard library
|
|
|
|
in Python 3.0, because its pace of releases is much more frequent than
|
|
|
|
Python's.
|
2008-08-31 11:29:31 -03:00
|
|
|
|
2008-09-27 11:12:33 -03:00
|
|
|
The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol
|
2007-10-16 19:58:03 -03:00
|
|
|
available, instead of restricting itself to protocol 1.
|
2009-10-11 17:16:16 -03:00
|
|
|
(Contributed by W. Barnes.)
|
2007-10-16 19:58:03 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The :mod:`cgi` module will now read variables from the query string
|
|
|
|
of an HTTP POST request. This makes it possible to use form actions
|
|
|
|
with URLs that include query strings such as
|
|
|
|
"/cgi-bin/add.py?category=1". (Contributed by Alexandre Fiori and
|
|
|
|
Nubis; :issue:`1817`.)
|
2008-04-20 13:54:02 -03:00
|
|
|
|
2009-01-03 16:55:06 -04:00
|
|
|
The :func:`parse_qs` and :func:`parse_qsl` functions have been
|
2008-09-27 11:12:33 -03:00
|
|
|
relocated from the :mod:`cgi` module to the :mod:`urlparse` module.
|
2009-01-03 16:55:06 -04:00
|
|
|
The versions still available in the :mod:`cgi` module will
|
2008-09-27 11:12:33 -03:00
|
|
|
trigger :exc:`PendingDeprecationWarning` messages in 2.6
|
|
|
|
(:issue:`600362`).
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The :mod:`cmath` module underwent extensive revision,
|
|
|
|
contributed by Mark Dickinson and Christian Heimes.
|
2008-05-24 15:31:28 -03:00
|
|
|
Five new functions were added:
|
2008-04-20 13:54:02 -03:00
|
|
|
|
|
|
|
* :func:`polar` converts a complex number to polar form, returning
|
2008-08-31 11:29:31 -03:00
|
|
|
the modulus and argument of the complex number.
|
2008-04-19 17:31:16 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* :func:`rect` does the opposite, turning a modulus, argument pair
|
2008-04-20 13:54:02 -03:00
|
|
|
back into the corresponding complex number.
|
2008-04-19 17:31:16 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* :func:`phase` returns the argument (also called the angle) of a complex
|
|
|
|
number.
|
2008-04-19 17:31:16 -03:00
|
|
|
|
2008-04-20 13:54:02 -03:00
|
|
|
* :func:`isnan` returns True if either
|
2008-05-24 15:31:28 -03:00
|
|
|
the real or imaginary part of its argument is a NaN.
|
2008-04-20 13:54:02 -03:00
|
|
|
|
|
|
|
* :func:`isinf` returns True if either the real or imaginary part of
|
|
|
|
its argument is infinite.
|
|
|
|
|
|
|
|
The revisions also improved the numerical soundness of the
|
|
|
|
:mod:`cmath` module. For all functions, the real and imaginary
|
|
|
|
parts of the results are accurate to within a few units of least
|
|
|
|
precision (ulps) whenever possible. See :issue:`1381` for the
|
|
|
|
details. The branch cuts for :func:`asinh`, :func:`atanh`: and
|
|
|
|
:func:`atan` have also been corrected.
|
|
|
|
|
|
|
|
The tests for the module have been greatly expanded; nearly 2000 new
|
|
|
|
test cases exercise the algebraic functions.
|
2008-04-19 17:31:16 -03:00
|
|
|
|
|
|
|
On IEEE 754 platforms, the :mod:`cmath` module now handles IEEE 754
|
|
|
|
special values and floating-point exceptions in a manner consistent
|
|
|
|
with Annex 'G' of the C99 standard.
|
|
|
|
|
2007-10-23 17:55:47 -03:00
|
|
|
* A new data type in the :mod:`collections` module: :class:`namedtuple(typename,
|
2007-08-15 11:28:01 -03:00
|
|
|
fieldnames)` is a factory function that creates subclasses of the standard tuple
|
|
|
|
whose fields are accessible by name as well as index. For example::
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
>>> var_type = collections.namedtuple('variable',
|
2007-09-24 21:09:42 -03:00
|
|
|
... 'id name type size')
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> # Names are separated by spaces or commas.
|
|
|
|
>>> # 'id, name, type, size' would also work.
|
2007-12-14 14:12:21 -04:00
|
|
|
>>> var_type._fields
|
2007-09-24 21:09:42 -03:00
|
|
|
('id', 'name', 'type', 'size')
|
|
|
|
|
|
|
|
>>> var = var_type(1, 'frequency', 'int', 4)
|
2009-01-03 17:04:55 -04:00
|
|
|
>>> print var[0], var.id # Equivalent
|
2007-09-24 21:09:42 -03:00
|
|
|
1 1
|
2009-01-03 17:04:55 -04:00
|
|
|
>>> print var[2], var.type # Equivalent
|
2007-09-24 21:09:42 -03:00
|
|
|
int int
|
2007-12-14 14:12:21 -04:00
|
|
|
>>> var._asdict()
|
2007-10-16 19:58:03 -03:00
|
|
|
{'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
|
2008-02-15 17:21:25 -04:00
|
|
|
>>> v2 = var._replace(name='amplitude')
|
2007-09-24 21:09:42 -03:00
|
|
|
>>> v2
|
|
|
|
variable(id=1, name='amplitude', type='int', size=4)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
Several places in the standard library that returned tuples have
|
|
|
|
been modified to return :class:`namedtuple` instances. For example,
|
2008-05-24 15:31:28 -03:00
|
|
|
the :meth:`Decimal.as_tuple` method now returns a named tuple with
|
2008-01-14 21:47:32 -04:00
|
|
|
:attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Another change to the :mod:`collections` module is that the
|
2007-12-08 07:05:05 -04:00
|
|
|
:class:`deque` type now supports an optional *maxlen* parameter;
|
2008-05-24 15:31:28 -03:00
|
|
|
if supplied, the deque's size will be restricted to no more
|
2007-12-08 07:05:05 -04:00
|
|
|
than *maxlen* items. Adding more items to a full deque causes
|
2007-10-16 19:58:03 -03:00
|
|
|
old items to be discarded.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> from collections import deque
|
|
|
|
>>> dq=deque(maxlen=3)
|
|
|
|
>>> dq
|
|
|
|
deque([], maxlen=3)
|
|
|
|
>>> dq.append(1) ; dq.append(2) ; dq.append(3)
|
|
|
|
>>> dq
|
|
|
|
deque([1, 2, 3], maxlen=3)
|
|
|
|
>>> dq.append(4)
|
|
|
|
>>> dq
|
|
|
|
deque([2, 3, 4], maxlen=3)
|
|
|
|
|
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-09-27 11:12:33 -03:00
|
|
|
* The :mod:`Cookie` module's :class:`Morsel` objects now support an
|
|
|
|
:attr:`httponly` attribute. In some browsers. cookies with this attribute
|
|
|
|
set cannot be accessed or manipulated by JavaScript code.
|
|
|
|
(Contributed by Arvin Schnell; :issue:`1638033`.)
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* A new window method in the :mod:`curses` module,
|
|
|
|
:meth:`chgat`, changes the display attributes for a certain number of
|
2008-10-08 14:30:55 -03:00
|
|
|
characters on a single line. (Contributed by Fabian Kreutz.)
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
# Boldface text starting at y=0,x=21
|
2007-08-15 11:28:01 -03:00
|
|
|
# and affecting the rest of the line.
|
2008-10-08 14:30:55 -03:00
|
|
|
stdscr.chgat(0, 21, curses.A_BOLD)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-01-19 20:00:38 -04:00
|
|
|
The :class:`Textbox` class in the :mod:`curses.textpad` module
|
|
|
|
now supports editing in insert mode as well as overwrite mode.
|
|
|
|
Insert mode is enabled by supplying a true value for the *insert_mode*
|
|
|
|
parameter when creating the :class:`Textbox` instance.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
* The :mod:`datetime` module's :meth:`strftime` methods now support a
|
|
|
|
``%f`` format code that expands to the number of microseconds in the
|
|
|
|
object, zero-padded on
|
2008-04-10 18:29:01 -03:00
|
|
|
the left to six places. (Contributed by Skip Montanaro; :issue:`1158`.)
|
2008-03-20 19:49:26 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`decimal` module was updated to version 1.66 of
|
2007-09-24 21:09:42 -03:00
|
|
|
`the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features
|
|
|
|
include some methods for some basic mathematical functions such as
|
|
|
|
:meth:`exp` and :meth:`log10`::
|
|
|
|
|
|
|
|
>>> Decimal(1).exp()
|
|
|
|
Decimal("2.718281828459045235360287471")
|
|
|
|
>>> Decimal("2.7182818").ln()
|
|
|
|
Decimal("0.9999999895305022877376682436")
|
|
|
|
>>> Decimal(1000).log10()
|
|
|
|
Decimal("3")
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
The :meth:`as_tuple` method of :class:`Decimal` objects now returns a
|
2008-01-14 21:47:32 -04:00
|
|
|
named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-01-14 21:47:32 -04:00
|
|
|
(Implemented by Facundo Batista and Mark Dickinson. Named tuple
|
|
|
|
support added by Raymond Hettinger.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`difflib` module's :class:`SequenceMatcher` class
|
2008-08-31 11:29:31 -03:00
|
|
|
now returns named tuples representing matches,
|
|
|
|
with :attr:`a`, :attr:`b`, and :attr:`size` attributes.
|
2008-01-14 21:47:32 -04:00
|
|
|
(Contributed by Raymond Hettinger.)
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* An optional ``timeout`` parameter, specifying a timeout measured in
|
|
|
|
seconds, was added to the :class:`ftplib.FTP` class constructor as
|
|
|
|
well as the :meth:`connect` method. (Added by Facundo Batista.)
|
|
|
|
Also, the :class:`FTP` class's :meth:`storbinary` and
|
|
|
|
:meth:`storlines` now take an optional *callback* parameter that
|
|
|
|
will be called with each block of data after the data has been sent.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Phil Schwartz; :issue:`1221598`.)
|
2007-09-01 18:18:31 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :func:`reduce` built-in function is also available in the
|
2010-03-07 11:23:59 -04:00
|
|
|
:mod:`functools` module. In Python 3.0, the builtin has been
|
2008-08-31 11:29:31 -03:00
|
|
|
dropped and :func:`reduce` is only available from :mod:`functools`;
|
2010-03-07 11:23:59 -04:00
|
|
|
currently there are no plans to drop the builtin in the 2.x series.
|
2008-08-31 11:29:31 -03:00
|
|
|
(Patched by Christian Heimes; :issue:`1739906`.)
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
* When possible, the :mod:`getpass` module will now use
|
2008-08-31 11:29:31 -03:00
|
|
|
:file:`/dev/tty` to print a prompt message and read the password,
|
|
|
|
falling back to standard error and standard input. If the
|
|
|
|
password may be echoed to the terminal, a warning is printed before
|
|
|
|
the prompt is displayed. (Contributed by Gregory P. Smith.)
|
2008-06-05 20:35:31 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :func:`glob.glob` function can now return Unicode filenames if
|
2008-04-10 18:29:01 -03:00
|
|
|
a Unicode path was used and Unicode filenames are matched within the
|
|
|
|
directory. (:issue:`1001604`)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* A new function in the :mod:`heapq` module, ``merge(iter1, iter2, ...)``,
|
|
|
|
takes any number of iterables returning data in sorted
|
2008-09-04 10:26:24 -03:00
|
|
|
order, and returns a new generator that returns the contents of all
|
2008-03-20 19:49:26 -03:00
|
|
|
the iterators, also in sorted order. For example::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
|
|
|
|
[1, 2, 3, 5, 8, 9, 16]
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
Another new function, ``heappushpop(heap, item)``,
|
2008-05-24 15:31:28 -03:00
|
|
|
pushes *item* onto *heap*, then pops off and returns the smallest item.
|
2008-03-20 19:49:26 -03:00
|
|
|
This is more efficient than making a call to :func:`heappush` and then
|
|
|
|
:func:`heappop`.
|
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
:mod:`heapq` is now implemented to only use less-than comparison,
|
|
|
|
instead of the less-than-or-equal comparison it previously used.
|
2008-08-31 11:29:31 -03:00
|
|
|
This makes :mod:`heapq`'s usage of a type match the
|
2008-06-05 20:35:31 -03:00
|
|
|
:meth:`list.sort` method.
|
2007-08-15 11:28:01 -03:00
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* An optional ``timeout`` parameter, specifying a timeout measured in
|
|
|
|
seconds, was added to the :class:`httplib.HTTPConnection` and
|
|
|
|
:class:`HTTPSConnection` class constructors. (Added by Facundo
|
|
|
|
Batista.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Most of the :mod:`inspect` module's functions, such as
|
|
|
|
:func:`getmoduleinfo` and :func:`getargs`, now return named tuples.
|
2008-01-14 21:47:32 -04:00
|
|
|
In addition to behaving like tuples, the elements of the return value
|
|
|
|
can also be accessed as attributes.
|
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Some new functions in the module include
|
|
|
|
:func:`isgenerator`, :func:`isgeneratorfunction`,
|
2008-03-03 21:50:32 -04:00
|
|
|
and :func:`isabstract`.
|
|
|
|
|
|
|
|
* The :mod:`itertools` module gained several new functions.
|
|
|
|
|
|
|
|
``izip_longest(iter1, iter2, ...[, fillvalue])`` makes tuples from
|
|
|
|
each of the elements; if some of the iterables are shorter than
|
|
|
|
others, the missing values are set to *fillvalue*. For example::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
|
|
|
|
((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product
|
|
|
|
of the supplied iterables, a set of tuples containing
|
|
|
|
every possible combination of the elements returned from each iterable. ::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.product([1,2,3], [4,5,6]))
|
|
|
|
[(1, 4), (1, 5), (1, 6),
|
2009-01-03 17:04:55 -04:00
|
|
|
(2, 4), (2, 5), (2, 6),
|
|
|
|
(3, 4), (3, 5), (3, 6)]
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
The optional *repeat* keyword argument is used for taking the
|
2008-05-24 15:31:28 -03:00
|
|
|
product of an iterable or a set of iterables with themselves,
|
2008-03-03 21:50:32 -04:00
|
|
|
repeated *N* times. With a single iterable argument, *N*-tuples
|
|
|
|
are returned::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.product([1,2], repeat=3))
|
|
|
|
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
|
|
|
|
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
With two iterables, *2N*-tuples are returned. ::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.product([1,2], [3,4], repeat=2))
|
|
|
|
[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
|
|
|
|
(1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
|
|
|
|
(2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
|
|
|
|
(2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
``combinations(iterable, r)`` returns sub-sequences of length *r* from
|
2008-03-03 21:50:32 -04:00
|
|
|
the elements of *iterable*. ::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.combinations('123', 2))
|
|
|
|
[('1', '2'), ('1', '3'), ('2', '3')]
|
|
|
|
>>> list(itertools.combinations('123', 3))
|
|
|
|
[('1', '2', '3')]
|
|
|
|
>>> list(itertools.combinations('1234', 3))
|
|
|
|
[('1', '2', '3'), ('1', '2', '4'),
|
|
|
|
('1', '3', '4'), ('2', '3', '4')]
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2008-03-05 21:36:27 -04:00
|
|
|
``permutations(iter[, r])`` returns all the permutations of length *r* of
|
2008-05-24 15:31:28 -03:00
|
|
|
the iterable's elements. If *r* is not specified, it will default to the
|
2008-05-05 17:59:05 -03:00
|
|
|
number of elements produced by the iterable. ::
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.permutations([1,2,3,4], 2))
|
|
|
|
[(1, 2), (1, 3), (1, 4),
|
|
|
|
(2, 1), (2, 3), (2, 4),
|
|
|
|
(3, 1), (3, 2), (3, 4),
|
|
|
|
(4, 1), (4, 2), (4, 3)]
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-04-08 18:22:53 -03:00
|
|
|
``itertools.chain(*iterables)`` is an existing function in
|
2008-03-05 21:36:27 -04:00
|
|
|
:mod:`itertools` that gained a new constructor in Python 2.6.
|
2008-05-24 15:31:28 -03:00
|
|
|
``itertools.chain.from_iterable(iterable)`` takes a single
|
2008-03-03 21:50:32 -04:00
|
|
|
iterable that should return other iterables. :func:`chain` will
|
|
|
|
then return all the elements of the first iterable, then
|
|
|
|
all the elements of the second, and so on. ::
|
|
|
|
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
(All contributed by Raymond Hettinger.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`logging` module's :class:`FileHandler` class
|
2008-03-04 20:44:41 -04:00
|
|
|
and its subclasses :class:`WatchedFileHandler`, :class:`RotatingFileHandler`,
|
2008-05-24 15:31:28 -03:00
|
|
|
and :class:`TimedRotatingFileHandler` now
|
2008-08-31 11:29:31 -03:00
|
|
|
have an optional *delay* parameter to their constructors. If *delay*
|
2008-03-04 20:44:41 -04:00
|
|
|
is true, opening of the log file is deferred until the first
|
|
|
|
:meth:`emit` call is made. (Contributed by Vinay Sajip.)
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
:class:`TimedRotatingFileHandler` also has a *utc* constructor
|
|
|
|
parameter. If the argument is true, UTC time will be used
|
2008-06-05 20:35:31 -03:00
|
|
|
in determining when midnight occurs and in generating filenames;
|
|
|
|
otherwise local time will be used.
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* Several new functions were added to the :mod:`math` module:
|
|
|
|
|
|
|
|
* :func:`~math.isinf` and :func:`~math.isnan` determine whether a given float
|
|
|
|
is a (positive or negative) infinity or a NaN (Not a Number), respectively.
|
|
|
|
|
|
|
|
* :func:`~math.copysign` copies the sign bit of an IEEE 754 number,
|
|
|
|
returning the absolute value of *x* combined with the sign bit of
|
|
|
|
*y*. For example, ``math.copysign(1, -0.0)`` returns -1.0.
|
|
|
|
(Contributed by Christian Heimes.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* :func:`~math.factorial` computes the factorial of a number.
|
|
|
|
(Contributed by Raymond Hettinger; :issue:`2138`.)
|
|
|
|
|
|
|
|
* :func:`~math.fsum` adds up the stream of numbers from an iterable,
|
|
|
|
and is careful to avoid loss of precision through using partial sums.
|
|
|
|
(Contributed by Jean Brouwers, Raymond Hettinger, and Mark Dickinson;
|
|
|
|
:issue:`2819`.)
|
|
|
|
|
|
|
|
* :func:`~math.acosh`, :func:`~math.asinh`
|
|
|
|
and :func:`~math.atanh` compute the inverse hyperbolic functions.
|
|
|
|
|
|
|
|
* :func:`~math.log1p` returns the natural logarithm of *1+x*
|
|
|
|
(base *e*).
|
|
|
|
|
|
|
|
* :func:`trunc` rounds a number toward zero, returning the closest
|
|
|
|
:class:`Integral` that's between the function's argument and zero.
|
|
|
|
Added as part of the backport of
|
|
|
|
`PEP 3141's type hierarchy for numbers <#pep-3141>`__.
|
|
|
|
|
|
|
|
* The :mod:`math` module has been improved to give more consistent
|
|
|
|
behaviour across platforms, especially with respect to handling of
|
|
|
|
floating-point exceptions and IEEE 754 special values.
|
|
|
|
|
|
|
|
Whenever possible, the module follows the recommendations of the C99
|
|
|
|
standard about 754's special values. For example, ``sqrt(-1.)``
|
|
|
|
should now give a :exc:`ValueError` across almost all platforms,
|
|
|
|
while ``sqrt(float('NaN'))`` should return a NaN on all IEEE 754
|
|
|
|
platforms. Where Annex 'F' of the C99 standard recommends signaling
|
|
|
|
'divide-by-zero' or 'invalid', Python will raise :exc:`ValueError`.
|
|
|
|
Where Annex 'F' of the C99 standard recommends signaling 'overflow',
|
|
|
|
Python will raise :exc:`OverflowError`. (See :issue:`711019` and
|
|
|
|
:issue:`1640`.)
|
|
|
|
|
|
|
|
(Contributed by Christian Heimes and Mark Dickinson.)
|
|
|
|
|
|
|
|
* :class:`mmap` objects now have a :meth:`rfind` method that searches for a
|
|
|
|
substring beginning at the end of the string and searching
|
|
|
|
backwards. The :meth:`find` method also gained an *end* parameter
|
|
|
|
giving an index at which to stop searching.
|
2008-01-19 15:14:05 -04:00
|
|
|
(Contributed by John Lenton.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`operator` module gained a
|
|
|
|
:func:`methodcaller` function that takes a name and an optional
|
|
|
|
set of arguments, returning a callable that will call
|
2008-03-03 21:50:32 -04:00
|
|
|
the named function on any arguments passed to it. For example::
|
|
|
|
|
|
|
|
>>> # Equivalent to lambda s: s.replace('old', 'new')
|
|
|
|
>>> replacer = operator.methodcaller('replace', 'old', 'new')
|
|
|
|
>>> replacer('old wine in old bottles')
|
|
|
|
'new wine in new bottles'
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-03-04 03:25:54 -04:00
|
|
|
(Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.)
|
2008-03-03 21:50:32 -04:00
|
|
|
|
|
|
|
The :func:`attrgetter` function now accepts dotted names and performs
|
|
|
|
the corresponding attribute lookups::
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
>>> inst_name = operator.attrgetter(
|
|
|
|
... '__class__.__name__')
|
2008-03-03 21:50:32 -04:00
|
|
|
>>> inst_name('')
|
|
|
|
'str'
|
|
|
|
>>> inst_name(help)
|
|
|
|
'_Helper'
|
|
|
|
|
2008-03-04 03:25:54 -04:00
|
|
|
(Contributed by Georg Brandl, after a suggestion by Barry Warsaw.)
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The :mod:`os` module now wraps several new system calls.
|
|
|
|
``fchmod(fd, mode)`` and ``fchown(fd, uid, gid)`` change the mode
|
|
|
|
and ownership of an opened file, and ``lchmod(path, mode)`` changes
|
|
|
|
the mode of a symlink. (Contributed by Georg Brandl and Christian
|
|
|
|
Heimes.)
|
|
|
|
|
|
|
|
:func:`chflags` and :func:`lchflags` are wrappers for the
|
|
|
|
corresponding system calls (where they're available), changing the
|
|
|
|
flags set on a file. Constants for the flag values are defined in
|
|
|
|
the :mod:`stat` module; some possible values include
|
|
|
|
:const:`UF_IMMUTABLE` to signal the file may not be changed and
|
|
|
|
:const:`UF_APPEND` to indicate that data can only be appended to the
|
|
|
|
file. (Contributed by M. Levinson.)
|
|
|
|
|
2008-09-04 10:26:24 -03:00
|
|
|
``os.closerange(low, high)`` efficiently closes all file descriptors
|
2008-08-31 11:29:31 -03:00
|
|
|
from *low* to *high*, ignoring any errors and not including *high* itself.
|
|
|
|
This function is now used by the :mod:`subprocess` module to make starting
|
|
|
|
processes faster. (Contributed by Georg Brandl; :issue:`1663329`.)
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The ``os.environ`` object's :meth:`clear` method will now unset the
|
|
|
|
environment variables using :func:`os.unsetenv` in addition to clearing
|
|
|
|
the object's keys. (Contributed by Martin Horcicka; :issue:`1181`.)
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2007-09-24 21:09:42 -03:00
|
|
|
* The :func:`os.walk` function now has a ``followlinks`` parameter. If
|
2007-09-13 19:49:34 -03:00
|
|
|
set to True, it will follow symlinks pointing to directories and
|
|
|
|
visit the directory's contents. For backward compatibility, the
|
|
|
|
parameter's default value is false. Note that the function can fall
|
|
|
|
into an infinite recursion if there's a symlink that points to a
|
2008-04-10 18:29:01 -03:00
|
|
|
parent directory. (:issue:`1273829`)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-09-01 17:43:36 -03:00
|
|
|
* In the :mod:`os.path` module, the :func:`splitext` function
|
|
|
|
has been changed to not split on leading period characters.
|
|
|
|
This produces better results when operating on Unix's dot-files.
|
|
|
|
For example, ``os.path.splitext('.ipython')``
|
|
|
|
now returns ``('.ipython', '')`` instead of ``('', '.ipython')``.
|
2010-04-05 09:51:45 -03:00
|
|
|
(:issue:`1115886`)
|
2007-09-01 17:43:36 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
A new function, ``os.path.relpath(path, start='.')``, returns a relative path
|
2007-09-01 18:18:31 -03:00
|
|
|
from the ``start`` path, if it's supplied, or from the current
|
|
|
|
working directory to the destination ``path``. (Contributed by
|
2008-04-10 18:29:01 -03:00
|
|
|
Richard Barran; :issue:`1339796`.)
|
2007-09-01 18:18:31 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
On Windows, :func:`os.path.expandvars` will now expand environment variables
|
2008-08-31 11:29:31 -03:00
|
|
|
given in the form "%var%", and "~user" will be expanded into the
|
2008-04-10 18:29:01 -03:00
|
|
|
user's home directory path. (Contributed by Josiah Carlson;
|
|
|
|
:issue:`957650`.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The Python debugger provided by the :mod:`pdb` module
|
2008-08-31 11:29:31 -03:00
|
|
|
gained a new command: "run" restarts the Python program being debugged
|
2007-09-24 21:09:42 -03:00
|
|
|
and can optionally take new command-line arguments for the program.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Rocky Bernstein; :issue:`1393667`.)
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2009-11-18 14:52:23 -04:00
|
|
|
* The :func:`pdb.post_mortem` function, used to begin debugging a
|
2008-04-07 20:57:07 -03:00
|
|
|
traceback, will now use the traceback returned by :func:`sys.exc_info`
|
2008-04-10 18:29:01 -03:00
|
|
|
if no traceback is supplied. (Contributed by Facundo Batista;
|
|
|
|
:issue:`1106316`.)
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`pickletools` module now has an :func:`optimize` function
|
|
|
|
that takes a string containing a pickle and removes some unused
|
2008-03-03 21:50:32 -04:00
|
|
|
opcodes, returning a shorter pickle that contains the same data structure.
|
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2008-04-20 23:14:24 -03:00
|
|
|
* A :func:`get_data` function was added to the :mod:`pkgutil`
|
|
|
|
module that returns the contents of resource files included
|
|
|
|
with an installed Python package. For example::
|
|
|
|
|
2008-04-21 19:57:00 -03:00
|
|
|
>>> import pkgutil
|
2008-10-08 14:30:55 -03:00
|
|
|
>>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
|
|
|
|
BaseException
|
2008-04-21 19:57:00 -03:00
|
|
|
+-- SystemExit
|
|
|
|
+-- KeyboardInterrupt
|
|
|
|
+-- GeneratorExit
|
|
|
|
+-- Exception
|
|
|
|
+-- StopIteration
|
|
|
|
+-- StandardError
|
2008-10-08 14:30:55 -03:00
|
|
|
...
|
2008-04-20 23:14:24 -03:00
|
|
|
|
|
|
|
(Contributed by Paul Moore; :issue:`2439`.)
|
|
|
|
|
2008-01-08 10:30:55 -04:00
|
|
|
* The :mod:`pyexpat` module's :class:`Parser` objects now allow setting
|
2008-05-24 15:31:28 -03:00
|
|
|
their :attr:`buffer_size` attribute to change the size of the buffer
|
2008-01-08 10:30:55 -04:00
|
|
|
used to hold character data.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Achim Gaedke; :issue:`1137`.)
|
2008-01-08 10:30:55 -04:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The :mod:`Queue` module now provides queue variants that retrieve entries
|
2008-05-24 15:31:28 -03:00
|
|
|
in different orders. The :class:`PriorityQueue` class stores
|
|
|
|
queued items in a heap and retrieves them in priority order,
|
2008-01-26 09:50:51 -04:00
|
|
|
and :class:`LifoQueue` retrieves the most recently added entries first,
|
|
|
|
meaning that it behaves like a stack.
|
|
|
|
(Contributed by Raymond Hettinger.)
|
|
|
|
|
2007-12-18 22:02:04 -04:00
|
|
|
* The :mod:`random` module's :class:`Random` objects can
|
|
|
|
now be pickled on a 32-bit system and unpickled on a 64-bit
|
|
|
|
system, and vice versa. Unfortunately, this change also means
|
|
|
|
that Python 2.6's :class:`Random` objects can't be unpickled correctly
|
|
|
|
on earlier versions of Python.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Shawn Ligocki; :issue:`1727780`.)
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-03-25 21:30:02 -03:00
|
|
|
The new ``triangular(low, high, mode)`` function returns random
|
|
|
|
numbers following a triangular distribution. The returned values
|
2008-05-24 15:31:28 -03:00
|
|
|
are between *low* and *high*, not including *high* itself, and
|
2008-08-31 11:29:31 -03:00
|
|
|
with *mode* as the most frequently occurring value
|
2008-04-05 00:38:39 -03:00
|
|
|
in the distribution. (Contributed by Wladmir van der Laan and
|
2008-04-10 18:29:01 -03:00
|
|
|
Raymond Hettinger; :issue:`1681432`.)
|
2008-03-25 21:30:02 -03:00
|
|
|
|
2008-01-14 21:47:32 -04:00
|
|
|
* Long regular expression searches carried out by the :mod:`re`
|
2008-08-31 11:29:31 -03:00
|
|
|
module will check for signals being delivered, so
|
2008-06-20 20:43:12 -03:00
|
|
|
time-consuming searches can now be interrupted.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Josh Hoyt and Ralf Schmitt; :issue:`846388`.)
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
The regular expression module is implemented by compiling bytecodes
|
|
|
|
for a tiny regex-specific virtual machine. Untrusted code
|
|
|
|
could create malicious strings of bytecode directly and cause crashes,
|
|
|
|
so Python 2.6 includes a verifier for the regex bytecode.
|
|
|
|
(Contributed by Guido van Rossum from work for Google App Engine;
|
|
|
|
:issue:`3487`.)
|
|
|
|
|
2008-06-20 20:43:12 -03:00
|
|
|
* The :mod:`rlcompleter` module's :meth:`Completer.complete()` method
|
|
|
|
will now ignore exceptions triggered while evaluating a name.
|
|
|
|
(Fixed by Lorenz Quack; :issue:`2250`.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`sched` module's :class:`scheduler` instances now
|
|
|
|
have a read-only :attr:`queue` attribute that returns the
|
2008-03-04 20:44:41 -04:00
|
|
|
contents of the scheduler's queue, represented as a list of
|
2008-03-05 03:10:35 -04:00
|
|
|
named tuples with the fields ``(time, priority, action, argument)``.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Raymond Hettinger; :issue:`1861`.)
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-03-25 21:30:02 -03:00
|
|
|
* The :mod:`select` module now has wrapper functions
|
2012-01-14 11:42:02 -04:00
|
|
|
for the Linux :c:func:`epoll` and BSD :c:func:`kqueue` system calls.
|
2008-08-31 11:29:31 -03:00
|
|
|
:meth:`modify` method was added to the existing :class:`poll`
|
2008-03-25 21:30:02 -03:00
|
|
|
objects; ``pollobj.modify(fd, eventmask)`` takes a file descriptor
|
2008-08-31 11:29:31 -03:00
|
|
|
or file object and an event mask, modifying the recorded event mask
|
|
|
|
for that file.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Christian Heimes; :issue:`1657`.)
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-08-30 19:56:54 -03:00
|
|
|
* The :func:`shutil.copytree` function now has an optional *ignore* argument
|
2008-07-06 14:44:17 -03:00
|
|
|
that takes a callable object. This callable will receive each directory path
|
|
|
|
and a list of the directory's contents, and returns a list of names that
|
2008-08-26 21:27:18 -03:00
|
|
|
will be ignored, not copied.
|
2008-07-06 14:44:17 -03:00
|
|
|
|
|
|
|
The :mod:`shutil` module also provides an :func:`ignore_patterns`
|
2008-09-04 10:26:24 -03:00
|
|
|
function for use with this new parameter. :func:`ignore_patterns`
|
|
|
|
takes an arbitrary number of glob-style patterns and returns a
|
|
|
|
callable that will ignore any files and directories that match any
|
|
|
|
of these patterns. The following example copies a directory tree,
|
|
|
|
but skips both :file:`.svn` directories and Emacs backup files,
|
|
|
|
which have names ending with '~'::
|
2008-07-06 14:44:17 -03:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
shutil.copytree('Doc/library', '/tmp/library',
|
2008-07-07 13:51:09 -03:00
|
|
|
ignore=shutil.ignore_patterns('*~', '.svn'))
|
2008-07-06 14:44:17 -03:00
|
|
|
|
|
|
|
(Contributed by Tarek Ziadé; :issue:`2663`.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Integrating signal handling with GUI handling event loops
|
2007-12-22 13:27:02 -04:00
|
|
|
like those used by Tkinter or GTk+ has long been a problem; most
|
2008-08-31 11:29:31 -03:00
|
|
|
software ends up polling, waking up every fraction of a second to check
|
|
|
|
if any GUI events have occurred.
|
2007-12-22 13:27:02 -04:00
|
|
|
The :mod:`signal` module can now make this more efficient.
|
|
|
|
Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor
|
2008-05-24 15:31:28 -03:00
|
|
|
to be used; when a signal is received, a byte is written to that
|
2007-12-22 13:27:02 -04:00
|
|
|
file descriptor. There's also a C-level function,
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PySignal_SetWakeupFd`, for setting the descriptor.
|
2007-12-22 13:27:02 -04:00
|
|
|
|
|
|
|
Event loops will use this by opening a pipe to create two descriptors,
|
2008-04-05 00:38:39 -03:00
|
|
|
one for reading and one for writing. The writable descriptor
|
2007-12-22 13:27:02 -04:00
|
|
|
will be passed to :func:`set_wakeup_fd`, and the readable descriptor
|
|
|
|
will be added to the list of descriptors monitored by the event loop via
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`select` or :c:func:`poll`.
|
2008-05-24 15:31:28 -03:00
|
|
|
On receiving a signal, a byte will be written and the main event loop
|
2008-08-31 11:29:31 -03:00
|
|
|
will be woken up, avoiding the need to poll.
|
2007-12-22 13:27:02 -04:00
|
|
|
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Adam Olsen; :issue:`1583`.)
|
2007-12-22 13:27:02 -04:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
The :func:`siginterrupt` function is now available from Python code,
|
|
|
|
and allows changing whether signals can interrupt system calls or not.
|
|
|
|
(Contributed by Ralf Schmitt.)
|
|
|
|
|
2008-04-05 00:38:39 -03:00
|
|
|
The :func:`setitimer` and :func:`getitimer` functions have also been
|
2008-08-31 11:29:31 -03:00
|
|
|
added (where they're available). :func:`setitimer`
|
2008-04-05 00:38:39 -03:00
|
|
|
allows setting interval timers that will cause a signal to be
|
|
|
|
delivered to the process after a specified time, measured in
|
|
|
|
wall-clock time, consumed process time, or combined process+system
|
2008-04-10 18:29:01 -03:00
|
|
|
time. (Contributed by Guilherme Polo; :issue:`2240`.)
|
2008-04-05 00:38:39 -03:00
|
|
|
|
2007-09-01 17:43:36 -03:00
|
|
|
* The :mod:`smtplib` module now supports SMTP over SSL thanks to the
|
|
|
|
addition of the :class:`SMTP_SSL` class. This class supports an
|
2008-08-31 11:29:31 -03:00
|
|
|
interface identical to the existing :class:`SMTP` class.
|
|
|
|
(Contributed by Monty Taylor.) Both class constructors also have an
|
|
|
|
optional ``timeout`` parameter that specifies a timeout for the
|
|
|
|
initial connection attempt, measured in seconds. (Contributed by
|
|
|
|
Facundo Batista.)
|
|
|
|
|
|
|
|
An implementation of the LMTP protocol (:rfc:`2033`) was also added
|
|
|
|
to the module. LMTP is used in place of SMTP when transferring
|
|
|
|
e-mail between agents that don't manage a mail queue. (LMTP
|
|
|
|
implemented by Leif Hedstrom; :issue:`957003`.)
|
|
|
|
|
2009-11-18 14:52:23 -04:00
|
|
|
:meth:`SMTP.starttls` now complies with :rfc:`3207` and forgets any
|
2008-08-31 11:29:31 -03:00
|
|
|
knowledge obtained from the server not obtained from the TLS
|
|
|
|
negotiation itself. (Patch contributed by Bill Fenner;
|
2008-04-10 18:29:01 -03:00
|
|
|
:issue:`829951`.)
|
2008-01-17 03:43:20 -04:00
|
|
|
|
2008-01-14 21:47:32 -04:00
|
|
|
* The :mod:`socket` module now supports TIPC (http://tipc.sf.net),
|
|
|
|
a high-performance non-IP-based protocol designed for use in clustered
|
|
|
|
environments. TIPC addresses are 4- or 5-tuples.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Alberto Bertogli; :issue:`1646`.)
|
2008-01-19 12:34:09 -04:00
|
|
|
|
2009-10-05 18:25:35 -03:00
|
|
|
A new function, :func:`create_connection`, takes an address and
|
|
|
|
connects to it using an optional timeout value, returning the
|
|
|
|
connected socket object. This function also looks up the address's
|
|
|
|
type and connects to it using IPv4 or IPv6 as appropriate. Changing
|
|
|
|
your code to use :func:`create_connection` instead of
|
|
|
|
``socket(socket.AF_INET, ...)`` may be all that's required to make
|
|
|
|
your code work with IPv6.
|
2008-04-14 23:24:15 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The base classes in the :mod:`SocketServer` module now support
|
|
|
|
calling a :meth:`handle_timeout` method after a span of inactivity
|
|
|
|
specified by the server's :attr:`timeout` attribute. (Contributed
|
|
|
|
by Michael Pomraning.) The :meth:`serve_forever` method
|
2008-04-08 22:08:32 -03:00
|
|
|
now takes an optional poll interval measured in seconds,
|
|
|
|
controlling how often the server will check for a shutdown request.
|
2008-05-24 15:31:28 -03:00
|
|
|
(Contributed by Pedro Werneck and Jeffrey Yasskin;
|
2008-04-10 18:29:01 -03:00
|
|
|
:issue:`742598`, :issue:`1193577`.)
|
2008-03-05 21:36:27 -04:00
|
|
|
|
2008-09-17 10:04:53 -03:00
|
|
|
* The :mod:`sqlite3` module, maintained by Gerhard Haering,
|
2009-01-03 16:55:06 -04:00
|
|
|
has been updated from version 2.3.2 in Python 2.5 to
|
2008-09-17 10:04:53 -03:00
|
|
|
version 2.4.1.
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
* The :mod:`struct` module now supports the C99 :c:type:`_Bool` type,
|
2008-05-24 15:31:28 -03:00
|
|
|
using the format character ``'?'``.
|
2008-03-05 21:36:27 -04:00
|
|
|
(Contributed by David Remahl.)
|
2008-04-20 23:14:24 -03:00
|
|
|
|
|
|
|
* The :class:`Popen` objects provided by the :mod:`subprocess` module
|
|
|
|
now have :meth:`terminate`, :meth:`kill`, and :meth:`send_signal` methods.
|
|
|
|
On Windows, :meth:`send_signal` only supports the :const:`SIGTERM`
|
|
|
|
signal, and all these methods are aliases for the Win32 API function
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`TerminateProcess`.
|
2008-04-20 23:14:24 -03:00
|
|
|
(Contributed by Christian Heimes.)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* A new variable in the :mod:`sys` module, :attr:`float_info`, is an
|
|
|
|
object containing information derived from the :file:`float.h` file
|
|
|
|
about the platform's floating-point support. Attributes of this
|
|
|
|
object include :attr:`mant_dig` (number of digits in the mantissa),
|
|
|
|
:attr:`epsilon` (smallest difference between 1.0 and the next
|
|
|
|
largest value representable), and several others. (Contributed by
|
|
|
|
Christian Heimes; :issue:`1534`.)
|
2007-12-18 22:02:04 -04:00
|
|
|
|
2008-01-15 10:38:05 -04:00
|
|
|
Another new variable, :attr:`dont_write_bytecode`, controls whether Python
|
|
|
|
writes any :file:`.pyc` or :file:`.pyo` files on importing a module.
|
|
|
|
If this variable is true, the compiled files are not written. The
|
|
|
|
variable is initially set on start-up by supplying the :option:`-B`
|
|
|
|
switch to the Python interpreter, or by setting the
|
|
|
|
:envvar:`PYTHONDONTWRITEBYTECODE` environment variable before
|
2008-05-24 15:31:28 -03:00
|
|
|
running the interpreter. Python code can subsequently
|
2008-01-15 10:38:05 -04:00
|
|
|
change the value of this variable to control whether bytecode files
|
|
|
|
are written or not.
|
|
|
|
(Contributed by Neal Norwitz and Georg Brandl.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
Information about the command-line arguments supplied to the Python
|
2008-06-05 20:35:31 -03:00
|
|
|
interpreter is available by reading attributes of a named
|
|
|
|
tuple available as ``sys.flags``. For example, the :attr:`verbose`
|
|
|
|
attribute is true if Python
|
2008-01-14 21:47:32 -04:00
|
|
|
was executed in verbose mode, :attr:`debug` is true in debugging mode, etc.
|
|
|
|
These attributes are all read-only.
|
|
|
|
(Contributed by Christian Heimes.)
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
A new function, :func:`getsizeof`, takes a Python object and returns
|
2008-06-05 20:35:31 -03:00
|
|
|
the amount of memory used by the object, measured in bytes. Built-in
|
|
|
|
objects return correct results; third-party extensions may not,
|
2008-08-26 21:27:18 -03:00
|
|
|
but can define a :meth:`__sizeof__` method to return the
|
2008-06-05 20:35:31 -03:00
|
|
|
object's size.
|
|
|
|
(Contributed by Robert Schuppenies; :issue:`2898`.)
|
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
It's now possible to determine the current profiler and tracer functions
|
2008-05-24 15:31:28 -03:00
|
|
|
by calling :func:`sys.getprofile` and :func:`sys.gettrace`.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Georg Brandl; :issue:`1648`.)
|
2008-03-04 20:44:41 -04:00
|
|
|
|
2008-09-19 09:39:23 -03:00
|
|
|
* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) tarfiles in
|
|
|
|
addition to the POSIX.1-1988 (ustar) and GNU tar formats that were
|
|
|
|
already supported. The default format is GNU tar; specify the
|
|
|
|
``format`` parameter to open a file using a different format::
|
2007-09-17 22:36:16 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
tar = tarfile.open("output.tar", "w",
|
|
|
|
format=tarfile.PAX_FORMAT)
|
2007-09-17 22:36:16 -03:00
|
|
|
|
2008-09-19 09:39:23 -03:00
|
|
|
The new ``encoding`` and ``errors`` parameters specify an encoding and
|
|
|
|
an error handling scheme for character conversions. ``'strict'``,
|
|
|
|
``'ignore'``, and ``'replace'`` are the three standard ways Python can
|
|
|
|
handle errors,;
|
2008-08-31 11:29:31 -03:00
|
|
|
``'utf-8'`` is a special value that replaces bad characters with
|
|
|
|
their UTF-8 representation. (Character conversions occur because the
|
|
|
|
PAX format supports Unicode filenames, defaulting to UTF-8 encoding.)
|
2007-09-17 22:36:16 -03:00
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
The :meth:`TarFile.add` method now accepts an ``exclude`` argument that's
|
2007-09-17 22:36:16 -03:00
|
|
|
a function that can be used to exclude certain filenames from
|
2008-05-24 15:31:28 -03:00
|
|
|
an archive.
|
|
|
|
The function must take a filename and return true if the file
|
2007-09-17 22:36:16 -03:00
|
|
|
should be excluded or false if it should be archived.
|
|
|
|
The function is applied to both the name initially passed to :meth:`add`
|
|
|
|
and to the names of files in recursively-added directories.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2007-09-17 22:36:16 -03:00
|
|
|
(All changes contributed by Lars Gustäbel).
|
|
|
|
|
|
|
|
* An optional ``timeout`` parameter was added to the
|
|
|
|
:class:`telnetlib.Telnet` class constructor, specifying a timeout
|
|
|
|
measured in seconds. (Added by Facundo Batista.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :class:`tempfile.NamedTemporaryFile` class usually deletes
|
|
|
|
the temporary file it created when the file is closed. This
|
|
|
|
behaviour can now be changed by passing ``delete=False`` to the
|
2008-04-10 18:29:01 -03:00
|
|
|
constructor. (Contributed by Damien Miller; :issue:`1537850`.)
|
2007-09-17 22:36:16 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
A new class, :class:`SpooledTemporaryFile`, behaves like
|
|
|
|
a temporary file but stores its data in memory until a maximum size is
|
|
|
|
exceeded. On reaching that limit, the contents will be written to
|
2008-03-03 21:50:32 -04:00
|
|
|
an on-disk temporary file. (Contributed by Dustin J. Mitchell.)
|
|
|
|
|
|
|
|
The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes
|
2008-05-24 15:31:28 -03:00
|
|
|
both work as context managers, so you can write
|
2008-03-03 21:50:32 -04:00
|
|
|
``with tempfile.NamedTemporaryFile() as tmp: ...``.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Alexander Belopolsky; :issue:`2021`.)
|
2008-03-03 21:50:32 -04:00
|
|
|
|
2009-01-03 16:55:06 -04:00
|
|
|
* The :mod:`test.test_support` module gained a number
|
|
|
|
of context managers useful for writing tests.
|
|
|
|
:func:`EnvironmentVarGuard` is a
|
2008-08-31 11:29:31 -03:00
|
|
|
context manager that temporarily changes environment variables and
|
2008-05-24 15:31:28 -03:00
|
|
|
automatically restores them to their old values.
|
2007-09-17 22:36:16 -03:00
|
|
|
|
|
|
|
Another context manager, :class:`TransientResource`, can surround calls
|
|
|
|
to resources that may or may not be available; it will catch and
|
|
|
|
ignore a specified list of exceptions. For example,
|
|
|
|
a network test may ignore certain failures when connecting to an
|
|
|
|
external web site::
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
with test_support.TransientResource(IOError,
|
|
|
|
errno=errno.ETIMEDOUT):
|
2008-05-24 15:31:28 -03:00
|
|
|
f = urllib.urlopen('https://sf.net')
|
2007-09-17 22:36:16 -03:00
|
|
|
...
|
|
|
|
|
2009-01-03 16:55:06 -04:00
|
|
|
Finally, :func:`check_warnings` resets the :mod:`warning` module's
|
2008-09-27 11:12:33 -03:00
|
|
|
warning filters and returns an object that will record all warning
|
|
|
|
messages triggered (:issue:`3781`)::
|
|
|
|
|
|
|
|
with test_support.check_warnings() as wrec:
|
|
|
|
warnings.simplefilter("always")
|
2008-10-08 14:30:55 -03:00
|
|
|
# ... code that triggers a warning ...
|
2008-09-27 11:12:33 -03:00
|
|
|
assert str(wrec.message) == "function is outdated"
|
2008-10-08 14:30:55 -03:00
|
|
|
assert len(wrec.warnings) == 1, "Multiple warnings raised"
|
2008-09-27 11:12:33 -03:00
|
|
|
|
2007-09-17 22:36:16 -03:00
|
|
|
(Contributed by Brett Cannon.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`textwrap` module can now preserve existing whitespace
|
2007-09-13 19:49:34 -03:00
|
|
|
at the beginnings and ends of the newly-created lines
|
|
|
|
by specifying ``drop_whitespace=False``
|
|
|
|
as an argument::
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
>>> S = """This sentence has a bunch of
|
|
|
|
... extra whitespace."""
|
2007-09-13 19:49:34 -03:00
|
|
|
>>> print textwrap.fill(S, width=15)
|
|
|
|
This sentence
|
|
|
|
has a bunch
|
|
|
|
of extra
|
|
|
|
whitespace.
|
|
|
|
>>> print textwrap.fill(S, drop_whitespace=False, width=15)
|
|
|
|
This sentence
|
|
|
|
has a bunch
|
|
|
|
of extra
|
|
|
|
whitespace.
|
2008-05-24 15:31:28 -03:00
|
|
|
>>>
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Dwayne Bailey; :issue:`1581073`.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-09-04 10:26:24 -03:00
|
|
|
* The :mod:`threading` module API is being changed to use properties
|
|
|
|
such as :attr:`daemon` instead of :meth:`setDaemon` and
|
|
|
|
:meth:`isDaemon` methods, and some methods have been renamed to use
|
|
|
|
underscores instead of camel-case; for example, the
|
|
|
|
:meth:`activeCount` method is renamed to :meth:`active_count`. Both
|
|
|
|
the 2.6 and 3.0 versions of the module support the same properties
|
|
|
|
and renamed methods, but don't remove the old methods. No date has been set
|
2009-01-03 16:55:06 -04:00
|
|
|
for the deprecation of the old APIs in Python 3.x; the old APIs won't
|
2008-09-04 10:26:24 -03:00
|
|
|
be removed in any 2.x version.
|
2008-09-03 18:48:20 -03:00
|
|
|
(Carried out by several people, most notably Benjamin Peterson.)
|
2008-08-26 21:27:18 -03:00
|
|
|
|
|
|
|
The :mod:`threading` module's :class:`Thread` objects
|
|
|
|
gained an :attr:`ident` property that returns the thread's
|
|
|
|
identifier, a nonzero integer. (Contributed by Gregory P. Smith;
|
2008-06-19 23:05:57 -03:00
|
|
|
:issue:`2871`.)
|
2008-06-05 20:35:31 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`timeit` module now accepts callables as well as strings
|
2007-09-01 17:43:36 -03:00
|
|
|
for the statement being timed and for the setup code.
|
2008-05-24 15:31:28 -03:00
|
|
|
Two convenience functions were added for creating
|
|
|
|
:class:`Timer` instances:
|
|
|
|
``repeat(stmt, setup, time, repeat, number)`` and
|
2007-09-01 17:43:36 -03:00
|
|
|
``timeit(stmt, setup, time, number)`` create an instance and call
|
2008-04-10 18:29:01 -03:00
|
|
|
the corresponding method. (Contributed by Erik Demaine;
|
|
|
|
:issue:`1533909`.)
|
2007-09-01 17:43:36 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
* The :mod:`Tkinter` module now accepts lists and tuples for options,
|
2008-08-26 21:27:18 -03:00
|
|
|
separating the elements by spaces before passing the resulting value to
|
2008-06-20 08:39:54 -03:00
|
|
|
Tcl/Tk.
|
2008-06-20 20:43:12 -03:00
|
|
|
(Contributed by Guilherme Polo; :issue:`2906`.)
|
2008-06-20 08:39:54 -03:00
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
* The :mod:`turtle` module for turtle graphics was greatly enhanced by
|
|
|
|
Gregor Lingl. New features in the module include:
|
|
|
|
|
|
|
|
* Better animation of turtle movement and rotation.
|
2008-08-31 11:29:31 -03:00
|
|
|
* Control over turtle movement using the new :meth:`delay`,
|
|
|
|
:meth:`tracer`, and :meth:`speed` methods.
|
2008-08-26 21:27:18 -03:00
|
|
|
* The ability to set new shapes for the turtle, and to
|
2008-06-05 20:35:31 -03:00
|
|
|
define a new coordinate system.
|
2008-08-31 11:29:31 -03:00
|
|
|
* Turtles now have an :meth:`undo()` method that can roll back actions.
|
2008-06-05 20:35:31 -03:00
|
|
|
* Simple support for reacting to input events such as mouse and keyboard
|
|
|
|
activity, making it possible to write simple games.
|
2008-08-26 21:27:18 -03:00
|
|
|
* A :file:`turtle.cfg` file can be used to customize the starting appearance
|
2008-06-05 20:35:31 -03:00
|
|
|
of the turtle's screen.
|
|
|
|
* The module's docstrings can be replaced by new docstrings that have been
|
|
|
|
translated into another language.
|
2008-08-26 21:27:18 -03:00
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
(:issue:`1513695`)
|
|
|
|
|
2007-09-13 19:49:34 -03:00
|
|
|
* An optional ``timeout`` parameter was added to the
|
|
|
|
:func:`urllib.urlopen` function and the
|
2008-05-24 15:31:28 -03:00
|
|
|
:class:`urllib.ftpwrapper` class constructor, as well as the
|
2007-09-13 19:49:34 -03:00
|
|
|
:func:`urllib2.urlopen` function. The parameter specifies a timeout
|
|
|
|
measured in seconds. For example::
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
>>> u = urllib2.urlopen("http://slow.example.com",
|
|
|
|
timeout=3)
|
2007-09-13 19:49:34 -03:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
urllib2.URLError: <urlopen error timed out>
|
2008-05-24 15:31:28 -03:00
|
|
|
>>>
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
(Added by Facundo Batista.)
|
2007-09-13 19:49:34 -03:00
|
|
|
|
2008-09-27 11:12:33 -03:00
|
|
|
* The Unicode database provided by the :mod:`unicodedata` module
|
2009-01-03 16:55:06 -04:00
|
|
|
has been updated to version 5.1.0. (Updated by
|
2008-09-27 11:12:33 -03:00
|
|
|
Martin von Loewis; :issue:`3811`.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`warnings` module's :func:`formatwarning` and :func:`showwarning`
|
2008-05-09 08:46:05 -03:00
|
|
|
gained an optional *line* argument that can be used to supply the
|
|
|
|
line of source code. (Added as part of :issue:`1631171`, which re-implemented
|
|
|
|
part of the :mod:`warnings` module in C code.)
|
|
|
|
|
2008-09-27 11:12:33 -03:00
|
|
|
A new function, :func:`catch_warnings`, is a context manager
|
|
|
|
intended for testing purposes that lets you temporarily modify the
|
|
|
|
warning filters and then restore their original values (:issue:`3781`).
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2008-05-09 08:46:05 -03:00
|
|
|
* The XML-RPC :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
|
2007-10-20 16:35:18 -03:00
|
|
|
classes can now be prevented from immediately opening and binding to
|
2007-09-24 21:09:42 -03:00
|
|
|
their socket by passing True as the ``bind_and_activate``
|
|
|
|
constructor parameter. This can be used to modify the instance's
|
2008-05-24 15:31:28 -03:00
|
|
|
:attr:`allow_reuse_address` attribute before calling the
|
|
|
|
:meth:`server_bind` and :meth:`server_activate` methods to
|
2007-09-24 21:09:42 -03:00
|
|
|
open the socket and begin listening for connections.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Peter Parente; :issue:`1599845`.)
|
2007-09-24 21:09:42 -03:00
|
|
|
|
2007-10-20 16:35:18 -03:00
|
|
|
:class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header`
|
2008-05-24 15:31:28 -03:00
|
|
|
attribute; if true, the exception and formatted traceback are returned
|
|
|
|
as HTTP headers "X-Exception" and "X-Traceback". This feature is
|
2007-10-20 16:35:18 -03:00
|
|
|
for debugging purposes only and should not be used on production servers
|
2008-08-31 11:29:31 -03:00
|
|
|
because the tracebacks might reveal passwords or other sensitive
|
2008-05-24 15:31:28 -03:00
|
|
|
information. (Contributed by Alan McIntyre as part of his
|
2007-10-20 16:35:18 -03:00
|
|
|
project for Google's Summer of Code 2007.)
|
|
|
|
|
2008-04-20 23:14:24 -03:00
|
|
|
* The :mod:`xmlrpclib` module no longer automatically converts
|
2008-05-24 15:31:28 -03:00
|
|
|
:class:`datetime.date` and :class:`datetime.time` to the
|
2008-04-20 23:14:24 -03:00
|
|
|
:class:`xmlrpclib.DateTime` type; the conversion semantics were
|
|
|
|
not necessarily correct for all applications. Code using
|
2008-05-24 15:31:28 -03:00
|
|
|
:mod:`xmlrpclib` should convert :class:`date` and :class:`time`
|
|
|
|
instances. (:issue:`1330538`) The code can also handle
|
2008-06-05 20:35:31 -03:00
|
|
|
dates before 1900 (contributed by Ralf Schmitt; :issue:`2014`)
|
|
|
|
and 64-bit integers represented by using ``<i8>`` in XML-RPC responses
|
2008-06-19 23:05:57 -03:00
|
|
|
(contributed by Riku Lindblad; :issue:`2985`).
|
2008-08-26 21:27:18 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`zipfile` module's :class:`ZipFile` class now has
|
|
|
|
:meth:`extract` and :meth:`extractall` methods that will unpack
|
|
|
|
a single file or all the files in the archive to the current directory, or
|
2008-01-14 21:47:32 -04:00
|
|
|
to a specified directory::
|
|
|
|
|
|
|
|
z = zipfile.ZipFile('python-251.zip')
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
# Unpack a single file, writing it relative
|
|
|
|
# to the /tmp directory.
|
2008-01-14 21:47:32 -04:00
|
|
|
z.extract('Python/sysmodule.c', '/tmp')
|
|
|
|
|
|
|
|
# Unpack all the files in the archive.
|
|
|
|
z.extractall()
|
|
|
|
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Alan McIntyre; :issue:`467924`.)
|
2008-01-14 21:47:32 -04:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
The :meth:`open`, :meth:`read` and :meth:`extract` methods can now
|
2008-06-05 20:35:31 -03:00
|
|
|
take either a filename or a :class:`ZipInfo` object. This is useful when an
|
|
|
|
archive accidentally contains a duplicated filename.
|
|
|
|
(Contributed by Graham Horler; :issue:`1775025`.)
|
2008-05-09 08:46:05 -03:00
|
|
|
|
2008-06-05 20:35:31 -03:00
|
|
|
Finally, :mod:`zipfile` now supports using Unicode filenames
|
|
|
|
for archived files. (Contributed by Alexey Borzenkov; :issue:`1734346`.)
|
2008-08-26 21:27:18 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
|
|
|
.. whole new modules get described in subsections here
|
2007-10-20 16:35:18 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
The :mod:`ast` module
|
|
|
|
----------------------
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
The :mod:`ast` module provides an Abstract Syntax Tree
|
|
|
|
representation of Python code, and Armin Ronacher
|
|
|
|
contributed a set of helper functions that perform a variety of
|
|
|
|
common tasks. These will be useful for HTML templating
|
|
|
|
packages, code analyzers, and similar tools that process
|
|
|
|
Python code.
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
The :func:`parse` function takes an expression and returns an AST.
|
|
|
|
The :func:`dump` function outputs a representation of a tree, suitable
|
|
|
|
for debugging::
|
|
|
|
|
|
|
|
import ast
|
|
|
|
|
|
|
|
t = ast.parse("""
|
|
|
|
d = {}
|
|
|
|
for i in 'abcdefghijklm':
|
2008-10-08 14:30:55 -03:00
|
|
|
d[i + i] = ord(i) - ord('a') + 1
|
2008-06-20 08:39:54 -03:00
|
|
|
print d
|
|
|
|
""")
|
|
|
|
print ast.dump(t)
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
This outputs a deeply nested tree::
|
|
|
|
|
|
|
|
Module(body=[
|
|
|
|
Assign(targets=[
|
2008-10-08 14:30:55 -03:00
|
|
|
Name(id='d', ctx=Store())
|
2008-08-31 12:41:48 -03:00
|
|
|
], value=Dict(keys=[], values=[]))
|
|
|
|
For(target=Name(id='i', ctx=Store()),
|
2008-10-08 14:30:55 -03:00
|
|
|
iter=Str(s='abcdefghijklm'), body=[
|
|
|
|
Assign(targets=[
|
|
|
|
Subscript(value=
|
|
|
|
Name(id='d', ctx=Load()),
|
|
|
|
slice=
|
|
|
|
Index(value=
|
|
|
|
BinOp(left=Name(id='i', ctx=Load()), op=Add(),
|
|
|
|
right=Name(id='i', ctx=Load()))), ctx=Store())
|
|
|
|
], value=
|
|
|
|
BinOp(left=
|
|
|
|
BinOp(left=
|
|
|
|
Call(func=
|
|
|
|
Name(id='ord', ctx=Load()), args=[
|
|
|
|
Name(id='i', ctx=Load())
|
|
|
|
], keywords=[], starargs=None, kwargs=None),
|
|
|
|
op=Sub(), right=Call(func=
|
|
|
|
Name(id='ord', ctx=Load()), args=[
|
|
|
|
Str(s='a')
|
|
|
|
], keywords=[], starargs=None, kwargs=None)),
|
|
|
|
op=Add(), right=Num(n=1)))
|
|
|
|
], orelse=[])
|
|
|
|
Print(dest=None, values=[
|
|
|
|
Name(id='d', ctx=Load())
|
2008-08-31 12:41:48 -03:00
|
|
|
], nl=True)
|
|
|
|
])
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
The :func:`literal_eval` method takes a string or an AST
|
2008-08-31 12:41:48 -03:00
|
|
|
representing a literal expression, parses and evaluates it, and
|
|
|
|
returns the resulting value. A literal expression is a Python
|
|
|
|
expression containing only strings, numbers, dictionaries,
|
|
|
|
etc. but no statements or function calls. If you need to
|
2008-10-04 00:08:56 -03:00
|
|
|
evaluate an expression but cannot accept the security risk of using an
|
2008-08-31 12:41:48 -03:00
|
|
|
:func:`eval` call, :func:`literal_eval` will handle it safely::
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
>>> literal = '("a", "b", {2:4, 3:8, 1:2})'
|
|
|
|
>>> print ast.literal_eval(literal)
|
|
|
|
('a', 'b', {1: 2, 2: 4, 3: 8})
|
|
|
|
>>> print ast.literal_eval('"a" + "b"')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: malformed string
|
|
|
|
|
2008-07-06 14:44:17 -03:00
|
|
|
The module also includes :class:`NodeVisitor` and
|
|
|
|
:class:`NodeTransformer` classes for traversing and modifying an AST,
|
|
|
|
and functions for common transformations such as changing line
|
|
|
|
numbers.
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
|
|
|
The :mod:`future_builtins` module
|
|
|
|
--------------------------------------
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
Python 3.0 makes many changes to the repertoire of built-in
|
2008-06-20 08:39:54 -03:00
|
|
|
functions, and most of the changes can't be introduced in the Python
|
|
|
|
2.x series because they would break compatibility.
|
2008-08-26 21:27:18 -03:00
|
|
|
The :mod:`future_builtins` module provides versions
|
|
|
|
of these built-in functions that can be imported when writing
|
2008-06-20 08:39:54 -03:00
|
|
|
3.0-compatible code.
|
|
|
|
|
|
|
|
The functions in this module currently include:
|
|
|
|
|
2008-09-04 10:26:24 -03:00
|
|
|
* ``ascii(obj)``: equivalent to :func:`repr`. In Python 3.0,
|
2008-08-26 21:27:18 -03:00
|
|
|
:func:`repr` will return a Unicode string, while :func:`ascii` will
|
2008-06-20 08:39:54 -03:00
|
|
|
return a pure ASCII bytestring.
|
|
|
|
|
2008-09-04 10:26:24 -03:00
|
|
|
* ``filter(predicate, iterable)``,
|
|
|
|
``map(func, iterable1, ...)``: the 3.0 versions
|
2010-03-07 11:23:59 -04:00
|
|
|
return iterators, unlike the 2.x builtins which return lists.
|
2008-06-20 08:39:54 -03:00
|
|
|
|
2008-09-04 10:26:24 -03:00
|
|
|
* ``hex(value)``, ``oct(value)``: instead of calling the
|
2008-08-26 21:27:18 -03:00
|
|
|
:meth:`__hex__` or :meth:`__oct__` methods, these versions will
|
2008-06-20 08:39:54 -03:00
|
|
|
call the :meth:`__index__` method and convert the result to hexadecimal
|
2008-08-31 12:41:48 -03:00
|
|
|
or octal. :func:`oct` will use the new ``0o`` notation for its
|
|
|
|
result.
|
2008-06-20 08:39:54 -03:00
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
The :mod:`json` module: JavaScript Object Notation
|
|
|
|
--------------------------------------------------------------------
|
2008-05-05 17:21:38 -03:00
|
|
|
|
|
|
|
The new :mod:`json` module supports the encoding and decoding of Python types in
|
|
|
|
JSON (Javascript Object Notation). JSON is a lightweight interchange format
|
|
|
|
often used in web applications. For more information about JSON, see
|
|
|
|
http://www.json.org.
|
|
|
|
|
2009-07-26 11:37:28 -03:00
|
|
|
:mod:`json` comes with support for decoding and encoding most built-in Python
|
2008-05-05 17:21:38 -03:00
|
|
|
types. The following example encodes and decodes a dictionary::
|
|
|
|
|
|
|
|
>>> import json
|
|
|
|
>>> data = {"spam" : "foo", "parrot" : 42}
|
|
|
|
>>> in_json = json.dumps(data) # Encode the data
|
|
|
|
>>> in_json
|
|
|
|
'{"parrot": 42, "spam": "foo"}'
|
|
|
|
>>> json.loads(in_json) # Decode into a Python object
|
|
|
|
{"spam" : "foo", "parrot" : 42}
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
It's also possible to write your own decoders and encoders to support
|
|
|
|
more types. Pretty-printing of the JSON strings is also supported.
|
2008-05-05 17:21:38 -03:00
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
:mod:`json` (originally called simplejson) was written by Bob
|
|
|
|
Ippolito.
|
2008-05-05 17:21:38 -03:00
|
|
|
|
|
|
|
|
2008-01-26 09:50:51 -04:00
|
|
|
.. ======================================================================
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
The :mod:`plistlib` module: A Property-List Parser
|
2008-01-26 09:50:51 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
The ``.plist`` format is commonly used on Mac OS X to
|
2008-08-31 12:41:48 -03:00
|
|
|
store basic data types (numbers, strings, lists,
|
|
|
|
and dictionaries) by serializing them into an XML-based format.
|
|
|
|
It resembles the XML-RPC serialization of data types.
|
2008-01-26 09:50:51 -04:00
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
Despite being primarily used on Mac OS X, the format
|
2008-01-26 09:50:51 -04:00
|
|
|
has nothing Mac-specific about it and the Python implementation works
|
|
|
|
on any platform that Python supports, so the :mod:`plistlib` module
|
|
|
|
has been promoted to the standard library.
|
|
|
|
|
|
|
|
Using the module is simple::
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import plistlib
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
# Create data structure
|
|
|
|
data_struct = dict(lastAccessed=datetime.datetime.now(),
|
2008-10-08 14:30:55 -03:00
|
|
|
version=1,
|
|
|
|
categories=('Personal','Shared','Private'))
|
2008-01-26 09:50:51 -04:00
|
|
|
|
|
|
|
# Create string containing XML.
|
|
|
|
plist_str = plistlib.writePlistToString(data_struct)
|
|
|
|
new_struct = plistlib.readPlistFromString(plist_str)
|
|
|
|
print data_struct
|
|
|
|
print new_struct
|
|
|
|
|
|
|
|
# Write data structure to a file and read it back.
|
|
|
|
plistlib.writePlist(data_struct, '/tmp/customizations.plist')
|
|
|
|
new_struct = plistlib.readPlist('/tmp/customizations.plist')
|
|
|
|
|
|
|
|
# read/writePlist accepts file-like objects as well as paths.
|
|
|
|
plistlib.writePlist(data_struct, sys.stdout)
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
.. ======================================================================
|
2008-01-26 09:50:51 -04:00
|
|
|
|
2008-07-13 18:43:52 -03:00
|
|
|
ctypes Enhancements
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
Thomas Heller continued to maintain and enhance the
|
|
|
|
:mod:`ctypes` module.
|
2008-07-13 18:43:52 -03:00
|
|
|
|
|
|
|
:mod:`ctypes` now supports a :class:`c_bool` datatype
|
|
|
|
that represents the C99 ``bool`` type. (Contributed by David Remahl;
|
|
|
|
:issue:`1649190`.)
|
|
|
|
|
|
|
|
The :mod:`ctypes` string, buffer and array types have improved
|
|
|
|
support for extended slicing syntax,
|
|
|
|
where various combinations of ``(start, stop, step)`` are supplied.
|
|
|
|
(Implemented by Thomas Wouters.)
|
|
|
|
|
|
|
|
.. Revision 57769
|
|
|
|
|
2008-08-26 23:12:18 -03:00
|
|
|
All :mod:`ctypes` data types now support
|
|
|
|
:meth:`from_buffer` and :meth:`from_buffer_copy`
|
|
|
|
methods that create a ctypes instance based on a
|
|
|
|
provided buffer object. :meth:`from_buffer_copy` copies
|
|
|
|
the contents of the object,
|
|
|
|
while :meth:`from_buffer` will share the same memory area.
|
|
|
|
|
2008-07-13 18:43:52 -03:00
|
|
|
A new calling convention tells :mod:`ctypes` to clear the ``errno`` or
|
|
|
|
Win32 LastError variables at the outset of each wrapped call.
|
|
|
|
(Implemented by Thomas Heller; :issue:`1798`.)
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
You can now retrieve the Unix ``errno`` variable after a function
|
|
|
|
call. When creating a wrapped function, you can supply
|
|
|
|
``use_errno=True`` as a keyword parameter to the :func:`DLL` function
|
|
|
|
and then call the module-level methods :meth:`set_errno` and
|
|
|
|
:meth:`get_errno` to set and retrieve the error value.
|
2008-07-13 18:43:52 -03:00
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
The Win32 LastError variable is similarly supported by
|
2008-07-13 18:43:52 -03:00
|
|
|
the :func:`DLL`, :func:`OleDLL`, and :func:`WinDLL` functions.
|
|
|
|
You supply ``use_last_error=True`` as a keyword parameter
|
|
|
|
and then call the module-level methods :meth:`set_last_error`
|
2008-08-26 21:27:18 -03:00
|
|
|
and :meth:`get_last_error`.
|
2008-07-13 18:43:52 -03:00
|
|
|
|
|
|
|
The :func:`byref` function, used to retrieve a pointer to a ctypes
|
2008-08-30 19:56:54 -03:00
|
|
|
instance, now has an optional *offset* parameter that is a byte
|
2008-07-13 18:43:52 -03:00
|
|
|
count that will be added to the returned pointer.
|
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
Improved SSL Support
|
|
|
|
--------------------------------------------------
|
2008-06-19 23:05:57 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
Bill Janssen made extensive improvements to Python 2.6's support for
|
2008-08-31 12:41:48 -03:00
|
|
|
the Secure Sockets Layer by adding a new module, :mod:`ssl`, that's
|
|
|
|
built atop the `OpenSSL <http://www.openssl.org/>`__ library.
|
|
|
|
This new module provides more control over the protocol negotiated,
|
|
|
|
the X.509 certificates used, and has better support for writing SSL
|
|
|
|
servers (as opposed to clients) in Python. The existing SSL support
|
|
|
|
in the :mod:`socket` module hasn't been removed and continues to work,
|
2008-06-20 08:39:54 -03:00
|
|
|
though it will be removed in Python 3.0.
|
2008-06-19 23:05:57 -03:00
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
To use the new module, you must first create a TCP connection in the
|
2008-06-20 08:39:54 -03:00
|
|
|
usual way and then pass it to the :func:`ssl.wrap_socket` function.
|
|
|
|
It's possible to specify whether a certificate is required, and to
|
|
|
|
obtain certificate info by calling the :meth:`getpeercert` method.
|
2008-06-19 23:05:57 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
.. seealso::
|
2008-06-19 23:05:57 -03:00
|
|
|
|
2008-06-20 08:39:54 -03:00
|
|
|
The documentation for the :mod:`ssl` module.
|
2008-06-19 23:05:57 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-11-18 14:52:23 -04:00
|
|
|
Deprecations and Removals
|
|
|
|
=========================
|
|
|
|
|
|
|
|
* String exceptions have been removed. Attempting to use them raises a
|
|
|
|
:exc:`TypeError`.
|
|
|
|
|
|
|
|
* Changes to the :class:`Exception` interface
|
|
|
|
as dictated by :pep:`352` continue to be made. For 2.6,
|
|
|
|
the :attr:`message` attribute is being deprecated in favor of the
|
|
|
|
:attr:`args` attribute.
|
|
|
|
|
|
|
|
* (3.0-warning mode) Python 3.0 will feature a reorganized standard
|
|
|
|
library that will drop many outdated modules and rename others.
|
|
|
|
Python 2.6 running in 3.0-warning mode will warn about these modules
|
|
|
|
when they are imported.
|
|
|
|
|
|
|
|
The list of deprecated modules is:
|
|
|
|
:mod:`audiodev`,
|
|
|
|
:mod:`bgenlocations`,
|
|
|
|
:mod:`buildtools`,
|
|
|
|
:mod:`bundlebuilder`,
|
|
|
|
:mod:`Canvas`,
|
|
|
|
:mod:`compiler`,
|
|
|
|
:mod:`dircache`,
|
|
|
|
:mod:`dl`,
|
|
|
|
:mod:`fpformat`,
|
|
|
|
:mod:`gensuitemodule`,
|
|
|
|
:mod:`ihooks`,
|
|
|
|
:mod:`imageop`,
|
|
|
|
:mod:`imgfile`,
|
|
|
|
:mod:`linuxaudiodev`,
|
|
|
|
:mod:`mhlib`,
|
|
|
|
:mod:`mimetools`,
|
|
|
|
:mod:`multifile`,
|
|
|
|
:mod:`new`,
|
|
|
|
:mod:`pure`,
|
|
|
|
:mod:`statvfs`,
|
|
|
|
:mod:`sunaudiodev`,
|
|
|
|
:mod:`test.testall`, and
|
|
|
|
:mod:`toaiff`.
|
|
|
|
|
|
|
|
* The :mod:`gopherlib` module has been removed.
|
|
|
|
|
|
|
|
* The :mod:`MimeWriter` module and :mod:`mimify` module
|
|
|
|
have been deprecated; use the :mod:`email`
|
|
|
|
package instead.
|
|
|
|
|
|
|
|
* The :mod:`md5` module has been deprecated; use the :mod:`hashlib` module
|
|
|
|
instead.
|
|
|
|
|
|
|
|
* The :mod:`posixfile` module has been deprecated; :func:`fcntl.lockf`
|
|
|
|
provides better locking.
|
|
|
|
|
|
|
|
* The :mod:`popen2` module has been deprecated; use the :mod:`subprocess`
|
|
|
|
module.
|
|
|
|
|
|
|
|
* The :mod:`rgbimg` module has been removed.
|
|
|
|
|
|
|
|
* The :mod:`sets` module has been deprecated; it's better to
|
|
|
|
use the built-in :class:`set` and :class:`frozenset` types.
|
|
|
|
|
|
|
|
* The :mod:`sha` module has been deprecated; use the :mod:`hashlib` module
|
|
|
|
instead.
|
|
|
|
|
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Build and C API Changes
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Changes to Python's build process and to the C API include:
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
* Python now must be compiled with C89 compilers (after 19
|
|
|
|
years!). This means that the Python source tree has dropped its
|
2012-01-14 11:42:02 -04:00
|
|
|
own implementations of :c:func:`memmove` and :c:func:`strerror`, which
|
2008-08-31 12:41:48 -03:00
|
|
|
are in the C89 standard library.
|
|
|
|
|
|
|
|
* Python 2.6 can be built with Microsoft Visual Studio 2008 (version
|
|
|
|
9.0), and this is the new default compiler. See the
|
|
|
|
:file:`PCbuild` directory for the build files. (Implemented by
|
|
|
|
Christian Heimes.)
|
2007-11-23 09:37:39 -04:00
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
* On Mac OS X, Python 2.6 can be compiled as a 4-way universal build.
|
2008-08-26 21:27:18 -03:00
|
|
|
The :program:`configure` script
|
2008-06-19 23:05:57 -03:00
|
|
|
can take a :option:`--with-universal-archs=[32-bit|64-bit|all]`
|
|
|
|
switch, controlling whether the binaries are built for 32-bit
|
|
|
|
architectures (x86, PowerPC), 64-bit (x86-64 and PPC-64), or both.
|
|
|
|
(Contributed by Ronald Oussoren.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The BerkeleyDB module now has a C API object, available as
|
2007-10-16 19:58:03 -03:00
|
|
|
``bsddb.db.api``. This object can be used by other C extensions
|
|
|
|
that wish to use the :mod:`bsddb` module for their own purposes.
|
2009-10-11 17:16:16 -03:00
|
|
|
(Contributed by Duncan Grisby.)
|
2007-10-16 19:58:03 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The new buffer interface, previously described in
|
2008-04-10 18:29:01 -03:00
|
|
|
`the PEP 3118 section <#pep-3118-revised-buffer-protocol>`__,
|
2012-01-14 11:42:02 -04:00
|
|
|
adds :c:func:`PyObject_GetBuffer` and :c:func:`PyBuffer_Release`,
|
2008-04-10 18:29:01 -03:00
|
|
|
as well as a few other functions.
|
2007-10-16 19:58:03 -03:00
|
|
|
|
2008-04-07 20:57:07 -03:00
|
|
|
* Python's use of the C stdio library is now thread-safe, or at least
|
|
|
|
as thread-safe as the underlying library is. A long-standing potential
|
2008-05-24 15:31:28 -03:00
|
|
|
bug occurred if one thread closed a file object while another thread
|
|
|
|
was reading from or writing to the object. In 2.6 file objects
|
|
|
|
have a reference count, manipulated by the
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyFile_IncUseCount` and :c:func:`PyFile_DecUseCount`
|
2008-05-24 15:31:28 -03:00
|
|
|
functions. File objects can't be closed unless the reference count
|
2012-01-14 11:42:02 -04:00
|
|
|
is zero. :c:func:`PyFile_IncUseCount` should be called while the GIL
|
2008-05-24 15:31:28 -03:00
|
|
|
is still held, before carrying out an I/O operation using the
|
2012-01-14 11:42:02 -04:00
|
|
|
``FILE *`` pointer, and :c:func:`PyFile_DecUseCount` should be called
|
2008-04-07 20:57:07 -03:00
|
|
|
immediately after the GIL is re-acquired.
|
|
|
|
(Contributed by Antoine Pitrou and Gregory P. Smith.)
|
|
|
|
|
2008-04-10 18:29:01 -03:00
|
|
|
* Importing modules simultaneously in two different threads no longer
|
|
|
|
deadlocks; it will now raise an :exc:`ImportError`. A new API
|
2012-01-14 11:42:02 -04:00
|
|
|
function, :c:func:`PyImport_ImportModuleNoBlock`, will look for a
|
2008-04-10 18:29:01 -03:00
|
|
|
module in ``sys.modules`` first, then try to import it after
|
|
|
|
acquiring an import lock. If the import lock is held by another
|
2008-08-31 12:41:48 -03:00
|
|
|
thread, an :exc:`ImportError` is raised.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Christian Heimes.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Several functions return information about the platform's
|
2012-01-14 11:42:02 -04:00
|
|
|
floating-point support. :c:func:`PyFloat_GetMax` returns
|
2007-12-18 22:02:04 -04:00
|
|
|
the maximum representable floating point value,
|
2012-01-14 11:42:02 -04:00
|
|
|
and :c:func:`PyFloat_GetMin` returns the minimum
|
|
|
|
positive value. :c:func:`PyFloat_GetInfo` returns an object
|
2007-12-18 22:02:04 -04:00
|
|
|
containing more information from the :file:`float.h` file, such as
|
|
|
|
``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"``
|
|
|
|
(smallest difference between 1.0 and the next largest value
|
|
|
|
representable), and several others.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Christian Heimes; :issue:`1534`.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-06-19 23:05:57 -03:00
|
|
|
* C functions and methods that use
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyComplex_AsCComplex` will now accept arguments that
|
2008-06-19 23:05:57 -03:00
|
|
|
have a :meth:`__complex__` method. In particular, the functions in the
|
|
|
|
:mod:`cmath` module will now accept objects with this method.
|
|
|
|
This is a backport of a Python 3.0 change.
|
|
|
|
(Contributed by Mark Dickinson; :issue:`1675423`.)
|
|
|
|
|
2008-01-03 21:16:12 -04:00
|
|
|
* Python's C API now includes two functions for case-insensitive string
|
2008-02-22 08:31:45 -04:00
|
|
|
comparisons, ``PyOS_stricmp(char*, char*)``
|
2008-01-03 21:16:12 -04:00
|
|
|
and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Christian Heimes; :issue:`1635`.)
|
2008-01-03 21:16:12 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Many C extensions define their own little macro for adding
|
|
|
|
integers and strings to the module's dictionary in the
|
|
|
|
``init*`` function. Python 2.6 finally defines standard macros
|
2012-01-14 11:42:02 -04:00
|
|
|
for adding values to a module, :c:macro:`PyModule_AddStringMacro`
|
|
|
|
and :c:macro:`PyModule_AddIntMacro()`. (Contributed by
|
2008-04-07 20:57:07 -03:00
|
|
|
Christian Heimes.)
|
|
|
|
|
2008-03-04 20:44:41 -04:00
|
|
|
* Some macros were renamed in both 3.0 and 2.6 to make it clearer that
|
|
|
|
they are macros,
|
2012-01-14 11:42:02 -04:00
|
|
|
not functions. :c:macro:`Py_Size()` became :c:macro:`Py_SIZE()`,
|
|
|
|
:c:macro:`Py_Type()` became :c:macro:`Py_TYPE()`, and
|
|
|
|
:c:macro:`Py_Refcnt()` became :c:macro:`Py_REFCNT()`.
|
2008-03-04 20:44:41 -04:00
|
|
|
The mixed-case macros are still available
|
|
|
|
in Python 2.6 for backward compatibility.
|
2008-04-10 18:29:01 -03:00
|
|
|
(:issue:`1629`)
|
2008-01-03 21:16:12 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Distutils now places C extensions it builds in a
|
2008-01-26 09:50:51 -04:00
|
|
|
different directory when running on a debug version of Python.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Contributed by Collin Winter; :issue:`1530959`.)
|
2008-01-26 09:50:51 -04:00
|
|
|
|
2008-03-03 21:50:32 -04:00
|
|
|
* Several basic data types, such as integers and strings, maintain
|
|
|
|
internal free lists of objects that can be re-used. The data
|
|
|
|
structures for these free lists now follow a naming convention: the
|
|
|
|
variable is always named ``free_list``, the counter is always named
|
2008-08-31 12:41:48 -03:00
|
|
|
``numfree``, and a macro ``Py<typename>_MAXFREELIST`` is
|
2008-03-03 21:50:32 -04:00
|
|
|
always defined.
|
2008-01-26 09:50:51 -04:00
|
|
|
|
2008-10-04 00:08:56 -03:00
|
|
|
* A new Makefile target, "make patchcheck", prepares the Python source tree
|
2008-05-24 15:31:28 -03:00
|
|
|
for making a patch: it fixes trailing whitespace in all modified
|
2008-04-08 22:08:32 -03:00
|
|
|
``.py`` files, checks whether the documentation has been changed,
|
|
|
|
and reports whether the :file:`Misc/ACKS` and :file:`Misc/NEWS` files
|
|
|
|
have been updated.
|
|
|
|
(Contributed by Brett Cannon.)
|
|
|
|
|
2008-04-20 23:14:24 -03:00
|
|
|
Another new target, "make profile-opt", compiles a Python binary
|
|
|
|
using GCC's profile-guided optimization. It compiles Python with
|
|
|
|
profiling enabled, runs the test suite to obtain a set of profiling
|
|
|
|
results, and then compiles using these results for optimization.
|
|
|
|
(Contributed by Gregory P. Smith.)
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-01-03 21:16:12 -04:00
|
|
|
Port-Specific Changes: Windows
|
|
|
|
-----------------------------------
|
|
|
|
|
2008-05-04 08:50:53 -03:00
|
|
|
* The support for Windows 95, 98, ME and NT4 has been dropped.
|
|
|
|
Python 2.6 requires at least Windows 2000 SP4.
|
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
* The new default compiler on Windows is Visual Studio 2008 (version
|
|
|
|
9.0). The build directories for Visual Studio 2003 (version 7.1) and
|
|
|
|
2005 (version 8.0) were moved into the PC/ directory. The new
|
|
|
|
:file:`PCbuild` directory supports cross compilation for X64, debug
|
|
|
|
builds and Profile Guided Optimization (PGO). PGO builds are roughly
|
|
|
|
10% faster than normal builds. (Contributed by Christian Heimes
|
|
|
|
with help from Amaury Forgeot d'Arc and Martin von Loewis.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`msvcrt` module now supports
|
2008-01-03 21:16:12 -04:00
|
|
|
both the normal and wide char variants of the console I/O
|
2008-05-24 15:31:28 -03:00
|
|
|
API. The :func:`getwch` function reads a keypress and returns a Unicode
|
2008-01-03 21:16:12 -04:00
|
|
|
value, as does the :func:`getwche` function. The :func:`putwch` function
|
|
|
|
takes a Unicode character and writes it to the console.
|
2008-01-17 19:01:44 -04:00
|
|
|
(Contributed by Christian Heimes.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-08-31 12:41:48 -03:00
|
|
|
* :func:`os.path.expandvars` will now expand environment variables in
|
|
|
|
the form "%var%", and "~user" will be expanded into the user's home
|
|
|
|
directory path. (Contributed by Josiah Carlson; :issue:`957650`.)
|
2008-01-17 08:00:15 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`socket` module's socket objects now have an
|
|
|
|
:meth:`ioctl` method that provides a limited interface to the
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`WSAIoctl` system interface.
|
2008-01-17 08:00:15 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :mod:`_winreg` module now has a function,
|
|
|
|
:func:`ExpandEnvironmentStrings`,
|
2008-01-14 21:47:32 -04:00
|
|
|
that expands environment variable references such as ``%NAME%``
|
|
|
|
in an input string. The handle objects provided by this
|
2008-05-24 15:31:28 -03:00
|
|
|
module now support the context protocol, so they can be used
|
2008-01-17 19:01:44 -04:00
|
|
|
in :keyword:`with` statements. (Contributed by Christian Heimes.)
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
:mod:`_winreg` also has better support for x64 systems,
|
2008-04-07 20:57:07 -03:00
|
|
|
exposing the :func:`DisableReflectionKey`, :func:`EnableReflectionKey`,
|
|
|
|
and :func:`QueryReflectionKey` functions, which enable and disable
|
|
|
|
registry reflection for 32-bit processes running on 64-bit systems.
|
2008-04-10 18:29:01 -03:00
|
|
|
(:issue:`1753245`)
|
2008-04-07 20:57:07 -03:00
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
* The :mod:`msilib` module's :class:`Record` object
|
|
|
|
gained :meth:`GetInteger` and :meth:`GetString` methods that
|
|
|
|
return field values as an integer or a string.
|
2008-06-20 20:43:12 -03:00
|
|
|
(Contributed by Floris Bruynooghe; :issue:`2125`.)
|
2008-06-20 08:39:54 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
Port-Specific Changes: Mac OS X
|
2008-06-05 20:35:31 -03:00
|
|
|
-----------------------------------
|
|
|
|
|
2008-08-26 21:27:18 -03:00
|
|
|
* When compiling a framework build of Python, you can now specify the
|
|
|
|
framework name to be used by providing the
|
|
|
|
:option:`--with-framework-name=` option to the
|
2008-06-05 20:35:31 -03:00
|
|
|
:program:`configure` script.
|
|
|
|
|
2008-08-31 11:29:31 -03:00
|
|
|
* The :mod:`macfs` module has been removed. This in turn required the
|
|
|
|
:func:`macostools.touched` function to be removed because it depended on the
|
|
|
|
:mod:`macfs` module. (:issue:`1490190`)
|
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
* Many other Mac OS modules have been deprecated and will removed in
|
2008-08-31 11:29:31 -03:00
|
|
|
Python 3.0:
|
|
|
|
:mod:`_builtinSuites`,
|
|
|
|
:mod:`aepack`,
|
|
|
|
:mod:`aetools`,
|
|
|
|
:mod:`aetypes`,
|
|
|
|
:mod:`applesingle`,
|
|
|
|
:mod:`appletrawmain`,
|
|
|
|
:mod:`appletrunner`,
|
|
|
|
:mod:`argvemulator`,
|
|
|
|
:mod:`Audio_mac`,
|
|
|
|
:mod:`autoGIL`,
|
|
|
|
:mod:`Carbon`,
|
|
|
|
:mod:`cfmfile`,
|
|
|
|
:mod:`CodeWarrior`,
|
|
|
|
:mod:`ColorPicker`,
|
|
|
|
:mod:`EasyDialogs`,
|
|
|
|
:mod:`Explorer`,
|
|
|
|
:mod:`Finder`,
|
|
|
|
:mod:`FrameWork`,
|
|
|
|
:mod:`findertools`,
|
|
|
|
:mod:`ic`,
|
|
|
|
:mod:`icglue`,
|
|
|
|
:mod:`icopen`,
|
|
|
|
:mod:`macerrors`,
|
|
|
|
:mod:`MacOS`,
|
|
|
|
:mod:`macfs`,
|
|
|
|
:mod:`macostools`,
|
|
|
|
:mod:`macresource`,
|
|
|
|
:mod:`MiniAEFrame`,
|
|
|
|
:mod:`Nav`,
|
|
|
|
:mod:`Netscape`,
|
|
|
|
:mod:`OSATerminology`,
|
|
|
|
:mod:`pimp`,
|
|
|
|
:mod:`PixMapWrapper`,
|
|
|
|
:mod:`StdSuites`,
|
|
|
|
:mod:`SystemEvents`,
|
|
|
|
:mod:`Terminal`, and
|
|
|
|
:mod:`terminalcommand`.
|
|
|
|
|
|
|
|
.. ======================================================================
|
|
|
|
|
|
|
|
Port-Specific Changes: IRIX
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
A number of old IRIX-specific modules were deprecated and will
|
|
|
|
be removed in Python 3.0:
|
|
|
|
:mod:`al` and :mod:`AL`,
|
|
|
|
:mod:`cd`,
|
|
|
|
:mod:`cddb`,
|
|
|
|
:mod:`cdplayer`,
|
|
|
|
:mod:`CL` and :mod:`cl`,
|
|
|
|
:mod:`DEVICE`,
|
|
|
|
:mod:`ERRNO`,
|
|
|
|
:mod:`FILE`,
|
|
|
|
:mod:`FL` and :mod:`fl`,
|
|
|
|
:mod:`flp`,
|
|
|
|
:mod:`fm`,
|
|
|
|
:mod:`GET`,
|
|
|
|
:mod:`GLWS`,
|
|
|
|
:mod:`GL` and :mod:`gl`,
|
|
|
|
:mod:`IN`,
|
|
|
|
:mod:`IOCTL`,
|
|
|
|
:mod:`jpeg`,
|
|
|
|
:mod:`panelparser`,
|
|
|
|
:mod:`readcd`,
|
|
|
|
:mod:`SV` and :mod:`sv`,
|
|
|
|
:mod:`torgb`,
|
|
|
|
:mod:`videoreader`, and
|
|
|
|
:mod:`WAIT`.
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
Porting to Python 2.6
|
|
|
|
=====================
|
|
|
|
|
2008-03-25 21:30:02 -03:00
|
|
|
This section lists previously described changes and other bugfixes
|
|
|
|
that may require changes to your code:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-09-01 22:13:42 -03:00
|
|
|
* Classes that aren't supposed to be hashable should
|
|
|
|
set ``__hash__ = None`` in their definitions to indicate
|
|
|
|
the fact.
|
|
|
|
|
2009-01-07 23:39:46 -04:00
|
|
|
* String exceptions have been removed. Attempting to use them raises a
|
|
|
|
:exc:`TypeError`.
|
|
|
|
|
2008-01-04 14:24:41 -04:00
|
|
|
* The :meth:`__init__` method of :class:`collections.deque`
|
2008-01-03 21:16:12 -04:00
|
|
|
now clears any existing contents of the deque
|
|
|
|
before adding elements from the iterable. This change makes the
|
2008-08-31 11:29:31 -03:00
|
|
|
behavior match ``list.__init__()``.
|
2008-01-03 21:16:12 -04:00
|
|
|
|
2008-09-15 10:08:32 -03:00
|
|
|
* :meth:`object.__init__` previously accepted arbitrary arguments and
|
|
|
|
keyword arguments, ignoring them. In Python 2.6, this is no longer
|
2009-01-03 16:55:06 -04:00
|
|
|
allowed and will result in a :exc:`TypeError`. This will affect
|
|
|
|
:meth:`__init__` methods that end up calling the corresponding
|
2008-09-15 10:08:32 -03:00
|
|
|
method on :class:`object` (perhaps through using :func:`super`).
|
|
|
|
See :issue:`1683368` for discussion.
|
2008-09-14 23:53:23 -03:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* The :class:`Decimal` constructor now accepts leading and trailing
|
2008-01-14 21:47:32 -04:00
|
|
|
whitespace when passed a string. Previously it would raise an
|
|
|
|
:exc:`InvalidOperation` exception. On the other hand, the
|
|
|
|
:meth:`create_decimal` method of :class:`Context` objects now
|
2008-05-24 15:31:28 -03:00
|
|
|
explicitly disallows extra whitespace, raising a
|
2008-01-14 21:47:32 -04:00
|
|
|
:exc:`ConversionSyntax` exception.
|
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* Due to an implementation accident, if you passed a file path to
|
2008-01-14 21:47:32 -04:00
|
|
|
the built-in :func:`__import__` function, it would actually import
|
2008-05-24 15:31:28 -03:00
|
|
|
the specified file. This was never intended to work, however, and
|
|
|
|
the implementation now explicitly checks for this case and raises
|
2008-01-14 21:47:32 -04:00
|
|
|
an :exc:`ImportError`.
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
* C API: the :c:func:`PyImport_Import` and :c:func:`PyImport_ImportModule`
|
2008-05-24 15:31:28 -03:00
|
|
|
functions now default to absolute imports, not relative imports.
|
2008-04-10 18:29:01 -03:00
|
|
|
This will affect C extensions that import other modules.
|
|
|
|
|
2008-09-01 22:13:42 -03:00
|
|
|
* C API: extension data types that shouldn't be hashable
|
|
|
|
should define their ``tp_hash`` slot to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyObject_HashNotImplemented`.
|
2008-09-01 22:13:42 -03:00
|
|
|
|
2007-10-20 16:35:18 -03:00
|
|
|
* The :mod:`socket` module exception :exc:`socket.error` now inherits
|
|
|
|
from :exc:`IOError`. Previously it wasn't a subclass of
|
|
|
|
:exc:`StandardError` but now it is, through :exc:`IOError`.
|
2008-04-10 18:29:01 -03:00
|
|
|
(Implemented by Gregory P. Smith; :issue:`1706815`.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-02-23 12:23:05 -04:00
|
|
|
* The :mod:`xmlrpclib` module no longer automatically converts
|
2008-05-24 15:31:28 -03:00
|
|
|
:class:`datetime.date` and :class:`datetime.time` to the
|
2008-02-23 12:23:05 -04:00
|
|
|
:class:`xmlrpclib.DateTime` type; the conversion semantics were
|
|
|
|
not necessarily correct for all applications. Code using
|
2008-05-24 15:31:28 -03:00
|
|
|
:mod:`xmlrpclib` should convert :class:`date` and :class:`time`
|
2008-04-10 18:29:01 -03:00
|
|
|
instances. (:issue:`1330538`)
|
2008-02-23 12:23:05 -04:00
|
|
|
|
2008-05-24 15:31:28 -03:00
|
|
|
* (3.0-warning mode) The :class:`Exception` class now warns
|
|
|
|
when accessed using slicing or index access; having
|
2008-03-25 21:30:02 -03:00
|
|
|
:class:`Exception` behave like a tuple is being phased out.
|
|
|
|
|
|
|
|
* (3.0-warning mode) inequality comparisons between two dictionaries
|
2008-03-20 19:49:26 -03:00
|
|
|
or two objects that don't implement comparison methods are reported
|
|
|
|
as warnings. ``dict1 == dict2`` still works, but ``dict1 < dict2``
|
|
|
|
is being phased out.
|
2008-05-24 15:31:28 -03:00
|
|
|
|
2008-03-20 19:49:26 -03:00
|
|
|
Comparisons between cells, which are an implementation detail of Python's
|
|
|
|
scoping rules, also cause warnings because such comparisons are forbidden
|
|
|
|
entirely in 3.0.
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. ======================================================================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2008-10-08 18:11:33 -03:00
|
|
|
.. _26acks:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Acknowledgements
|
|
|
|
================
|
|
|
|
|
2008-09-05 12:15:56 -03:00
|
|
|
The author would like to thank the following people for offering
|
|
|
|
suggestions, corrections and assistance with various drafts of this
|
2009-01-03 16:55:06 -04:00
|
|
|
article: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy,
|
|
|
|
Jim Jewett, Kent Johnson, Chris Lambacher, Martin Michlmayr,
|
2008-10-16 17:15:47 -03:00
|
|
|
Antoine Pitrou, Brian Warner.
|
2007-08-15 11:28:01 -03:00
|
|
|
|