Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9691750

Browse files
committed
Issue #22599: Enhance tokenize.open() to be able to call it during Python
finalization. Before the module kept a reference to the builtins module, but the module attributes are cleared during Python finalization. Instead, keep directly a reference to the open() function. This enhancement is not perfect, calling tokenize.open() can still fail if called very late during Python finalization. Usually, the function is called by the linecache module which is called to display a traceback or emit a warning.
1 parent 3ab745e commit 9691750

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Lib/tokenize.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
__credits__ = ('GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, '
2525
'Skip Montanaro, Raymond Hettinger, Trent Nelson, '
2626
'Michael Foord')
27-
import builtins
2827
from codecs import lookup, BOM_UTF8
2928
import collections
3029
from io import TextIOWrapper
@@ -430,11 +429,13 @@ def find_cookie(line):
430429
return default, [first, second]
431430

432431

432+
_builtin_open = open
433+
433434
def open(filename):
434435
"""Open a file in read only mode using the encoding detected by
435436
detect_encoding().
436437
"""
437-
buffer = builtins.open(filename, 'rb')
438+
buffer = _builtin_open(filename, 'rb')
438439
encoding, lines = detect_encoding(buffer.readline)
439440
buffer.seek(0)
440441
text = TextIOWrapper(buffer, encoding, line_buffering=True)
@@ -657,7 +658,7 @@ def error(message, filename=None, location=None):
657658
# Tokenize the input
658659
if args.filename:
659660
filename = args.filename
660-
with builtins.open(filename, 'rb') as f:
661+
with _builtin_open(filename, 'rb') as f:
661662
tokens = list(tokenize(f.readline))
662663
else:
663664
filename = "<stdin>"

0 commit comments

Comments
 (0)