Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

................
  r66801 | andrew.kuchling | 2008-10-04 23:51:59 +0200 (Sat, 04 Oct 2008) | 1 line

  Punctuation fix; expand dict.update docstring to be clearer
................
  r66803 | benjamin.peterson | 2008-10-05 00:15:31 +0200 (Sun, 05 Oct 2008) | 1 line

  fix typo
................
  r66804 | andrew.kuchling | 2008-10-05 02:11:56 +0200 (Sun, 05 Oct 2008) | 1 line

  #1415508 from Rocky Bernstein: add docstrings for enable_interspersed_args(), disable_interspersed_args()
................
  r66813 | andrew.kuchling | 2008-10-06 14:07:04 +0200 (Mon, 06 Oct 2008) | 3 lines

  Per Greg Ward, optparse is no longer being externally maintained.
  I'll look at the bugs in the Optik bug tracker and copy them to the Python bug
  tracker if they're still relevant.
................
  r66854 | georg.brandl | 2008-10-08 19:20:20 +0200 (Wed, 08 Oct 2008) | 2 lines

  #4059: patch up some sqlite docs.
................
  r66855 | georg.brandl | 2008-10-08 19:30:55 +0200 (Wed, 08 Oct 2008) | 2 lines

  #4058: fix some whatsnew markup.
................
  r66856 | georg.brandl | 2008-10-08 20:47:17 +0200 (Wed, 08 Oct 2008) | 3 lines

  #3935: properly support list subclasses in the C impl. of bisect.
  Patch reviewed by Raymond.
................
  r66866 | benjamin.peterson | 2008-10-09 22:54:43 +0200 (Thu, 09 Oct 2008) | 1 line

  update paragraph about __future__ for 2.6
................
  r66870 | armin.rigo | 2008-10-10 10:40:44 +0200 (Fri, 10 Oct 2008) | 2 lines

  Typo: "ThreadError" is the name in the C source.
................
  r66871 | benjamin.peterson | 2008-10-10 22:38:49 +0200 (Fri, 10 Oct 2008) | 1 line

  fix a small typo
................
  r66872 | benjamin.peterson | 2008-10-10 22:51:37 +0200 (Fri, 10 Oct 2008) | 1 line

  talk about how you can unzip with zip
................
  r66874 | benjamin.peterson | 2008-10-11 00:23:41 +0200 (Sat, 11 Oct 2008) | 1 line

  PyGILState_Acquire -> PyGILState_Ensure
................
  r66887 | benjamin.peterson | 2008-10-13 23:51:40 +0200 (Mon, 13 Oct 2008) | 1 line

  document how to disable fixers
................
  r66903 | benjamin.peterson | 2008-10-15 22:34:09 +0200 (Wed, 15 Oct 2008) | 1 line

  don't recurse into directories that start with '.'
................
  r66905 | benjamin.peterson | 2008-10-15 23:05:55 +0200 (Wed, 15 Oct 2008) | 1 line

  support the optional line argument for idle
................
  r66911 | benjamin.peterson | 2008-10-16 01:10:28 +0200 (Thu, 16 Oct 2008) | 41 lines

  Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3

  ........
    r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line

    mention what the fixes directory is for
  ........
    r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line

    use assertFalse and assertTrue
  ........
    r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line

    instead of abusing the pattern matcher, use start_tree to find a next binding
  ........
    r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line

    don't print tokens to stdout when -v is given
  ........
    r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line

    add the -x option to disable fixers
  ........
    r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line

    cut down on some crud
  ........
    r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line

    add an optional set literal fixer
  ........
    r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line

    don't write backup files by default
  ........
    r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line

    add the -n option; it stops backupfiles from being written
  ........
................
  r66913 | benjamin.peterson | 2008-10-16 20:52:14 +0200 (Thu, 16 Oct 2008) | 1 line

  document that deque indexing is O(n) #4123
................
  r66927 | andrew.kuchling | 2008-10-16 22:15:47 +0200 (Thu, 16 Oct 2008) | 1 line

  Fix wording (2.6.1 backport candidate)
................
  r66932 | benjamin.peterson | 2008-10-16 23:09:28 +0200 (Thu, 16 Oct 2008) | 1 line

  check for error conditions in _json #3623
................
  r66938 | benjamin.peterson | 2008-10-16 23:27:54 +0200 (Thu, 16 Oct 2008) | 1 line

  fix possible ref leak
................
  r66942 | benjamin.peterson | 2008-10-16 23:48:06 +0200 (Thu, 16 Oct 2008) | 1 line

  fix more possible ref leaks in _json and use Py_CLEAR
................
  r66962 | benjamin.peterson | 2008-10-17 22:01:01 +0200 (Fri, 17 Oct 2008) | 1 line

  clarify CALL_FUNCTION #4141
................
  r66964 | georg.brandl | 2008-10-17 23:41:49 +0200 (Fri, 17 Oct 2008) | 2 lines

  Fix duplicate word.
................
  r66973 | armin.ronacher | 2008-10-19 10:27:43 +0200 (Sun, 19 Oct 2008) | 3 lines

  Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
  r66974 | benjamin.peterson | 2008-10-19 15:59:01 +0200 (Sun, 19 Oct 2008) | 1 line

  fix compiler warning
................
  r66977 | benjamin.peterson | 2008-10-19 21:39:16 +0200 (Sun, 19 Oct 2008) | 1 line

  mention -n
................
  r66992 | benjamin.peterson | 2008-10-21 22:51:13 +0200 (Tue, 21 Oct 2008) | 1 line

  make sure to call iteritems()
................
  r66998 | benjamin.peterson | 2008-10-22 22:57:43 +0200 (Wed, 22 Oct 2008) | 1 line

  fix a few typos
................
  r66999 | benjamin.peterson | 2008-10-22 23:05:30 +0200 (Wed, 22 Oct 2008) | 1 line

  and another typo...
................
  r67002 | hirokazu.yamamoto | 2008-10-23 02:37:33 +0200 (Thu, 23 Oct 2008) | 1 line

  Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
................
  r67005 | walter.doerwald | 2008-10-23 15:11:39 +0200 (Thu, 23 Oct 2008) | 2 lines

  Use the correct names of the stateless codec functions (Fixes issue 4178).
................
  r67007 | benjamin.peterson | 2008-10-23 23:43:48 +0200 (Thu, 23 Oct 2008) | 1 line

  only nonempty __slots__ don't work
................
  r67028 | benjamin.peterson | 2008-10-26 01:27:07 +0200 (Sun, 26 Oct 2008) | 1 line

  don't use a catch-all
................
  r67040 | armin.rigo | 2008-10-28 18:01:21 +0100 (Tue, 28 Oct 2008) | 5 lines

  Fix one of the tests: it relied on being present in an "output test" in
  order to actually test what it was supposed to test, i.e. that the code
  in the __del__ method did not crash.  Use instead the new helper
  test_support.captured_output().
................
  r67041 | benjamin.peterson | 2008-10-29 21:33:00 +0100 (Wed, 29 Oct 2008) | 1 line

  mention the version gettempdir() was added
................
  r67044 | amaury.forgeotdarc | 2008-10-30 00:15:57 +0100 (Thu, 30 Oct 2008) | 3 lines

  Correct error message in io.open():
  closefd=True is the only accepted value with a file name.
................
  r67070 | benjamin.peterson | 2008-10-31 21:41:44 +0100 (Fri, 31 Oct 2008) | 1 line

  rephrase has_key doc
................
  r67089 | benjamin.peterson | 2008-11-03 21:43:20 +0100 (Mon, 03 Nov 2008) | 1 line

  clarify by splitting into multiple paragraphs
................
  r67091 | benjamin.peterson | 2008-11-03 23:34:57 +0100 (Mon, 03 Nov 2008) | 1 line

  move a FileIO test to test_fileio
................
  r67101 | georg.brandl | 2008-11-04 21:49:35 +0100 (Tue, 04 Nov 2008) | 2 lines

  #4167: fix markup glitches.
................
  r67117 | georg.brandl | 2008-11-06 11:17:58 +0100 (Thu, 06 Nov 2008) | 2 lines

  #4268: Use correct module for two toplevel functions.
................
  r67118 | georg.brandl | 2008-11-06 11:19:11 +0100 (Thu, 06 Nov 2008) | 2 lines

  #4267: small fixes in sqlite3 docs.
................
  r67119 | georg.brandl | 2008-11-06 11:20:49 +0100 (Thu, 06 Nov 2008) | 2 lines

  #4245: move Thread section to the top.
................
  r67123 | georg.brandl | 2008-11-06 19:49:15 +0100 (Thu, 06 Nov 2008) | 2 lines

  #4247: add "pass" examples to tutorial.
................
  r67124 | andrew.kuchling | 2008-11-06 20:23:02 +0100 (Thu, 06 Nov 2008) | 1 line

  Fix grammar error; reword two paragraphs
................
This commit is contained in:
Georg Brandl 2008-11-07 08:56:27 +00:00
parent 939582fd06
commit 4aef703c45
48 changed files with 940 additions and 540 deletions

View file

@ -16,6 +16,7 @@ docs@python.org), and we'll be glad to correct the problem.
* A. Amoroso
* Pehr Anderson
* Oliver Andrich
* Heidi Annexstad
* Jesús Cea Avión
* Daniel Barclay
* Chris Barker
@ -189,6 +190,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Reuben Sumner
* Kalle Svensson
* Jim Tittsler
* David Turner
* Ville Vainio
* Martijn Vries
* Charles G. Waldman

View file

@ -755,11 +755,11 @@ created.
:cmacro:`Py_END_ALLOW_THREADS` macros is acceptable.
The return value is an opaque "handle" to the thread state when
:cfunc:`PyGILState_Acquire` was called, and must be passed to
:cfunc:`PyGILState_Ensure` was called, and must be passed to
:cfunc:`PyGILState_Release` to ensure Python is left in the same state. Even
though recursive calls are allowed, these handles *cannot* be shared - each
unique call to :cfunc:`PyGILState_Ensure` must save the handle for its call to
:cfunc:`PyGILState_Release`.
unique call to :cfunc:`PyGILState_Ensure` must save the handle for its call
to :cfunc:`PyGILState_Release`.
When the function returns, the current thread will hold the GIL. Failure is a
fatal error.

View file

@ -37,8 +37,8 @@ It can be converted to Python 3.x code via 2to3 on the command line::
A diff against the original source file is printed. 2to3 can also write the
needed modifications right back to the source file. (Of course, a backup of the
original is also be made.) Writing the changes back is enabled with the
:option:`-w` flag::
original is also be made unless :option:`-n` is also given.) Writing the
changes back is enabled with the :option:`-w` flag::
$ 2to3 -w example.py
@ -50,16 +50,19 @@ After transformation, :file:`example.py` looks like this::
name = input()
greet(name)
Comments and and exact indentation are preserved throughout the translation
process.
Comments and exact indentation are preserved throughout the translation process.
By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists
all available fixers. An explicit set of fixers to run can be given by use of
the :option:`-f` flag. The following example runs only the ``imports`` and
``has_key`` fixers::
all available fixers. An explicit set of fixers to run can be given with
:option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The
following example runs only the ``imports`` and ``has_key`` fixers::
$ 2to3 -f imports -f has_key example.py
This command runs every fixer except the ``apply`` fixer::
$ 2to3 -x apply example.py
Some fixers are *explicit*, meaning they aren't run by default and must be
listed on the command line to be run. Here, in addition to the default fixers,
the ``idioms`` fixer is run::
@ -68,25 +71,25 @@ the ``idioms`` fixer is run::
Notice how passing ``all`` enables all default fixers.
Sometimes 2to3 will find a place in your source code that needs to be
changed, but 2to3 cannot fix automatically. In this case, 2to3 will print a
warning beneath the diff for a file. You should address the warning in order to
have compliant 3.x code.
Sometimes 2to3 will find a place in your source code that needs to be changed,
but 2to3 cannot fix automatically. In this case, 2to3 will print a warning
beneath the diff for a file. You should address the warning in order to have
compliant 3.x code.
2to3 can also refactor doctests. To enable this mode, use the :option:`-d`
flag. Note that *only* doctests will be refactored. This also doesn't require
the module to be valid Python. For example, doctest like examples in a reST
document could also be refactored with this option.
The :option:`-v` option enables the output of more information on the
translation process.
The :option:`-v` option enables output of more information on the translation
process.
When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead
of a statement. This is useful when ``from __future__ import print_function``
is being used. If this option is not given, the print fixer will surround
print calls in an extra set of parentheses because it cannot differentiate
between the print statement with parentheses (such as ``print ("a" + "b" +
"c")``) and a true function call.
When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead of
a statement. This is useful when ``from __future__ import print_function`` is
being used. If this option is not given, the print fixer will surround print
calls in an extra set of parentheses because it cannot differentiate between the
print statement with parentheses (such as ``print ("a" + "b" + "c")``) and a
true function call.
:mod:`lib2to3` - 2to3's library

View file

@ -32,9 +32,9 @@ It defines the following functions:
* ``name`` The name of the encoding;
* ``encoder`` The stateless encoding function;
* ``encode`` The stateless encoding function;
* ``decoder`` The stateless decoding function;
* ``decode`` The stateless decoding function;
* ``incrementalencoder`` An incremental encoder class or factory function;
@ -46,7 +46,7 @@ It defines the following functions:
The various functions or classes take the following arguments:
*encoder* and *decoder*: These must be functions or methods which have the same
*encode* and *decode*: These must be functions or methods which have the same
interface as the :meth:`encode`/:meth:`decode` methods of Codec instances (see
Codec Interface). The functions/methods are expected to work in a stateless
mode.

View file

@ -247,7 +247,9 @@ Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
In addition to the above, deques support iteration, pickling, ``len(d)``,
``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
the :keyword:`in` operator, and subscript references such as ``d[-1]``.
the :keyword:`in` operator, and subscript references such as ``d[-1]``. Indexed
access is O(1) at both ends but slows to O(n) in the middle. For fast random
access, use lists instead.
Example:

View file

@ -755,7 +755,8 @@ the more significant byte last.
opcode finds the keyword parameters first. For each keyword argument, the value
is on top of the key. Below the keyword parameters, the positional parameters
are on the stack, with the right-most parameter on top. Below the parameters,
the function object to call is on the stack.
the function object to call is on the stack. Pops all function arguments, and
the function itself off the stack, and pushes the return value.
.. opcode:: MAKE_FUNCTION (argc)

View file

@ -159,6 +159,7 @@ Since creating a message object structure from a string or a file object is such
a common task, two functions are provided as a convenience. They are available
in the top-level :mod:`email` package namespace.
.. currentmodule:: email
.. function:: message_from_string(s[, _class[, strict]])

View file

@ -315,7 +315,7 @@ followed by ``lines`` for the text version or ``binary`` for the binary version.
.. method:: FTP.quit()
Send a ``QUIT`` command to the server and close the connection. This is the
"polite" way to close a connection, but it may raise an exception of the server
"polite" way to close a connection, but it may raise an exception if the server
responds with an error to the ``QUIT`` command. This implies a call to the
:meth:`close` method which renders the :class:`FTP` instance useless for
subsequent calls (see below).

View file

@ -1397,6 +1397,18 @@ available. They are listed here in alphabetical order.
makes possible an idiom for clustering a data series into n-length groups
using ``zip(*[iter(s)]*n)``.
:func:`zip` in conjunction with the ``*`` operator can be used to unzip a
list::
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == x2, y == y2
True
.. versionadded:: 2.0
.. versionchanged:: 2.4

View file

@ -95,7 +95,7 @@ The module also offers three general purpose functions based on heaps.
Merge multiple sorted inputs into a single sorted output (for example, merge
timestamped entries from multiple log files). Returns an :term:`iterator`
over over the sorted values.
over the sorted values.
Similar to ``sorted(itertools.chain(*iterables))`` but returns an iterable, does
not pull the data into memory all at once, and assumes that each of the input

View file

@ -363,7 +363,7 @@ http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Filter specific flags
*:const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags*
:const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags
+----------------------------+--------------------------------------------+
| Constant | Meaning |
@ -372,7 +372,7 @@ http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_VNODE` filter flags*
:const:`KQ_FILTER_VNODE` filter flags
+----------------------------+--------------------------------------------+
| Constant | Meaning |
@ -393,7 +393,7 @@ http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_PROC` filter flags*
:const:`KQ_FILTER_PROC` filter flags
+----------------------------+--------------------------------------------+
| Constant | Meaning |
@ -416,7 +416,7 @@ http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
| :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_NETDEV` filter flags* [not available on Mac OS X]
:const:`KQ_FILTER_NETDEV` filter flags [not available on Mac OS X]
+----------------------------+--------------------------------------------+
| Constant | Meaning |

View file

@ -276,11 +276,15 @@ The module :mod:`socket` exports the following constants and functions:
.. function:: gethostname()
Return a string containing the hostname of the machine where the Python
interpreter is currently executing. If you want to know the current machine's IP
address, you may want to use ``gethostbyname(gethostname())``. This operation
assumes that there is a valid address-to-host mapping for the host, and the
assumption does not always hold. Note: :func:`gethostname` doesn't always return
the fully qualified domain name; use ``getfqdn()`` (see above).
interpreter is currently executing.
If you want to know the current machine's IP address, you may want to use
``gethostbyname(gethostname())``. This operation assumes that there is a
valid address-to-host mapping for the host, and the assumption does not
always hold.
Note: :func:`gethostname` doesn't always return the fully qualified domain
name; use ``getfqdn()`` (see above).
.. function:: gethostbyaddr(ip_address)

View file

@ -27,7 +27,7 @@ represents the database. Here the data will be stored in the
You can also supply the special name ``:memory:`` to create a database in RAM.
Once you have a :class:`Connection`, you can create a :class:`Cursor` object
and call its :meth:`execute` method to perform SQL commands::
and call its :meth:`~Cursor.execute` method to perform SQL commands::
c = conn.cursor()
@ -52,7 +52,7 @@ is insecure; it makes your program vulnerable to an SQL injection attack.
Instead, use the DB-API's parameter substitution. Put ``?`` as a placeholder
wherever you want to use a value, and then provide a tuple of values as the
second argument to the cursor's :meth:`execute` method. (Other database modules
second argument to the cursor's :meth:`~Cursor.execute` method. (Other database modules
may use a different placeholder, such as ``%s`` or ``:1``.) For example::
# Never do this -- insecure!
@ -64,15 +64,15 @@ may use a different placeholder, such as ``%s`` or ``:1``.) For example::
c.execute('select * from stocks where symbol=?', t)
# Larger example
for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
):
]:
c.execute('insert into stocks values (?,?,?,?,?)', t)
To retrieve data after executing a SELECT statement, you can either treat the
cursor as an :term:`iterator`, call the cursor's :meth:`fetchone` method to
retrieve a single matching row, or call :meth:`fetchall` to get a list of the
cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to
retrieve a single matching row, or call :meth:`~Cursor.fetchall` to get a list of the
matching rows.
This example uses the iterator form::
@ -130,7 +130,7 @@ Module functions and constants
returns. It will look for a string formed [mytype] in there, and then decide
that 'mytype' is the type of the column. It will try to find an entry of
'mytype' in the converters dictionary and then use the converter function found
there to return the value. The column name found in :attr:`cursor.description`
there to return the value. The column name found in :attr:`Cursor.description`
is only the first word of the column name, i. e. if you use something like
``'as "x [datetime]"'`` in your SQL, then we will parse out everything until the
first blank for the column name: the column name would simply be "x".
@ -217,11 +217,13 @@ Module functions and constants
Connection Objects
------------------
A :class:`Connection` instance has the following attributes and methods:
.. class:: Connection
A SQLite database connection has the following attributes and methods:
.. attribute:: Connection.isolation_level
Get or set the current isolation level. None for autocommit mode or one of
Get or set the current isolation level. :const:`None` for autocommit mode or one of
"DEFERRED", "IMMEDIATE" or "EXLUSIVE". See section
:ref:`sqlite3-controlling-transactions` for a more detailed explanation.
@ -236,7 +238,7 @@ A :class:`Connection` instance has the following attributes and methods:
.. method:: Connection.commit()
This method commits the current transaction. If you don't call this method,
anything you did since the last call to commit() is not visible from from
anything you did since the last call to ``commit()`` is not visible from from
other database connections. If you wonder why you don't see the data you've
written to the database, please check you didn't forget to call this method.
@ -386,9 +388,9 @@ A :class:`Connection` instance has the following attributes and methods:
.. attribute:: Connection.text_factory
Using this attribute you can control what objects are returned for the TEXT data
type. By default, this attribute is set to :class:`unicode` and the
:mod:`sqlite3` module will return Unicode objects for TEXT. If you want to
Using this attribute you can control what objects are returned for the ``TEXT``
data type. By default, this attribute is set to :class:`unicode` and the
:mod:`sqlite3` module will return Unicode objects for ``TEXT``. If you want to
return bytestrings instead, you can set it to :class:`str`.
For efficiency reasons, there's also a way to return Unicode objects only for
@ -424,10 +426,9 @@ A :class:`Connection` instance has the following attributes and methods:
import sqlite3, os
con = sqlite3.connect('existing_db.db')
full_dump = os.linesep.join(con.iterdump())
f = open('dump.sql', 'w')
f.writelines(full_dump)
f.close()
with open('dump.sql', 'w') as f:
for line in con.iterdump():
f.write('%s\n' % line)
.. _sqlite3-cursor-objects:
@ -435,8 +436,9 @@ A :class:`Connection` instance has the following attributes and methods:
Cursor Objects
--------------
A :class:`Cursor` instance has the following attributes and methods:
.. class:: Cursor
A SQLite database cursor has the following attributes and methods:
.. method:: Cursor.execute(sql, [parameters])
@ -475,7 +477,7 @@ A :class:`Cursor` instance has the following attributes and methods:
.. method:: Cursor.executescript(sql_script)
This is a nonstandard convenience method for executing multiple SQL statements
at once. It issues a COMMIT statement first, then executes the SQL script it
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
gets as a parameter.
*sql_script* can be a bytestring or a Unicode string.
@ -488,7 +490,7 @@ A :class:`Cursor` instance has the following attributes and methods:
.. method:: Cursor.fetchone()
Fetches the next row of a query result set, returning a single sequence,
or ``None`` when no more data is available.
or :const:`None` when no more data is available.
.. method:: Cursor.fetchmany([size=cursor.arraysize])
@ -527,8 +529,8 @@ A :class:`Cursor` instance has the following attributes and methods:
into :attr:`rowcount`.
As required by the Python DB API Spec, the :attr:`rowcount` attribute "is -1 in
case no executeXX() has been performed on the cursor or the rowcount of the last
operation is not determinable by the interface".
case no ``executeXX()`` has been performed on the cursor or the rowcount of the
last operation is not determinable by the interface".
This includes ``SELECT`` statements because we cannot determine the number of
rows a query produced until all rows were fetched.
@ -540,6 +542,81 @@ A :class:`Cursor` instance has the following attributes and methods:
method. For operations other than ``INSERT`` or when :meth:`executemany` is
called, :attr:`lastrowid` is set to :const:`None`.
.. attribute:: Cursor.description
This read-only attribute provides the column names of the last query. To
remain compatible with the Python DB API, it returns a 7-tuple for each
column where the last six items of each tuple are :const:`None`.
It is set for ``SELECT`` statements without any matching rows as well.
.. _sqlite3-row-objects:
Row Objects
-----------
.. class:: Row
A :class:`Row` instance serves as a highly optimized
:attr:`~Connection.row_factory` for :class:`Connection` objects.
It tries to mimic a tuple in most of its features.
It supports mapping access by column name and index, iteration,
representation, equality testing and :func:`len`.
If two :class:`Row` objects have exactly the same columns and their
members are equal, they compare equal.
.. versionchanged:: 2.6
Added iteration and equality (hashability).
.. method:: keys
This method returns a tuple of column names. Immediately after a query,
it is the first member of each tuple in :attr:`Cursor.description`.
.. versionadded:: 2.6
Let's assume we initialize a table as in the example given above::
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute('''create table stocks
(date text, trans text, symbol text,
qty real, price real)''')
c.execute("""insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()
Now we plug :class:`Row` in::
>>> conn.row_factory = sqlite3.Row
>>> c = conn.cursor()
>>> c.execute('select * from stocks')
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
>>> r = c.fetchone()
>>> type(r)
<type 'sqlite3.Row'>
>>> r
(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.140000000000001)
>>> len(r)
5
>>> r[2]
u'RHAT'
>>> r.keys()
['date', 'trans', 'symbol', 'qty', 'price']
>>> r['qty']
100.0
>>> for member in r: print member
...
2006-01-05
BUY
RHAT
100.0
35.14
.. _sqlite3-types:
SQLite and Python types
@ -549,43 +626,46 @@ SQLite and Python types
Introduction
^^^^^^^^^^^^
SQLite natively supports the following types: NULL, INTEGER, REAL, TEXT, BLOB.
SQLite natively supports the following types: ``NULL``, ``INTEGER``,
``REAL``, ``TEXT``, ``BLOB``.
The following Python types can thus be sent to SQLite without any problem:
+------------------------+-------------+
| Python type | SQLite type |
+========================+=============+
| ``None`` | NULL |
+------------------------+-------------+
| ``int`` | INTEGER |
+------------------------+-------------+
| ``long`` | INTEGER |
+------------------------+-------------+
| ``float`` | REAL |
+------------------------+-------------+
| ``str (UTF8-encoded)`` | TEXT |
+------------------------+-------------+
| ``unicode`` | TEXT |
+------------------------+-------------+
| ``buffer`` | BLOB |
+------------------------+-------------+
+-----------------------------+-------------+
| Python type | SQLite type |
+=============================+=============+
| :const:`None` | ``NULL`` |
+-----------------------------+-------------+
| :class:`int` | ``INTEGER`` |
+-----------------------------+-------------+
| :class:`long` | ``INTEGER`` |
+-----------------------------+-------------+
| :class:`float` | ``REAL`` |
+-----------------------------+-------------+
| :class:`str` (UTF8-encoded) | ``TEXT`` |
+-----------------------------+-------------+
| :class:`unicode` | ``TEXT`` |
+-----------------------------+-------------+
| :class:`buffer` | ``BLOB`` |
+-----------------------------+-------------+
This is how SQLite types are converted to Python types by default:
+-------------+---------------------------------------------+
| SQLite type | Python type |
+=============+=============================================+
| ``NULL`` | None |
+-------------+---------------------------------------------+
| ``INTEGER`` | int or long, depending on size |
+-------------+---------------------------------------------+
| ``REAL`` | float |
+-------------+---------------------------------------------+
| ``TEXT`` | depends on text_factory, unicode by default |
+-------------+---------------------------------------------+
| ``BLOB`` | buffer |
+-------------+---------------------------------------------+
+-------------+----------------------------------------------+
| SQLite type | Python type |
+=============+==============================================+
| ``NULL`` | :const:`None` |
+-------------+----------------------------------------------+
| ``INTEGER`` | :class:`int` or :class:`long`, |
| | depending on size |
+-------------+----------------------------------------------+
| ``REAL`` | :class:`float` |
+-------------+----------------------------------------------+
| ``TEXT`` | depends on :attr:`~Connection.text_factory`, |
| | :class:`unicode` by default |
+-------------+----------------------------------------------+
| ``BLOB`` | :class:`buffer` |
+-------------+----------------------------------------------+
The type system of the :mod:`sqlite3` module is extensible in two ways: you can
store additional Python types in a SQLite database via object adaptation, and
@ -713,9 +793,10 @@ Controlling Transactions
------------------------
By default, the :mod:`sqlite3` module opens transactions implicitly before a
Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE),
and commits transactions implicitly before a non-DML, non-query statement (i. e.
anything other than SELECT/INSERT/UPDATE/DELETE/REPLACE).
Data Modification Language (DML) statement (i.e.
``INSERT``/``UPDATE``/``DELETE``/``REPLACE``), and commits transactions
implicitly before a non-DML, non-query statement (i. e.
anything other than ``SELECT`` or the aforementioned).
So if you are within a transaction and issue a command like ``CREATE TABLE
...``, ``VACUUM``, ``PRAGMA``, the :mod:`sqlite3` module will commit implicitly
@ -724,15 +805,15 @@ is that some of these commands don't work within transactions. The other reason
is that pysqlite needs to keep track of the transaction state (if a transaction
is active or not).
You can control which kind of "BEGIN" statements pysqlite implicitly executes
You can control which kind of ``BEGIN`` statements pysqlite implicitly executes
(or none at all) via the *isolation_level* parameter to the :func:`connect`
call, or via the :attr:`isolation_level` property of connections.
If you want **autocommit mode**, then set :attr:`isolation_level` to None.
Otherwise leave it at its default, which will result in a plain "BEGIN"
statement, or set it to one of SQLite's supported isolation levels: DEFERRED,
IMMEDIATE or EXCLUSIVE.
statement, or set it to one of SQLite's supported isolation levels: "DEFERRED",
"IMMEDIATE" or "EXCLUSIVE".
@ -748,7 +829,7 @@ Using the nonstandard :meth:`execute`, :meth:`executemany` and
be written more concisely because you don't have to create the (often
superfluous) :class:`Cursor` objects explicitly. Instead, the :class:`Cursor`
objects are created implicitly and these shortcut methods return the cursor
objects. This way, you can execute a SELECT statement and iterate over it
objects. This way, you can execute a ``SELECT`` statement and iterate over it
directly using only a single call on the :class:`Connection` object.
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py

View file

@ -1924,7 +1924,8 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
.. method:: has_key(key)
``dict.has_key(key)`` is equivalent to ``key in d``, but deprecated.
Test for the presence of *key* in the dictionary. :meth:`has_key` is
deprecated in favor of ``key in d``.
.. method:: items()

View file

@ -318,8 +318,8 @@ Replacing /bin/sh shell backquote
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
Replacing shell pipe line
^^^^^^^^^^^^^^^^^^^^^^^^^
Replacing shell pipeline
^^^^^^^^^^^^^^^^^^^^^^^^
::

View file

@ -224,6 +224,8 @@ the appropriate function arguments, instead.
:data:`tempdir` is not ``None``, this simply returns its contents; otherwise,
the search described above is performed, and the result returned.
.. versionadded:: 2.3
.. data:: template

View file

@ -99,7 +99,7 @@ It defines the following constant and functions:
*size* argument specifies the stack size to be used for subsequently created
threads, and must be 0 (use platform or configured default) or a positive
integer value of at least 32,768 (32kB). If changing the thread stack size is
unsupported, a :exc:`ThreadError` is raised. If the specified stack size is
unsupported, the :exc:`error` exception is raised. If the specified stack size is
invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32kB
is currently the minimum supported stack size value to guarantee sufficient
stack space for the interpreter itself. Note that some platforms may have

View file

@ -180,6 +180,166 @@ when implemented, are mapped to module-level functions.
All of the methods described below are executed atomically.
.. _thread-objects:
Thread Objects
--------------
This class represents an activity that is run in a separate thread of control.
There are two ways to specify the activity: by passing a callable object to the
constructor, or by overriding the :meth:`run` method in a subclass. No other
methods (except for the constructor) should be overridden in a subclass. In
other words, *only* override the :meth:`__init__` and :meth:`run` methods of
this class.
Once a thread object is created, its activity must be started by calling the
thread's :meth:`start` method. This invokes the :meth:`run` method in a
separate thread of control.
Once the thread's activity is started, the thread is considered 'alive'. It
stops being alive when its :meth:`run` method terminates -- either normally, or
by raising an unhandled exception. The :meth:`is_alive` method tests whether the
thread is alive.
Other threads can call a thread's :meth:`join` method. This blocks the calling
thread until the thread whose :meth:`join` method is called is terminated.
A thread has a name. The name can be passed to the constructor, and read or
changed through the :attr:`name` attribute.
A thread can be flagged as a "daemon thread". The significance of this flag is
that the entire Python program exits when only daemon threads are left. The
initial value is inherited from the creating thread. The flag can be set
through the :attr:`daemon` attribute.
There is a "main thread" object; this corresponds to the initial thread of
control in the Python program. It is not a daemon thread.
There is the possibility that "dummy thread objects" are created. These are
thread objects corresponding to "alien threads", which are threads of control
started outside the threading module, such as directly from C code. Dummy
thread objects have limited functionality; they are always considered alive and
daemonic, and cannot be :meth:`join`\ ed. They are never deleted, since it is
impossible to detect the termination of alien threads.
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={})
This constructor should always be called with keyword arguments. Arguments are:
*group* should be ``None``; reserved for future extension when a
:class:`ThreadGroup` class is implemented.
*target* is the callable object to be invoked by the :meth:`run` method.
Defaults to ``None``, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of the form
"Thread-*N*" where *N* is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ``()``.
*kwargs* is a dictionary of keyword arguments for the target invocation.
Defaults to ``{}``.
If the subclass overrides the constructor, it must make sure to invoke the base
class constructor (``Thread.__init__()``) before doing anything else to the
thread.
.. method:: Thread.start()
Start the thread's activity.
It must be called at most once per thread object. It arranges for the object's
:meth:`run` method to be invoked in a separate thread of control.
This method will raise a :exc:`RuntimeException` if called more than once on the
same thread object.
.. method:: Thread.run()
Method representing the thread's activity.
You may override this method in a subclass. The standard :meth:`run` method
invokes the callable object passed to the object's constructor as the *target*
argument, if any, with sequential and keyword arguments taken from the *args*
and *kwargs* arguments, respectively.
.. method:: Thread.join([timeout])
Wait until the thread terminates. This blocks the calling thread until the
thread whose :meth:`join` method is called terminates -- either normally or
through an unhandled exception -- or until the optional timeout occurs.
When the *timeout* argument is present and not ``None``, it should be a floating
point number specifying a timeout for the operation in seconds (or fractions
thereof). As :meth:`join` always returns ``None``, you must call :meth:`isAlive`
after :meth:`join` to decide whether a timeout happened -- if the thread is
still alive, the :meth:`join` call timed out.
When the *timeout* argument is not present or ``None``, the operation will block
until the thread terminates.
A thread can be :meth:`join`\ ed many times.
:meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
the current thread as that would cause a deadlock. It is also an error to
:meth:`join` a thread before it has been started and attempts to do so
raises the same exception.
.. method:: Thread.getName()
Thread.setName()
Old API for :attr:`~Thread.name`.
.. attribute:: Thread.name
A string used for identification purposes only. It has no semantics.
Multiple threads may be given the same name. The initial name is set by the
constructor.
.. attribute:: Thread.ident
The 'thread identifier' of this thread or ``None`` if the thread has not been
started. This is a nonzero integer. See the :func:`thread.get_ident()`
function. Thread identifiers may be recycled when a thread exits and another
thread is created. The identifier is available even after the thread has
exited.
.. versionadded:: 2.6
.. method:: Thread.is_alive()
Thread.isAlive()
Return whether the thread is alive.
Roughly, a thread is alive from the moment the :meth:`start` method returns
until its :meth:`run` method terminates. The module function :func:`enumerate`
returns a list of all alive threads.
.. method:: Thread.isDaemon()
Thread.setDaemon()
Old API for :attr:`~Thread.daemon`.
.. attribute:: Thread.daemon
The thread's daemon flag. This must be set before :meth:`start` is called,
otherwise :exc:`RuntimeError` is raised.
The initial value is inherited from the creating thread.
The entire Python program exits when no alive non-daemon threads are left.
.. _lock-objects:
Lock Objects
@ -539,166 +699,6 @@ An event object manages an internal flag that can be set to true with the
thereof).
.. _thread-objects:
Thread Objects
--------------
This class represents an activity that is run in a separate thread of control.
There are two ways to specify the activity: by passing a callable object to the
constructor, or by overriding the :meth:`run` method in a subclass. No other
methods (except for the constructor) should be overridden in a subclass. In
other words, *only* override the :meth:`__init__` and :meth:`run` methods of
this class.
Once a thread object is created, its activity must be started by calling the
thread's :meth:`start` method. This invokes the :meth:`run` method in a
separate thread of control.
Once the thread's activity is started, the thread is considered 'alive'. It
stops being alive when its :meth:`run` method terminates -- either normally, or
by raising an unhandled exception. The :meth:`is_alive` method tests whether the
thread is alive.
Other threads can call a thread's :meth:`join` method. This blocks the calling
thread until the thread whose :meth:`join` method is called is terminated.
A thread has a name. The name can be passed to the constructor, and read or
changed through the :attr:`name` attribute.
A thread can be flagged as a "daemon thread". The significance of this flag is
that the entire Python program exits when only daemon threads are left. The
initial value is inherited from the creating thread. The flag can be set
through the :attr:`daemon` attribute.
There is a "main thread" object; this corresponds to the initial thread of
control in the Python program. It is not a daemon thread.
There is the possibility that "dummy thread objects" are created. These are
thread objects corresponding to "alien threads", which are threads of control
started outside the threading module, such as directly from C code. Dummy
thread objects have limited functionality; they are always considered alive and
daemonic, and cannot be :meth:`join`\ ed. They are never deleted, since it is
impossible to detect the termination of alien threads.
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={})
This constructor should always be called with keyword arguments. Arguments are:
*group* should be ``None``; reserved for future extension when a
:class:`ThreadGroup` class is implemented.
*target* is the callable object to be invoked by the :meth:`run` method.
Defaults to ``None``, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of the form
"Thread-*N*" where *N* is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ``()``.
*kwargs* is a dictionary of keyword arguments for the target invocation.
Defaults to ``{}``.
If the subclass overrides the constructor, it must make sure to invoke the base
class constructor (``Thread.__init__()``) before doing anything else to the
thread.
.. method:: Thread.start()
Start the thread's activity.
It must be called at most once per thread object. It arranges for the object's
:meth:`run` method to be invoked in a separate thread of control.
This method will raise a :exc:`RuntimeException` if called more than once on the
same thread object.
.. method:: Thread.run()
Method representing the thread's activity.
You may override this method in a subclass. The standard :meth:`run` method
invokes the callable object passed to the object's constructor as the *target*
argument, if any, with sequential and keyword arguments taken from the *args*
and *kwargs* arguments, respectively.
.. method:: Thread.join([timeout])
Wait until the thread terminates. This blocks the calling thread until the
thread whose :meth:`join` method is called terminates -- either normally or
through an unhandled exception -- or until the optional timeout occurs.
When the *timeout* argument is present and not ``None``, it should be a floating
point number specifying a timeout for the operation in seconds (or fractions
thereof). As :meth:`join` always returns ``None``, you must call :meth:`isAlive`
after :meth:`join` to decide whether a timeout happened -- if the thread is
still alive, the :meth:`join` call timed out.
When the *timeout* argument is not present or ``None``, the operation will block
until the thread terminates.
A thread can be :meth:`join`\ ed many times.
:meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
the current thread as that would cause a deadlock. It is also an error to
:meth:`join` a thread before it has been started and attempts to do so
raises the same exception.
.. method:: Thread.getName()
Thread.setName()
Old API for :attr:`~Thread.name`.
.. attribute:: Thread.name
A string used for identification purposes only. It has no semantics.
Multiple threads may be given the same name. The initial name is set by the
constructor.
.. attribute:: Thread.ident
The 'thread identifier' of this thread or ``None`` if the thread has not been
started. This is a nonzero integer. See the :func:`thread.get_ident()`
function. Thread identifiers may be recycled when a thread exits and another
thread is created. The identifier is available even after the thread has
exited.
.. versionadded:: 2.6
.. method:: Thread.is_alive()
Thread.isAlive()
Return whether the thread is alive.
Roughly, a thread is alive from the moment the :meth:`start` method returns
until its :meth:`run` method terminates. The module function :func:`enumerate`
returns a list of all alive threads.
.. method:: Thread.isDaemon()
Thread.setDaemon()
Old API for :attr:`~Thread.daemon`.
.. attribute:: Thread.daemon
The thread's daemon flag. This must be set before :meth:`start` is called,
otherwise :exc:`RuntimeError` is raised.
The initial value is inherited from the creating thread.
The entire Python program exits when no alive non-daemon threads are left.
.. _timer-objects:
Timer Objects

View file

@ -1663,8 +1663,8 @@ Notes on using *__slots__*
defined. As a result, subclasses will have a *__dict__* unless they also define
*__slots__*.
* *__slots__* do not work for classes derived from "variable-length" built-in
types such as :class:`long`, :class:`str` and :class:`tuple`.
* Nonempty *__slots__* does not work for classes derived from "variable-length"
built-in types such as :class:`long`, :class:`str` and :class:`tuple`.
* Any non-string iterable may be assigned to *__slots__*. Mappings may also be
used; however, in the future, special meaning may be assigned to the values

View file

@ -788,10 +788,11 @@ can appear before a future statement are:
* blank lines, and
* other future statements.
The features recognized by Python 2.5 are ``absolute_import``, ``division``,
``generators``, ``nested_scopes`` and ``with_statement``. ``generators`` and
``nested_scopes`` are redundant in Python version 2.3 and above because they
are always enabled.
The features recognized by Python 2.6 are ``unicode_literals``,
``print_function``, ``absolute_import``, ``division``, ``generators``,
``nested_scopes`` and ``with_statement``. ``generators``, ``with_statement``,
``nested_scopes`` are redundant in Python version 2.6 and above because they are
always enabled.
A future statement is recognized and treated specially at compile time: Changes
to the semantics of core constructs are often implemented by generating

View file

@ -166,6 +166,41 @@ required syntactically but the program requires no action. For example::
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
This is commonly used for creating minimal classes such as exceptions, or
for ignoring unwanted exceptions::
>>> class ParserError(Exception):
... pass
...
>>> try:
... import audioop
... except ImportError:
... pass
...
Another place :keyword:`pass` can be used is as a place-holder for a function or
conditional body when you are working on new code, allowing you to keep
thinking at a more abstract level. However, as :keyword:`pass` is silently
ignored, a better choice may be to raise a :exc:`NotImplementedError`
exception::
>>> def initlog(*args):
... raise NotImplementedError # Open logfile if not already open
... if not logfp:
... raise NotImplementedError # Set up dummy log back-end
... raise NotImplementedError('Call log initialization handler')
...
If :keyword:`pass` were used here and you later ran tests, they may fail
without indicating why. Using :exc:`NotImplementedError` causes this code
to raise an exception, telling you exactly where the incomplete code
is. Note the two calling styles of the exceptions above.
The first style, with no message but with an accompanying comment,
lets you easily leave the comment when you remove the exception,
which ideally would be a good description for
the block of code the exception is a placeholder for. However, the
third example, providing a message for the exception, will produce
a more useful traceback.
.. _tut-functions:

View file

@ -662,33 +662,33 @@ and :meth:`Semaphore` to create shared locks.)
from multiprocessing import Pool, Manager
def factorial(N, dictionary):
"Compute a factorial."
# Calculate the result
fact = 1L
for i in range(1, N+1):
fact = fact * i
"Compute a factorial."
# Calculate the result
fact = 1L
for i in range(1, N+1):
fact = fact * i
# Store result in dictionary
dictionary[N] = fact
dictionary[N] = fact
if __name__ == '__main__':
p = Pool(5)
mgr = Manager()
d = mgr.dict() # Create shared dictionary
p = Pool(5)
mgr = Manager()
d = mgr.dict() # Create shared dictionary
# Run tasks using the pool
for N in range(1, 1000, 10):
p.apply_async(factorial, (N, d))
# Run tasks using the pool
for N in range(1, 1000, 10):
p.apply_async(factorial, (N, d))
# Mark pool as closed -- no more tasks can be added.
p.close()
# Mark pool as closed -- no more tasks can be added.
p.close()
# Wait for tasks to exit
p.join()
# Wait for tasks to exit
p.join()
# Output results
for k, v in sorted(d.items()):
print k, v
# Output results
for k, v in sorted(d.items()):
print k, v
This will produce the output::
@ -723,32 +723,33 @@ 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::
# 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'
>>> # 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'
Curly brackets can be escaped by doubling them::
format("Empty dict: {{}}") -> "Empty dict: {}"
>>> format("Empty dict: {{}}")
"Empty dict: {}"
Field names can be integers indicating positional arguments, such as
``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
supply compound field names that read attributes or access dictionary keys::
import sys
'Platform: {0.platform}\nPython version: {0.version}'.format(sys) ->
'Platform: darwin\n
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
>>> import sys
>>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
Platform: darwin
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
import mimetypes
'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
'Content-type: video/mp4'
>>> import mimetypes
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
'Content-type: video/mp4'
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
@ -760,30 +761,25 @@ So far we've shown how to specify which field to substitute into the
resulting string. The precise formatting used is also controllable by
adding a colon followed by a format specifier. For example::
# 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'
>>> # 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'
Format specifiers can reference other fields through nesting::
fmt = '{0:{1}}'
width = 15
fmt.format('Invoice #1234', width) ->
'Invoice #1234 '
width = 35
fmt.format('Invoice #1234', width) ->
'Invoice #1234 '
>>> fmt = '{0:{1}}'
>>> width = 15
>>> fmt.format('Invoice #1234', width)
'Invoice #1234 '
>>> width = 35
>>> fmt.format('Invoice #1234', width)
'Invoice #1234 '
The alignment of a field within the desired width can be specified:
@ -798,7 +794,7 @@ Character Effect
Format specifiers can also include a presentation type, which
controls how the value is formatted. For example, floating-point numbers
can be formatted as a general number or in exponential notation:
can be formatted as a general number or in exponential notation::
>>> '{0:g}'.format(3.75)
'3.75'
@ -806,25 +802,27 @@ can be formatted as a general number or in exponential notation:
'3.750000e+00'
A variety of presentation types are available. Consult the 2.6
documentation for a :ref:`complete list <formatstrings>`; here's a sample::
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.
===== ========================================================================
``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.
===== ========================================================================
Classes and types can define a :meth:`__format__` method to control how they're
formatted. It receives a single argument, the format specifier::
@ -865,13 +863,14 @@ by doing ``def print(...)`` or importing a new function from somewhere else.
Python 2.6 has a ``__future__`` import that removes ``print`` as language
syntax, letting you use the functional form instead. For example::
from __future__ import print_function
print('# of entries', len(dictionary), file=sys.stderr)
>>> from __future__ import print_function
>>> print('# of entries', len(dictionary), file=sys.stderr)
The signature of the new function is::
def print(*args, sep=' ', end='\n', file=None)
The parameters are:
* *args*: positional arguments whose values will be printed out.
@ -1002,6 +1001,8 @@ Byte arrays support most of the methods of string types, such as
and some of the methods of lists, such as :meth:`append`,
:meth:`pop`, and :meth:`reverse`.
::
>>> b = bytearray('ABC')
>>> b.append('d')
>>> b.append(ord('e'))
@ -1224,8 +1225,8 @@ To check whether an object supports a particular interface, you can
now write::
def func(d):
if not isinstance(d, collections.MutableMapping):
raise ValueError("Mapping object expected, not %r" % d)
if not isinstance(d, collections.MutableMapping):
raise ValueError("Mapping object expected, not %r" % d)
Don't feel that you must now begin writing lots of checks as in the
above example. Python has a strong tradition of duck-typing, where
@ -1237,22 +1238,22 @@ do it where it's absolutely necessary.
You can write your own ABCs by using ``abc.ABCMeta`` as the
metaclass in a class definition::
from abc import ABCMeta, abstractmethod
from abc import ABCMeta, abstractmethod
class Drawable():
__metaclass__ = ABCMeta
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
class Square(Drawable):
def draw(self, x, y, scale):
...
class Square(Drawable):
def draw(self, x, y, scale):
...
In the :class:`Drawable` ABC above, the :meth:`draw_doubled` method
@ -1272,7 +1273,7 @@ try to create an instance of a subclass lacking the method::
>>> class Circle(Drawable):
... pass
...
>>> c=Circle()
>>> c = Circle()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Circle with abstract methods draw
@ -1331,7 +1332,7 @@ built-in returns the binary representation for a number::
The :func:`int` and :func:`long` built-ins will now accept the "0o"
and "0b" prefixes when base-8 or base-2 are requested, or when the
*base* argument is zero (signalling that the base used should be
determined from the string):
determined from the string)::
>>> int ('0o52', 0)
42
@ -1504,7 +1505,7 @@ Some smaller changes made to the core Python language are:
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
It's also become legal to provide keyword arguments after a ``*args`` argument
to a function call.
to a function call. ::
>>> def f(*args, **kw):
... print args, kw
@ -1545,17 +1546,17 @@ Some smaller changes made to the core Python language are:
property. You would use them like this::
class C(object):
@property
def x(self):
return self._x
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
@x.deleter
def x(self):
del self._x
class D(C):
@C.x.getter
@ -1878,8 +1879,8 @@ changes, or look through the Subversion logs for all the details.
>>> var_type = collections.namedtuple('variable',
... 'id name type size')
# Names are separated by spaces or commas.
# 'id, name, type, size' would also work.
>>> # Names are separated by spaces or commas.
>>> # 'id, name, type, size' would also work.
>>> var_type._fields
('id', 'name', 'type', 'size')
@ -1929,11 +1930,13 @@ changes, or look through the Subversion logs for all the details.
* A new window method in the :mod:`curses` module,
:meth:`chgat`, changes the display attributes for a certain number of
characters on a single line. (Contributed by Fabian Kreutz.) ::
characters on a single line. (Contributed by Fabian Kreutz.)
::
# Boldface text starting at y=0,x=21
# and affecting the rest of the line.
stdscr.chgat(0,21, curses.A_BOLD)
stdscr.chgat(0, 21, curses.A_BOLD)
The :class:`Textbox` class in the :mod:`curses.textpad` module
now supports editing in insert mode as well as overwrite mode.
@ -1999,8 +2002,8 @@ changes, or look through the Subversion logs for all the details.
order, and returns a new generator that returns the contents of all
the iterators, also in sorted order. For example::
heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
[1, 2, 3, 5, 8, 9, 16]
>>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
[1, 2, 3, 5, 8, 9, 16]
Another new function, ``heappushpop(heap, item)``,
pushes *item* onto *heap*, then pops off and returns the smallest item.
@ -2034,57 +2037,55 @@ changes, or look through the Subversion logs for all the details.
each of the elements; if some of the iterables are shorter than
others, the missing values are set to *fillvalue*. For example::
itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)
>>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))
``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. ::
itertools.product([1,2,3], [4,5,6]) ->
(1, 4), (1, 5), (1, 6),
(2, 4), (2, 5), (2, 6),
(3, 4), (3, 5), (3, 6)
>>> list(itertools.product([1,2,3], [4,5,6]))
[(1, 4), (1, 5), (1, 6),
(2, 4), (2, 5), (2, 6),
(3, 4), (3, 5), (3, 6)]
The optional *repeat* keyword argument is used for taking the
product of an iterable or a set of iterables with themselves,
repeated *N* times. With a single iterable argument, *N*-tuples
are returned::
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)
>>> 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)]
With two iterables, *2N*-tuples are returned. ::
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)
>>> 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)]
``combinations(iterable, r)`` returns sub-sequences of length *r* from
the elements of *iterable*. ::
itertools.combinations('123', 2) ->
('1', '2'), ('1', '3'), ('2', '3')
itertools.combinations('123', 3) ->
('1', '2', '3')
itertools.combinations('1234', 3) ->
('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'),
('2', '3', '4')
>>> 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')]
``permutations(iter[, r])`` returns all the permutations of length *r* of
the iterable's elements. If *r* is not specified, it will default to the
number of elements produced by the iterable. ::
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)
>>> 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)]
``itertools.chain(*iterables)`` is an existing function in
:mod:`itertools` that gained a new constructor in Python 2.6.
@ -2093,8 +2094,8 @@ changes, or look through the Subversion logs for all the details.
then return all the elements of the first iterable, then
all the elements of the second, and so on. ::
chain.from_iterable([[1,2,3], [4,5,6]]) ->
1, 2, 3, 4, 5, 6
>>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
[1, 2, 3, 4, 5, 6]
(All contributed by Raymond Hettinger.)
@ -2265,16 +2266,15 @@ changes, or look through the Subversion logs for all the details.
with an installed Python package. For example::
>>> import pkgutil
>>> pkgutil.get_data('test', 'exception_hierarchy.txt')
'BaseException
>>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
...'
>>>
...
(Contributed by Paul Moore; :issue:`2439`.)
@ -2548,9 +2548,9 @@ changes, or look through the Subversion logs for all the details.
with test_support.check_warnings() as wrec:
warnings.simplefilter("always")
... code that triggers a warning ...
# ... code that triggers a warning ...
assert str(wrec.message) == "function is outdated"
assert len(wrec.warnings) == 1, "Multiple warnings raised"
assert len(wrec.warnings) == 1, "Multiple warnings raised"
(Contributed by Brett Cannon.)
@ -2724,7 +2724,7 @@ for debugging::
t = ast.parse("""
d = {}
for i in 'abcdefghijklm':
d[i + i] = ord(i) - ord('a') + 1
d[i + i] = ord(i) - ord('a') + 1
print d
""")
print ast.dump(t)
@ -2733,32 +2733,32 @@ This outputs a deeply nested tree::
Module(body=[
Assign(targets=[
Name(id='d', ctx=Store())
Name(id='d', ctx=Store())
], value=Dict(keys=[], values=[]))
For(target=Name(id='i', ctx=Store()),
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())
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())
], nl=True)
])
@ -2862,8 +2862,8 @@ Using the module is simple::
# Create data structure
data_struct = dict(lastAccessed=datetime.datetime.now(),
version=1,
categories=('Personal','Shared','Private'))
version=1,
categories=('Personal','Shared','Private'))
# Create string containing XML.
plist_str = plistlib.writePlistToString(data_struct)

View file

@ -2,6 +2,7 @@
import decimal
from unittest import TestCase
import json
import json.decoder
class TestScanString(TestCase):
@ -100,3 +101,9 @@ def _test_scanstring(self, scanstring):
self.assertEquals(
scanstring('["Bad value", truth]', 2, None, True),
(u'Bad value', 12))
def test_issue3623(self):
self.assertRaises(ValueError, json.decoder.scanstring, b"xxx", 1,
"xxx")
self.assertRaises(UnicodeDecodeError,
json.encoder.encode_basestring_ascii, b"xx\xff")

View file

@ -28,15 +28,19 @@ class FixNext(fixer_base.BaseFix):
any* > >
|
global=global_stmt< 'global' any* 'next' any* >
|
mod=file_input< any+ >
"""
order = "pre" # Pre-order tree traversal
def start_tree(self, tree, filename):
super(FixNext, self).start_tree(tree, filename)
self.shadowed_next = False
n = find_binding('next', tree)
if n:
self.warning(n, bind_warning)
self.shadowed_next = True
else:
self.shadowed_next = False
def transform(self, node, results):
assert results
@ -69,11 +73,6 @@ def transform(self, node, results):
elif "global" in results:
self.warning(node, bind_warning)
self.shadowed_next = True
elif mod:
n = find_binding('next', mod)
if n:
self.warning(n, bind_warning)
self.shadowed_next = True
### The following functions help test if node is part of an assignment

View file

@ -0,0 +1,52 @@
"""
Optional fixer to transform set() calls to set literals.
"""
# Author: Benjamin Peterson
from lib2to3 import fixer_base, pytree
from lib2to3.fixer_util import token, syms
class FixSetLiteral(fixer_base.BaseFix):
explicit = True
PATTERN = """power< 'set' trailer< '('
(atom=atom< '[' (items=listmaker< any ((',' any)* [',']) >
|
single=any) ']' >
|
atom< '(' items=testlist_gexp< any ((',' any)* [',']) > ')' >
)
')' > >
"""
def transform(self, node, results):
single = results.get("single")
if single:
# Make a fake listmaker
fake = pytree.Node(syms.listmaker, [single.clone()])
single.replace(fake)
items = fake
else:
items = results["items"]
# Build the contents of the literal
literal = [pytree.Leaf(token.LBRACE, "{")]
literal.extend(n.clone() for n in items.children)
literal.append(pytree.Leaf(token.RBRACE, "}"))
# Set the prefix of the right brace to that of the ')' or ']'
literal[-1].set_prefix(items.get_next_sibling().get_prefix())
maker = pytree.Node(syms.dictsetmaker, literal)
maker.set_prefix(node.get_prefix())
# If the original was a one tuple, we need to remove the extra comma.
if len(maker.children) == 4:
n = maker.children[2]
n.remove()
maker.children[-1].set_prefix(n.get_prefix())
# Finally, replace the set call with our shiny new literal.
return maker

View file

@ -15,10 +15,31 @@ class StdoutRefactoringTool(refactor.RefactoringTool):
Prints output to stdout.
"""
def __init__(self, fixers, options, explicit, nobackups):
self.nobackups = nobackups
super(StdoutRefactoringTool, self).__init__(fixers, options, explicit)
def log_error(self, msg, *args, **kwargs):
self.errors.append((msg, args, kwargs))
self.logger.error(msg, *args, **kwargs)
def write_file(self, new_text, filename, old_text):
if not self.nobackups:
# Make backup
backup = filename + ".bak"
if os.path.lexists(backup):
try:
os.remove(backup)
except os.error, err:
self.log_message("Can't remove backup %s", backup)
try:
os.rename(filename, backup)
except os.error, err:
self.log_message("Can't rename %s to %s", filename, backup)
# Actually write the new file
super(StdoutRefactoringTool, self).write_file(new_text,
filename, old_text)
def print_output(self, lines):
for line in lines:
print line
@ -39,7 +60,9 @@ def main(fixer_pkg, args=None):
parser.add_option("-d", "--doctests_only", action="store_true",
help="Fix up doctests only")
parser.add_option("-f", "--fix", action="append", default=[],
help="Each FIX specifies a transformation; default all")
help="Each FIX specifies a transformation; default: all")
parser.add_option("-x", "--nofix", action="append", default=[],
help="Prevent a fixer from being run.")
parser.add_option("-l", "--list-fixes", action="store_true",
help="List available transformations (fixes/fix_*.py)")
parser.add_option("-p", "--print-function", action="store_true",
@ -48,10 +71,14 @@ def main(fixer_pkg, args=None):
help="More verbose logging")
parser.add_option("-w", "--write", action="store_true",
help="Write back modified files")
parser.add_option("-n", "--nobackups", action="store_true", default=False,
help="Don't write backups for modified files.")
# Parse command line arguments
refactor_stdin = False
options, args = parser.parse_args(args)
if not options.write and options.nobackups:
parser.error("Can't use -n without -w")
if options.list_fixes:
print "Available transformations for the -f/--fix option:"
for fixname in refactor.get_all_fix_names(fixer_pkg):
@ -74,15 +101,22 @@ def main(fixer_pkg, args=None):
# Initialize the refactoring tool
rt_opts = {"print_function" : options.print_function}
avail_names = refactor.get_fixers_from_package(fixer_pkg)
explicit = []
avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
explicit = set()
if options.fix:
explicit = [fixer_pkg + ".fix_" + fix
for fix in options.fix if fix != "all"]
fixer_names = avail_names if "all" in options.fix else explicit
all_present = False
for fix in options.fix:
if fix == "all":
all_present = True
else:
explicit.add(fixer_pkg + ".fix_" + fix)
requested = avail_fixes.union(explicit) if all_present else explicit
else:
fixer_names = avail_names
rt = StdoutRefactoringTool(fixer_names, rt_opts, explicit=explicit)
requested = avail_fixes.union(explicit)
fixer_names = requested.difference(unwanted_fixes)
rt = StdoutRefactoringTool(sorted(fixer_names), rt_opts, sorted(explicit),
options.nobackups)
# Refactor all files and directories passed as arguments
if not rt.errors:

View file

@ -36,9 +36,7 @@ def get_all_fix_names(fixer_pkg, remove_prefix=True):
pkg = __import__(fixer_pkg, [], [], ["*"])
fixer_dir = os.path.dirname(pkg.__file__)
fix_names = []
names = os.listdir(fixer_dir)
names.sort()
for name in names:
for name in sorted(os.listdir(fixer_dir)):
if name.startswith("fix_") and name.endswith(".py"):
if remove_prefix:
name = name[4:]
@ -253,7 +251,7 @@ def refactor_string(self, data, name):
there were errors during the parse.
"""
try:
tree = self.driver.parse_string(data,1)
tree = self.driver.parse_string(data)
except Exception, err:
self.log_error("Can't parse %s: %s: %s",
name, err.__class__.__name__, err)
@ -352,23 +350,13 @@ def processed_file(self, new_text, filename, old_text=None, write=False):
else:
self.log_debug("Not writing changes to %s", filename)
def write_file(self, new_text, filename, old_text=None):
def write_file(self, new_text, filename, old_text):
"""Writes a string to a file.
It first shows a unified diff between the old text and the new text, and
then rewrites the file; the latter is only done if the write option is
set.
"""
backup = filename + ".bak"
if os.path.lexists(backup):
try:
os.remove(backup)
except os.error, err:
self.log_message("Can't remove backup %s", backup)
try:
os.rename(filename, backup)
except os.error, err:
self.log_message("Can't rename %s to %s", filename, backup)
try:
f = open(filename, "w")
except os.error, err:

View file

@ -1,5 +1,6 @@
Files in this directory:
In this directory:
- py2_test_grammar.py -- test file that exercises most/all of Python 2.x's grammar.
- py3_test_grammar.py -- test file that exercises most/all of Python 3.x's grammar.
- infinite_recursion.py -- test file that causes lib2to3's faster recursive pattern matching
scheme to fail, but passes when lib2to3 falls back to iterative pattern matching.
- fixes/ -- for use by test_refactor.py

View file

@ -3385,6 +3385,134 @@ def test_prefix(self):
"""
self.check_both(b, a)
class Test_set_literal(FixerTestCase):
fixer = "set_literal"
def test_basic(self):
b = """set([1, 2, 3])"""
a = """{1, 2, 3}"""
self.check(b, a)
b = """set((1, 2, 3))"""
a = """{1, 2, 3}"""
self.check(b, a)
b = """set((1,))"""
a = """{1}"""
self.check(b, a)
b = """set([1])"""
self.check(b, a)
b = """set((a, b))"""
a = """{a, b}"""
self.check(b, a)
b = """set([a, b])"""
self.check(b, a)
b = """set((a*234, f(args=23)))"""
a = """{a*234, f(args=23)}"""
self.check(b, a)
b = """set([a*23, f(23)])"""
a = """{a*23, f(23)}"""
self.check(b, a)
b = """set([a-234**23])"""
a = """{a-234**23}"""
self.check(b, a)
def test_listcomps(self):
b = """set([x for x in y])"""
a = """{x for x in y}"""
self.check(b, a)
b = """set([x for x in y if x == m])"""
a = """{x for x in y if x == m}"""
self.check(b, a)
b = """set([x for x in y for a in b])"""
a = """{x for x in y for a in b}"""
self.check(b, a)
b = """set([f(x) - 23 for x in y])"""
a = """{f(x) - 23 for x in y}"""
self.check(b, a)
def test_whitespace(self):
b = """set( [1, 2])"""
a = """{1, 2}"""
self.check(b, a)
b = """set([1 , 2])"""
a = """{1 , 2}"""
self.check(b, a)
b = """set([ 1 ])"""
a = """{ 1 }"""
self.check(b, a)
b = """set( [1] )"""
a = """{1}"""
self.check(b, a)
b = """set([ 1, 2 ])"""
a = """{ 1, 2 }"""
self.check(b, a)
b = """set([x for x in y ])"""
a = """{x for x in y }"""
self.check(b, a)
b = """set(
[1, 2]
)
"""
a = """{1, 2}\n"""
self.check(b, a)
def test_comments(self):
b = """set((1, 2)) # Hi"""
a = """{1, 2} # Hi"""
self.check(b, a)
# This isn't optimal behavior, but the fixer is optional.
b = """
# Foo
set( # Bar
(1, 2)
)
"""
a = """
# Foo
{1, 2}
"""
self.check(b, a)
def test_unchanged(self):
s = """set()"""
self.unchanged(s)
s = """set(a)"""
self.unchanged(s)
s = """set(a, b, c)"""
self.unchanged(s)
# Don't transform generators because they might have to be lazy.
s = """set(x for x in y)"""
self.unchanged(s)
s = """set(x for x in y if z)"""
self.unchanged(s)
s = """set(a*823-23**2 + f(23))"""
self.unchanged(s)
class Test_sys_exc(FixerTestCase):
fixer = "sys_exc"

View file

@ -353,29 +353,29 @@ def testBasicPatterns(self):
# Build a pattern matching a leaf
pl = pytree.LeafPattern(100, "foo", name="pl")
r = {}
self.assertEqual(pl.match(root, results=r), False)
self.assertFalse(pl.match(root, results=r))
self.assertEqual(r, {})
self.assertEqual(pl.match(n1, results=r), False)
self.assertFalse(pl.match(n1, results=r))
self.assertEqual(r, {})
self.assertEqual(pl.match(n2, results=r), False)
self.assertFalse(pl.match(n2, results=r))
self.assertEqual(r, {})
self.assertEqual(pl.match(l1, results=r), True)
self.assertTrue(pl.match(l1, results=r))
self.assertEqual(r, {"pl": l1})
r = {}
self.assertEqual(pl.match(l2, results=r), False)
self.assertFalse(pl.match(l2, results=r))
self.assertEqual(r, {})
# Build a pattern matching a node
pn = pytree.NodePattern(1000, [pl], name="pn")
self.assertEqual(pn.match(root, results=r), False)
self.assertFalse(pn.match(root, results=r))
self.assertEqual(r, {})
self.assertEqual(pn.match(n1, results=r), False)
self.assertFalse(pn.match(n1, results=r))
self.assertEqual(r, {})
self.assertEqual(pn.match(n2, results=r), True)
self.assertTrue(pn.match(n2, results=r))
self.assertEqual(r, {"pn": n2, "pl": l3})
r = {}
self.assertEqual(pn.match(l1, results=r), False)
self.assertFalse(pn.match(l1, results=r))
self.assertEqual(r, {})
self.assertEqual(pn.match(l2, results=r), False)
self.assertFalse(pn.match(l2, results=r))
self.assertEqual(r, {})
def testWildcardPatterns(self):
@ -391,11 +391,11 @@ def testWildcardPatterns(self):
pn = pytree.NodePattern(1000, [pl], name="pn")
pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
r = {}
self.assertEqual(pw.match_seq([root], r), False)
self.assertFalse(pw.match_seq([root], r))
self.assertEqual(r, {})
self.assertEqual(pw.match_seq([n1], r), False)
self.assertFalse(pw.match_seq([n1], r))
self.assertEqual(r, {})
self.assertEqual(pw.match_seq([n2], r), True)
self.assertTrue(pw.match_seq([n2], r))
# These are easier to debug
self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
self.assertEqual(r["pl"], l1)
@ -404,7 +404,7 @@ def testWildcardPatterns(self):
# But this is equivalent
self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
r = {}
self.assertEqual(pw.match_seq([l1, l3], r), True)
self.assertTrue(pw.match_seq([l1, l3], r))
self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
self.assert_(r["pl"] is l3)
r = {}

View file

@ -123,7 +123,6 @@ def print_output(self, lines):
def test_refactor_file(self):
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
backup = test_file + ".bak"
old_contents = open(test_file, "r").read()
rt = self.rt()
@ -133,14 +132,8 @@ def test_refactor_file(self):
rt.refactor_file(test_file, True)
try:
self.assertNotEqual(old_contents, open(test_file, "r").read())
self.assertTrue(os.path.exists(backup))
self.assertEqual(old_contents, open(backup, "r").read())
finally:
open(test_file, "w").write(old_contents)
try:
os.unlink(backup)
except OSError:
pass
def test_refactor_docstring(self):
rt = self.rt()

View file

@ -1,21 +1,13 @@
"""optparse - a powerful, extensible, and easy-to-use option parser.
"""A powerful, extensible, and easy-to-use option parser.
By Greg Ward <gward@python.net>
Originally distributed as Optik; see http://optik.sourceforge.net/ .
If you have problems with this module, please do not file bugs,
patches, or feature requests with Python; instead, use Optik's
SourceForge project page:
http://sourceforge.net/projects/optik
Originally distributed as Optik.
For support, use the optik-users@lists.sourceforge.net mailing list
(http://lists.sourceforge.net/lists/listinfo/optik-users).
"""
# Python developers: please do not make changes to this file, since
# it is automatically generated from the Optik source code.
__version__ = "1.5.3"
__all__ = ['Option',
@ -1274,9 +1266,19 @@ def set_usage(self, usage):
self.usage = usage
def enable_interspersed_args(self):
"""Set parsing to not stop on the first non-option, allowing
interspersing switches with command arguments. This is the
default behavior. See also disable_interspersed_args() and the
class documentation description of the attribute
allow_interspersed_args."""
self.allow_interspersed_args = True
def disable_interspersed_args(self):
"""Set parsing to stop on the first non-option. Use this if
you have a command processor which runs another command that
has options of its own and you want to make sure these options
don't get confused.
"""
self.allow_interspersed_args = False
def set_process_default_values(self, process):

View file

@ -196,6 +196,17 @@ def test_vsBuiltinSort(self, n=500):
def test_backcompatibility(self):
self.assertEqual(self.module.insort, self.module.insort_right)
def test_listDerived(self):
class List(list):
data = []
def insert(self, index, item):
self.data.insert(index, item)
lst = List()
self.module.insort_left(lst, 10)
self.module.insort_right(lst, 5)
self.assertEqual([5, 10], lst.data)
class TestInsortPython(TestInsort):
module = py_bisect

View file

@ -397,7 +397,7 @@ def test_rpartition(self):
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
def test_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
b = self.type2test(b)
ps = pickle.dumps(b, proto)
@ -947,7 +947,7 @@ def test_pickle(self):
a = ByteArraySubclass(b"abcd")
a.x = 10
a.y = ByteArraySubclass(b"efgh")
for proto in range(pickle.HIGHEST_PROTOCOL):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
b = pickle.loads(pickle.dumps(a, proto))
self.assertNotEqual(id(a), id(b))
self.assertEqual(a, b)

View file

@ -1136,14 +1136,10 @@ def __init__(self):
def __del__(self_):
self.assertEqual(self_.a, 1)
self.assertEqual(self_.b, 2)
save_stderr = sys.stderr
sys.stderr = sys.stdout
h = H()
try:
with test_support.captured_output('stderr') as s:
h = H()
del h
finally:
sys.stderr = save_stderr
self.assertEqual(s.getvalue(), '')
def test_slots_special(self):
# Testing __dict__ and __weakref__ in __slots__...

View file

@ -7,7 +7,7 @@
from array import array
from weakref import proxy
from test.test_support import TESTFN, findfile, run_unittest
from test.test_support import TESTFN, findfile, check_warnings, run_unittest
from UserList import UserList
import _fileio
@ -241,6 +241,14 @@ def testAppend(self):
def testInvalidInit(self):
self.assertRaises(TypeError, _fileio._FileIO, "1", 0, 0)
def testWarnings(self):
with check_warnings() as w:
self.assertEqual(w.warnings, [])
self.assertRaises(TypeError, _fileio._FileIO, [])
self.assertEqual(w.warnings, [])
self.assertRaises(ValueError, _fileio._FileIO, "/some/invalid/name", "rt")
self.assertEqual(w.warnings, [])
def test_main():
# Historically, these tests have been sloppy about removing TESTFN.

View file

@ -1236,13 +1236,6 @@ def testImport__all__(self):
else:
self.assert_(issubclass(obj, io.IOBase))
def test_fileio_warnings(self):
with test_support.check_warnings() as w:
self.assertEqual(w.warnings, [])
self.assertRaises(TypeError, io.FileIO, [])
self.assertEqual(w.warnings, [])
self.assertRaises(ValueError, io.FileIO, "/some/invalid/name", "rt")
self.assertEqual(w.warnings, [])
def test_main():
test_support.run_unittest(IOTest, BytesIOTest, StringIOTest,

View file

@ -98,7 +98,6 @@ def test_iter(self):
class ProxyTests(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
# Save all proxy related env vars
self._saved_environ = dict([(k, v) for k, v in os.environ.iteritems()
if k.lower().find('proxy') >= 0])
@ -107,9 +106,8 @@ def setUp(self):
del os.environ[k]
def tearDown(self):
unittest.TestCase.tearDown(self)
# Restore all proxy related env vars
for k, v in self._saved_environ:
for k, v in self._saved_environ.iteritems():
os.environ[k] = v
def test_getproxies_environment_keep_no_proxies(self):

View file

@ -61,7 +61,7 @@ def test_xrange(self):
def test_pickling(self):
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
(13, 21, 3), (-2, 2, 2)]
for proto in range(pickle.HIGHEST_PROTOCOL):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for t in testcases:
r = xrange(*t)
self.assertEquals(list(pickle.loads(pickle.dumps(r, proto))),

View file

@ -49,6 +49,8 @@ Library
- Issue #4092: Return ArgInfo as promised in the documentation from
inspect.getargvalues.
- Issue #3935: Properly support list subclasses in bisect's C implementation.
- Issue #4014: Don't claim that Python has an Alpha release status, in addition
to claiming it is Mature.

View file

@ -82,7 +82,7 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw)
index = internal_bisect_right(list, item, lo, hi);
if (index < 0)
return NULL;
if (PyList_Check(list)) {
if (PyList_CheckExact(list)) {
if (PyList_Insert(list, index, item) < 0)
return NULL;
} else {
@ -183,7 +183,7 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw)
index = internal_bisect_left(list, item, lo, hi);
if (index < 0)
return NULL;
if (PyList_Check(list)) {
if (PyList_CheckExact(list)) {
if (PyList_Insert(list, index, item) < 0)
return NULL;
} else {

View file

@ -251,7 +251,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
self->closefd = 1;
if (!closefd) {
PyErr_SetString(PyExc_ValueError,
"Cannot use closefd=True with file name");
"Cannot use closefd=False with file name");
goto error;
}

View file

@ -179,11 +179,13 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
if (errmsg_fn == NULL)
return;
Py_XDECREF(decoder);
Py_DECREF(decoder);
}
pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
PyErr_SetObject(PyExc_ValueError, pymsg);
Py_DECREF(pymsg);
if (pymsg) {
PyErr_SetObject(PyExc_ValueError, pymsg);
Py_DECREF(pymsg);
}
/*
def linecol(doc, pos):
@ -269,6 +271,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict)
goto bail;
}
if (PyList_Append(chunks, chunk)) {
Py_DECREF(chunk);
goto bail;
}
Py_DECREF(chunk);
@ -370,6 +373,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict)
goto bail;
}
if (PyList_Append(chunks, chunk)) {
Py_DECREF(chunk);
goto bail;
}
Py_DECREF(chunk);
@ -379,8 +383,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict)
if (rval == NULL) {
goto bail;
}
Py_DECREF(chunks);
chunks = NULL;
Py_CLEAR(chunks);
return Py_BuildValue("(Nn)", rval, end);
bail:
Py_XDECREF(chunks);
@ -429,6 +432,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict)
goto bail;
}
if (PyList_Append(chunks, chunk)) {
Py_DECREF(chunk);
goto bail;
}
Py_DECREF(chunk);
@ -530,6 +534,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict)
goto bail;
}
if (PyList_Append(chunks, chunk)) {
Py_DECREF(chunk);
goto bail;
}
Py_DECREF(chunk);
@ -539,8 +544,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict)
if (rval == NULL) {
goto bail;
}
Py_DECREF(chunks);
chunks = NULL;
Py_CLEAR(chunks);
return Py_BuildValue("(Nn)", rval, end);
bail:
Py_XDECREF(chunks);

View file

@ -2065,12 +2065,12 @@ PyDoc_STRVAR(setdefault_doc__,
"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
PyDoc_STRVAR(pop__doc__,
"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
If key is not found, d is returned if given, otherwise KeyError is raised");
PyDoc_STRVAR(popitem__doc__,
"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
2-tuple; but raise KeyError if D is empty");
2-tuple; but raise KeyError if D is empty.");
PyDoc_STRVAR(keys__doc__,
"D.keys() -> list of D's keys");
@ -2082,8 +2082,10 @@ PyDoc_STRVAR(values__doc__,
"D.values() -> list of D's values");
PyDoc_STRVAR(update__doc__,
"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
In either case, this is followed by: for k in F: D[k] = F[k]");
PyDoc_STRVAR(fromkeys__doc__,
"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\

View file

@ -824,12 +824,30 @@ def visitModule(self, mod):
return 0;
}
static int add_ast_fields(void)
{
PyObject *empty_tuple, *d;
if (PyType_Ready(&AST_type) < 0)
return -1;
d = AST_type.tp_dict;
empty_tuple = PyTuple_New(0);
if (!empty_tuple ||
PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
Py_XDECREF(empty_tuple);
return -1;
}
Py_DECREF(empty_tuple);
return 0;
}
""", 0, reflow=False)
self.emit("static int init_types(void)",0)
self.emit("{", 0)
self.emit("static int initialized;", 1)
self.emit("if (initialized) return 1;", 1)
self.emit("if (add_ast_fields() < 0) return 0;", 1)
for dfn in mod.dfns:
self.visit(dfn)
self.emit("initialized = 1;", 1)

View file

@ -615,11 +615,29 @@ static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
return 0;
}
static int add_ast_fields(void)
{
PyObject *empty_tuple, *d;
if (PyType_Ready(&AST_type) < 0)
return -1;
d = AST_type.tp_dict;
empty_tuple = PyTuple_New(0);
if (!empty_tuple ||
PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
Py_XDECREF(empty_tuple);
return -1;
}
Py_DECREF(empty_tuple);
return 0;
}
static int init_types(void)
{
static int initialized;
if (initialized) return 1;
if (add_ast_fields() < 0) return 0;
mod_type = make_type("mod", &AST_type, NULL, 0);
if (!mod_type) return 0;
if (!add_attributes(mod_type, NULL, 0)) return 0;

View file

@ -12,7 +12,7 @@
# our pysource module finds Python source files
try:
import pysource
except:
except ImportError:
# emulate the module with a simple os.walk
class pysource:
has_python_ext = looks_like_python = can_be_compiled = None

View file

@ -99,7 +99,8 @@ def check(file):
for name in names:
fullname = os.path.join(file, name)
if ((recurse and os.path.isdir(fullname) and
not os.path.islink(fullname))
not os.path.islink(fullname) and
not os.path.split(fullname)[1].startswith("."))
or name.lower().endswith(".py")):
check(fullname)
return