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
272d888c
Commit
272d888c
authored
Jun 16, 2017
by
Victor Stinner
Committed by
GitHub
Jun 16, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-29783: Replace codecs.open() with io.open() (#599)
parent
faa63d1e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
18 additions
and
39 deletions
+18
-39
Doc/tools/extensions/pyspecific.py
Doc/tools/extensions/pyspecific.py
+2
-5
Lib/codecs.py
Lib/codecs.py
+4
-3
Lib/lib2to3/pgen2/driver.py
Lib/lib2to3/pgen2/driver.py
+1
-4
Lib/lib2to3/refactor.py
Lib/lib2to3/refactor.py
+10
-26
Lib/test/test_argparse.py
Lib/test/test_argparse.py
+1
-1
No files found.
Doc/tools/extensions/pyspecific.py
View file @
272d888c
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
"""
"""
import
re
import
re
import
codecs
import
io
from
os
import
path
from
os
import
path
from
time
import
asctime
from
time
import
asctime
from
pprint
import
pformat
from
pprint
import
pformat
...
@@ -254,11 +254,8 @@ class MiscNews(Directive):
...
@@ -254,11 +254,8 @@ class MiscNews(Directive):
fpath = path.join(source_dir, fname)
fpath = path.join(source_dir, fname)
self.state.document.settings.record_dependencies.add(fpath)
self.state.document.settings.record_dependencies.add(fpath)
try:
try:
fp = codecs.open(fpath, encoding='utf-8')
with io.open(fpath, encoding='utf-8') as fp:
try:
content = fp.read()
content = fp.read()
finally:
fp.close()
except Exception:
except Exception:
text = 'The NEWS file is not available.'
text = 'The NEWS file is not available.'
node = nodes.strong(text, text)
node = nodes.strong(text, text)
...
...
Lib/codecs.py
View file @
272d888c
...
@@ -5,9 +5,10 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
...
@@ -5,9 +5,10 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
"""
#"
"""
import
builtins
,
sys
import
builtins
import
sys
### Registry and builtin stateless codec functions
### Registry and builtin stateless codec functions
...
@@ -739,7 +740,7 @@ class StreamReaderWriter:
...
@@ -739,7 +740,7 @@ class StreamReaderWriter:
"""
"""
return
getattr
(
self
.
stream
,
name
)
return
getattr
(
self
.
stream
,
name
)
# these are needed to make "with
codecs.open
(...)" work properly
# these are needed to make "with
StreamReaderWriter
(...)" work properly
def
__enter__
(
self
):
def
__enter__
(
self
):
return
self
return
self
...
...
Lib/lib2to3/pgen2/driver.py
View file @
272d888c
...
@@ -94,11 +94,8 @@ class Driver(object):
...
@@ -94,11 +94,8 @@ class Driver(object):
def
parse_file
(
self
,
filename
,
encoding
=
None
,
debug
=
False
):
def
parse_file
(
self
,
filename
,
encoding
=
None
,
debug
=
False
):
"""Parse a file and return the syntax tree."""
"""Parse a file and return the syntax tree."""
stream
=
codecs
.
open
(
filename
,
"r"
,
encoding
)
with
io
.
open
(
filename
,
"r"
,
encoding
=
encoding
)
as
stream
:
try
:
return
self
.
parse_stream
(
stream
,
debug
)
return
self
.
parse_stream
(
stream
,
debug
)
finally
:
stream
.
close
()
def
parse_string
(
self
,
text
,
debug
=
False
):
def
parse_string
(
self
,
text
,
debug
=
False
):
"""Parse a string and return the syntax tree."""
"""Parse a string and return the syntax tree."""
...
...
Lib/lib2to3/refactor.py
View file @
272d888c
...
@@ -12,12 +12,12 @@ __author__ = "Guido van Rossum <guido@python.org>"
...
@@ -12,12 +12,12 @@ __author__ = "Guido van Rossum <guido@python.org>"
# Python imports
# Python imports
import
io
import
os
import
os
import
sys
import
sys
import
logging
import
logging
import
operator
import
operator
import
collections
import
collections
import
io
from
itertools
import
chain
from
itertools
import
chain
# Local imports
# Local imports
...
@@ -107,22 +107,6 @@ def get_fixers_from_package(pkg_name):
...
@@ -107,22 +107,6 @@ def get_fixers_from_package(pkg_name):
def
_identity
(
obj
):
def
_identity
(
obj
):
return
obj
return
obj
if
sys
.
version_info
<
(
3
,
0
):
import
codecs
_open_with_encoding
=
codecs
.
open
# codecs.open doesn't translate newlines sadly.
def
_from_system_newlines
(
input
):
return
input
.
replace
(
"
\
r
\
n
"
,
"
\
n
"
)
def
_to_system_newlines
(
input
):
if
os
.
linesep
!=
"
\
n
"
:
return
input
.
replace
(
"
\
n
"
,
os
.
linesep
)
else
:
return
input
else
:
_open_with_encoding
=
open
_from_system_newlines
=
_identity
_to_system_newlines
=
_identity
def
_detect_future_features
(
source
):
def
_detect_future_features
(
source
):
have_docstring
=
False
have_docstring
=
False
...
@@ -330,8 +314,8 @@ class RefactoringTool(object):
...
@@ -330,8 +314,8 @@ class RefactoringTool(object):
encoding
=
tokenize
.
detect_encoding
(
f
.
readline
)[
0
]
encoding
=
tokenize
.
detect_encoding
(
f
.
readline
)[
0
]
finally
:
finally
:
f
.
close
()
f
.
close
()
with
_open_with_encoding
(
filename
,
"r"
,
encoding
=
encoding
)
as
f
:
with
io
.
open
(
filename
,
"r"
,
encoding
=
encoding
)
as
f
:
return
_from_system_newlines
(
f
.
read
()
),
encoding
return
f
.
read
(
),
encoding
def
refactor_file
(
self
,
filename
,
write
=
False
,
doctests_only
=
False
):
def
refactor_file
(
self
,
filename
,
write
=
False
,
doctests_only
=
False
):
"""Refactors a file."""
"""Refactors a file."""
...
@@ -530,16 +514,16 @@ class RefactoringTool(object):
...
@@ -530,16 +514,16 @@ class RefactoringTool(object):
set.
set.
"""
"""
try
:
try
:
f
=
_open_with_encoding
(
filename
,
"w"
,
encoding
=
encoding
)
f
p
=
io
.
open
(
filename
,
"w"
,
encoding
=
encoding
)
except
OSError
as
err
:
except
OSError
as
err
:
self
.
log_error
(
"Can't create %s: %s"
,
filename
,
err
)
self
.
log_error
(
"Can't create %s: %s"
,
filename
,
err
)
return
return
try
:
f
.
write
(
_to_system_newlines
(
new_text
))
with
fp
:
except
OSError
as
err
:
try
:
self
.
log_error
(
"Can't write %s: %s"
,
filename
,
err
)
fp
.
write
(
new_text
)
finally
:
except
OSError
as
err
:
f
.
close
(
)
self
.
log_error
(
"Can't write %s: %s"
,
filename
,
err
)
self
.
log_debug
(
"Wrote changes to %s"
,
filename
)
self
.
log_debug
(
"Wrote changes to %s"
,
filename
)
self
.
wrote
=
True
self
.
wrote
=
True
...
...
Lib/test/test_argparse.py
View file @
272d888c
...
@@ -4598,7 +4598,7 @@ class TestEncoding(TestCase):
...
@@ -4598,7 +4598,7 @@ class TestEncoding(TestCase):
def
_test_module_encoding
(
self
,
path
):
def
_test_module_encoding
(
self
,
path
):
path
,
_
=
os
.
path
.
splitext
(
path
)
path
,
_
=
os
.
path
.
splitext
(
path
)
path
+=
".py"
path
+=
".py"
with
codecs
.
open
(
path
,
'r'
,
'utf-8'
)
as
f
:
with
open
(
path
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
f
.
read
()
f
.
read
()
def
test_argparse_module_encoding
(
self
):
def
test_argparse_module_encoding
(
self
):
...
...
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