Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ print a message for each get or set. Overriding :meth:`__getattribute__` is
alternate approach that could do this for every attribute. However, this
descriptor is useful for monitoring just a few chosen attributes::

class RevealAccess(object):
class RevealAccess:
"""A data descriptor that sets and returns values
normally and prints a message logging their access.
"""
Expand All @@ -162,7 +162,7 @@ descriptor is useful for monitoring just a few chosen attributes::
print('Updating', self.name)
self.val = val

>>> class MyClass(object):
>>> class MyClass:
... x = RevealAccess(10, 'var "x"')
... y = 5
...
Expand Down Expand Up @@ -194,7 +194,7 @@ triggers function calls upon access to an attribute. Its signature is::

The documentation shows a typical use to define a managed attribute ``x``::

class C(object):
class C:
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
Expand All @@ -203,7 +203,7 @@ The documentation shows a typical use to define a managed attribute ``x``::
To see how :func:`property` is implemented in terms of the descriptor protocol,
here is a pure Python equivalent::

class Property(object):
class Property:
"Emulate PyProperty_Type() in Objects/descrobject.c"

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
Expand Down Expand Up @@ -250,7 +250,7 @@ to be recalculated on every access; however, the programmer does not want to
affect existing client code accessing the attribute directly. The solution is
to wrap access to the value attribute in a property data descriptor::

class Cell(object):
class Cell:
. . .
def getvalue(self):
"Recalculate the cell before returning value"
Expand All @@ -277,7 +277,7 @@ binding methods during attribute access. This means that all functions are
non-data descriptors which return bound methods when they are invoked from an
object. In pure Python, it works like this::

class Function(object):
class Function:
. . .
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
Expand All @@ -287,7 +287,7 @@ object. In pure Python, it works like this::

Running the interpreter shows how the function descriptor works in practice::

>>> class D(object):
>>> class D:
... def f(self, x):
... return x
...
Expand Down Expand Up @@ -367,7 +367,7 @@ It can be called either from an object or the class: ``s.erf(1.5) --> .9332`` o
Since staticmethods return the underlying function with no changes, the example
calls are unexciting::

>>> class E(object):
>>> class E:
... def f(x):
... print(x)
... f = staticmethod(f)
Expand All @@ -380,7 +380,7 @@ calls are unexciting::
Using the non-data descriptor protocol, a pure Python version of
:func:`staticmethod` would look like this::

class StaticMethod(object):
class StaticMethod:
"Emulate PyStaticMethod_Type() in Objects/funcobject.c"

def __init__(self, f):
Expand All @@ -393,7 +393,7 @@ Unlike static methods, class methods prepend the class reference to the
argument list before calling the function. This format is the same
for whether the caller is an object or a class::

>>> class E(object):
>>> class E:
... def f(klass, x):
... return klass.__name__, x
... f = classmethod(f)
Expand All @@ -410,7 +410,7 @@ is to create alternate class constructors. In Python 2.3, the classmethod
:func:`dict.fromkeys` creates a new dictionary from a list of keys. The pure
Python equivalent is::

class Dict(object):
class Dict:
. . .
def fromkeys(klass, iterable, value=None):
"Emulate dict_fromkeys() in Objects/dictobject.c"
Expand All @@ -428,7 +428,7 @@ Now a new dictionary of unique keys can be constructed like this::
Using the non-data descriptor protocol, a pure Python version of
:func:`classmethod` would look like this::

class ClassMethod(object):
class ClassMethod:
"Emulate PyClassMethod_Type() in Objects/funcobject.c"

def __init__(self, f):
Expand Down
11 changes: 10 additions & 1 deletion Doc/howto/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,18 @@ look at that next. Be sure to try the following in a newly-started Python
interpreter, and don't just continue from the session described above::

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

.. versionchanged:: 3.9
The *encoding* argument was added. In earlier Python versions, or if not
specified, the encoding used is the default value used by :func:`open`. While
not shown in the above example, an *errors* argument can also now be passed,
which determines how encoding errors are handled. For available values and
the default, see the documentation for :func:`open`.

And now if we open the file and look at what we have, we should find the log
messages:
Expand All @@ -141,6 +149,7 @@ messages:
DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too
ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö

This example also shows how you can set the logging level which acts as the
threshold for tracking. In this case, because we set the threshold to
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/copyreg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The example below would like to show how to register a pickle function and how
it will be used:

>>> import copyreg, copy, pickle
>>> class C(object):
>>> class C:
... def __init__(self, a):
... self.a = a
...
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ The :mod:`functools` module defines the following functions:

Example::

>>> class Cell(object):
>>> class Cell:
... def __init__(self):
... self._alive = False
... @property
Expand Down
34 changes: 26 additions & 8 deletions Doc/library/logging.handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,22 @@ sends logging output to a disk file. It inherits the output functionality from
:class:`StreamHandler`.


.. class:: FileHandler(filename, mode='a', encoding=None, delay=False)
.. class:: FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)

Returns a new instance of the :class:`FileHandler` class. The specified file is
opened and used as the stream for logging. If *mode* is not specified,
:const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
first call to :meth:`emit`. By default, the file grows indefinitely. If
*errors* is specified, it's used to determine how encoding errors are handled.

.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.

.. versionchanged:: 3.9
The *errors* parameter was added.

.. method:: close()

Closes the file.
Expand Down Expand Up @@ -168,18 +172,22 @@ exclusive locks - and so there is no need for such a handler. Furthermore,
for this value.


.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False)
.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False, errors=None)

Returns a new instance of the :class:`WatchedFileHandler` class. The specified
file is opened and used as the stream for logging. If *mode* is not specified,
:const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
first call to :meth:`emit`. By default, the file grows indefinitely. If
*errors* is provided, it determines how encoding errors are handled.

.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.

.. versionchanged:: 3.9
The *errors* parameter was added.

.. method:: reopenIfNeeded()

Checks to see if the file has changed. If it has, the existing stream is
Expand All @@ -205,7 +213,7 @@ module, is the base class for the rotating file handlers,
not need to instantiate this class, but it has attributes and methods you may
need to override.

.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False)
.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False, errors=None)

The parameters are as for :class:`FileHandler`. The attributes are:

Expand Down Expand Up @@ -284,13 +292,14 @@ The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
module, supports rotation of disk log files.


.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)
.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None)

Returns a new instance of the :class:`RotatingFileHandler` class. The specified
file is opened and used as the stream for logging. If *mode* is not specified,
``'a'`` is used. If *encoding* is not ``None``, it is used to open the file
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
first call to :meth:`emit`. By default, the file grows indefinitely. If
*errors* is provided, it determines how encoding errors are handled.

You can use the *maxBytes* and *backupCount* values to allow the file to
:dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
Expand All @@ -311,6 +320,9 @@ module, supports rotation of disk log files.
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.

.. versionchanged:: 3.9
The *errors* parameter was added.

.. method:: doRollover()

Does a rollover, as described above.
Expand All @@ -331,7 +343,7 @@ The :class:`TimedRotatingFileHandler` class, located in the
timed intervals.


.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)
.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None)

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
Expand Down Expand Up @@ -391,6 +403,9 @@ timed intervals.
rollover, and subsequent rollovers would be calculated via the normal
interval calculation.

If *errors* is specified, it's used to determine how encoding errors are
handled.

.. note:: Calculation of the initial rollover time is done when the handler
is initialised. Calculation of subsequent rollover times is done only
when rollover occurs, and rollover occurs only when emitting output. If
Expand All @@ -411,6 +426,9 @@ timed intervals.
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.

.. versionchanged:: 3.9
The *errors* parameter was added.

.. method:: doRollover()

Does a rollover, as described above.
Expand Down
13 changes: 13 additions & 0 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,16 @@ functions.
| | carrying out the configuration as specified |
| | by the other arguments. |
+--------------+---------------------------------------------+
| *encoding* | If this keyword argument is specified along |
| | with *filename*, its value is used when the |
| | FileHandler is created, and thus used when |
| | opening the output file. |
+--------------+---------------------------------------------+
| *errors* | If this keyword argument is specified along |
| | with *filename*, its value is used when the |
| | FileHandler is created, and thus used when |
| | opening the output file. |
+--------------+---------------------------------------------+

.. versionchanged:: 3.2
The *style* argument was added.
Expand All @@ -1209,6 +1219,9 @@ functions.
.. versionchanged:: 3.8
The *force* argument was added.

.. versionchanged:: 3.9
The *encoding* and *errors* arguments were added.

.. function:: shutdown()

Informs the logging system to perform an orderly shutdown by flushing and
Expand Down
2 changes: 2 additions & 0 deletions Doc/tools/susp-ignored.csv
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ howto/ipaddress,,::,IPv6Address('2001:db8::ffff:ffff')
howto/ipaddress,,:ffff,IPv6Address('2001:db8::ffff:ffff')
howto/logging,,:And,"WARNING:And this, too"
howto/logging,,:And,"WARNING:root:And this, too"
howto/logging,,:And,"ERROR:root:And non-ASCII stuff, too, like "
howto/logging,,:Doing,INFO:root:Doing something
howto/logging,,:Finished,INFO:root:Finished
howto/logging,,:logger,severity:logger name:message
Expand All @@ -90,6 +91,7 @@ howto/logging,,:root,DEBUG:root:This message should go to the log file
howto/logging,,:root,INFO:root:Doing something
howto/logging,,:root,INFO:root:Finished
howto/logging,,:root,INFO:root:So should this
howto/logging,,:root,"ERROR:root:And non-ASCII stuff, too, like "
howto/logging,,:root,INFO:root:Started
howto/logging,,:root,"WARNING:root:And this, too"
howto/logging,,:root,WARNING:root:Look before you leap!
Expand Down
Loading