[Python-checkins] gh-103935: Use `io.open_code()` when executing code in trace and profile modules (GH-103947)

zooba webhook-mailer at python.org
Thu Apr 27 16:29:44 EDT 2023


https://github.com/python/cpython/commit/d50f01ad0a965c6d41e24ef77be0fe643efa1bfd
commit: d50f01ad0a965c6d41e24ef77be0fe643efa1bfd
branch: main
author: Tian Gao <gaogaotiantian at hotmail.com>
committer: zooba <steve.dower at microsoft.com>
date: 2023-04-27T20:29:35Z
summary:

gh-103935: Use `io.open_code()` when executing code in trace and profile modules (GH-103947)

files:
A Misc/NEWS.d/next/Library/2023-04-27-20-03-08.gh-issue-103935.Uaf2M0.rst
M Lib/cProfile.py
M Lib/profile.py
M Lib/trace.py

diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index f7000a8bfa0d..135a12c3965c 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -8,6 +8,7 @@
 
 import _lsprof
 import importlib.machinery
+import io
 import profile as _pyprofile
 
 # ____________________________________________________________
@@ -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)
diff --git a/Lib/profile.py b/Lib/profile.py
index 453e56285c51..4b82523b03d6 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -25,6 +25,7 @@
 
 
 import importlib.machinery
+import io
 import sys
 import time
 import marshal
@@ -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)
diff --git a/Lib/trace.py b/Lib/trace.py
index 213e46517d68..fb9a423ea09f 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -49,6 +49,7 @@
 """
 __all__ = ['Trace', 'CoverageResults']
 
+import io
 import linecache
 import os
 import sys
@@ -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 = {
diff --git a/Misc/NEWS.d/next/Library/2023-04-27-20-03-08.gh-issue-103935.Uaf2M0.rst b/Misc/NEWS.d/next/Library/2023-04-27-20-03-08.gh-issue-103935.Uaf2M0.rst
new file mode 100644
index 000000000000..71b2d87249c4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-04-27-20-03-08.gh-issue-103935.Uaf2M0.rst
@@ -0,0 +1 @@
+Use :func:`io.open_code` for files to be executed instead of raw :func:`open`



More information about the Python-checkins mailing list