Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5c28cfdc
Commit
5c28cfdc
authored
Feb 05, 2011
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix entries pertaining to file I/O
parent
8d8f7c5e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
27 deletions
+30
-27
Doc/howto/pyporting.rst
Doc/howto/pyporting.rst
+30
-27
No files found.
Doc/howto/pyporting.rst
View file @
5c28cfdc
...
@@ -251,17 +251,6 @@ Python 2, but ``b'a' + 'b'`` in Python 3 is a :exc:`TypeError`. A similar issue
...
@@ -251,17 +251,6 @@ Python 2, but ``b'a' + 'b'`` in Python 3 is a :exc:`TypeError`. A similar issue
also comes about when doing comparisons between bytes and strings.
also comes about when doing comparisons between bytes and strings.
:mod:`io` Module
''''''''''''''''
The built-in ``open()`` function in Python 2 always returns a Python 2 string,
not a unicode string. This is problematic as Python 3's :func:`open` returns a
string if a file is not opened as binary and bytes if it is.
To help with compatibility, use :func:`io.open` instead of the built-in
``open()``. Since :func:`io.open` is essentially the same function in both
Python 2 and Python 3 it will help iron out any issues that might arise.
Handle Common "Gotchas"
Handle Common "Gotchas"
-----------------------
-----------------------
There are a few things that just consistently come up as sticking points for
There are a few things that just consistently come up as sticking points for
...
@@ -269,6 +258,34 @@ people which 2to3 cannot handle automatically or can easily be done in Python 2
...
@@ -269,6 +258,34 @@ people which 2to3 cannot handle automatically or can easily be done in Python 2
to help modernize your code.
to help modernize your code.
Specify when opening a file as binary
'''''''''''''''''''''''''''''''''''''
Unless you have been working on Windows, there is a chance you have not always
bothered to add the ``b`` mode when opening a binary file (e.g., ``rb`` for
binary reading). Under Python 3, binary files and text files are clearly
distinct and mutually incompatible; see the :mod:`io` module for details.
Therefore, you **must** make a decision of whether a file will be used for
binary access (allowing to read and/or write bytes data) or text access
(allowing to read and/or write unicode data).
Text files
''''''''''
Text files created using ``open()`` under Python 2 return byte strings,
while under Python 3 they return unicode strings. Depending on your porting
strategy, this can be an issue.
If you want text files to return unicode strings in Python 2, you have two
possibilities:
* Under Python 2.6 and higher, use :func:`io.open`. Since :func:`io.open`
is essentially the same function in both Python 2 and Python 3, it will
help iron out any issues that might arise.
* If pre-2.6 compatibility is needed, then you should use :func:`codecs.open`
instead. This will make sure that you get back unicode strings in Python 2.
Subclass ``object``
Subclass ``object``
'''''''''''''''''''
'''''''''''''''''''
New-style classes have been around since Python 2.2. You need to make sure you
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/)::
...
@@ -392,23 +409,9 @@ http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/)::
return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
Specify when opening a file as binary
'''''''''''''''''''''''''''''''''''''
Unless you have been working on Windows, there is a chance you have not always
bothered to add the ``b`` mode when opening a file (e.g., ``
Use :func:``codecs.open()``
'''''''''''''''''''''''''''
If you are not able to limit your Python 2 compatibility to 2.6 or newer (and
thus get to use :func:`io.open`), then you should make sure you use
:func:`codecs.open` over the built-in ``open()`` function. This will make sure
that you get back unicode strings in Python 2 when reading in text and an
instance of ``str`` when dealing with bytes.
Don't Index on Exceptions
Don't Index on Exceptions
'''''''''''''''''''''''''
'''''''''''''''''''''''''
In Python 2, the following worked::
In Python 2, the following worked::
>>> exc = Exception(1, 2, 3)
>>> exc = Exception(1, 2, 3)
...
@@ -423,9 +426,9 @@ sequence containing all arguments passed to the :meth:`__init__` method.
...
@@ -423,9 +426,9 @@ sequence containing all arguments passed to the :meth:`__init__` method.
Even better is to use documented attributes the exception provides.
Even better is to use documented attributes the exception provides.
Don't use ``__getslice__`` & Friends
Don't use ``__getslice__`` & Friends
''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''
Been deprecated for a while, but Python 3 finally drops support for
Been deprecated for a while, but Python 3 finally drops support for
``__getslice__()``, etc. Move completely over to :meth:`__getitem__` and
``__getslice__()``, etc. Move completely over to :meth:`__getitem__` and
friends.
friends.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment