mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
(Merge 3.5) Issue #23840: tokenize.open() now closes the temporary binary file
on error to fix a resource warning.
This commit is contained in:
commit
24d262af0b
3 changed files with 21 additions and 6 deletions
|
@ -834,7 +834,7 @@ from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
|
|||
STRING, ENDMARKER, ENCODING, tok_name, detect_encoding,
|
||||
open as tokenize_open, Untokenizer)
|
||||
from io import BytesIO
|
||||
from unittest import TestCase
|
||||
from unittest import TestCase, mock
|
||||
import os, sys, glob
|
||||
import token
|
||||
|
||||
|
@ -1246,6 +1246,14 @@ class TestDetectEncoding(TestCase):
|
|||
ins = Bunk(lines, path)
|
||||
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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue