X Tutup
Skip to content

Commit f91df04

Browse files
committed
Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r69364 | kristjan.jonsson | 2009-02-06 04:17:34 -0600 (Fri, 06 Feb 2009) | 1 line Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error. ........ r69365 | armin.rigo | 2009-02-06 05:46:26 -0600 (Fri, 06 Feb 2009) | 2 lines Ivan on IRC in #twisted reported this crasher. ........ r69409 | georg.brandl | 2009-02-07 06:21:17 -0600 (Sat, 07 Feb 2009) | 1 line python#5174: fix wrong file closing in example. ........ r69410 | neil.schemenauer | 2009-02-07 08:53:31 -0600 (Sat, 07 Feb 2009) | 4 lines Fix broken test in test_hotshot. Treating the current directory as an empty file is sloppy and non-portable. Use NamedTemporaryFile to make an empty file. ........ r69413 | neil.schemenauer | 2009-02-07 12:35:16 -0600 (Sat, 07 Feb 2009) | 2 lines Add test for issue #999042, explict global statement works. ........ r69417 | benjamin.peterson | 2009-02-07 17:01:19 -0600 (Sat, 07 Feb 2009) | 1 line document individual 2to3 fixers ........ r69435 | benjamin.peterson | 2009-02-08 08:38:13 -0600 (Sun, 08 Feb 2009) | 1 line document numliterals fixer ........ r69442 | benjamin.peterson | 2009-02-08 09:14:57 -0600 (Sun, 08 Feb 2009) | 1 line a few edits and typos ........ r69447 | vinay.sajip | 2009-02-08 13:06:08 -0600 (Sun, 08 Feb 2009) | 2 lines Issue python#5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5. ........ r69495 | kristjan.jonsson | 2009-02-10 07:32:24 -0600 (Tue, 10 Feb 2009) | 1 line Issue 4804. Add a function to test the validity of file descriptors on Windows, and stop using global runtime settings to silence the warnings / assertions. ........ r69519 | gregory.p.smith | 2009-02-11 17:45:25 -0600 (Wed, 11 Feb 2009) | 3 lines Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even on LP64 platforms (most 64-bit Linux, bsd, unix systems). ........ r69520 | benjamin.peterson | 2009-02-11 21:50:00 -0600 (Wed, 11 Feb 2009) | 1 line os.fsync() should be used to ensure that data is written to disk ........ r69521 | benjamin.peterson | 2009-02-11 22:17:04 -0600 (Wed, 11 Feb 2009) | 1 line no need for this __bases__ trick anymore ........
1 parent a38dd8f commit f91df04

File tree

12 files changed

+341
-59
lines changed

12 files changed

+341
-59
lines changed

Doc/library/2to3.rst

Lines changed: 263 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ adapted to custom applications in which Python code needs to be edited
1414
automatically.
1515

1616

17+
.. _2to3-using:
18+
1719
Using 2to3
1820
----------
1921

@@ -52,10 +54,10 @@ After transformation, :file:`example.py` looks like this::
5254

5355
Comments and exact indentation are preserved throughout the translation process.
5456

55-
By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists
56-
all available fixers. An explicit set of fixers to run can be given with
57-
:option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The
58-
following example runs only the ``imports`` and ``has_key`` fixers::
57+
By default, 2to3 runs a set of :ref:`predefined fixers <2to3-fixers>`. The
58+
:option:`-l` flag lists all available fixers. An explicit set of fixers to run
59+
can be given with :option:`-f`. Likewise the :option:`-x` explicitly disables a
60+
fixer. The following example runs only the ``imports`` and ``has_key`` fixers::
5961

6062
$ 2to3 -f imports -f has_key example.py
6163

@@ -84,12 +86,263 @@ document could also be refactored with this option.
8486
The :option:`-v` option enables output of more information on the translation
8587
process.
8688

87-
When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead of
88-
a statement. This is useful when ``from __future__ import print_function`` is
89-
being used. If this option is not given, the print fixer will surround print
90-
calls in an extra set of parentheses because it cannot differentiate between the
91-
print statement with parentheses (such as ``print ("a" + "b" + "c")``) and a
92-
true function call.
89+
When the :option:`-p` is passed, the :2to3fixer:`print` fixer ``print`` as a
90+
function instead of a statement. This is useful when ``from __future__ import
91+
print_function`` is being used. If this option is not given, the print fixer
92+
will surround print calls in an extra set of parentheses because it cannot
93+
differentiate between the print statement with parentheses (such as ``print
94+
("a" + "b" + "c")``) and a true function call.
95+
96+
97+
.. _2to3-fixers:
98+
99+
Fixers
100+
------
101+
102+
Each step of tranforming code is encapsulated in a fixer. The command ``2to3
103+
-l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on
104+
and off individually. They are described here in more detail.
105+
106+
107+
.. 2to3fixer:: apply
108+
109+
Removes usage of :func:`apply`. For example ``apply(function, *args,
110+
**kwargs)`` is converted to ``function(*args, **kwargs)``.
111+
112+
.. 2to3fixer:: basestring
113+
114+
Converts :class:`basestring` to :class:`str`.
115+
116+
.. 2to3fixer:: buffer
117+
118+
Converts :class:`buffer` to :class:`memoryview`. This fixer is optional
119+
because the :class:`memoryview` API is similar but not exactly the same as
120+
that of :class:`buffer`.
121+
122+
.. 2to3fixer:: callable
123+
124+
Converts ``callable(x)`` to ``hasattr(x, "__call_")``.
125+
126+
.. 2to3fixer:: dict
127+
128+
Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to
129+
:meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and
130+
:meth:`dict.itervalues` to :meth:`dict.values`. It also wraps existing
131+
usages of :meth:`dict.items`, :meth:`dict.keys`, and :meth:`dict.values` in a
132+
call to :class:`list`.
133+
134+
.. 2to3fixer:: except
135+
136+
Converts ``except X, T`` to ``except X as T``.
137+
138+
.. 2to3fixer:: exec
139+
140+
Converts the :keyword:`exec` statement to the :func:`exec` function.
141+
142+
.. 2to3fixer:: execfile
143+
144+
Removes usage of :func:`execfile`. The argument to :func:`execfile` is
145+
wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
146+
147+
.. 2to3fixer:: filter
148+
149+
Wraps :func:`filter` usage in a :class:`list` call.
150+
151+
.. 2to3fixer:: funcattrs
152+
153+
Fixes function attributes that have been renamed. For example,
154+
``my_function.func_closure`` is converted to ``my_function.__closure__``.
155+
156+
.. 2to3fixer:: future
157+
158+
Removes ``from __future__ import new_feature`` statements.
159+
160+
.. 2to3fixer:: getcwdu
161+
162+
Renames :func:`os.getcwdu` to :func:`os.getcwd`.
163+
164+
.. 2to3fixer:: has_key
165+
166+
Changes ``dict.has_key(key)`` to ``key in dict``.
167+
168+
.. 2to3fixer:: idioms
169+
170+
This optional fixer preforms several transformations that make Python code
171+
more idiomatic. Type comparisions like ``type(x) is SomeClass`` and
172+
``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``.
173+
``while 1`` becomes ``while True``. This fixer also tries to make use of
174+
:func:`sorted` in appropiate places. For example, this block ::
175+
176+
L = list(some_iterable)
177+
L.sort()
178+
179+
is changed to ::
180+
181+
L = sorted(some_iterable)
182+
183+
.. 2to3fixer:: import
184+
185+
Detects sibling imports and converts them to relative imports.
186+
187+
.. 2to3fixer:: imports
188+
189+
Handles module renames in the standard library.
190+
191+
.. 2to3fixer:: imports2
192+
193+
Handles other modules renames in the standard library. It is separate from
194+
the :2to3fixer:`imports` fixer only because of technical limitations.
195+
196+
.. 2to3fixer:: input
197+
198+
Converts ``input(prompt)`` to ``eval(input(prompt))``
199+
200+
.. 2to3fixer:: intern
201+
202+
Converts :func:`intern` to :func:`sys.intern`.
203+
204+
.. 2to3fixer:: isinstance
205+
206+
Fixes duplicate types in the second argument of :func:`isinstance`. For
207+
example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x,
208+
(int))``.
209+
210+
.. 2to3fixer:: itertools_imports
211+
212+
Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and
213+
:func:`itertools.imap`. Imports of :func:`itertools.ifilterfalse` are also
214+
changed to :func:`itertools.filterfalse`.
215+
216+
.. 2to3fixer:: itertools
217+
218+
Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and
219+
:func:`itertools.imap` to their builtin equivalents.
220+
:func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`.
221+
222+
.. 2to3fixer:: long
223+
224+
Strips the ``L`` prefix on long literals and renames :class:`long` to
225+
:class:`int`.
226+
227+
.. 2to3fixer:: map
228+
229+
Wraps :func:`map` in a :class:`list` call. It also changes ``map(None, x)``
230+
to ``list(x)``. Using ``from future_builtins import map`` disables this
231+
fixer.
232+
233+
.. 2to3fixer:: metaclass
234+
235+
Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class
236+
body) to the new (``class X(metaclass=Meta)``).
237+
238+
.. 2to3fixer:: methodattrs
239+
240+
Fixes old method attribute names. For example, ``meth.im_func`` is converted
241+
to ``meth.__func__``.
242+
243+
.. 2to3fixer:: ne
244+
245+
Converts the old not-equal syntax, ``<>``, to ``!=``.
246+
247+
.. 2to3fixer:: next
248+
249+
Converts the use of iterator's :meth:`next` methods to the :func:`next`
250+
function. It also renames :meth:`next` methods to :meth:`~object.__next__`.
251+
252+
.. 2to3fixer:: nonzero
253+
254+
Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`.
255+
256+
.. 2to3fixer:: numliterals
257+
258+
Converts octal literals into the new syntax.
259+
260+
.. 2to3fixer:: paren
261+
262+
Add extra parenthesis where they are required in list comprehensions. For
263+
example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``.
264+
265+
.. 2to3fixer:: print
266+
267+
Converts the :keyword:`print` statement to the :func:`print` function.
268+
269+
.. 2to3fixer:: raises
270+
271+
Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise
272+
E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be
273+
incorrect because substituting tuples for exceptions has been removed in 3.0.
274+
275+
.. 2to3fixer:: raw_input
276+
277+
Converts :func:`raw_input` to :func:`input`.
278+
279+
.. 2to3fixer:: reduce
280+
281+
Handles the move of :func:`reduce` to :func:`functools.reduce`.
282+
283+
.. 2to3fixer:: renames
284+
285+
Changes :data:`sys.maxint` to :data:`sys.maxsize`.
286+
287+
.. 2to3fixer:: repr
288+
289+
Replaces backtick repr with the :func:`repr` function.
290+
291+
.. 2to3fixer:: set_literal
292+
293+
Replaces use of the :class:`set` constructor with set literals. This fixer
294+
is optional.
295+
296+
.. 2to3fixer:: standard_error
297+
298+
Renames :exc:`StandardError` to :exc:`Exception`.
299+
300+
.. 2to3fixer:: sys_exc
301+
302+
Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`,
303+
:data:`sys.exc_traceback` to use :func:`sys.exc_info`.
304+
305+
.. 2to3fixer:: throw
306+
307+
Fixes the API change in generator's :meth:`throw` method.
308+
309+
.. 2to3fixer:: tuple_params
310+
311+
Removes implicit tuple parameter unpacking. This fixer inserts temporary
312+
variables.
313+
314+
.. 2to3fixer:: types
315+
316+
Fixes code broken from the removal of some members in the :mod:`types`
317+
module.
318+
319+
.. 2to3fixer:: unicode
320+
321+
Renames :class:`unicode` to :class:`str`.
322+
323+
.. 2to3fixer:: urllib
324+
325+
Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib`
326+
package.
327+
328+
.. 2to3fixer:: ws_comma
329+
330+
Removes excess whitespace from comma separated items. This fixer is
331+
optional.
332+
333+
.. 2to3fixer:: xrange
334+
335+
Renames :func:`xrange` to :func:`range` and wraps existing :func:`range`
336+
calls with :class:`list`.
337+
338+
.. 2to3fixer:: xreadlines
339+
340+
Changes ``for x in file.xreadlines()`` to ``for x in file``.
341+
342+
.. 2to3fixer:: zip
343+
344+
Wraps :func:`zip` usage in a :class:`list` call. This is disabled when
345+
``from future_builtins import zip`` appears.
93346
94347
95348
:mod:`lib2to3` - 2to3's library

Doc/library/stdtypes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,11 @@ Files have the following methods:
20612061
Flush the internal buffer, like ``stdio``'s :cfunc:`fflush`. This may be a
20622062
no-op on some file-like objects.
20632063

2064+
.. note::
2065+
2066+
:meth:`flush` does not necessarily write the file's data to disk. Use
2067+
:meth:`flush` followed by :func:`os.fsync` to ensure this behavior.
2068+
20642069

20652070
.. method:: file.fileno()
20662071

Doc/tools/sphinxext/pyspecific.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ def setup(app):
122122
app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
123123
app.add_description_unit('opcode', 'opcode', '%s (opcode)',
124124
parse_opcode_signature)
125+
app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)')

Include/fileobject.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ PyAPI_FUNC(int) _Py_SetFileSystemEncoding(PyObject *);
3030
PyAPI_FUNC(PyObject *) PyFile_NewStdPrinter(int);
3131
PyAPI_DATA(PyTypeObject) PyStdPrinter_Type;
3232

33+
#if defined _MSC_VER && _MSC_VER >= 1400
34+
/* A routine to check if a file descriptor is valid on Windows. Returns 0
35+
* and sets errno to EBADF if it isn't. This is to avoid Assertions
36+
* from various functions in the Windows CRT beginning with
37+
* Visual Studio 2005
38+
*/
39+
int _PyVerify_fd(int fd);
40+
#else
41+
#define _PyVerify_fd(A) (1) /* dummy */
42+
#endif
43+
3344
#ifdef __cplusplus
3445
}
3546
#endif

Lib/logging/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,19 @@ def emit(self, record):
758758
"""
759759
try:
760760
msg = self.format(record)
761+
stream = self.stream
761762
fs = "%s\n"
762763
if not _unicode: #if no unicode support...
763-
self.stream.write(fs % msg)
764+
stream.write(fs % msg)
764765
else:
765766
try:
766-
if getattr(self.stream, 'encoding', None) is not None:
767-
self.stream.write(fs % msg.encode(self.stream.encoding))
767+
if (isinstance(msg, unicode) or
768+
getattr(stream, 'encoding', None) is None):
769+
stream.write(fs % msg)
768770
else:
769-
self.stream.write(fs % msg)
771+
stream.write(fs % msg.encode(stream.encoding))
770772
except UnicodeError:
771-
self.stream.write(fs % msg.encode("UTF-8"))
773+
stream.write(fs % msg.encode("UTF-8"))
772774
self.flush()
773775
except (KeyboardInterrupt, SystemExit):
774776
raise
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
The compiler (>= 2.5) recurses happily.
3+
"""
4+
5+
compile('()'*9**5, '?', 'exec')

0 commit comments

Comments
 (0)
X Tutup