Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c28cfd

Browse files
committed
Fix entries pertaining to file I/O
1 parent 8d8f7c5 commit 5c28cfd

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

Doc/howto/pyporting.rst

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,24 +251,41 @@ Python 2, but ``b'a' + 'b'`` in Python 3 is a :exc:`TypeError`. A similar issue
251251
also comes about when doing comparisons between bytes and strings.
252252

253253

254-
:mod:`io` Module
255-
''''''''''''''''
256-
The built-in ``open()`` function in Python 2 always returns a Python 2 string,
257-
not a unicode string. This is problematic as Python 3's :func:`open` returns a
258-
string if a file is not opened as binary and bytes if it is.
259-
260-
To help with compatibility, use :func:`io.open` instead of the built-in
261-
``open()``. Since :func:`io.open` is essentially the same function in both
262-
Python 2 and Python 3 it will help iron out any issues that might arise.
263-
264-
265254
Handle Common "Gotchas"
266255
-----------------------
267256
There are a few things that just consistently come up as sticking points for
268257
people which 2to3 cannot handle automatically or can easily be done in Python 2
269258
to help modernize your code.
270259

271260

261+
Specify when opening a file as binary
262+
'''''''''''''''''''''''''''''''''''''
263+
264+
Unless you have been working on Windows, there is a chance you have not always
265+
bothered to add the ``b`` mode when opening a binary file (e.g., ``rb`` for
266+
binary reading). Under Python 3, binary files and text files are clearly
267+
distinct and mutually incompatible; see the :mod:`io` module for details.
268+
Therefore, you **must** make a decision of whether a file will be used for
269+
binary access (allowing to read and/or write bytes data) or text access
270+
(allowing to read and/or write unicode data).
271+
272+
Text files
273+
''''''''''
274+
275+
Text files created using ``open()`` under Python 2 return byte strings,
276+
while under Python 3 they return unicode strings. Depending on your porting
277+
strategy, this can be an issue.
278+
279+
If you want text files to return unicode strings in Python 2, you have two
280+
possibilities:
281+
282+
* Under Python 2.6 and higher, use :func:`io.open`. Since :func:`io.open`
283+
is essentially the same function in both Python 2 and Python 3, it will
284+
help iron out any issues that might arise.
285+
286+
* If pre-2.6 compatibility is needed, then you should use :func:`codecs.open`
287+
instead. This will make sure that you get back unicode strings in Python 2.
288+
272289
Subclass ``object``
273290
'''''''''''''''''''
274291
New-style classes have been around since Python 2.2. You need to make sure you
@@ -392,23 +409,9 @@ http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/)::
392409
return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
393410

394411

395-
Specify when opening a file as binary
396-
'''''''''''''''''''''''''''''''''''''
397-
Unless you have been working on Windows, there is a chance you have not always
398-
bothered to add the ``b`` mode when opening a file (e.g., ``
399-
400-
401-
Use :func:``codecs.open()``
402-
'''''''''''''''''''''''''''
403-
If you are not able to limit your Python 2 compatibility to 2.6 or newer (and
404-
thus get to use :func:`io.open`), then you should make sure you use
405-
:func:`codecs.open` over the built-in ``open()`` function. This will make sure
406-
that you get back unicode strings in Python 2 when reading in text and an
407-
instance of ``str`` when dealing with bytes.
408-
409-
410412
Don't Index on Exceptions
411413
'''''''''''''''''''''''''
414+
412415
In Python 2, the following worked::
413416

414417
>>> exc = Exception(1, 2, 3)
@@ -423,9 +426,9 @@ sequence containing all arguments passed to the :meth:`__init__` method.
423426

424427
Even better is to use documented attributes the exception provides.
425428

426-
427429
Don't use ``__getslice__`` & Friends
428430
''''''''''''''''''''''''''''''''''''
431+
429432
Been deprecated for a while, but Python 3 finally drops support for
430433
``__getslice__()``, etc. Move completely over to :meth:`__getitem__` and
431434
friends.

0 commit comments

Comments
 (0)