]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] gh-103935: Use `io.open_code()` when executing code in trace and profile module...
authorSteve Dower <steve.dower@python.org>
Mon, 22 May 2023 10:40:02 +0000 (11:40 +0100)
committerGitHub <noreply@github.com>
Mon, 22 May 2023 10:40:02 +0000 (12:40 +0200)
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
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 406a9b7cf11be954539c31cb62aa295c7b3d099b..44cb90c02297230b64532530ce6fa5e138eb87d8 100755 (executable)
@@ -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,
index df4450dac6a1b65fff2c090b74743ba8474c2964..bf43e37d0b24571c66f3ea0ab3d6f97c45c2eaa1 100755 (executable)
@@ -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,
index 89f17d485f35e8291336250bfc8e0166c8d08353..3b5e564978d823fcdd88a253d001ef9506f6141d 100755 (executable)
@@ -49,6 +49,7 @@ Sample use, programmatically
 """
 __all__ = ['Trace', 'CoverageResults']
 
+import io
 import linecache
 import os
 import sys
@@ -732,7 +733,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`