Skip to content

Commit aba9796

Browse files
committed
Merged revisions 85450-85455,85460-85465 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k ........ r85450 | georg.brandl | 2010-10-14 08:35:53 +0200 (Do, 14 Okt 2010) | 1 line python#7642: update to os.system() docs. ........ r85451 | georg.brandl | 2010-10-14 08:41:42 +0200 (Do, 14 Okt 2010) | 1 line python#3865: add note about benchmarking with profilers, and move licensing stuff to bottom of document. ........ r85452 | georg.brandl | 2010-10-14 08:43:22 +0200 (Do, 14 Okt 2010) | 1 line python#10046: small correction to atexit docs. ........ r85453 | georg.brandl | 2010-10-14 08:46:08 +0200 (Do, 14 Okt 2010) | 1 line python#6825: small correction to split() docs. ........ r85454 | georg.brandl | 2010-10-14 08:48:47 +0200 (Do, 14 Okt 2010) | 1 line Mention 2to3. ........ r85455 | georg.brandl | 2010-10-14 08:59:45 +0200 (Do, 14 Okt 2010) | 1 line #1710703: write zipfile structures also in the case of closing a new, but empty, archive. ........ r85460 | georg.brandl | 2010-10-14 09:24:28 +0200 (Do, 14 Okt 2010) | 1 line python#9964: fix running test_import under -O or -OO. ........ r85461 | georg.brandl | 2010-10-14 09:29:08 +0200 (Do, 14 Okt 2010) | 1 line python#9964: fix lib2to3 fixer fix_operator when running under -OO. ........ r85462 | georg.brandl | 2010-10-14 09:32:52 +0200 (Do, 14 Okt 2010) | 1 line python#9964: fix running test_xml_etree under -OO. ........ r85463 | georg.brandl | 2010-10-14 09:34:56 +0200 (Do, 14 Okt 2010) | 1 line Better check for "any optimize option given". ........ r85464 | georg.brandl | 2010-10-14 09:42:27 +0200 (Do, 14 Okt 2010) | 1 line python#9964: fix running test_compileall under -O and -OO. ........ r85465 | georg.brandl | 2010-10-14 10:08:56 +0200 (Do, 14 Okt 2010) | 1 line python#9964: fix running test_cmd_line_script under -O and -OO. ........
1 parent 229fab3 commit aba9796

File tree

9 files changed

+133
-82
lines changed

9 files changed

+133
-82
lines changed

Doc/library/atexit.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ functions. Functions thus registered are automatically executed upon normal
1212
interpreter termination.
1313

1414
Note: the functions registered via this module are not called when the program
15-
is killed by a signal, when a Python fatal internal error is detected, or when
16-
:func:`os._exit` is called.
15+
is killed by a signal not handled by Python, when a Python fatal internal error
16+
is detected, or when :func:`os._exit` is called.
1717

1818

1919
.. function:: register(func, *args, **kargs)

Doc/library/os.path.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@ applications should use string objects to access all files.
258258

259259
.. function:: split(path)
260260

261-
Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last
262-
pathname component and *head* is everything leading up to that. The *tail* part
263-
will never contain a slash; if *path* ends in a slash, *tail* will be empty. If
264-
there is no slash in *path*, *head* will be empty. If *path* is empty, both
265-
*head* and *tail* are empty. Trailing slashes are stripped from *head* unless
266-
it is the root (one or more slashes only). In nearly all cases, ``join(head,
267-
tail)`` equals *path* (the only exception being when there were multiple slashes
268-
separating *head* from *tail*).
261+
Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
262+
last pathname component and *head* is everything leading up to that. The
263+
*tail* part will never contain a slash; if *path* ends in a slash, *tail*
264+
will be empty. If there is no slash in *path*, *head* will be empty. If
265+
*path* is empty, both *head* and *tail* are empty. Trailing slashes are
266+
stripped from *head* unless it is the root (one or more slashes only). In
267+
all cases, ``join(head, tail)`` returns a path to the same location as *path*
268+
(but the strings may differ).
269269

270270

271271
.. function:: splitdrive(path)

Doc/library/os.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,25 +1760,25 @@ written in Python, such as a mail server's external command delivery program.
17601760

17611761
Execute the command (a string) in a subshell. This is implemented by calling
17621762
the Standard C function :cfunc:`system`, and has the same limitations.
1763-
Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1764-
executed command.
1763+
Changes to :data:`sys.stdin`, etc. are not reflected in the environment of
1764+
the executed command. If *command* generates any output, it will be sent to
1765+
the interpreter standard output stream.
17651766

17661767
On Unix, the return value is the exit status of the process encoded in the
1767-
format specified for :func:`wait`. Note that POSIX does not specify the meaning
1768-
of the return value of the C :cfunc:`system` function, so the return value of
1769-
the Python function is system-dependent.
1770-
1771-
On Windows, the return value is that returned by the system shell after running
1772-
*command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1773-
:program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1774-
:program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1775-
the command run; on systems using a non-native shell, consult your shell
1776-
documentation.
1777-
1778-
The :mod:`subprocess` module provides more powerful facilities for spawning new
1779-
processes and retrieving their results; using that module is preferable to using
1780-
this function. Use the :mod:`subprocess` module. Check especially the
1781-
:ref:`subprocess-replacements` section.
1768+
format specified for :func:`wait`. Note that POSIX does not specify the
1769+
meaning of the return value of the C :cfunc:`system` function, so the return
1770+
value of the Python function is system-dependent.
1771+
1772+
On Windows, the return value is that returned by the system shell after
1773+
running *command*. The shell is given by the Windows environment variable
1774+
:envvar:`COMSPEC`: it is usually :program:`cmd.exe`, which returns the exit
1775+
status of the command run; on systems using a non-native shell, consult your
1776+
shell documentation.
1777+
1778+
The :mod:`subprocess` module provides more powerful facilities for spawning
1779+
new processes and retrieving their results; using that module is preferable
1780+
to using this function. See the :ref:`subprocess-replacements` section in
1781+
the :mod:`subprocess` documentation for some helpful recipes.
17821782

17831783
Availability: Unix, Windows.
17841784

Doc/library/profile.rst

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,6 @@ The Python Profilers
1010
.. module:: profile
1111
:synopsis: Python source profiler.
1212

13-
.. index:: single: InfoSeek Corporation
14-
15-
Copyright © 1994, by InfoSeek Corporation, all rights reserved.
16-
17-
Written by James Roskind. [#]_
18-
19-
Permission to use, copy, modify, and distribute this Python software and its
20-
associated documentation for any purpose (subject to the restriction in the
21-
following sentence) without fee is hereby granted, provided that the above
22-
copyright notice appears in all copies, and that both that copyright notice and
23-
this permission notice appear in supporting documentation, and that the name of
24-
InfoSeek not be used in advertising or publicity pertaining to distribution of
25-
the software without specific, written prior permission. This permission is
26-
explicitly restricted to the copying and modification of the software to remain
27-
in Python, compiled Python, or other languages (such as C) wherein the modified
28-
or derived code is exclusively imported into a Python module.
29-
30-
INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
31-
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
32-
SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
33-
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
34-
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
35-
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3613

3714
.. _profiler-introduction:
3815

@@ -43,33 +20,38 @@ Introduction to the profilers
4320
single: deterministic profiling
4421
single: profiling, deterministic
4522

46-
A :dfn:`profiler` is a program that describes the run time performance
47-
of a program, providing a variety of statistics. This documentation
48-
describes the profiler functionality provided in the modules
49-
:mod:`cProfile`, :mod:`profile` and :mod:`pstats`. This profiler
50-
provides :dfn:`deterministic profiling` of Python programs. It also
51-
provides a series of report generation tools to allow users to rapidly
52-
examine the results of a profile operation.
23+
A :dfn:`profiler` is a program that describes the run time performance of a
24+
program, providing a variety of statistics. This documentation describes the
25+
profiler functionality provided in the modules :mod:`cProfile`, :mod:`profile`
26+
and :mod:`pstats`. This profiler provides :dfn:`deterministic profiling` of
27+
Python programs. It also provides a series of report generation tools to allow
28+
users to rapidly examine the results of a profile operation.
5329

5430
The Python standard library provides two different profilers:
5531

56-
#. :mod:`cProfile` is recommended for most users; it's a C extension
57-
with reasonable overhead
58-
that makes it suitable for profiling long-running programs.
59-
Based on :mod:`lsprof`,
60-
contributed by Brett Rosen and Ted Czotter.
32+
1. :mod:`cProfile` is recommended for most users; it's a C extension with
33+
reasonable overhead that makes it suitable for profiling long-running
34+
programs. Based on :mod:`lsprof`, contributed by Brett Rosen and Ted
35+
Czotter.
6136

62-
#. :mod:`profile`, a pure Python module whose interface is imitated by
63-
:mod:`cProfile`. Adds significant overhead to profiled programs.
64-
If you're trying to extend
65-
the profiler in some way, the task might be easier with this module.
66-
Copyright © 1994, by InfoSeek Corporation.
37+
2. :mod:`profile`, a pure Python module whose interface is imitated by
38+
:mod:`cProfile`. Adds significant overhead to profiled programs. If you're
39+
trying to extend the profiler in some way, the task might be easier with this
40+
module. Copyright © 1994, by InfoSeek Corporation.
6741

6842
The :mod:`profile` and :mod:`cProfile` modules export the same interface, so
6943
they are mostly interchangeable; :mod:`cProfile` has a much lower overhead but
70-
is newer and might not be available on all systems.
71-
:mod:`cProfile` is really a compatibility layer on top of the internal
72-
:mod:`_lsprof` module.
44+
is newer and might not be available on all systems. :mod:`cProfile` is really a
45+
compatibility layer on top of the internal :mod:`_lsprof` module.
46+
47+
.. note::
48+
49+
The profiler modules are designed to provide an execution profile for a given
50+
program, not for benchmarking purposes (for that, there is :mod:`timeit` for
51+
resonably accurate results). This particularly applies to benchmarking
52+
Python code against C code: the profilers introduce overhead for Python code,
53+
but not for C-level functions, and so the C code would seem faster than any
54+
Python one.
7355

7456

7557
.. _profile-instant:
@@ -608,8 +590,26 @@ The resulting profiler will then call :func:`your_time_func`.
608590
best results with a custom timer, it might be necessary to hard-code it in the C
609591
source of the internal :mod:`_lsprof` module.
610592

611-
.. rubric:: Footnotes
612593

613-
.. [#] Updated and converted to LaTeX by Guido van Rossum. Further updated by Armin
614-
Rigo to integrate the documentation for the new :mod:`cProfile` module of Python
615-
2.5.
594+
Copyright and License Notices
595+
=============================
596+
597+
Copyright © 1994, by InfoSeek Corporation, all rights reserved.
598+
599+
Permission to use, copy, modify, and distribute this Python software and its
600+
associated documentation for any purpose (subject to the restriction in the
601+
following sentence) without fee is hereby granted, provided that the above
602+
copyright notice appears in all copies, and that both that copyright notice and
603+
this permission notice appear in supporting documentation, and that the name of
604+
InfoSeek not be used in advertising or publicity pertaining to distribution of
605+
the software without specific, written prior permission. This permission is
606+
explicitly restricted to the copying and modification of the software to remain
607+
in Python, compiled Python, or other languages (such as C) wherein the modified
608+
or derived code is exclusively imported into a Python module.
609+
610+
INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
611+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
612+
SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
613+
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
614+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
615+
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Doc/library/zipfile.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ ZipFile Objects
120120
because the default :program:`zip` and :program:`unzip` commands on Unix (the
121121
InfoZIP utilities) don't support these extensions.
122122

123+
If the file is created with mode ``'a'`` or ``'w'`` and then
124+
:meth:`close`\ d without adding any files to the archive, the appropriate
125+
ZIP structures for an empty archive will be written to the file.
126+
123127

124128
.. method:: ZipFile.close()
125129

Lib/test/test_zipfile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,31 @@ def test_read_return_size_stored(self):
902902
def test_read_return_size_deflated(self):
903903
self.check_read_return_size(zipfile.ZIP_DEFLATED)
904904

905+
def test_empty_zipfile(self):
906+
# Check that creating a file in 'w' or 'a' mode and closing without
907+
# adding any files to the archives creates a valid empty ZIP file
908+
zipf = zipfile.ZipFile(TESTFN, mode="w")
909+
zipf.close()
910+
try:
911+
zipf = zipfile.ZipFile(TESTFN, mode="r")
912+
except zipfile.BadZipFile:
913+
self.fail("Unable to create empty ZIP file in 'w' mode")
914+
915+
zipf = zipfile.ZipFile(TESTFN, mode="a")
916+
zipf.close()
917+
try:
918+
zipf = zipfile.ZipFile(TESTFN, mode="r")
919+
except:
920+
self.fail("Unable to create empty ZIP file in 'a' mode")
921+
922+
def test_open_empty_file(self):
923+
# Issue 1710703: Check that opening a file with less than 22 bytes
924+
# raises a BadZipfile exception (rather than the previously unhelpful
925+
# IOError)
926+
f = open(TESTFN, 'w')
927+
f.close()
928+
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
929+
905930
def tearDown(self):
906931
support.unlink(TESTFN)
907932
support.unlink(TESTFN2)

Lib/zipfile.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ def _EndRecData64(fpin, offset, endrec):
158158
"""
159159
Read the ZIP64 end-of-archive records and use that to update endrec
160160
"""
161-
fpin.seek(offset - sizeEndCentDir64Locator, 2)
161+
try:
162+
fpin.seek(offset - sizeEndCentDir64Locator, 2)
163+
except IOError:
164+
# If the seek fails, the file is not large enough to contain a ZIP64
165+
# end-of-archive record, so just return the end record we were given.
166+
return endrec
167+
162168
data = fpin.read(sizeEndCentDir64Locator)
163169
sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
164170
if sig != stringEndArchive64Locator:
@@ -723,14 +729,22 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
723729
if key == 'r':
724730
self._GetContents()
725731
elif key == 'w':
726-
pass
732+
# set the modified flag so central directory gets written
733+
# even if no files are added to the archive
734+
self._didModify = True
727735
elif key == 'a':
728-
try: # See if file is a zip file
736+
try:
737+
# See if file is a zip file
729738
self._RealGetContents()
730739
# seek to start of directory and overwrite
731740
self.fp.seek(self.start_dir, 0)
732-
except BadZipfile: # file is not a zip file, just append
741+
except BadZipfile:
742+
# file is not a zip file, just append
733743
self.fp.seek(0, 2)
744+
745+
# set the modified flag so central directory gets written
746+
# even if no files are added to the archive
747+
self._didModify = True
734748
else:
735749
if not self._filePassed:
736750
self.fp.close()
@@ -751,7 +765,10 @@ def _GetContents(self):
751765
def _RealGetContents(self):
752766
"""Read in the table of contents for the ZIP file."""
753767
fp = self.fp
754-
endrec = _EndRecData(fp)
768+
try:
769+
endrec = _EndRecData(fp)
770+
except IOError:
771+
raise BadZipfile("File is not a zip file")
755772
if not endrec:
756773
raise BadZipfile("File is not a zip file")
757774
if self.debug > 1:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Library
1818

1919
- Issue #10459: Update CJK character names to Unicode 5.1.
2020

21+
- Issue #1710703: Write structures for an empty ZIP archive when a ZipFile is
22+
created in modes 'a' or 'w' and then closed without adding any files. Raise
23+
BadZipfile (rather than IOError) when opening small non-ZIP files.
24+
2125
- Issue #4493: urllib.request adds '/' in front of path components which does not
2226
start with '/. Common behavior exhibited by browsers and other clients.
2327

Tools/README

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ modulator Interactively generate boiler plate for an extension
2626

2727
pynche A Tkinter-based color editor.
2828

29-
scripts A number of useful single-file programs, e.g. tabnanny.py
30-
(by Tim Peters), which checks for inconsistent mixing
31-
of tabs and spaces.
29+
scripts A number of useful single-file programs, e.g. tabnanny.py
30+
by Tim Peters, which checks for inconsistent mixing of
31+
tabs and spaces, and 2to3, which converts Python 2 code
32+
to Python 3 code.
3233

3334
unicode Tools used to generate unicode database files for
3435
Python 2.0 (by Fredrik Lundh).

0 commit comments

Comments
 (0)