Commit 8457b69c authored by Georg Brandl's avatar Georg Brandl

Merged revisions...

Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines

  Added a note to [] that special forms & special chars lose their meaning
  and backrefs can't be used inside []
........
  r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines

  #3010: clarification about stdin/use_rawinput.
........
  r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines

  #3005: add explaining sentence to easydialogs docs.
........
  r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines

  Add plain text make target.
........
  r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines

  Spaces vs. tabs.
........
  r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines

  Make the _H #define's match the header file names.  Fix comments to
  mention the correct type names.
........
  r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines

  Adds a Thread.getIdent() method to provide the _get_ident() value for
  any given threading.Thread object.  feature request issue 2871.
........
  r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines

  Fixed complex.__getnewargs__() to not emit another complex object.
........
  r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line

  Document 'utc' parameter
........
  r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line

  Add various items
........
  r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line

  Grammar fix
........
  r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines

  Markup fix.
........
  r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines

  Add a reminder for the maintainer of whatsnew.
........
  r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines

  Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
  to ease standalone use of the algorithm.
........
  r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines

  Revert unwanted changes.
........
  r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines

  X-ref to numbers module.
........
  r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines

  Document the "st" API, to avoid confusion with the "new" AST.
  Add a note about using the new AST module.
........
  r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines

  Add Gregor Lingl.
........
  r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines

  Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
  r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line

  Add Arnaud for his efforts on multi-arg set operations.
........
  r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines

  #2536: fix itertools.permutations and itertools.combinations docstrings.
........
parent 1497d8e3
......@@ -157,6 +157,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Bernhard Reiter
* Armin Rigo
* Wes Rishel
* Armin Ronacher
* Jim Roskind
* Guido van Rossum
* Donald Wallace Rouse II
......
......@@ -21,6 +21,7 @@ help:
@echo " web to make file usable by Sphinx.web"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " text to make plain text files"
@echo " changes to make an overview over all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " coverage to check documentation coverage for library and C API"
......@@ -75,6 +76,10 @@ latex: build
@echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \
"run these through (pdf)latex."
text: BUILDER = text
text: build
@echo "Build finished; the text files are in build/text."
changes: BUILDER = changes
changes: build
@echo "The overview file is in build/changes."
......
......@@ -56,6 +56,8 @@ Available make targets are:
* "latex", which builds LaTeX source files that can be run with "pdflatex"
to produce PDF documents.
* "text", which builds a plain text file for each source file.
* "linkcheck", which checks all external references to see whether they are
broken, redirected or malformed, and outputs this information to stdout
as well as a plain-text (.txt) file.
......
.. _ast:
Abstract Syntax Trees
=====================
.. module:: _ast
:synopsis: Abstract Syntax Tree classes.
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
The ``_ast`` module helps Python applications to process trees of the Python
abstract syntax grammar. The abstract syntax itself might change with each
Python release; this module helps to find out programmatically what the current
grammar looks like.
An abstract syntax tree can be generated by passing :data:`_ast.PyCF_ONLY_AST`
as a flag to the :func:`compile` builtin function. The result will be a tree of
objects whose classes all inherit from :class:`_ast.AST`.
A modified abstract syntax tree can be compiled into a Python code object using
the built-in :func:`compile` function.
The actual classes are derived from the ``Parser/Python.asdl`` file, which is
reproduced below. There is one class defined for each left-hand side symbol in
the abstract grammar (for example, ``_ast.stmt`` or ``_ast.expr``). In addition,
there is one class defined for each constructor on the right-hand side; these
classes inherit from the classes for the left-hand side trees. For example,
``_ast.BinOp`` inherits from ``_ast.expr``. For production rules with
alternatives (aka "sums"), the left-hand side class is abstract: only instances
of specific constructor nodes are ever created.
Each concrete class has an attribute ``_fields`` which gives the names of all
child nodes.
Each instance of a concrete class has one attribute for each child node, of the
type as defined in the grammar. For example, ``_ast.BinOp`` instances have an
attribute ``left`` of type ``_ast.expr``. Instances of ``_ast.expr`` and
``_ast.stmt`` subclasses also have lineno and col_offset attributes. The lineno
is the line number of source text (1 indexed so the first line is line 1) and
the col_offset is the utf8 byte offset of the first token that generated the
node. The utf8 offset is recorded because the parser uses utf8 internally.
If these attributes are marked as optional in the grammar (using a question
mark), the value might be ``None``. If the attributes can have zero-or-more
values (marked with an asterisk), the values are represented as Python lists.
All possible attributes must be present and have valid values when compiling an
AST with :func:`compile`.
The constructor of a class ``_ast.T`` parses their arguments as follows:
* If there are positional arguments, there must be as many as there are items in
``T._fields``; they will be assigned as attributes of these names.
* If there are keyword arguments, they will set the attributes of the same names
to the given values.
For example, to create and populate a ``UnaryOp`` node, you could use ::
node = _ast.UnaryOp()
node.op = _ast.USub()
node.operand = _ast.Num()
node.operand.n = 5
node.operand.lineno = 0
node.operand.col_offset = 0
node.lineno = 0
node.col_offset = 0
or the more compact ::
node = _ast.UnaryOp(_ast.USub(), _ast.Num(5, lineno=0, col_offset=0),
lineno=0, col_offset=0)
Abstract Grammar
----------------
The module defines a string constant ``__version__`` which is the decimal
subversion revision number of the file shown below.
The abstract grammar is currently defined as follows:
.. literalinclude:: ../../Parser/Python.asdl
......@@ -9,9 +9,9 @@
.. much of the content adapted from docstrings
This module provides the infrastructure for defining abstract base classes
(ABCs) in Python, as outlined in :pep:`3119`; see the PEP for why this
was added to Python. (See also, :pep:`3141` regarding a type hierarchy
for numbers based on ABCs.)
(ABCs) in Python, as outlined in :pep:`3119`; see the PEP for why this was added
to Python. (See also :pep:`3141` and the :mod:`numbers` module regarding a type
hierarchy for numbers based on ABCs.)
The :mod:`collections` module has some concrete classes that derive from
ABCs; these can, of course, be further derived. In addition the
......
.. _ast:
Abstract Syntax Trees
=====================
.. module:: ast
:synopsis: Abstract Syntax Tree classes and manipulation.
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
.. sectionauthor:: Georg Brandl <georg@python.org>
.. versionadded:: 2.5
The low-level ``_ast`` module containing only the node classes.
.. versionadded:: 2.6
The high-level ``ast`` module containing all helpers.
The :mod:`ast` module helps Python applications to process trees of the Python
abstract syntax grammar. The abstract syntax itself might change with each
Python release; this module helps to find out programmatically what the current
grammar looks like.
An abstract syntax tree can be generated by passing :data:`_ast.PyCF_ONLY_AST`
as a flag to the :func:`compile` builtin function, or using the :func:`parse`
helper provided in this module. The result will be a tree of objects whose
classes all inherit from :class:`ast.AST`.
A modified abstract syntax tree can be compiled into a Python code object using
the built-in :func:`compile` function.
Node classes
------------
.. class:: AST
This is the base of all AST node classes. The actual node classes are
derived from the :file:`Parser/Python.asdl` file, which is reproduced
:ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
module and re-exported in :mod:`ast`.
There is one class defined for each left-hand side symbol in the abstract
grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
there is one class defined for each constructor on the right-hand side; these
classes inherit from the classes for the left-hand side trees. For example,
:class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
with alternatives (aka "sums"), the left-hand side class is abstract: only
instances of specific constructor nodes are ever created.
.. attribute:: _fields
Each concrete class has an attribute :attr:`_fields` which gives the names
of all child nodes.
Each instance of a concrete class has one attribute for each child node,
of the type as defined in the grammar. For example, :class:`ast.BinOp`
instances have an attribute :attr:`left` of type :class:`ast.expr`.
If these attributes are marked as optional in the grammar (using a
question mark), the value might be ``None``. If the attributes can have
zero-or-more values (marked with an asterisk), the values are represented
as Python lists. All possible attributes must be present and have valid
values when compiling an AST with :func:`compile`.
.. attribute:: lineno
col_offset
Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
:attr:`lineno` and :attr:`col_offset` attributes. The :attr:`lineno` is
the line number of source text (1-indexed so the first line is line 1) and
the :attr:`col_offset` is the UTF-8 byte offset of the first token that
generated the node. The UTF-8 offset is recorded because the parser uses
UTF-8 internally.
The constructor of a class :class:`ast.T` parses its arguments as follows:
* If there are positional arguments, there must be as many as there are items
in :attr:`T._fields`; they will be assigned as attributes of these names.
* If there are keyword arguments, they will set the attributes of the same
names to the given values.
For example, to create and populate an :class:`ast.UnaryOp` node, you could
use ::
node = ast.UnaryOp()
node.op = ast.USub()
node.operand = ast.Num()
node.operand.n = 5
node.operand.lineno = 0
node.operand.col_offset = 0
node.lineno = 0
node.col_offset = 0
or the more compact ::
node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
lineno=0, col_offset=0)
.. _abstract-grammar:
Abstract Grammar
----------------
The module defines a string constant ``__version__`` which is the decimal
Subversion revision number of the file shown below.
The abstract grammar is currently defined as follows:
.. literalinclude:: ../../Parser/Python.asdl
:mod:`ast` Helpers
------------------
.. versionadded:: 2.6
Apart from the node classes, :mod:`ast` module defines these utility functions
and classes for traversing abstract syntax trees:
.. function:: parse(expr, filename='<unknown>', mode='exec')
Parse an expression into an AST node. Equivalent to ``compile(expr,
filename, mode, PyCF_ONLY_AST)``.
.. function:: literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and ``None``.
This can be used for safely evaluating strings containing Python expressions
from untrusted sources without the need to parse the values oneself.
.. function:: get_docstring(node, clean=True):
Return the docstring of the given *node* (which must be a
:class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None``
if it has no docstring. If *clean* is true, clean up the docstring's
indentation with :func:`inspect.cleandoc`.
.. function:: fix_missing_locations(node)
When you compile a node tree with :func:`compile`, the compiler expects
:attr:`lineno` and :attr:`col_offset` attributes for every node that supports
them. This is rather tedious to fill in for generated nodes, so this helper
adds these attributes recursively where not already set, by setting them to
the values of the parent node. It works recursively starting at *node*.
.. function:: increment_lineno(node, n=1)
Increment the line number of each node in the tree starting at *node* by *n*.
This is useful to "move code" to a different location in a file.
.. function:: copy_location(new_node, old_node)
Copy source location (:attr:`lineno` and :attr:`col_offset`) from *old_node*
to *new_node* if possible, and return *new_node*.
.. function:: iter_fields(node)
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
that is present on *node*.
.. function:: iter_child_nodes(node)
Yield all direct child nodes of *node*, that is, all fields that are nodes
and all items of fields that are lists of nodes.
.. function:: walk(node)
Recursively yield all child nodes of *node*, in no specified order. This is
useful if you only want to modify nodes in place and don't care about the
context.
.. class:: NodeVisitor()
A node visitor base class that walks the abstract syntax tree and calls a
visitor function for every node found. This function may return a value
which is forwarded by the `visit` method.
This class is meant to be subclassed, with the subclass adding visitor
methods.
.. method:: visit(node)
Visit a node. The default implementation calls the method called
:samp:`self.visit_{classname}` where *classname* is the name of the node
class, or :meth:`generic_visit` if that method doesn't exist.
.. method:: generic_visit(node)
This visitor calls :meth:`visit` on all children of the node.
Note that child nodes of nodes that have a custom visitor method won't be
visited unless the visitor calls :meth:`generic_visit` or visits them
itself.
Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
during traversal. For this a special visitor exists
(:class:`NodeTransformer`) that allows modifications.
.. class:: NodeTransformer()
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
allows modification of nodes.
The `NodeTransformer` will walk the AST and use the return value of the
visitor methods to replace or remove the old node. If the return value of
the visitor method is ``None``, the node will be removed from its location,
otherwise it is replaced with the return value. The return value may be the
original node in which case no replacement takes place.
Here is an example transformer that rewrites all occurrences of name lookups
(``foo``) to ``data['foo']``::
class RewriteName(NodeTransformer):
def visit_Name(self, node):
return copy_location(Subscript(
value=Name(id='data', ctx=Load()),
slice=Index(value=Str(s=node.id)),
ctx=node.ctx
), node)
Keep in mind that if the node you're operating on has child nodes you must
either transform the child nodes yourself or call the :meth:`generic_visit`
method for the node first.
For nodes that were part of a collection of statements (that applies to all
statement nodes), the visitor may also return a list of nodes rather than
just a single node.
Usually you use the transformer like this::
node = YourTransformer().visit(node)
.. function:: dump(node, annotate_fields=True, include_attributes=False)
Return a formatted dump of the tree in *node*. This is mainly useful for
debugging purposes. The returned string will show the names and the values
for fields. This makes the code impossible to evaluate, so if evaluation is
wanted *annotate_fields* must be set to False. Attributes such as line
numbers and column offsets are dumped by default. If this is wanted,
*include_attributes* can be set to ``True``.
......@@ -26,7 +26,12 @@ interface.
The optional arguments *stdin* and *stdout* specify the input and output file
objects that the Cmd instance or subclass instance will use for input and
output. If not specified, they will default to *sys.stdin* and *sys.stdout*.
output. If not specified, they will default to :data:`sys.stdin` and
:data:`sys.stdout`.
If you want a given *stdin* to be used, make sure to set the instance's
:attr:`use_rawinput` attribute to ``False``, otherwise *stdin* will be
ignored.
.. _cmd-objects:
......
......@@ -320,13 +320,9 @@ attributes:
Retrieving source code
----------------------
.. function:: getdoc(object)
Get the documentation string for an object. All tabs are expanded to spaces. To
clean up docstrings that are indented to line up with blocks of code, any
whitespace than can be uniformly removed from the second line onwards is
removed.
Get the documentation string for an object, cleaned up with :func:`cleandoc`.
.. function:: getcomments(object)
......@@ -373,6 +369,15 @@ Retrieving source code
cannot be retrieved.
.. function:: cleandoc(doc)
Clean up indentation from docstrings that are indented to line up with blocks
of code. Any whitespace that can be uniformly removed from the second line
onwards is removed. Also, all tabs are expanded to spaces.
.. versionadded:: 2.6
.. _inspect-classes-functions:
Classes and functions
......
......@@ -15,7 +15,7 @@ These modules include:
.. toctree::
parser.rst
_ast.rst
ast.rst
symbol.rst
token.rst
keyword.rst
......
......@@ -1618,7 +1618,7 @@ The :class:`TimedRotatingFileHandler` class, located in the
timed intervals.
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount[, encoding[, delay]]]]])
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount[, encoding[, delay[, utc]]]]]])
Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
specified file is opened and used as the stream for logging. On rotating it also
......@@ -1626,7 +1626,7 @@ timed intervals.
*interval*.
You can use the *when* to specify the type of *interval*. The list of possible
values is, note that they are not case sensitive:
values is below. Note that they are not case sensitive.
+----------------+-----------------------+
| Value | Type of interval |
......@@ -1647,7 +1647,11 @@ timed intervals.
The system will save old log files by appending extensions to the filename.
The extensions are date-and-time based, using the strftime format
``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
rollover interval. If *backupCount* is nonzero, at most *backupCount* files
rollover interval.
If the *utc* argument is true, times in UTC will be used; otherwise
local time is used.
If *backupCount* is nonzero, at most *backupCount* files
will be kept, and if more would be created when rollover occurs, the oldest
one is deleted. The deletion logic uses the interval to determine which
files to delete, so changing the interval may leave old files lying around.
......
This diff is collapsed.
......@@ -643,6 +643,17 @@ impossible to detect the termination of alien threads.
constructor.
.. method:: Thread.getIdent()
Return the 'thread identifier' of this thread or None if the thread has not
been started. This is a nonzero integer. See the :mod:`thread` module's
:func:`get_ident()` function. Thread identifiers may be recycled when a
thread exits and another thread is created. The identifier is returned
even after the thread has exited.
.. versionadded:: 2.6
.. method:: Thread.isAlive()
Return whether the thread is alive.
......
......@@ -2205,10 +2205,10 @@ Port-Specific Changes
* MacOS X (10.3 and higher): dynamic loading of modules now uses the
:cfunc:`dlopen` function instead of MacOS-specific functions.
* MacOS X: a :option:`--enable-universalsdk` switch was added to the
* MacOS X: an :option:`--enable-universalsdk` switch was added to the
:program:`configure` script that compiles the interpreter as a universal binary
able to run on both PowerPC and Intel processors. (Contributed by Ronald
Oussoren.)
Oussoren; :issue:`2573`.)
* Windows: :file:`.dll` is no longer supported as a filename extension for
extension modules. :file:`.pyd` is now the only filename extension that will be
......
......@@ -648,6 +648,7 @@ the type's :meth:`__format__` method with the provided specifier::
>>> format(75.6564, '.2f')
'75.66'
.. seealso::
:ref:`formatstrings`
......@@ -1252,6 +1253,11 @@ Here are all of the changes that Python 2.6 makes to the core Python language.
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
* A new built-in, ``next(*iterator*, [*default*])`` returns the next item
from the specified iterator. If the *default* argument is supplied,
it will be returned if *iterator* has been exhausted; otherwise,
the :exc:`StopIteration` exception will be raised. (:issue:`2719`)
* Tuples now have an :meth:`index` method matching the list type's
:meth:`index` method::
......@@ -1553,6 +1559,7 @@ details.
:mod:`terminalcommand`.
A number of old IRIX-specific modules were deprecated:
:mod:`al` and :mod:`AL`,
:mod:`cd`,
:mod:`cddb`,
:mod:`cdplayer`,
......@@ -1664,7 +1671,11 @@ details.
(Contributed by Raymond Hettinger.)
* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
* XXX Describe the new ctypes calling convention that allows safe
access to errno.
(Implemented by Thomas Heller; :issue:`1798`.)
* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
that represents the C99 ``bool`` type. (Contributed by David Remahl;
:issue:`1649190`.)
......@@ -1733,7 +1744,14 @@ details.
to drop the built-in in the 2.x series. (Patched by
Christian Heimes; :issue:`1739906`.)
* The :func:`glob.glob` function can now return Unicode filenames if
* When possible, the :mod:`getpass` module will now use
:file:`/dev/tty` (when available) to print
a prompting message and read the password, falling back to using
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.)
* The :func:`glob.glob` function can now return Unicode filenames if
a Unicode path was used and Unicode filenames are matched within the
directory. (:issue:`1001604`)
......@@ -1752,6 +1770,10 @@ details.
This is more efficient than making a call to :func:`heappush` and then
:func:`heappop`.
:mod:`heapq` is now implemented to only use less-than comparison,
instead of the less-than-or-equal comparison it previously used.
This makes :mod:`heapq`'s usage of a type match that of the
:meth:`list.sort` method.
(Contributed by Raymond Hettinger.)
* An optional ``timeout`` parameter was added to the
......@@ -1846,6 +1868,11 @@ details.
is true, opening of the log file is deferred until the first
:meth:`emit` call is made. (Contributed by Vinay Sajip.)
:class:`TimedRotatingFileHandler` also has a *utc* constructor
parameter. If the argument is true, UTC time will be used
in determining when midnight occurs and in generating filenames;
otherwise local time will be used.
* 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`)
......@@ -2112,13 +2139,21 @@ details.
are written or not.
(Contributed by Neal Norwitz and Georg Brandl.)
Information about the command-line arguments supplied to the Python
interpreter are available as attributes of a ``sys.flags`` named
tuple. For example, the :attr:`verbose` attribute is true if Python
Information about the command-line arguments supplied to the Python
interpreter is available by reading attributes of a named
tuple available as ``sys.flags``. For example, the :attr:`verbose`
attribute is true if Python
was executed in verbose mode, :attr:`debug` is true in debugging mode, etc.
These attributes are all read-only.
(Contributed by Christian Heimes.)
A new function, :func:`getsizeof`, takes a Python object and returns
the amount of memory used by the object, measured in bytes. Built-in
objects return correct results; third-party extensions may not,
but can define a :meth:`__sizeof__` method to return the
object's size.
(Contributed by Robert Schuppenies; :issue:`2898`.)
It's now possible to determine the current profiler and tracer functions
by calling :func:`sys.getprofile` and :func:`sys.gettrace`.
(Contributed by Georg Brandl; :issue:`1648`.)
......@@ -2204,7 +2239,11 @@ details.
(Contributed by Dwayne Bailey; :issue:`1581073`.)
* The :mod:`timeit` module now accepts callables as well as strings
* The :mod:`threading` module's :class:`Thread` objects
gained a :meth:`getIdent` method that returns the thread's
identifier, a nonzero integer. (Contributed by XXX; :issue:`2871`.)
* The :mod:`timeit` module now accepts callables as well as strings
for the statement being timed and for the setup code.
Two convenience functions were added for creating
:class:`Timer` instances:
......@@ -2213,6 +2252,24 @@ details.
the corresponding method. (Contributed by Erik Demaine;
:issue:`1533909`.)
* 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.
* Control over turtle movement using the new delay(),
tracer(), and speed() methods.
* The ability to set new shapes for the turtle, and to
define a new coordinate system.
* Turtles now have an undo() method that can roll back actions.
* Simple support for reacting to input events such as mouse and keyboard
activity, making it possible to write simple games.
* A :file:`turtle.cfg` file can be used to customize the starting appearance
of the turtle's screen.
* The module's docstrings can be replaced by new docstrings that have been
translated into another language.
(:issue:`1513695`)
* An optional ``timeout`` parameter was added to the
:func:`urllib.urlopen` function and the
:class:`urllib.ftpwrapper` class constructor, as well as the
......@@ -2255,8 +2312,10 @@ details.
not necessarily correct for all applications. Code using
:mod:`xmlrpclib` should convert :class:`date` and :class:`time`
instances. (:issue:`1330538`) The code can also handle
dates before 1900. (Contributed by Ralf Schmitt; :issue:`2014`.)
dates before 1900 (contributed by Ralf Schmitt; :issue:`2014`)
and 64-bit integers represented by using ``<i8>`` in XML-RPC responses
(contributed by XXX; :issue:`2985`).
* 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
......@@ -2272,9 +2331,14 @@ details.
(Contributed by Alan McIntyre; :issue:`467924`.)
Also, :mod:`zipfile` now supports using Unicode filenames
for archived files. (Contributed by Alexey Borzenkov; :issue:`1734346`.)
The :meth:`open`, :meth:`read` and :meth:`extract` methods can now
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`.)
Finally, :mod:`zipfile` now supports using Unicode filenames
for archived files. (Contributed by Alexey Borzenkov; :issue:`1734346`.)
.. ======================================================================
.. whole new modules get described in subsections here
......@@ -2469,10 +2533,8 @@ Changes to Python's build process and to the C API include:
results, and then compiles using these results for optimization.
(Contributed by Gregory P. Smith.)
.. ======================================================================
Port-Specific Changes: Windows
-----------------------------------
......@@ -2517,6 +2579,16 @@ Port-Specific Changes: Windows
.. ======================================================================
Port-Specific Changes: MacOS X
-----------------------------------
* 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
:program:`configure` script.
.. ======================================================================
.. _section-other:
......
/* Bytes object interface */
/* ByteArray object interface */
#ifndef Py_BYTESOBJECT_H
#define Py_BYTESOBJECT_H
#ifndef Py_BYTEARRAYOBJECT_H
#define Py_BYTEARRAYOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
......@@ -50,4 +50,4 @@ PyAPI_FUNC(int) PyByteArray_Resize(PyObject *, Py_ssize_t);
#ifdef __cplusplus
}
#endif
#endif /* !Py_BYTESOBJECT_H */
#endif /* !Py_BYTEARRAYOBJECT_H */
/* String object interface */
/* Bytes (String) object interface */
#ifndef Py_STRINGOBJECT_H
#define Py_STRINGOBJECT_H
#ifndef Py_BYTESOBJECT_H
#define Py_BYTESOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
......@@ -107,4 +107,4 @@ PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
#ifdef __cplusplus
}
#endif
#endif /* !Py_STRINGOBJECT_H */
#endif /* !Py_BYTESOBJECT_H */
This diff is collapsed.
......@@ -368,6 +368,13 @@ def getdoc(object):
return None
if not isinstance(doc, str):
return None
return cleandoc(doc)
def cleandoc(doc):
"""Clean up indentation from docstrings.
Any whitespace that can be uniformly removed from the second line
onwards is removed."""
try:
lines = doc.expandtabs().split('\n')
except UnicodeError:
......
import sys, unittest
from test import support
import _ast
import ast
def to_tuple(t):
if t is None or isinstance(t, (str, int, complex)):
......@@ -117,9 +117,9 @@ eval_tests = [
class AST_Tests(unittest.TestCase):
def _assert_order(self, ast_node, parent_pos):
if not isinstance(ast_node, _ast.AST) or ast_node._fields is None:
if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
return
if isinstance(ast_node, (_ast.expr, _ast.stmt, _ast.excepthandler)):
if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
node_pos = (ast_node.lineno, ast_node.col_offset)
self.assert_(node_pos >= parent_pos)
parent_pos = (ast_node.lineno, ast_node.col_offset)
......@@ -136,29 +136,29 @@ class AST_Tests(unittest.TestCase):
(single_tests, single_results, "single"),
(eval_tests, eval_results, "eval")):
for i, o in zip(input, output):
ast_tree = compile(i, "?", kind, _ast.PyCF_ONLY_AST)
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
self.assertEquals(to_tuple(ast_tree), o)
self._assert_order(ast_tree, (0, 0))
def test_nodeclasses(self):
x = _ast.BinOp(1, 2, 3, lineno=0)
x = ast.BinOp(1, 2, 3, lineno=0)
self.assertEquals(x.left, 1)
self.assertEquals(x.op, 2)
self.assertEquals(x.right, 3)
self.assertEquals(x.lineno, 0)
# node raises exception when not given enough arguments
self.assertRaises(TypeError, _ast.BinOp, 1, 2)
self.assertRaises(TypeError, ast.BinOp, 1, 2)
# can set attributes through kwargs too
x = _ast.BinOp(left=1, op=2, right=3, lineno=0)
x = ast.BinOp(left=1, op=2, right=3, lineno=0)
self.assertEquals(x.left, 1)
self.assertEquals(x.op, 2)
self.assertEquals(x.right, 3)
self.assertEquals(x.lineno, 0)
# this used to fail because Sub._fields was None
x = _ast.Sub()
x = ast.Sub()
def test_pickling(self):
import pickle
......@@ -175,8 +175,99 @@ class AST_Tests(unittest.TestCase):
ast2 = mod.loads(mod.dumps(ast, protocol))
self.assertEquals(to_tuple(ast2), to_tuple(ast))
class ASTHelpers_Test(unittest.TestCase):
def test_parse(self):
a = ast.parse('foo(1 + 1)')
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
self.assertEqual(ast.dump(a), ast.dump(b))
def test_dump(self):
node = ast.parse('spam(eggs, "and cheese")')
self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
"keywords=[], starargs=None, kwargs=None))])"
)
self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
"Str('and cheese')], [], None, None))])"
)
self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
"col_offset=11)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
)
def test_copy_location(self):
src = ast.parse('1 + 1', mode='eval')
src.body.right = ast.copy_location(ast.Num(2), src.body.right)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
'col_offset=0))'
)
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
def test_increment_lineno(self):
src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src, n=3), src)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
'col_offset=0))'
)
def test_iter_fields(self):
node = ast.parse('foo()', mode='eval')
d = dict(ast.iter_fields(node.body))
self.assertEqual(d.pop('func').id, 'foo')
self.assertEqual(d, {'keywords': [], 'kwargs': None,
'args': [], 'starargs': None})
def test_iter_child_nodes(self):
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
iterator = ast.iter_child_nodes(node.body)
self.assertEqual(next(iterator).id, 'spam')
self.assertEqual(next(iterator).n, 23)
self.assertEqual(next(iterator).n, 42)
self.assertEqual(ast.dump(next(iterator)),
"keyword(arg='eggs', value=Str(s='leek'))"
)
def test_get_docstring(self):
node = ast.parse('def foo():\n """line one\n line two"""')
self.assertEqual(ast.get_docstring(node.body[0]),
'line one\nline two')
def test_literal_eval(self):
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
self.assertRaises(ValueError, ast.literal_eval, 'foo()')
def test_main():
support.run_unittest(AST_Tests)
support.run_unittest(AST_Tests, ASTHelpers_Test)
def main():
if __name__ != '__main__':
......
......@@ -352,6 +352,14 @@ class ComplexTest(unittest.TestCase):
except (OSError, IOError):
pass
def test_getnewargs(self):
self.assertEqual((1+2j).__getnewargs__(), (1.0, 2.0))
self.assertEqual((1-2j).__getnewargs__(), (1.0, -2.0))
self.assertEqual((2j).__getnewargs__(), (0.0, 2.0))
self.assertEqual((-0j).__getnewargs__(), (0.0, -0.0))
self.assertEqual(complex(0, INF).__getnewargs__(), (0.0, INF))
self.assertEqual(complex(INF, 0).__getnewargs__(), (INF, 0.0))
if float.__getformat__("double").startswith("IEEE"):
def test_plus_minus_0j(self):
# test that -0j and 0j literals are not identified
......
......@@ -195,6 +195,10 @@ class TestRetrievingSourceCode(GetSourceBase):
self.assertEqual(inspect.getdoc(git.abuse),
'Another\n\ndocstring\n\ncontaining\n\ntabs')
def test_cleandoc(self):
self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'),
'An\nindented\ndocstring.')
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
......
......@@ -3,6 +3,7 @@
import test.support
from test.support import verbose
import random
import re
import sys
import threading
import _thread
......@@ -71,6 +72,8 @@ class ThreadTests(unittest.TestCase):
for i in range(NUMTASKS):
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
threads.append(t)
self.failUnlessEqual(t.getIdent(), None)
self.assert_(re.match('<TestThread\(.*, initial\)>', repr(t)))
t.start()
if verbose:
......@@ -78,6 +81,8 @@ class ThreadTests(unittest.TestCase):
for t in threads:
t.join(NUMTASKS)
self.assert_(not t.isAlive())
self.failIfEqual(t.getIdent(), 0)
self.assert_(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
if verbose:
print('all tasks done')
self.assertEqual(numrunning.get(), 0)
......
......@@ -401,6 +401,7 @@ class Thread(_Verbose):
self._args = args
self._kwargs = kwargs
self._daemonic = self._set_daemon()
self._ident = None
self._started = Event()
self._stopped = False
self._block = Condition(Lock())
......@@ -421,7 +422,9 @@ class Thread(_Verbose):
if self._stopped:
status = "stopped"
if self._daemonic:
status = status + " daemon"
status += " daemon"
if self._ident is not None:
status += " %s" % self._ident
return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
def start(self):
......@@ -469,9 +472,10 @@ class Thread(_Verbose):
def _bootstrap_inner(self):
try:
self._ident = _get_ident()
self._started.set()
_active_limbo_lock.acquire()
_active[_get_ident()] = self
_active[self._ident] = self
del _limbo[self]
_active_limbo_lock.release()
if __debug__:
......@@ -536,7 +540,7 @@ class Thread(_Verbose):
with _active_limbo_lock:
self._stop()
try:
# We don't call self.__delete() because it also
# We don't call self._delete() because it also
# grabs _active_limbo_lock.
del _active[_get_ident()]
except:
......@@ -625,6 +629,10 @@ class Thread(_Verbose):
assert self._initialized, "Thread.__init__() not called"
self._name = str(name)
def getIdent(self):
assert self._initialized, "Thread.__init__() not called"
return self._ident
def isAlive(self):
assert self._initialized, "Thread.__init__() not called"
return self._started.isSet() and not self._stopped
......
......@@ -155,6 +155,7 @@ Ben Darnell
Jonathan Dasteel
John DeGood
Vincent Delft
Arnaud Delobelle
Erik Demaine
Roger Dev
Raghuram Devarakonda
......@@ -573,6 +574,7 @@ Andy Robinson
Kevin Rodgers
Giampaolo Rodola
Mike Romberg
Armin Ronacher
Case Roole
Timothy Roscoe
Jim Roskind
......
......@@ -17,6 +17,9 @@ the format to accommodate documentation needs as they arise.
Permissions History
-------------------
- Gregor Lingl was given SVN access on 10 June 2008 by MvL,
for work on the turtle module.
- Robert Schuppenies was given SVN access on 21 May 2008 by MvL,
for GSoC contributions.
......
......@@ -2019,7 +2019,7 @@ empty:
}
PyDoc_STRVAR(combinations_doc,
"combinations(iterables) --> combinations object\n\
"combinations(iterable[, r]) --> combinations object\n\
\n\
Return successive r-length combinations of elements in the iterable.\n\n\
combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
......@@ -2294,10 +2294,10 @@ empty:
}
PyDoc_STRVAR(permutations_doc,
"permutations(iterables[, r]) --> permutations object\n\
"permutations(iterable[, r]) --> permutations object\n\
\n\
Return successive r-length permutations of elements in the iterable.\n\n\
permutations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
static PyTypeObject permutations_type = {
PyVarObject_HEAD_INIT(NULL, 0)
......
......@@ -693,7 +693,8 @@ PyDoc_STRVAR(complex_conjugate_doc,
static PyObject *
complex_getnewargs(PyComplexObject *v)
{
return Py_BuildValue("(D)", &v->cval);
Py_complex c = v->cval;
return Py_BuildValue("(dd)", c.real, c.imag);
}
#if 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment