Skip to content

Commit d50f01a

Browse files
gh-103935: Use io.open_code() when executing code in trace and profile modules (GH-103947)
1 parent bf0b8a9 commit d50f01a

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

Lib/cProfile.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import _lsprof
1010
import importlib.machinery
11+
import io
1112
import profile as _pyprofile
1213

1314
# ____________________________________________________________
@@ -168,7 +169,7 @@ def main():
168169
else:
169170
progname = args[0]
170171
sys.path.insert(0, os.path.dirname(progname))
171-
with open(progname, 'rb') as fp:
172+
with io.open_code(progname) as fp:
172173
code = compile(fp.read(), progname, 'exec')
173174
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
174175
origin=progname)

Lib/profile.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
import importlib.machinery
28+
import io
2829
import sys
2930
import time
3031
import marshal
@@ -588,7 +589,7 @@ def main():
588589
else:
589590
progname = args[0]
590591
sys.path.insert(0, os.path.dirname(progname))
591-
with open(progname, 'rb') as fp:
592+
with io.open_code(progname) as fp:
592593
code = compile(fp.read(), progname, 'exec')
593594
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
594595
origin=progname)

Lib/trace.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"""
5050
__all__ = ['Trace', 'CoverageResults']
5151

52+
import io
5253
import linecache
5354
import os
5455
import sys
@@ -716,7 +717,7 @@ def parse_ignore_dir(s):
716717
sys.argv = [opts.progname, *opts.arguments]
717718
sys.path[0] = os.path.dirname(opts.progname)
718719

719-
with open(opts.progname, 'rb') as fp:
720+
with io.open_code(opts.progname) as fp:
720721
code = compile(fp.read(), opts.progname, 'exec')
721722
# try to emulate __main__ namespace as much as possible
722723
globs = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use :func:`io.open_code` for files to be executed instead of raw :func:`open`

0 commit comments

Comments
 (0)