8000 Merged revisions 60234-60244 via svnmerge from · python/cpython@e7a15bb · GitHub
[go: up one dir, main page]

Skip to content

Commit e7a15bb

Browse files
committed
Merged revisions 60234-60244 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60234 | gregory.p.smith | 2008-01-24 10:38:26 +0100 (Thu, 24 Jan 2008) | 4 lines Fix issue1789: The tutorial contained a misuse of the struct module. (also remove an unneeded import struct from test_largefile) ........ r60237 | vinay.sajip | 2008-01-24 13:37:08 +0100 (Thu, 24 Jan 2008) | 1 line Added optional delay argument to FileHandler and subclasses. ........ r60238 | vinay.sajip | 2008-01-24 13:37:33 +0100 (Thu, 24 Jan 2008) | 1 line Added optional delay argument to FileHandler and subclasses. ........ r60239 | vinay.sajip | 2008-01-24 13:38:30 +0100 (Thu, 24 Jan 2008) | 1 line Added documentation for optional delay argument to FileHandler and subclasses. ........ r60240 | vinay.sajip | 2008-01-24 13:43:33 +0100 (Thu, 24 Jan 2008) | 1 line Updated for optional delay argument to FileHandler and subclasses. ........ r60243 | guido.van.rossum | 2008-01-24 16:53:22 +0100 (Thu, 24 Jan 2008) | 2 lines Fi debug turd -- a call accidentally left out. ........
1 parent e83ebd9 commit e7a15bb

File tree

6 files changed

+59
-29
lines changed

6 files changed

+59
-29
lines changed

Doc/library/logging.rst

Lines changed: 18 additions & 8 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -1517,12 +1517,13 @@ sends logging output to a disk file. It inherits the output functionality from
15171517
:class:`StreamHandler`.
15181518

15191519

1520-
.. class:: FileHandler(filename[, mode[, encoding]])
1520+
.. class:: FileHandler(filename[, mode[, encoding[, delay]]])
15211521

15221522
Returns a new instance of the :class:`FileHandler` class. The specified file is
15231523
opened and used as the stream for logging. If *mode* is not specified,
15241524
:const:`'a'` is used. If *encoding* is not *None*, it is used to open the file
1525-
with that encoding. By default, the file grows indefinitely.
1525+
with that encoding. If *delay* is true, then file opening is deferred until the
1526+
first call to :meth:`emit`. By default, the file grows indefinitely.
15261527

15271528

15281529
.. method:: FileHandler.close()
@@ -1556,12 +1557,13 @@ exclusive locks - and so there is no need for such a handler. Furthermore,
15561557
this value.
15571558

15581559

1559-
.. class:: WatchedFileHandler(filename[,mode[, encoding]])
1560+
.. class:: WatchedFileHandler(filename[,mode[, encoding[, delay]]])
15601561

15611562
Returns a new instance of the :class:`WatchedFileHandler` class. The specified
15621563
file is opened and used as the stream for logging. If *mode* is not specified,
15631564
:const:`'a'` is used. If *encoding* is not *None*, it is used to open the file
1564-
with that encoding. By default, the file grows indefinitely.
1565+
with that encoding. If *delay* is true, then file opening is deferred until the
1566+
first call to :meth:`emit`. By default, the file grows indefinitely.
15651567

15661568

15671569
.. method:: WatchedFileHandler.emit(record)
@@ -1578,11 +1580,13 @@ The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
15781580
module, supports rotation of disk log files.
15791581

15801582

1581-
.. class:: RotatingFileHandler(filename[, mode[, maxBytes[, backupCount]]])
1583+
.. class:: RotatingFileHandler(filename[, mode[, maxBytes[, backupCount[, encoding[, delay]]]]])
15821584

15831585
Returns a new instance of the :class:`RotatingFileHandler` class. The specified
15841586
file is opened and used as the stream for logging. If *mode* is not specified,
1585-
``'a'`` is used. By default, the file grows indefinitely.
1587+
``'a'`` is used. If *encoding* is not *None*, it is used to open the file
1588+
with that encoding. If *delay* is true, then file opening is deferred until the
1589+
first call to :meth:`emit`. By default, the file grows indefinitely.
15861590

15871591
You can use the *maxBytes* and *backupCount* values to allow the file to
15881592
:dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
@@ -1616,7 +1620,7 @@ The :class:`TimedRotatingFileHandler` class, located in the
16161620
timed intervals.
16171621

16181622

1619-
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount]]])
1623+
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount[, encoding[, delay]]]]])
16201624

16211625
Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
16221626
specified file is opened and used as the stream for logging. On rotating it also
@@ -2053,7 +2057,13 @@ Currently, the useful mapping keys in a :class:`LogRecord` are:
20532057
record is computed using *msg* % *args*. If the formatting string contains
20542058
``'(asctime)'``, :meth:`formatTime` is called to format the event time. If there
20552059
is exception information, it is formatted using :meth:`formatException` and
2056-
appended to the message.
2060+
appended to the message. Note that the formatted exception information is cached
2061+
in attribute *exc_text*. This is useful because the exception information can
2062+
be pickled and sent across the wire, but you should be careful if you have more
2063+
than one :class:`Formatter` subclass which customizes the formatting of exception
2064+
information. In this case, you will have to clear the cached value after a
2065+
formatter has done its formatting, so that the next formatter to handle the event
2066+
doesn't use the cached value but recalculates it afresh.
20572067

20582068

20592069
.. method:: Formatter.formatTime(record[, datefmt])

Doc/tutorial/stdlib2.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,18 @@ Working with Binary Data Record Layouts
134134

135135
The :mod:`struct` module provides :func:`pack` and :func:`unpack` functions for
136136
working with variable length binary record formats. The following example shows
137-
how to loop through header information in a ZIP file (with pack codes ``"H"``
138-
and ``"L"`` representing two and four byte unsigned numbers respectively)::
137+
how to loop through header information in a ZIP file without using the
138+
:mod:`zipfile` module. Pack codes ``"H"`` and ``"I"`` represent two and four
139+
byte unsigned numbers respectively. The ``"<"`` indicates that they are
140+
standard size and in little-endian byte order::
139141

140142
import struct
141143

142144
data = open('myfile.zip', 'rb').read()
143145
start = 0
144146
for i in range(3): # show the first 3 file headers
145147
start += 14
146-
fields = struct.unpack('LLLHH', data[start:start+16])
148+
fields = struct.unpack('<IIIHH', data[start:start+16])
147149
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
148150

149151
start += 16

Lib/logging/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
__author__ = "Vinay Sajip <vinay_sajip@red-dove.com>"
4040
__status__ = "production"
41-
__version__ = "0.5.0.4"
42-
__date__ = "18 January 2008"
41+
__version__ = "0.5.0.5"
42+
__date__ = "24 January 2008"
4343

4444
#---------------------------------------------------------------------------
4545
# Miscellaneous module data
@@ -760,7 +760,7 @@ class FileHandler(StreamHandler):
760760
"""
761761
A handler class which writes formatted logging records to disk files.
762762
"""
763-
def __init__(self, filename, mode='a', encoding=None):
763+
def __init__(self, filename, mode='a', encoding=None, delay=0):
764764
"""
765765
Open the specified file and use it as the stream for logging.
766766
"""
@@ -771,8 +771,11 @@ def __init__(self, filename, mode='a', encoding=None):
771771
self.baseFilename = os.path.abspath(filename)
772772
self.mode = mode
773773
self.encoding = encoding
774-
stream = self._open()
775-
StreamHandler.__init__(self, stream)
774+
if delay:
775+
self.stream = None
776+
else:
777+
stream = self._open()
778+
StreamHandler.__init__(self, stream)
776779

777780
def close(self):
778781
"""
@@ -795,6 +798,18 @@ def _open(self):
795798
stream = codecs.open(self.baseFilename, self.mode, self.encoding)
796799
return stream
797800

801+
def emit(self, record):
802+
"""
803+
Emit a record.
804+
805+
If the stream was not opened because 'delay' was specified in the
806+
constructor, open it before calling the superclass's emit.
807+
"""
808+
if self.stream is None:
809+
stream = self._open()
810+
StreamHandler.__init__(self, stream)
811+
StreamHandler.emit(self, record)
812+
798813
#---------------------------------------------------------------------------
799814
# Manager classes and functions
800815
#---------------------------------------------------------------------------

Lib/logging/handlers.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ class BaseRotatingHandler(logging.FileHandler):
5151
Not meant to be instantiated directly. Instead, use RotatingFileHandler
5252
or TimedRotatingFileHandler.
5353
"""
54-
def __init__(self, filename, mode, encoding=None):
54+
def __init__(self, filename, mode, encoding=None, delay=0):
5555
"""
5656
Use the specified filename for streamed logging
5757
"""
5858
if codecs is None:
5959
encoding = None
60-
logging.FileHandler.__init__(self, filename, mode, encoding)
60+
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
6161
self.mode = mode
6262
self.encoding = encoding
6363

@@ -82,7 +82,7 @@ class RotatingFileHandler(BaseRotatingHandler):
8282
Handler for logging to a set of files, which switches from one file
8383
to the next when the current file reaches a certain size.
8484
"""
85-
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None):
85+
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
8686
"""
8787
Open the specified file and use it as the stream for logging.
8888
@@ -105,7 +105,7 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None)
105105
"""
106106
if maxBytes > 0:
107107
mode = 'a' # doesn't make sense otherwise!
108-
BaseRotatingHandler.__init__(self, filename, mode, encoding)
108+
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
109109
self.maxBytes = maxBytes
110110
self.backupCount = backupCount
111111

@@ -154,8 +154,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
154154
If backupCount is > 0, when rollover is done, no more than backupCount
155155
files are kept - the oldest ones are deleted.
156156
"""
157-
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None):
158-
BaseRotatingHandler.__init__(self, filename, 'a', encoding)
157+
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0):
158+
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
159159
self.when = when.upper()
160160
self.backupCount = backupCount
161161
# Calculate the real rollover interval, which is just the number of
@@ -300,10 +300,13 @@ class WatchedFileHandler(logging.FileHandler):
300300
This handler is based on a suggestion and patch by Chad J.
301301
Schroeder.
302302
"""
303-
def __init__(self, filename, mode='a', encoding=None):
304-
logging.FileHandler.__init__(self, filename, mode, encoding)
305-
stat = os.stat(self.baseFilename)
306-
self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
303+
def __init__(self, filename, mode='a', encoding=None, delay=0):
304+
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
305+
if not os.path.exists(self.baseFilename):
306+
self.dev, self.ino = -1, -1
307+
else:
308+
stat = os.stat(self.baseFilename)
309+
self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
307310

308311
def emit(self, record):
309312
"""
@@ -319,7 +322,7 @@ def emit(self, record):
319322
else:
320323
stat = os.stat(self.baseFilename)
321324
changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino)
322-
if changed:
325+
if changed and self.stream is not None:
323326
self.stream.flush()
324327
self.stream.close()
325328
self.stream = self._open()

Lib/test/test_descr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4249,7 +4249,7 @@ def __call__(self, *args):
42494249
builtins.__import__ = orig_import
42504250

42514251
def test_main():
4252-
#XXXweakref_segfault() # Must be first, somehow
4252+
weakref_segfault() # Must be first, somehow
42534253
wrapper_segfault() # NB This one is slow
42544254
do_this_first()
42554255
class_docstrings()

Lib/test/test_largefile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#----------------------------------------------------------------------
77

88
from test import test_support
9-
import os, struct, stat, sys
9+
import os, stat, sys
1010

1111
try:
1212
import signal

0 commit comments

Comments
 (0)
0