mirror of https://github.com/python/cpython
(Merge 3.6) Issue #23840: tokenize.open() now closes the temporary binary file
on error to fix a resource warning.
This commit is contained in:
commit
6a0ee2b7db
|
@ -834,7 +834,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
|
||||||
|
|
||||||
|
@ -1246,6 +1246,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):
|
||||||
|
|
||||||
|
|
|
@ -435,11 +435,15 @@ def open(filename):
|
||||||
detect_encoding().
|
detect_encoding().
|
||||||
"""
|
"""
|
||||||
buffer = _builtin_open(filename, 'rb')
|
buffer = _builtin_open(filename, 'rb')
|
||||||
|
try:
|
||||||
encoding, lines = detect_encoding(buffer.readline)
|
encoding, lines = detect_encoding(buffer.readline)
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
text = TextIOWrapper(buffer, encoding, line_buffering=True)
|
text = TextIOWrapper(buffer, encoding, line_buffering=True)
|
||||||
text.mode = 'r'
|
text.mode = 'r'
|
||||||
return text
|
return text
|
||||||
|
except:
|
||||||
|
buffer.close()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def tokenize(readline):
|
def tokenize(readline):
|
||||||
|
|
|
@ -335,6 +335,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #23840: tokenize.open() now closes the temporary binary file on error
|
||||||
|
to fix a resource warning.
|
||||||
|
|
||||||
- Issue #16914: new debuglevel 2 in smtplib adds timestamps to debug output.
|
- Issue #16914: new debuglevel 2 in smtplib adds timestamps to debug output.
|
||||||
|
|
||||||
- Issue #7159: urllib.request now supports sending auth credentials
|
- Issue #7159: urllib.request now supports sending auth credentials
|
||||||
|
|
Loading…
Reference in New Issue