Skip to content

Commit

Permalink
gh-103935: Use io.open_code() when executing code in trace and prof…
Browse files Browse the repository at this point in the history
…ile modules (GH-103947)
  • Loading branch information
gaogaotiantian committed Apr 27, 2023
1 parent bf0b8a9 commit d50f01a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import _lsprof
import importlib.machinery
import io
import profile as _pyprofile

# ____________________________________________________________
Expand Down Expand Up @@ -168,7 +169,7 @@ def main():
else:
progname = args[0]
sys.path.insert(0, os.path.dirname(progname))
with open(progname, 'rb') as fp:
with io.open_code(progname) as fp:
code = compile(fp.read(), progname, 'exec')
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
origin=progname)
Expand Down
3 changes: 2 additions & 1 deletion Lib/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


import importlib.machinery
import io
import sys
import time
import marshal
Expand Down Expand Up @@ -588,7 +589,7 @@ def main():
else:
progname = args[0]
sys.path.insert(0, os.path.dirname(progname))
with open(progname, 'rb') as fp:
with io.open_code(progname) as fp:
code = compile(fp.read(), progname, 'exec')
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
origin=progname)
Expand Down
3 changes: 2 additions & 1 deletion Lib/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"""
__all__ = ['Trace', 'CoverageResults']

import io
import linecache
import os
import sys
Expand Down Expand Up @@ -716,7 +717,7 @@ def parse_ignore_dir(s):
sys.argv = [opts.progname, *opts.arguments]
sys.path[0] = os.path.dirname(opts.progname)

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

0 comments on commit d50f01a

Please sign in to comment.