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
688a11a0
Commit
688a11a0
authored
May 26, 2015
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #23840: tokenize.open() now closes the temporary binary file on error to
fix a resource warning.
parent
f986f2b0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
6 deletions
+21
-6
Lib/test/test_tokenize.py
Lib/test/test_tokenize.py
+9
-1
Lib/tokenize.py
Lib/tokenize.py
+9
-5
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/test/test_tokenize.py
View file @
688a11a0
...
@@ -646,7 +646,7 @@ from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
...
@@ -646,7 +646,7 @@ from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
STRING
,
ENDMARKER
,
ENCODING
,
tok_name
,
detect_encoding
,
STRING
,
ENDMARKER
,
ENCODING
,
tok_name
,
detect_encoding
,
open
as
tokenize_open
,
Untokenizer
)
open
as
tokenize_open
,
Untokenizer
)
from
io
import
BytesIO
from
io
import
BytesIO
from
unittest
import
TestCase
from
unittest
import
TestCase
,
mock
import
os
,
sys
,
glob
import
os
,
sys
,
glob
import
token
import
token
...
@@ -1058,6 +1058,14 @@ class TestDetectEncoding(TestCase):
...
@@ -1058,6 +1058,14 @@ class TestDetectEncoding(TestCase):
ins
=
Bunk
(
lines
,
path
)
ins
=
Bunk
(
lines
,
path
)
detect_encoding
(
ins
.
readline
)
detect_encoding
(
ins
.
readline
)
def
test_open_error
(
self
):
# Issue #23840: open() must close the binary file on error
m
=
BytesIO
(
b'#coding:xxx'
)
with
mock
.
patch
(
'tokenize._builtin_open'
,
return_value
=
m
):
self
.
assertRaises
(
SyntaxError
,
tokenize_open
,
'foobar'
)
self
.
assertTrue
(
m
.
closed
)
class
TestTokenize
(
TestCase
):
class
TestTokenize
(
TestCase
):
...
...
Lib/tokenize.py
View file @
688a11a0
...
@@ -435,11 +435,15 @@ def open(filename):
...
@@ -435,11 +435,15 @@ def open(filename):
detect_encoding
().
detect_encoding
().
"""
"""
buffer = _builtin_open(filename, 'rb')
buffer = _builtin_open(filename, 'rb')
encoding, lines = detect_encoding(buffer.readline)
try:
buffer.seek(0)
encoding, lines = detect_encoding(buffer.readline)
text = TextIOWrapper(buffer, encoding, line_buffering=True)
buffer.seek(0)
text.mode = 'r'
text = TextIOWrapper(buffer, encoding, line_buffering=True)
return text
text.mode = 'r'
return text
except:
buffer.close()
raise
def tokenize(readline):
def tokenize(readline):
...
...
Misc/NEWS
View file @
688a11a0
...
@@ -59,6 +59,9 @@ Core and Builtins
...
@@ -59,6 +59,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #23840: tokenize.open() now closes the temporary binary file on error
to fix a resource warning.
- Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked
- Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked
cursor type.
cursor type.
...
...
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