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

ambv webhook-mailer at python.org
Mon May 22 06:40:11 EDT 2023


https://github.com/python/cpython/commit/9f89c471fb152fd031791f3c598ceba93d91dcf0
commit: 9f89c471fb152fd031791f3c598ceba93d91dcf0
branch: 3.8
author: Steve Dower <steve.dower at python.org>
committer: ambv <lukasz at langa.pl>
date: 2023-05-22T12:40:02+02:00
summary:

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

Co-authored-by: Tian Gao <gaogaotiantian at hotmail.com>

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 406a9b7cf11b..44cb90c02297 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -7,6 +7,7 @@
 __all__ = ["run", "runctx", "Profile"]
 
 import _lsprof
+import io
 import profile as _pyprofile
 
 # ____________________________________________________________
@@ -183,7 +184,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')
             globs = {
                 '__file__': progname,
diff --git a/Lib/profile.py b/Lib/profile.py
index df4450dac6a1..bf43e37d0b24 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -24,6 +24,7 @@
 # governing permissions and limitations under the License.
 
 
+import io
 import sys
 import time
 import marshal
@@ -603,7 +604,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')
             globs = {
                 '__file__': progname,
diff --git a/Lib/trace.py b/Lib/trace.py
index 89f17d485f35..3b5e564978d8 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -49,6 +49,7 @@
 """
 __all__ = ['Trace', 'CoverageResults']
 
+import io
 import linecache
 import os
 import sys
@@ -732,7 +733,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