Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from

svn+ssh://svn.python.org/python/branches/py3k

........
  r82805 | georg.brandl | 2010-07-11 11:42:10 +0200 (So, 11 Jul 2010) | 1 line

  #7935: cross-reference to ast.literal_eval() from eval() docs.
........
  r82806 | georg.brandl | 2010-07-11 12:22:44 +0200 (So, 11 Jul 2010) | 1 line

  #9223: link to Command class reference, and move Command interface docs nearer to class docs.
........
  r83523 | georg.brandl | 2010-08-02 14:06:18 +0200 (Mo, 02 Aug 2010) | 1 line

  #9209 and #7781: fix two crashes in pstats interactive browser.
........
  r83524 | georg.brandl | 2010-08-02 14:20:23 +0200 (Mo, 02 Aug 2010) | 1 line

  #9428: fix running scripts from profile/cProfile with their own name and the right namespace.  Same fix as for trace.py in #1690103.
........
  r83525 | georg.brandl | 2010-08-02 14:36:24 +0200 (Mo, 02 Aug 2010) | 1 line

  Get rid of spurious "threading" entries in trace output.
........
  r83526 | georg.brandl | 2010-08-02 14:40:22 +0200 (Mo, 02 Aug 2010) | 1 line

  Fix softspace relic.
........
  r83527 | georg.brandl | 2010-08-02 14:48:46 +0200 (Mo, 02 Aug 2010) | 1 line

  #3821: beginnings of a trace.py unittest.
........
  r83536 | georg.brandl | 2010-08-02 19:49:25 +0200 (Mo, 02 Aug 2010) | 1 line

  #8578: mention danger of not incref'ing weak referenced object.
........
  r83538 | georg.brandl | 2010-08-02 20:10:13 +0200 (Mo, 02 Aug 2010) | 1 line

  #6928: fix class docs w.r.t. new metaclasses.
........
  r83542 | georg.brandl | 2010-08-02 20:56:54 +0200 (Mo, 02 Aug 2010) | 1 line

  Move test_SimpleHTTPServer into test_httpservers.
........
  r83546 | georg.brandl | 2010-08-02 21:16:34 +0200 (Mo, 02 Aug 2010) | 1 line

  #7973: Fix distutils options spelling.
........
  r83547 | georg.brandl | 2010-08-02 21:19:26 +0200 (Mo, 02 Aug 2010) | 1 line

  #7386: add example that shows that trailing path separators are stripped.
........
  r83548 | georg.brandl | 2010-08-02 21:23:34 +0200 (Mo, 02 Aug 2010) | 1 line

  #8172: how does one use a property?
........
  r83550 | georg.brandl | 2010-08-02 21:32:43 +0200 (Mo, 02 Aug 2010) | 1 line

  #9451: strengthen warning about __*__ special name usage.
........
  r83551 | georg.brandl | 2010-08-02 21:35:06 +0200 (Mo, 02 Aug 2010) | 1 line

  Remove XXX comment that was displayed.
........
  r83552 | georg.brandl | 2010-08-02 21:36:36 +0200 (Mo, 02 Aug 2010) | 1 line

  #9438: clarify that constant names also cannot be assigned as attributes.
........
  r83553 | georg.brandl | 2010-08-02 21:39:17 +0200 (Mo, 02 Aug 2010) | 1 line

  Remove redundant information.
........
  r83554 | georg.brandl | 2010-08-02 21:43:05 +0200 (Mo, 02 Aug 2010) | 1 line

  #7280: note about nasmw.exe.
........
  r83555 | georg.brandl | 2010-08-02 21:44:48 +0200 (Mo, 02 Aug 2010) | 1 line

  #8861: remove unused variable.
........
  r83558 | georg.brandl | 2010-08-02 22:05:19 +0200 (Mo, 02 Aug 2010) | 1 line

  #8648: document UTF-7 codec functions.
........
  r83560 | georg.brandl | 2010-08-02 22:16:18 +0200 (Mo, 02 Aug 2010) | 1 line

  #9087: update json docstrings -- unicode and long do not exist anymore.
........
This commit is contained in:
Georg Brandl 2010-10-06 08:26:09 +00:00
parent 914a218fbe
commit 4009c9edfc
22 changed files with 266 additions and 219 deletions

View File

@ -641,6 +641,38 @@ These are the UTF-16 codec APIs:
Return *NULL* if an exception was raised by the codec. Return *NULL* if an exception was raised by the codec.
UTF-7 Codecs
""""""""""""
These are the UTF-7 codec APIs:
.. cfunction:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string
*s*. Return *NULL* if an exception was raised by the codec.
.. cfunction:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF7`. If
*consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not
be treated as an error. Those bytes will not be decoded and the number of
bytes that have been decoded will be stored in *consumed*.
.. cfunction:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors)
Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-7 and
return a Python bytes object. Return *NULL* if an exception was raised by
the codec.
If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise
special meaning) will be encoded in base-64. If *base64WhiteSpace* is
nonzero, whitespace will be encoded in base-64. Both are set to zero for the
Python "utf-7" codec.
Unicode-Escape Codecs Unicode-Escape Codecs
""""""""""""""""""""" """""""""""""""""""""

View File

@ -53,7 +53,14 @@ as much as it can.
.. cfunction:: PyObject* PyWeakref_GetObject(PyObject *ref) .. cfunction:: PyObject* PyWeakref_GetObject(PyObject *ref)
Return the referenced object from a weak reference, *ref*. If the referent is Return the referenced object from a weak reference, *ref*. If the referent is
no longer live, returns ``None``. no longer live, returns :const:`Py_None`.
.. warning::
This function returns a **borrowed reference** to the referenced object.
This means that you should always call :cfunc:`Py_INCREF` on the object
except if you know that it cannot be destroyed while you are still
using it.
.. cfunction:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref) .. cfunction:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)

View File

@ -147,11 +147,11 @@ setup script). Indirectly provides the :class:`distutils.dist.Distribution` and
In addition, the :mod:`distutils.core` module exposed a number of classes that In addition, the :mod:`distutils.core` module exposed a number of classes that
live elsewhere. live elsewhere.
* :class:`Extension` from :mod:`distutils.extension` * :class:`~distutils.extension.Extension` from :mod:`distutils.extension`
* :class:`Command` from :mod:`distutils.cmd` * :class:`~distutils.cmd.Command` from :mod:`distutils.cmd`
* :class:`Distribution` from :mod:`distutils.dist` * :class:`~distutils.dist.Distribution` from :mod:`distutils.dist`
A short description of each of these follows, but see the relevant module for A short description of each of these follows, but see the relevant module for
the full reference. the full reference.
@ -1679,8 +1679,8 @@ lines, and joining lines with backslashes.
=================================================================== ===================================================================
.. module:: distutils.cmd .. module:: distutils.cmd
:synopsis: This module provides the abstract base class Command. This class is subclassed :synopsis: This module provides the abstract base class Command. This class
by the modules in the distutils.command subpackage. is subclassed by the modules in the distutils.command subpackage.
This module supplies the abstract base class :class:`Command`. This module supplies the abstract base class :class:`Command`.
@ -1690,20 +1690,84 @@ This module supplies the abstract base class :class:`Command`.
Abstract base class for defining command classes, the "worker bees" of the Abstract base class for defining command classes, the "worker bees" of the
Distutils. A useful analogy for command classes is to think of them as Distutils. A useful analogy for command classes is to think of them as
subroutines with local variables called *options*. The options are declared in subroutines with local variables called *options*. The options are declared
:meth:`initialize_options` and defined (given their final values) in in :meth:`initialize_options` and defined (given their final values) in
:meth:`finalize_options`, both of which must be defined by every command class. :meth:`finalize_options`, both of which must be defined by every command
The distinction between the two is necessary because option values might come class. The distinction between the two is necessary because option values
from the outside world (command line, config file, ...), and any options might come from the outside world (command line, config file, ...), and any
dependent on other options must be computed after these outside influences have options dependent on other options must be computed after these outside
been processed --- hence :meth:`finalize_options`. The body of the subroutine, influences have been processed --- hence :meth:`finalize_options`. The body
where it does all its work based on the values of its options, is the of the subroutine, where it does all its work based on the values of its
:meth:`run` method, which must also be implemented by every command class. options, is the :meth:`run` method, which must also be implemented by every
command class.
The class constructor takes a single argument *dist*, a :class:`Distribution` The class constructor takes a single argument *dist*, a :class:`Distribution`
instance. instance.
Creating a new Distutils command
================================
This section outlines the steps to create a new Distutils command.
A new command lives in a module in the :mod:`distutils.command` package. There
is a sample template in that directory called :file:`command_template`. Copy
this file to a new module with the same name as the new command you're
implementing. This module should implement a class with the same name as the
module (and the command). So, for instance, to create the command
``peel_banana`` (so that users can run ``setup.py peel_banana``), you'd copy
:file:`command_template` to :file:`distutils/command/peel_banana.py`, then edit
it so that it's implementing the class :class:`peel_banana`, a subclass of
:class:`distutils.cmd.Command`.
Subclasses of :class:`Command` must define the following methods.
.. method:: Command.initialize_options()
Set default values for all the options that this command supports. Note that
these defaults may be overridden by other commands, by the setup script, by
config files, or by the command-line. Thus, this is not the place to code
dependencies between options; generally, :meth:`initialize_options`
implementations are just a bunch of ``self.foo = None`` assignments.
.. method:: Command.finalize_options()
Set final values for all the options that this command supports. This is
always called as late as possible, ie. after any option assignments from the
command-line or from other commands have been done. Thus, this is the place
to to code option dependencies: if *foo* depends on *bar*, then it is safe to
set *foo* from *bar* as long as *foo* still has the same value it was
assigned in :meth:`initialize_options`.
.. method:: Command.run()
A command's raison d'etre: carry out the action it exists to perform, controlled
by the options initialized in :meth:`initialize_options`, customized by other
commands, the setup script, the command-line, and config files, and finalized in
:meth:`finalize_options`. All terminal output and filesystem interaction should
be done by :meth:`run`.
.. attribute:: Command.sub_commands
*sub_commands* formalizes the notion of a "family" of commands,
e.g. ``install`` as the parent with sub-commands ``install_lib``,
``install_headers``, etc. The parent of a family of commands defines
*sub_commands* as a class attribute; it's a list of 2-tuples ``(command_name,
predicate)``, with *command_name* a string and *predicate* a function, a
string or ``None``. *predicate* is a method of the parent command that
determines whether the corresponding command is applicable in the current
situation. (E.g. we ``install_headers`` is only applicable if we have any C
header files to install.) If *predicate* is ``None``, that command is always
applicable.
*sub_commands* is usually defined at the *end* of a class, because
predicates can be methods of the class, so they must already have been
defined. The canonical example is the :command:`install` command.
:mod:`distutils.command` --- Individual Distutils commands :mod:`distutils.command` --- Individual Distutils commands
========================================================== ==========================================================
@ -1942,76 +2006,3 @@ The ``register`` command registers the package with the Python Package Index.
This is described in more detail in :pep:`301`. This is described in more detail in :pep:`301`.
.. % todo .. % todo
:mod:`distutils.command.check` --- Check the meta-data of a package
===================================================================
.. module:: distutils.command.check
:synopsis: Check the metadata of a package
The ``check`` command performs some tests on the meta-data of a package.
For example, it verifies that all required meta-data are provided as
the arguments passed to the :func:`setup` function.
.. % todo
Creating a new Distutils command
================================
This section outlines the steps to create a new Distutils command.
A new command lives in a module in the :mod:`distutils.command` package. There
is a sample template in that directory called :file:`command_template`. Copy
this file to a new module with the same name as the new command you're
implementing. This module should implement a class with the same name as the
module (and the command). So, for instance, to create the command
``peel_banana`` (so that users can run ``setup.py peel_banana``), you'd copy
:file:`command_template` to :file:`distutils/command/peel_banana.py`, then edit
it so that it's implementing the class :class:`peel_banana`, a subclass of
:class:`distutils.cmd.Command`.
Subclasses of :class:`Command` must define the following methods.
.. method:: Command.initialize_options()
Set default values for all the options that this command supports. Note that
these defaults may be overridden by other commands, by the setup script, by
config files, or by the command-line. Thus, this is not the place to code
dependencies between options; generally, :meth:`initialize_options`
implementations are just a bunch of ``self.foo = None`` assignments.
.. method:: Command.finalize_options()
Set final values for all the options that this command supports. This is
always called as late as possible, ie. after any option assignments from the
command-line or from other commands have been done. Thus, this is the place
to to code option dependencies: if *foo* depends on *bar*, then it is safe to
set *foo* from *bar* as long as *foo* still has the same value it was
assigned in :meth:`initialize_options`.
.. method:: Command.run()
A command's raison d'etre: carry out the action it exists to perform, controlled
by the options initialized in :meth:`initialize_options`, customized by other
commands, the setup script, the command-line, and config files, and finalized in
:meth:`finalize_options`. All terminal output and filesystem interaction should
be done by :meth:`run`.
*sub_commands* formalizes the notion of a "family" of commands, eg. ``install``
as the parent with sub-commands ``install_lib``, ``install_headers``, etc. The
parent of a family of commands defines *sub_commands* as a class attribute; it's
a list of 2-tuples ``(command_name, predicate)``, with *command_name* a string
and *predicate* a function, a string or None. *predicate* is a method of
the parent command that determines whether the corresponding command is
applicable in the current situation. (Eg. we ``install_headers`` is only
applicable if we have any C header files to install.) If *predicate* is None,
that command is always applicable.
*sub_commands* is usually defined at the \*end\* of a class, because predicates
can be methods of the class, so they must already have been defined. The
canonical example is the :command:`install` command.

View File

@ -176,7 +176,7 @@ easily specify multiple formats in one run. If you need to do both, you can
explicitly specify multiple :command:`bdist_\*` commands and their options:: explicitly specify multiple :command:`bdist_\*` commands and their options::
python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \ python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
bdist_wininst --target_version="2.0" bdist_wininst --target-version="2.0"
Creating RPM packages is driven by a :file:`.spec` file, much as using the Creating RPM packages is driven by a :file:`.spec` file, much as using the
Distutils is driven by the setup script. To make your life easier, the Distutils is driven by the setup script. To make your life easier, the

View File

@ -15,8 +15,8 @@ want to modify existing commands; many simply add a few file extensions that
should be copied into packages in addition to :file:`.py` files as a should be copied into packages in addition to :file:`.py` files as a
convenience. convenience.
Most distutils command implementations are subclasses of the :class:`Command` Most distutils command implementations are subclasses of the
class from :mod:`distutils.cmd`. New commands may directly inherit from :class:`distutils.cmd.Command` class. New commands may directly inherit from
:class:`Command`, while replacements often derive from :class:`Command` :class:`Command`, while replacements often derive from :class:`Command`
indirectly, directly subclassing the command they are replacing. Commands are indirectly, directly subclassing the command they are replacing. Commands are
required to derive from :class:`Command`. required to derive from :class:`Command`.

View File

@ -3,15 +3,6 @@ Built-in Constants
A small number of constants live in the built-in namespace. They are: A small number of constants live in the built-in namespace. They are:
.. note::
:data:`None`, :data:`False`, :data:`True` and :data:`__debug__` cannot be
reassigned (assignments to them raise :exc:`SyntaxError`), so they can be
considered "true" constants.
.. XXX False, True, None are keywords too
.. data:: False .. data:: False
The false value of the :class:`bool` type. Assignments to ``False`` The false value of the :class:`bool` type. Assignments to ``False``
@ -40,19 +31,23 @@ A small number of constants live in the built-in namespace. They are:
.. data:: Ellipsis .. data:: Ellipsis
The same as ``...``. Special value used mostly in conjunction with extended The same as ``...``. Special value used mostly in conjunction with extended
slicing syntax for user-defined container data types, as in :: slicing syntax for user-defined container data types.
.. XXX Someone who understands extended slicing should fill in here.
.. data:: __debug__ .. data:: __debug__
This constant is true if Python was not started with an :option:`-O` option. This constant is true if Python was not started with an :option:`-O` option.
Assignments to :const:`__debug__` are illegal and raise a :exc:`SyntaxError`.
See also the :keyword:`assert` statement. See also the :keyword:`assert` statement.
.. note::
The names :data:`None`, :data:`False`, :data:`True` and :data:`__debug__`
cannot be reassigned (assignments to them, even as an attribute name, raise
:exc:`SyntaxError`), so they can be considered "true" constants.
Constants added by the :mod:`site` module Constants added by the :mod:`site` module
----------------------------------------- -----------------------------------------

View File

@ -331,6 +331,9 @@ are always available. They are listed here in alphabetical order.
returns the current global and local dictionary, respectively, which may be returns the current global and local dictionary, respectively, which may be
useful to pass around for use by :func:`eval` or :func:`exec`. useful to pass around for use by :func:`eval` or :func:`exec`.
See :func:`ast.literal_eval` for a function that can safely evaluate strings
with expressions containing only literals.
.. function:: exec(object[, globals[, locals]]) .. function:: exec(object[, globals[, locals]])
@ -855,7 +858,7 @@ are always available. They are listed here in alphabetical order.
*fget* is a function for getting an attribute value, likewise *fset* is a *fget* is a function for getting an attribute value, likewise *fset* is a
function for setting, and *fdel* a function for del'ing, an attribute. Typical function for setting, and *fdel* a function for del'ing, an attribute. Typical
use is to define a managed attribute x:: use is to define a managed attribute ``x``::
class C(object): class C(object):
def __init__(self): def __init__(self):
@ -869,6 +872,9 @@ are always available. They are listed here in alphabetical order.
del self._x del self._x
x = property(getx, setx, delx, "I'm the 'x' property.") x = property(getx, setx, delx, "I'm the 'x' property.")
If then *c* is an instance of *C*, ``c.x`` will invoke the getter,
``c.x = value`` will invoke the setter and ``del c.x`` the deleter.
If given, *doc* will be the docstring of the property attribute. Otherwise, the If given, *doc* will be the docstring of the property attribute. Otherwise, the
property will copy *fget*'s docstring (if it exists). This makes it possible to property will copy *fget*'s docstring (if it exists). This makes it possible to
create read-only properties easily using :func:`property` as a :term:`decorator`:: create read-only properties easily using :func:`property` as a :term:`decorator`::

View File

@ -206,7 +206,9 @@ applications should use string objects to access all files.
.. function:: normpath(path) .. function:: normpath(path)
Normalize a pathname. This collapses redundant separators and up-level Normalize a pathname. This collapses redundant separators and up-level
references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``. references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all become
``A/B``.
It does not normalize the case (use :func:`normcase` for that). On Windows, it It does not normalize the case (use :func:`normcase` for that). On Windows, it
converts forward slashes to backward slashes. It should be understood that this converts forward slashes to backward slashes. It should be understood that this
may change the meaning of the path if it contains symbolic links! may change the meaning of the path if it contains symbolic links!

View File

@ -550,24 +550,27 @@ Class definitions
A class definition defines a class object (see section :ref:`types`): A class definition defines a class object (see section :ref:`types`):
.. XXX need to document PEP 3115 changes here (new metaclasses)
.. productionlist:: .. productionlist::
classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite` classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite`
inheritance: "(" [`expression_list`] ")" inheritance: "(" [`argument_list` [","] ] ")"
classname: `identifier` classname: `identifier`
A class definition is an executable statement. It first evaluates the A class definition is an executable statement. The inheritance list usually
inheritance list, if present. Each item in the inheritance list should evaluate gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so
to a class object or class type which allows subclassing. The class's suite is each item in the list should evaluate to a class object which allows
then executed in a new execution frame (see section :ref:`naming`), using a subclassing.
newly created local namespace and the original global namespace. (Usually, the
suite contains only function definitions.) When the class's suite finishes The class's suite is then executed in a new execution frame (see :ref:`naming`),
execution, its execution frame is discarded but its local namespace is using a newly created local namespace and the original global namespace.
saved. [#]_ A class object is then created using the inheritance list for the (Usually, the suite contains mostly function definitions.) When the class's
base classes and the saved local namespace for the attribute dictionary. The suite finishes execution, its execution frame is discarded but its local
class name is bound to this class object in the original local namespace. namespace is saved. [#]_ A class object is then created using the inheritance
list for the base classes and the saved local namespace for the attribute
dictionary. The class name is bound to this class object in the original local
namespace.
Class creation can be customized heavily using :ref:`metaclasses <metaclasses>`.
Classes can also be decorated; as with functions, :: Classes can also be decorated; as with functions, ::
@ -581,25 +584,20 @@ is equivalent to ::
Foo = f1(arg)(f2(Foo)) Foo = f1(arg)(f2(Foo))
**Programmer's note:** Variables defined in the class definition are class **Programmer's note:** Variables defined in the class definition are class
variables; they are shared by instances. Instance variables can be set in a attributes; they are shared by instances. Instance attributes can be set in a
method with ``self.name = value``. Both class and instance variables are method with ``self.name = value``. Both class and instance attributes are
accessible through the notation "``self.name``", and an instance variable hides accessible through the notation "``self.name``", and an instance attribute hides
a class variable with the same name when accessed in this way. Class variables a class attribute with the same name when accessed in this way. Class
can be used as defaults for instance variables, but using mutable values there attributes can be used as defaults for instance attributes, but using mutable
can lead to unexpected results. Descriptors can be used to create instance values there can lead to unexpected results. :ref:`Descriptors <descriptors>`
variables with different implementation details. can be used to create instance variables with different implementation details.
.. XXX add link to descriptor docs above
.. seealso:: .. seealso::
:pep:`3116` - Metaclasses in Python 3
:pep:`3129` - Class Decorators :pep:`3129` - Class Decorators
Class definitions, like function definitions, may be wrapped by one or more
:term:`decorator` expressions. The evaluation rules for the decorator
expressions are the same as for functions. The result must be a class object,
which is then bound to the class name.
.. rubric:: Footnotes .. rubric:: Footnotes

View File

@ -362,11 +362,12 @@ characters:
information on this convention. information on this convention.
``__*__`` ``__*__``
System-defined names. These names are defined by the interpreter and its System-defined names. These names are defined by the interpreter and its
implementation (including the standard library); applications should not expect implementation (including the standard library). Current system names are
to define additional names using this convention. The set of names of this discussed in the :ref:`specialnames` section and elsewhere. More will likely
class defined by Python may be extended in future versions. See section be defined in future versions of Python. *Any* use of ``__*__`` names, in
:ref:`specialnames`. any context, that does not follow explicitly documented use, is subject to
breakage without warning.
``__*`` ``__*``
Class-private names. Names in this category, when used within the context of a Class-private names. Names in this category, when used within the context of a

View File

@ -36,7 +36,7 @@ def run(statement, filename=None, sort=-1):
result = prof.print_stats(sort) result = prof.print_stats(sort)
return result return result
def runctx(statement, globals, locals, filename=None): def runctx(statement, globals, locals, filename=None, sort=-1):
"""Run statement under profiler, supplying your own globals and locals, """Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename. optionally saving results in filename.
@ -53,7 +53,7 @@ def runctx(statement, globals, locals, filename=None):
if filename is not None: if filename is not None:
prof.dump_stats(filename) prof.dump_stats(filename)
else: else:
result = prof.print_stats() result = prof.print_stats(sort)
return result return result
# Backwards compatibility. # Backwards compatibility.
@ -169,7 +169,8 @@ def main():
parser.add_option('-o', '--outfile', dest="outfile", parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None) help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort", parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1) help="Sort order when printing to stdout, based on pstats.Stats class",
default=-1)
if not sys.argv[1:]: if not sys.argv[1:]:
parser.print_usage() parser.print_usage()
@ -178,14 +179,18 @@ def main():
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
sys.argv[:] = args sys.argv[:] = args
if (len(sys.argv) > 0): if len(args) > 0:
sys.path.insert(0, os.path.dirname(sys.argv[0])) progname = args[0]
fp = open(sys.argv[0]) sys.path.insert(0, os.path.dirname(progname))
try: with open(progname, 'rb') as fp:
script = fp.read() code = compile(fp.read(), progname, 'exec')
finally: globs = {
fp.close() '__file__': progname,
run('exec(%r)' % script, options.outfile, options.sort) '__name__': '__main__',
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
else: else:
parser.print_usage() parser.print_usage()
return parser return parser

View File

@ -17,10 +17,9 @@ def wrapper(func, *args, **kwds):
wrapper(). wrapper().
""" """
res = None
try: try:
# Initialize curses # Initialize curses
stdscr=curses.initscr() stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode, # Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input # where no buffering is performed on keyboard input

View File

@ -148,7 +148,7 @@ class bdist_msi(Command):
if not self.skip_build and self.distribution.has_ext_modules()\ if not self.skip_build and self.distribution.has_ext_modules()\
and self.target_version != short_version: and self.target_version != short_version:
raise DistutilsOptionError( raise DistutilsOptionError(
"target version can only be %s, or the '--skip_build'" "target version can only be %s, or the '--skip-build'"
" option must be specified" % (short_version,)) " option must be specified" % (short_version,))
else: else:
self.versions = list(self.all_versions) self.versions = list(self.all_versions)

View File

@ -89,7 +89,7 @@ class bdist_wininst(Command):
short_version = get_python_version() short_version = get_python_version()
if self.target_version and self.target_version != short_version: if self.target_version and self.target_version != short_version:
raise DistutilsOptionError( raise DistutilsOptionError(
"target version can only be %s, or the '--skip_build'" \ "target version can only be %s, or the '--skip-build'" \
" option must be specified" % (short_version,)) " option must be specified" % (short_version,))
self.target_version = short_version self.target_version = short_version

View File

@ -125,14 +125,12 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
``.write()``-supporting file-like object). ``.write()``-supporting file-like object).
If ``skipkeys`` is true then ``dict`` keys that are not basic types If ``skipkeys`` is true then ``dict`` keys that are not basic types
(``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
skipped instead of raising a ``TypeError``. instead of raising a ``TypeError``.
If ``ensure_ascii`` is false, then the some chunks written to ``fp`` If ``ensure_ascii`` is false, then the strings written to ``fp`` can
may be ``unicode`` instances, subject to normal Python ``str`` to contain non-ASCII characters if they appear in strings contained in
``unicode`` coercion rules. Unless ``fp.write()`` explicitly ``obj``. Otherwise, all such characters are escaped in JSON strings.
understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
to cause an error.
If ``check_circular`` is false, then the circular reference check If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will for container types will be skipped and a circular reference will
@ -185,12 +183,12 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
"""Serialize ``obj`` to a JSON formatted ``str``. """Serialize ``obj`` to a JSON formatted ``str``.
If ``skipkeys`` is false then ``dict`` keys that are not basic types If ``skipkeys`` is false then ``dict`` keys that are not basic types
(``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
skipped instead of raising a ``TypeError``. instead of raising a ``TypeError``.
If ``ensure_ascii`` is false, then the return value will be a If ``ensure_ascii`` is false, then the return value can contain non-ASCII
``unicode`` instance subject to normal Python ``str`` to ``unicode`` characters if they appear in strings contained in ``obj``. Otherwise, all
coercion rules instead of being escaped to an ASCII ``str``. such characters are escaped in JSON strings.
If ``check_circular`` is false, then the circular reference check If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will for container types will be skipped and a circular reference will

View File

@ -263,9 +263,9 @@ class JSONDecoder(object):
+---------------+-------------------+ +---------------+-------------------+
| array | list | | array | list |
+---------------+-------------------+ +---------------+-------------------+
| string | unicode | | string | str |
+---------------+-------------------+ +---------------+-------------------+
| number (int) | int, long | | number (int) | int |
+---------------+-------------------+ +---------------+-------------------+
| number (real) | float | | number (real) | float |
+---------------+-------------------+ +---------------+-------------------+
@ -318,8 +318,8 @@ class JSONDecoder(object):
def decode(self, s, _w=WHITESPACE.match): def decode(self, s, _w=WHITESPACE.match):
"""Return the Python representation of ``s`` (a ``str`` or ``unicode`` """Return the Python representation of ``s`` (a ``str`` instance
instance containing a JSON document) containing a JSON document).
""" """
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) obj, end = self.raw_decode(s, idx=_w(s, 0).end())
@ -329,8 +329,8 @@ class JSONDecoder(object):
return obj return obj
def raw_decode(self, s, idx=0): def raw_decode(self, s, idx=0):
"""Decode a JSON document from ``s`` (a ``str`` or ``unicode`` """Decode a JSON document from ``s`` (a ``str`` beginning with
beginning with a JSON document) and return a 2-tuple of the Python a JSON document) and return a 2-tuple of the Python
representation and the index in ``s`` where the document ended. representation and the index in ``s`` where the document ended.
This can be used to decode a JSON document from a string that may This can be used to decode a JSON document from a string that may

View File

@ -77,9 +77,9 @@ class JSONEncoder(object):
+-------------------+---------------+ +-------------------+---------------+
| list, tuple | array | | list, tuple | array |
+-------------------+---------------+ +-------------------+---------------+
| str, unicode | string | | str | string |
+-------------------+---------------+ +-------------------+---------------+
| int, long, float | number | | int, float | number |
+-------------------+---------------+ +-------------------+---------------+
| True | true | | True | true |
+-------------------+---------------+ +-------------------+---------------+
@ -102,12 +102,12 @@ class JSONEncoder(object):
"""Constructor for JSONEncoder, with sensible defaults. """Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt If skipkeys is false, then it is a TypeError to attempt
encoding of keys that are not str, int, long, float or None. If encoding of keys that are not str, int, float or None. If
skipkeys is True, such items are simply skipped. skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str If ensure_ascii is true, the output is guaranteed to be str
objects with all incoming unicode characters escaped. If objects with all incoming non-ASCII characters escaped. If
ensure_ascii is false, the output will be unicode object. ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded If check_circular is true, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to objects will be checked for circular references during encoding to

View File

@ -75,7 +75,7 @@ def run(statement, filename=None, sort=-1):
else: else:
return prof.print_stats(sort) return prof.print_stats(sort)
def runctx(statement, globals, locals, filename=None): def runctx(statement, globals, locals, filename=None, sort=-1):
"""Run statement under profiler, supplying your own globals and locals, """Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename. optionally saving results in filename.
@ -90,7 +90,7 @@ def runctx(statement, globals, locals, filename=None):
if filename is not None: if filename is not None:
prof.dump_stats(filename) prof.dump_stats(filename)
else: else:
return prof.print_stats() return prof.print_stats(sort)
# Backwards compatibility. # Backwards compatibility.
def help(): def help():
@ -598,20 +598,28 @@ def main():
parser.add_option('-o', '--outfile', dest="outfile", parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None) help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort", parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1) help="Sort order when printing to stdout, based on pstats.Stats class",
default=-1)
if not sys.argv[1:]: if not sys.argv[1:]:
parser.print_usage() parser.print_usage()
sys.exit(2) sys.exit(2)
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
sys.argv[:] = args
if (len(args) > 0): if len(args) > 0:
sys.argv[:] = args progname = args[0]
sys.path.insert(0, os.path.dirname(sys.argv[0])) sys.path.insert(0, os.path.dirname(progname))
with open(sys.argv[0], 'rb') as fp: with open(progname, 'rb') as fp:
script = fp.read() code = compile(fp.read(), progname, 'exec')
run('exec(%r)' % script, options.outfile, options.sort) globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
else: else:
parser.print_usage() parser.print_usage()
return parser return parser

View File

@ -5,7 +5,7 @@
# Based on prior profile module by Sjoerd Mullender... # Based on prior profile module by Sjoerd Mullender...
# which was hacked somewhat by: Guido van Rossum # which was hacked somewhat by: Guido van Rossum
# #
# see profile.doc and profile.py for more info. # see profile.py for more info.
# Copyright 1994, by InfoSeek Corporation, all rights reserved. # Copyright 1994, by InfoSeek Corporation, all rights reserved.
# Written by James Roskind # Written by James Roskind
@ -65,7 +65,7 @@ class Stats:
minor key of 'the name of the function'. Look at the two tables in minor key of 'the name of the function'. Look at the two tables in
sort_stats() and get_sort_arg_defs(self) for more examples. sort_stats() and get_sort_arg_defs(self) for more examples.
All methods return self, so you can string together commands like: All methods return self, so you can string together commands like:
Stats('foo', 'goo').strip_dirs().sort_stats('calls').\ Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
print_stats(5).print_callers(5) print_stats(5).print_callers(5)
""" """
@ -149,7 +149,7 @@ class Stats:
if not arg_list: return self if not arg_list: return self
if len(arg_list) > 1: self.add(*arg_list[1:]) if len(arg_list) > 1: self.add(*arg_list[1:])
other = arg_list[0] other = arg_list[0]
if type(self) != type(other) or self.__class__ != other.__class__: if type(self) != type(other):
other = Stats(other) other = Stats(other)
self.files += other.files self.files += other.files
self.total_calls += other.total_calls self.total_calls += other.total_calls
@ -217,12 +217,12 @@ class Stats:
if not field: if not field:
self.fcn_list = 0 self.fcn_list = 0
return self return self
if len(field) == 1 and type(field[0]) == type(1): if len(field) == 1 and isinstance(field[0], int):
# Be compatible with old profiler # Be compatible with old profiler
field = [ {-1: "stdname", field = [ {-1: "stdname",
0:"calls", 0: "calls",
1:"time", 1: "time",
2: "cumulative" } [ field[0] ] ] 2: "cumulative"}[field[0]] ]
sort_arg_defs = self.get_sort_arg_defs() sort_arg_defs = self.get_sort_arg_defs()
sort_tuple = () sort_tuple = ()
@ -299,48 +299,53 @@ class Stats:
def eval_print_amount(self, sel, list, msg): def eval_print_amount(self, sel, list, msg):
new_list = list new_list = list
if type(sel) == type(""): if isinstance(sel, str):
try:
rex = re.compile(sel)
except re.error:
msg += " <Invalid regular expression %r>\n" % sel
return new_list, msg
new_list = [] new_list = []
for func in list: for func in list:
if re.search(sel, func_std_string(func)): if rex.search(func_std_string(func)):
new_list.append(func) new_list.append(func)
else: else:
count = len(list) count = len(list)
if type(sel) == type(1.0) and 0.0 <= sel < 1.0: if isinstance(sel, float) and 0.0 <= sel < 1.0:
count = int(count * sel + .5) count = int(count * sel + .5)
new_list = list[:count] new_list = list[:count]
elif type(sel) == type(1) and 0 <= sel < count: elif isinstance(sel, int) and 0 <= sel < count:
count = sel count = sel
new_list = list[:count] new_list = list[:count]
if len(list) != len(new_list): if len(list) != len(new_list):
msg = msg + " List reduced from %r to %r due to restriction <%r>\n" % ( msg += " List reduced from %r to %r due to restriction <%r>\n" % (
len(list), len(new_list), sel) len(list), len(new_list), sel)
return new_list, msg return new_list, msg
def get_print_list(self, sel_list): def get_print_list(self, sel_list):
width = self.max_name_len width = self.max_name_len
if self.fcn_list: if self.fcn_list:
list = self.fcn_list[:] stat_list = self.fcn_list[:]
msg = " Ordered by: " + self.sort_type + '\n' msg = " Ordered by: " + self.sort_type + '\n'
else: else:
list = self.stats.keys() stat_list = list(self.stats.keys())
msg = " Random listing order was used\n" msg = " Random listing order was used\n"
for selection in sel_list: for selection in sel_list:
list, msg = self.eval_print_amount(selection, list, msg) stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
count = len(list) count = len(stat_list)
if not list: if not stat_list:
return 0, list return 0, stat_list
print(msg, file=self.stream) print(msg, file=self.stream)
if count < len(self.stats): if count < len(self.stats):
width = 0 width = 0
for func in list: for func in stat_list:
if len(func_std_string(func)) > width: if len(func_std_string(func)) > width:
width = len(func_std_string(func)) width = len(func_std_string(func))
return width+2, list return width+2, stat_list
def print_stats(self, *amount): def print_stats(self, *amount):
for filename in self.files: for filename in self.files:
@ -561,12 +566,10 @@ if __name__ == '__main__':
def __init__(self, profile=None): def __init__(self, profile=None):
cmd.Cmd.__init__(self) cmd.Cmd.__init__(self)
self.prompt = "% " self.prompt = "% "
self.stats = None
self.stream = sys.stdout
if profile is not None: if profile is not None:
self.stats = Stats(profile) self.do_read(profile)
self.stream = self.stats.stream
else:
self.stats = None
self.stream = sys.stdout
def generic(self, fn, line): def generic(self, fn, line):
args = line.split() args = line.split()

View File

@ -487,8 +487,8 @@ class Trace:
import __main__ import __main__
dict = __main__.__dict__ dict = __main__.__dict__
if not self.donothing: if not self.donothing:
sys.settrace(self.globaltrace)
threading.settrace(self.globaltrace) threading.settrace(self.globaltrace)
sys.settrace(self.globaltrace)
try: try:
exec(cmd, dict, dict) exec(cmd, dict, dict)
finally: finally:
@ -500,8 +500,8 @@ class Trace:
if globals is None: globals = {} if globals is None: globals = {}
if locals is None: locals = {} if locals is None: locals = {}
if not self.donothing: if not self.donothing:
sys.settrace(self.globaltrace)
threading.settrace(self.globaltrace) threading.settrace(self.globaltrace)
sys.settrace(self.globaltrace)
try: try:
exec(cmd, globals, locals) exec(cmd, globals, locals)
finally: finally:
@ -616,7 +616,7 @@ class Trace:
print('%.2f' % (time.time() - self.start_time), end=' ') print('%.2f' % (time.time() - self.start_time), end=' ')
bname = os.path.basename(filename) bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno, print("%s(%d): %s" % (bname, lineno,
linecache.getline(filename, lineno)), end=' ') linecache.getline(filename, lineno)), end='')
return self.localtrace return self.localtrace
def localtrace_trace(self, frame, why, arg): def localtrace_trace(self, frame, why, arg):
@ -629,7 +629,7 @@ class Trace:
print('%.2f' % (time.time() - self.start_time), end=' ') print('%.2f' % (time.time() - self.start_time), end=' ')
bname = os.path.basename(filename) bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno, print("%s(%d): %s" % (bname, lineno,
linecache.getline(filename, lineno)), end=' ') linecache.getline(filename, lineno)), end='')
return self.localtrace return self.localtrace
def localtrace_count(self, frame, why, arg): def localtrace_count(self, frame, why, arg):

View File

@ -484,7 +484,7 @@ bail:
} }
PyDoc_STRVAR(pydoc_scanstring, PyDoc_STRVAR(pydoc_scanstring,
"scanstring(basestring, end, strict=True) -> (bytes, end)\n" "scanstring(string, end, strict=True) -> (string, end)\n"
"\n" "\n"
"Scan the string s for a JSON string. End is the index of the\n" "Scan the string s for a JSON string. End is the index of the\n"
"character in s after the quote that started the JSON string.\n" "character in s after the quote that started the JSON string.\n"
@ -512,7 +512,7 @@ py_scanstring(PyObject* self UNUSED, PyObject *args)
} }
else { else {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"first argument must be a string or bytes, not %.80s", "first argument must be a string, not %.80s",
Py_TYPE(pystr)->tp_name); Py_TYPE(pystr)->tp_name);
return NULL; return NULL;
} }
@ -520,7 +520,7 @@ py_scanstring(PyObject* self UNUSED, PyObject *args)
} }
PyDoc_STRVAR(pydoc_encode_basestring_ascii, PyDoc_STRVAR(pydoc_encode_basestring_ascii,
"encode_basestring_ascii(basestring) -> bytes\n" "encode_basestring_ascii(string) -> string\n"
"\n" "\n"
"Return an ASCII-only JSON representation of a Python string" "Return an ASCII-only JSON representation of a Python string"
); );

View File

@ -152,6 +152,8 @@ _ssl
You must install the NASM assembler from You must install the NASM assembler from
http://nasm.sf.net http://nasm.sf.net
for x86 builds. Put nasmw.exe anywhere in your PATH. for x86 builds. Put nasmw.exe anywhere in your PATH.
Note: recent releases of nasm only have nasm.exe. Just rename it to
nasmw.exe.
You can also install ActivePerl from You can also install ActivePerl from
http://www.activestate.com/Products/ActivePerl/ http://www.activestate.com/Products/ActivePerl/