]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103935: Use `io.open_code()` when executing code in trace and profile modules...
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 27 Apr 2023 20:29:35 +0000 (13:29 -0700)
committerGitHub <noreply@github.com>
Thu, 27 Apr 2023 20:29:35 +0000 (20:29 +0000)
Lib/cProfile.py
Lib/profile.py
Lib/trace.py
Misc/NEWS.d/next/Library/2023-04-27-20-03-08.gh-issue-103935.Uaf2M0.rst [new file with mode: 0644]

index f7000a8bfa0ddb34ec1fbf5319736391f1937a4f..135a12c3965c00e8d0afe984b5f9f099cc6c2db0 100755 (executable)
@@ -8,6 +8,7 @@ __all__ = ["run", "runctx", "Profile"]
 
 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)
index 453e56285c510c1843c0ecde27e9cd0676612020..4b82523b03d64b08eee4ba212a4d53286ee69a07 100755 (executable)
@@ -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)
index 213e46517d683d7e132936a042ec95cf1b465cb9..fb9a423ea09fce6651f8ee2009be399c973ce919 100755 (executable)
@@ -49,6 +49,7 @@ Sample use, programmatically
 """
 __all__ = ['Trace', 'CoverageResults']
 
+import io
 import linecache
 import os
 import sys
@@ -716,7 +717,7 @@ def main():
             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 (file)
index 0000000..71b2d87
--- /dev/null
@@ -0,0 +1 @@
+Use :func:`io.open_code` for files to be executed instead of raw :func:`open`