Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea16980

Browse files
committed
#14508: make gprof2html script runnable under python3
Not that I haven't tested it to make sure it works, just that it can run against an empty source file. Initial patch by Popa.Claudiu. Here we also add a test (which uses mock, which is why I didn't check it in on 3.2).
2 parents 690598a + 776c0df commit ea16980

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

Lib/test/test_tools.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import imp
1010
import unittest
11+
from unittest import mock
1112
import sysconfig
1213
import tempfile
1314
from test import support
@@ -40,7 +41,7 @@ class TestSundryScripts(unittest.TestCase):
4041
# added for a script it should be added to the whitelist below.
4142

4243
# scripts that have independent tests.
43-
whitelist = ['reindent.py']
44+
whitelist = ['reindent.py', 'pdeps.py', 'gprof2html']
4445
# scripts that can't be imported without running
4546
blacklist = ['make_ctype.py']
4647
# scripts that use windows-only modules
@@ -99,6 +100,28 @@ def test_inverse_attribute_error(self):
99100
self.pdeps.inverse({'a': []})
100101

101102

103+
class Gprof2htmlTests(unittest.TestCase):
104+
105+
def setUp(self):
106+
path = os.path.join(scriptsdir, 'gprof2html.py')
107+
self.gprof = imp.load_source('gprof2html', path)
108+
oldargv = sys.argv
109+
def fixup():
110+
sys.argv = oldargv
111+
self.addCleanup(fixup)
112+
sys.argv = []
113+
114+
def test_gprof(self):
115+
# Issue #14508: this used to fail with an NameError.
116+
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
117+
tempfile.TemporaryDirectory() as tmpdir:
118+
fn = os.path.join(tmpdir, 'abc')
119+
open(fn, 'w').close()
120+
sys.argv = ['gprof2html', fn]
121+
self.gprof.main()
122+
self.assertTrue(wmock.open.called)
123+
124+
102125
def test_main():
103126
support.run_unittest(*[obj for obj in globals().values()
104127
if isinstance(obj, type)])

Tools/scripts/gprof2html.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
</html>
2020
"""
2121

22-
def add_escapes(input):
23-
for line in input:
24-
yield cgi.escape(line)
22+
def add_escapes(filename):
23+
with open(filename) as fp:
24+
for line in fp:
25+
yield cgi.escape(line)
26+
2527

2628
def main():
2729
filename = "gprof.out"
2830
if sys.argv[1:]:
2931
filename = sys.argv[1]
3032
outputfilename = filename + ".html"
31-
input = add_escapes(file(filename))
32-
output = file(outputfilename, "w")
33+
input = add_escapes(filename)
34+
output = open(outputfilename, "w")
3335
output.write(header % filename)
3436
for line in input:
3537
output.write(line)

0 commit comments

Comments
 (0)