Skip to content

Commit d631371

Browse files
committed
Merged revisions 65209-65216,65225-65226,65233,65239,65246-65247,65255-65256 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r65209 | raymond.hettinger | 2008-07-23 19:08:18 -0500 (Wed, 23 Jul 2008) | 1 line Finish-up the partial conversion from int to Py_ssize_t for deque indices and length. ........ r65210 | raymond.hettinger | 2008-07-23 19:53:49 -0500 (Wed, 23 Jul 2008) | 1 line Parse to the correct datatype. ........ r65211 | benjamin.peterson | 2008-07-23 21:27:46 -0500 (Wed, 23 Jul 2008) | 1 line fix spacing ........ r65212 | benjamin.peterson | 2008-07-23 21:31:28 -0500 (Wed, 23 Jul 2008) | 1 line fix markup ........ r65213 | benjamin.peterson | 2008-07-23 21:45:37 -0500 (Wed, 23 Jul 2008) | 1 line add some documentation for 2to3 ........ r65214 | raymond.hettinger | 2008-07-24 00:38:48 -0500 (Thu, 24 Jul 2008) | 1 line Finish conversion from int to Py_ssize_t. ........ r65215 | raymond.hettinger | 2008-07-24 02:04:55 -0500 (Thu, 24 Jul 2008) | 1 line Convert from long to Py_ssize_t. ........ r65216 | georg.brandl | 2008-07-24 02:09:21 -0500 (Thu, 24 Jul 2008) | 2 lines Fix indentation. ........ r65225 | benjamin.peterson | 2008-07-25 11:55:37 -0500 (Fri, 25 Jul 2008) | 1 line teach .bzrignore about doc tools ........ r65226 | benjamin.peterson | 2008-07-25 12:02:11 -0500 (Fri, 25 Jul 2008) | 1 line document default value for fillvalue ........ r65233 | raymond.hettinger | 2008-07-25 13:43:33 -0500 (Fri, 25 Jul 2008) | 1 line Issue 1592: Better error reporting for operations on closed shelves. ........ r65239 | benjamin.peterson | 2008-07-25 16:59:53 -0500 (Fri, 25 Jul 2008) | 1 line fix indentation ........ r65246 | andrew.kuchling | 2008-07-26 08:08:19 -0500 (Sat, 26 Jul 2008) | 1 line This sentence continues to bug me; rewrite it for the second time ........ r65247 | andrew.kuchling | 2008-07-26 08:09:06 -0500 (Sat, 26 Jul 2008) | 1 line Remove extra words ........ r65255 | skip.montanaro | 2008-07-26 19:49:02 -0500 (Sat, 26 Jul 2008) | 3 lines Close issue 3437 - missing state change when Allow lines are processed. Adds test cases which use Allow: as well. ........ r65256 | skip.montanaro | 2008-07-26 19:50:41 -0500 (Sat, 26 Jul 2008) | 2 lines note robotparser bug fix. ........
1 parent 76b09ca commit d631371

File tree

14 files changed

+246
-55
lines changed

14 files changed

+246
-55
lines changed

.bzrignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ gb-18030-2000.xml
3535
tags
3636
TAGS
3737
.gdb_history
38+
Doc/tools/sphinx
39+
Doc/tools/jinja
40+
Doc/tools/pygments
41+
Doc/tools/docutils
3842
Modules/Setup
3943
Modules/Setup.config
4044
Modules/Setup.local
4145
Modules/config.c
4246
Parser/pgen
43-
Lib/plat-mac/errors.rsrc.df.rsrc
4447
Lib/lib2to3/Grammar*.pickle
4548
Lib/lib2to3/PatternGrammar*.pickle

Doc/glossary.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Glossary
1616
The typical Python prompt of the interactive shell when entering code for
1717
an indented code block.
1818

19+
2to3
20+
A tool that tries to convert Python 2.x code to Python 3.x code by
21+
handling most of the incompatibilites that can be detected by parsing the
22+
source and traversing the parse tree.
23+
24+
2to3 is available in the standard library as :mod:`lib2to3`; a standalone
25+
entry point is provided as :file:`Tools/scripts/2to3`. See
26+
:ref:`2to3-reference`.
27+
1928
abstract base class
2029
Abstract Base Classes (abbreviated ABCs) complement :term:`duck-typing` by
2130
providing a way to define interfaces when other techniques like :func:`hasattr`

Doc/library/2to3.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. _2to3-reference:
2+
3+
2to3 - Automated Python 2 to 3 code translation
4+
===============================================
5+
6+
.. sectionauthor:: Benjamin Peterson
7+
8+
2to3 is a Python program that reads your Python 2.x source code and applies a
9+
series of *fixers* to transform it into valid Python 3.x code.
10+
11+
12+
Using 2to3
13+
----------
14+
15+
2to3 can be run with a list of files to transform or a directory to recursively
16+
traverse looking for files with the ``.py`` extension.
17+
18+
Here is a sample Python 2.x source file, :file:`example.py`::
19+
20+
def greet(name):
21+
print "Hello, {0}!".format(name)
22+
print "What's your name?"
23+
name = raw_input()
24+
greet(name)
25+
26+
It can be converted to Python 3.x code via 2to3 on the command line::
27+
28+
$ 2to3 example.py
29+
30+
A diff against the original source file will be printed. 2to3 can also write
31+
the needed modifications right back to the source file. (A backup of the
32+
original file will also be made.) This is done with the :option:`-w` flag::
33+
34+
$ 2to3 -w example.py
35+
36+
:file:`example.py` will now look like this::
37+
38+
def greet(name):
39+
print("Hello, {0}!".format(name))
40+
print("What's your name?")
41+
name = input()
42+
greet(name)
43+
44+
Comments and and exact indentation will be preserved throughout the translation
45+
process.
46+
47+
By default, 2to3 will run a set of predefined fixers. The :option:`-l` flag
48+
lists all avaible fixers. An explicit set of fixers to run can be given by use
49+
of the :option:`-f` flag. The following example runs only the ``imports`` and
50+
``has_key`` fixers::
51+
52+
$ 2to3 -f imports -f has_key example.py
53+
54+
Some fixers are *explicit*, meaning they aren't run be default and must be
55+
listed on the command line. Here, in addition to the default fixers, the
56+
``idioms`` fixer is run::
57+
58+
$ 2to3 -f all -f idioms example.py
59+
60+
Notice how ``all`` enables all default fixers.
61+
62+
Sometimes 2to3 will find will find a place in your source code that needs to be
63+
changed, but 2to3 cannot fix automatically. In this case, 2to3 will print a
64+
warning beneath the diff for a file.
65+
66+
67+
:mod:`lib2to3` - 2to3's library
68+
-------------------------------
69+
70+
.. module:: lib2to3
71+
:synopsis: the 2to3 library
72+
.. moduleauthor:: Guido van Rossum
73+
.. moduleauthor:: Collin Winter
74+
75+
.. XXX What is the public interface anyway?

Doc/library/development.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The modules described in this chapter help you write software. For example, the
99
:mod:`pydoc` module takes a module and generates documentation based on the
1010
module's contents. The :mod:`doctest` and :mod:`unittest` modules contains
1111
frameworks for writing unit tests that automatically exercise code and verify
12-
that the expected output is produced.
12+
that the expected output is produced. :program:`2to3` can translate Python 2.x
13+
source code into valid Python 3.x code.
1314

1415
The list of modules described in this chapter is:
1516

@@ -19,4 +20,5 @@ The list of modules described in this chapter is:
1920
pydoc.rst
2021
doctest.rst
2122
unittest.rst
23+
2to3.rst
2224
test.rst

Doc/library/email.message.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Here are the methods of the :class:`Message` class:
9090

9191
.. method:: get_payload([i[, decode]])
9292

93-
Return a reference the current payload, which will be a list of
93+
Return the current payload, which will be a list of
9494
:class:`Message` objects when :meth:`is_multipart` is ``True``, or a
9595
string when :meth:`is_multipart` is ``False``. If the payload is a list
9696
and you mutate the list object, you modify the message's payload in place.

Doc/library/itertools.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ loops that truncate the stream.
295295
except IndexError:
296296
pass
297297

298-
If one of the iterables is potentially infinite, then the :func:`zip_longest`
299-
function should be wrapped with something that limits the number of calls (for
300-
example :func:`islice` or :func:`takewhile`).
298+
If one of the iterables is potentially infinite, then the
299+
:func:`izip_longest` function should be wrapped with something that limits
300+
the number of calls (for example :func:`islice` or :func:`takewhile`). If
301+
not specified, *fillvalue* defaults to ``None``.
301302

302303

303304
.. function:: permutations(iterable[, r])

Doc/whatsnew/2.6.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ The :meth:`register` method is useful when you've written a new
11441144
ABC that can describe an existing type or class, or if you want
11451145
to declare that some third-party class implements an ABC.
11461146
For example, if you defined a :class:`PrintableType` ABC,
1147-
it's legal to do:
1147+
it's legal to do::
11481148

11491149
# Register Python's types
11501150
PrintableType.register(int)

Lib/shelve.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464

6565
__all__ = ["Shelf","BsdDbShelf","DbfilenameShelf","open"]
6666

67+
class _ClosedDict(collections.MutableMapping):
68+
'Marker for a closed dict. Access attempts raise a ValueError.'
69+
70+
def closed(self, *args):
71+
raise ValueError('invalid operation on closed shelf')
72+
__iter__ = __len__ = __getitem__ = __setitem__ = __delitem__ = keys = closed
73+
74+
def __repr__(self):
75+
return '<Closed Dictionary>'
76+
6777
class Shelf(collections.MutableMapping):
6878
"""Base class for shelf implementations.
6979
@@ -127,7 +137,7 @@ def close(self):
127137
self.dict.close()
128138
except AttributeError:
129139
pass
130-
self.dict = 0
140+
self.dict = _ClosedDict()
131141

132142
def __del__(self):
133143
if not hasattr(self, 'writeback'):

Lib/test/test_robotparser.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,75 @@ def RobotTest(index, robots_txt, good_urls, bad_urls,
136136

137137
RobotTest(7, doc, good, bad)
138138

139+
# From Google: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=40364
140+
141+
# 8.
142+
doc = """
143+
User-agent: Googlebot
144+
Allow: /folder1/myfile.html
145+
Disallow: /folder1/
146+
"""
147+
148+
good = ['/folder1/myfile.html']
149+
bad = ['/folder1/anotherfile.html']
150+
151+
RobotTest(8, doc, good, bad, agent="Googlebot")
152+
153+
# 9. This file is incorrect because "Googlebot" is a substring of
154+
# "Googlebot-Mobile", so test 10 works just like test 9.
155+
doc = """
156+
User-agent: Googlebot
157+
Disallow: /
158+
159+
User-agent: Googlebot-Mobile
160+
Allow: /
161+
"""
162+
163+
good = []
164+
bad = ['/something.jpg']
165+
166+
RobotTest(9, doc, good, bad, agent="Googlebot")
167+
168+
good = []
169+
bad = ['/something.jpg']
170+
171+
RobotTest(10, doc, good, bad, agent="Googlebot-Mobile")
172+
173+
# 11. Get the order correct.
174+
doc = """
175+
User-agent: Googlebot-Mobile
176+
Allow: /
177+
178+
User-agent: Googlebot
179+
Disallow: /
180+
"""
181+
182+
good = []
183+
bad = ['/something.jpg']
184+
185+
RobotTest(11, doc, good, bad, agent="Googlebot")
186+
187+
good = ['/something.jpg']
188+
bad = []
189+
190+
RobotTest(12, doc, good, bad, agent="Googlebot-Mobile")
191+
192+
193+
# 13. Google also got the order wrong in #8. You need to specify the
194+
# URLs from more specific to more general.
195+
doc = """
196+
User-agent: Googlebot
197+
Allow: /folder1/myfile.html
198+
Disallow: /folder1/
199+
"""
200+
201+
good = ['/folder1/myfile.html']
202+
bad = ['/folder1/anotherfile.html']
203+
204+
RobotTest(13, doc, good, bad, agent="googlebot")
205+
206+
207+
139208
class NetworkTestCase(unittest.TestCase):
140209

141210
def testPasswordProtectedSite(self):

Lib/test/test_shelve.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ def tearDown(self):
4747
for f in glob.glob(self.fn+"*"):
4848
support.unlink(f)
4949

50+
def test_close(self):
51+
d1 = {}
52+
s = shelve.Shelf(d1, protocol=2, writeback=False)
53+
s['key1'] = [1,2,3,4]
54+
self.assertEqual(s['key1'], [1,2,3,4])
55+
self.assertEqual(len(s), 1)
56+
s.close()
57+
self.assertRaises(ValueError, len, s)
58+
try:
59+
s['key1']
60+
except ValueError:
61+
pass
62+
else:
63+
self.fail('Closed shelf should not find a key')
64+
5065
def test_ascii_file_shelf(self):
5166
s = shelve.open(self.fn, protocol=0)
5267
try:

0 commit comments

Comments
 (0)