Skip to content

Commit 9f89c47

Browse files
[3.8] gh-103935: Use io.open_code() when executing code in trace and profile modules (GH-103947) (#103954)
Co-authored-by: Tian Gao <[email protected]>
1 parent 2062fce commit 9f89c47

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

Lib/cProfile.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
__all__ = ["run", "runctx", "Profile"]
88

99
import _lsprof
10+
import io
1011
import profile as _pyprofile
1112

1213
# ____________________________________________________________
@@ -183,7 +184,7 @@ def main():
183184
else:
184185
progname = args[0]
185186
sys.path.insert(0, os.path.dirname(progname))
186-
with open(progname, 'rb') as fp:
187+
with io.open_code(progname) as fp:
187188
code = compile(fp.read(), progname, 'exec')
188189
globs = {
189190
'__file__': progname,

Lib/profile.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# governing permissions and limitations under the License.
2525

2626

27+
import io
2728
import sys
2829
import time
2930
import marshal
@@ -603,7 +604,7 @@ def main():
603604
else:
604605
progname = args[0]
605606
sys.path.insert(0, os.path.dirname(progname))
606-
with open(progname, 'rb') as fp:
607+
with io.open_code(progname) as fp:
607608
code = compile(fp.read(), progname, 'exec')
608609
globs = {
609610
'__file__': progname,

Lib/trace.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"""
5050
__all__ = ['Trace', 'CoverageResults']
5151

52+
import io
5253
import linecache
5354
import os
5455
import sys
@@ -732,7 +733,7 @@ def parse_ignore_dir(s):
732733
sys.argv = [opts.progname, *opts.arguments]
733734
sys.path[0] = os.path.dirname(opts.progname)
734735

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

0 commit comments

Comments
 (0)