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
"""""""""""""""""""""
.. cfunction:: PyObject* PyWeakref_GetObject(PyObject *ref)
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)
In addition, the :mod:`distutils.core` module exposed a number of classes that
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
the full reference.
===================================================================
.. module:: distutils.cmd
- :synopsis: This module provides the abstract base class Command. This class is subclassed
- by the modules in the distutils.command subpackage.
+ :synopsis: This module provides the abstract base class Command. This class
+ is subclassed by the modules in the distutils.command subpackage.
This module supplies the abstract base class :class:`Command`.
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
- subroutines with local variables called *options*. The options are declared in
- :meth:`initialize_options` and defined (given their final values) in
- :meth:`finalize_options`, both of which must be defined by every command class.
- The distinction between the two is necessary because option values might come
- from the outside world (command line, config file, ...), and any options
- dependent on other options must be computed after these outside influences have
- been processed --- hence :meth:`finalize_options`. The body of the subroutine,
- where it does all its work based on the values of its 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`
+ subroutines with local variables called *options*. The options are declared
+ in :meth:`initialize_options` and defined (given their final values) in
+ :meth:`finalize_options`, both of which must be defined by every command
+ class. The distinction between the two is necessary because option values
+ might come from the outside world (command line, config file, ...), and any
+ options dependent on other options must be computed after these outside
+ influences have been processed --- hence :meth:`finalize_options`. The body
+ of the subroutine, where it does all its work based on the values of its
+ 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`
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
==========================================================
This is described in more detail in :pep:`301`.
.. % 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.
explicitly specify multiple :command:`bdist_\*` commands and their options::
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
Distutils is driven by the setup script. To make your life easier, the
should be copied into packages in addition to :file:`.py` files as a
convenience.
-Most distutils command implementations are subclasses of the :class:`Command`
-class from :mod:`distutils.cmd`. New commands may directly inherit from
+Most distutils command implementations are subclasses of the
+:class:`distutils.cmd.Command` class. New commands may directly inherit from
:class:`Command`, while replacements often derive from :class:`Command`
indirectly, directly subclassing the command they are replacing. Commands are
required to derive from :class:`Command`.
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
The false value of the :class:`bool` type. Assignments to ``False``
.. data:: Ellipsis
- The same as ``...``. Special value used mostly in conjunction with extended
- slicing syntax for user-defined container data types, as in ::
-
- .. XXX Someone who understands extended slicing should fill in here.
+ The same as ``...``. Special value used mostly in conjunction with extended
+ slicing syntax for user-defined container data types.
.. data:: __debug__
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.
+.. 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
-----------------------------------------
returns the current global and local dictionary, respectively, which may be
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]])
*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
- use is to define a managed attribute x::
+ use is to define a managed attribute ``x``::
class C(object):
def __init__(self):
del self._x
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
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`::
.. function:: normpath(path)
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
converts forward slashes to backward slashes. It should be understood that this
may change the meaning of the path if it contains symbolic links!
A class definition defines a class object (see section :ref:`types`):
-.. XXX need to document PEP 3115 changes here (new metaclasses)
-
.. productionlist::
classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite`
- inheritance: "(" [`expression_list`] ")"
+ inheritance: "(" [`argument_list` [","] ] ")"
classname: `identifier`
-A class definition is an executable statement. It first evaluates the
-inheritance list, if present. Each item in the inheritance list should evaluate
-to a class object or class type which allows subclassing. The class's suite is
-then executed in a new execution frame (see section :ref:`naming`), using a
-newly created local namespace and the original global namespace. (Usually, the
-suite contains only function definitions.) When the class's suite finishes
-execution, its execution frame is discarded but its local 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.
+A class definition is an executable statement. The inheritance list usually
+gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so
+each item in the list should evaluate to a class object which allows
+subclassing.
+
+The class's suite is then executed in a new execution frame (see :ref:`naming`),
+using a newly created local namespace and the original global namespace.
+(Usually, the suite contains mostly function definitions.) When the class's
+suite finishes execution, its execution frame is discarded but its local
+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, ::
Foo = f1(arg)(f2(Foo))
**Programmer's note:** Variables defined in the class definition are class
-variables; they are shared by instances. Instance variables can be set in a
-method with ``self.name = value``. Both class and instance variables are
-accessible through the notation "``self.name``", and an instance variable hides
-a class variable with the same name when accessed in this way. Class variables
-can be used as defaults for instance variables, but using mutable values there
-can lead to unexpected results. Descriptors can be used to create instance
-variables with different implementation details.
+attributes; they are shared by instances. Instance attributes can be set in a
+method with ``self.name = value``. Both class and instance attributes are
+accessible through the notation "``self.name``", and an instance attribute hides
+a class attribute with the same name when accessed in this way. Class
+attributes can be used as defaults for instance attributes, but using mutable
+values there can lead to unexpected results. :ref:`Descriptors <descriptors>`
+can be used to create instance variables with different implementation details.
-.. XXX add link to descriptor docs above
.. seealso::
+ :pep:`3116` - Metaclasses in Python 3
: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
information on this convention.
``__*__``
- System-defined names. These names are defined by the interpreter and its
- implementation (including the standard library); applications should not expect
- to define additional names using this convention. The set of names of this
- class defined by Python may be extended in future versions. See section
- :ref:`specialnames`.
+ System-defined names. These names are defined by the interpreter and its
+ implementation (including the standard library). Current system names are
+ discussed in the :ref:`specialnames` section and elsewhere. More will likely
+ be defined in future versions of Python. *Any* use of ``__*__`` names, in
+ 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
result = prof.print_stats(sort)
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,
optionally saving results in filename.
if filename is not None:
prof.dump_stats(filename)
else:
- result = prof.print_stats()
+ result = prof.print_stats(sort)
return result
# Backwards compatibility.
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
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:]:
parser.print_usage()
(options, args) = parser.parse_args()
sys.argv[:] = args
- if (len(sys.argv) > 0):
- sys.path.insert(0, os.path.dirname(sys.argv[0]))
- fp = open(sys.argv[0])
- try:
- script = fp.read()
- finally:
- fp.close()
- run('exec(%r)' % script, options.outfile, options.sort)
+ if len(args) > 0:
+ progname = args[0]
+ sys.path.insert(0, os.path.dirname(progname))
+ with open(progname, 'rb') as fp:
+ code = compile(fp.read(), progname, 'exec')
+ globs = {
+ '__file__': progname,
+ '__name__': '__main__',
+ '__package__': None,
+ '__cached__': None,
+ }
+ runctx(code, globs, None, options.outfile, options.sort)
else:
parser.print_usage()
return parser
wrapper().
"""
- res = None
try:
# Initialize curses
- stdscr=curses.initscr()
+ stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
if not self.skip_build and self.distribution.has_ext_modules()\
and self.target_version != short_version:
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,))
else:
self.versions = list(self.all_versions)
short_version = get_python_version()
if self.target_version and self.target_version != short_version:
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,))
self.target_version = short_version
``.write()``-supporting file-like object).
If ``skipkeys`` is true then ``dict`` keys that are not basic types
- (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be
- skipped instead of raising a ``TypeError``.
+ (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
+ instead of raising a ``TypeError``.
- If ``ensure_ascii`` is false, then the some chunks written to ``fp``
- may be ``unicode`` instances, subject to normal Python ``str`` to
- ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
- understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
- to cause an error.
+ If ``ensure_ascii`` is false, then the strings written to ``fp`` can
+ contain non-ASCII characters if they appear in strings contained in
+ ``obj``. Otherwise, all such characters are escaped in JSON strings.
If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will
"""Serialize ``obj`` to a JSON formatted ``str``.
If ``skipkeys`` is false then ``dict`` keys that are not basic types
- (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be
- skipped instead of raising a ``TypeError``.
+ (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
+ instead of raising a ``TypeError``.
- If ``ensure_ascii`` is false, then the return value will be a
- ``unicode`` instance subject to normal Python ``str`` to ``unicode``
- coercion rules instead of being escaped to an ASCII ``str``.
+ If ``ensure_ascii`` is false, then the return value can contain non-ASCII
+ characters if they appear in strings contained in ``obj``. Otherwise, all
+ such characters are escaped in JSON strings.
If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will
+---------------+-------------------+
| array | list |
+---------------+-------------------+
- | string | unicode |
+ | string | str |
+---------------+-------------------+
- | number (int) | int, long |
+ | number (int) | int |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
def decode(self, s, _w=WHITESPACE.match):
- """Return the Python representation of ``s`` (a ``str`` or ``unicode``
- instance containing a JSON document)
+ """Return the Python representation of ``s`` (a ``str`` instance
+ containing a JSON document).
"""
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
return obj
def raw_decode(self, s, idx=0):
- """Decode a JSON document from ``s`` (a ``str`` or ``unicode``
- beginning with a JSON document) and return a 2-tuple of the Python
+ """Decode a JSON document from ``s`` (a ``str`` beginning with
+ a JSON document) and return a 2-tuple of the Python
representation and the index in ``s`` where the document ended.
This can be used to decode a JSON document from a string that may
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
- | str, unicode | string |
+ | str | string |
+-------------------+---------------+
- | int, long, float | number |
+ | int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
"""Constructor for JSONEncoder, with sensible defaults.
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.
If ensure_ascii is true, the output is guaranteed to be str
- objects with all incoming unicode characters escaped. If
- ensure_ascii is false, the output will be unicode object.
+ objects with all incoming non-ASCII characters escaped. If
+ ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to
else:
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,
optionally saving results in filename.
if filename is not None:
prof.dump_stats(filename)
else:
- return prof.print_stats()
+ return prof.print_stats(sort)
# Backwards compatibility.
def help():
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
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:]:
parser.print_usage()
sys.exit(2)
(options, args) = parser.parse_args()
-
- if (len(args) > 0):
- sys.argv[:] = args
- sys.path.insert(0, os.path.dirname(sys.argv[0]))
- with open(sys.argv[0], 'rb') as fp:
- script = fp.read()
- run('exec(%r)' % script, options.outfile, options.sort)
+ sys.argv[:] = args
+
+ if len(args) > 0:
+ progname = args[0]
+ sys.path.insert(0, os.path.dirname(progname))
+ with open(progname, 'rb') as fp:
+ code = compile(fp.read(), progname, 'exec')
+ globs = {
+ '__file__': progname,
+ '__name__': '__main__',
+ '__package__': None,
+ '__cached__': None,
+ }
+ runctx(code, globs, None, options.outfile, options.sort)
else:
parser.print_usage()
return parser
# Based on prior profile module by Sjoerd Mullender...
# 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.
# Written by James Roskind
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.
- 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').\
print_stats(5).print_callers(5)
"""
if not arg_list: return self
if len(arg_list) > 1: self.add(*arg_list[1:])
other = arg_list[0]
- if type(self) != type(other) or self.__class__ != other.__class__:
+ if type(self) != type(other):
other = Stats(other)
self.files += other.files
self.total_calls += other.total_calls
if not field:
self.fcn_list = 0
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
field = [ {-1: "stdname",
- 0:"calls",
- 1:"time",
- 2: "cumulative" } [ field[0] ] ]
+ 0: "calls",
+ 1: "time",
+ 2: "cumulative"}[field[0]] ]
sort_arg_defs = self.get_sort_arg_defs()
sort_tuple = ()
def eval_print_amount(self, sel, list, msg):
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 = []
for func in list:
- if re.search(sel, func_std_string(func)):
+ if rex.search(func_std_string(func)):
new_list.append(func)
else:
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)
new_list = list[:count]
- elif type(sel) == type(1) and 0 <= sel < count:
+ elif isinstance(sel, int) and 0 <= sel < count:
count = sel
new_list = list[:count]
if len(list) != len(new_list):
- msg = msg + " List reduced from %r to %r due to restriction <%r>\n" % (
- len(list), len(new_list), sel)
+ msg += " List reduced from %r to %r due to restriction <%r>\n" % (
+ len(list), len(new_list), sel)
return new_list, msg
def get_print_list(self, sel_list):
width = self.max_name_len
if self.fcn_list:
- list = self.fcn_list[:]
+ stat_list = self.fcn_list[:]
msg = " Ordered by: " + self.sort_type + '\n'
else:
- list = self.stats.keys()
+ stat_list = list(self.stats.keys())
msg = " Random listing order was used\n"
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:
- return 0, list
+ if not stat_list:
+ return 0, stat_list
print(msg, file=self.stream)
if count < len(self.stats):
width = 0
- for func in list:
+ for func in stat_list:
if len(func_std_string(func)) > width:
width = len(func_std_string(func))
- return width+2, list
+ return width+2, stat_list
def print_stats(self, *amount):
for filename in self.files:
def __init__(self, profile=None):
cmd.Cmd.__init__(self)
self.prompt = "% "
+ self.stats = None
+ self.stream = sys.stdout
if profile is not None:
- self.stats = Stats(profile)
- self.stream = self.stats.stream
- else:
- self.stats = None
- self.stream = sys.stdout
+ self.do_read(profile)
def generic(self, fn, line):
args = line.split()
import __main__
dict = __main__.__dict__
if not self.donothing:
- sys.settrace(self.globaltrace)
threading.settrace(self.globaltrace)
+ sys.settrace(self.globaltrace)
try:
exec(cmd, dict, dict)
finally:
if globals is None: globals = {}
if locals is None: locals = {}
if not self.donothing:
- sys.settrace(self.globaltrace)
threading.settrace(self.globaltrace)
+ sys.settrace(self.globaltrace)
try:
exec(cmd, globals, locals)
finally:
print('%.2f' % (time.time() - self.start_time), end=' ')
bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno,
- linecache.getline(filename, lineno)), end=' ')
+ linecache.getline(filename, lineno)), end='')
return self.localtrace
def localtrace_trace(self, frame, why, arg):
print('%.2f' % (time.time() - self.start_time), end=' ')
bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno,
- linecache.getline(filename, lineno)), end=' ')
+ linecache.getline(filename, lineno)), end='')
return self.localtrace
def localtrace_count(self, frame, why, arg):
}
PyDoc_STRVAR(pydoc_scanstring,
- "scanstring(basestring, end, strict=True) -> (bytes, end)\n"
+ "scanstring(string, end, strict=True) -> (string, end)\n"
"\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"
}
else {
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);
return NULL;
}
}
PyDoc_STRVAR(pydoc_encode_basestring_ascii,
- "encode_basestring_ascii(basestring) -> bytes\n"
+ "encode_basestring_ascii(string) -> string\n"
"\n"
"Return an ASCII-only JSON representation of a Python string"
);
You must install the NASM assembler from
http://nasm.sf.net
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
http://www.activestate.com/Products/ActivePerl/