[Python-checkins] cpython (merge 3.2 -> default): #14508: make gprof2html script runnable under python3

r.david.murray python-checkins at python.org
Wed Apr 11 21:17:51 CEST 2012


http://hg.python.org/cpython/rev/73fba223c0a5
changeset:   76251:73fba223c0a5
parent:      76249:23f648d7053b
parent:      76250:4d603d6782db
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Apr 11 15:17:37 2012 -0400
summary:
  #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).

files:
  Lib/test/test_tools.py      |  25 ++++++++++++++++++++++++-
  Tools/scripts/gprof2html.py |  12 +++++++-----
  2 files changed, 31 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_tools.py b/Lib/test/test_tools.py
--- a/Lib/test/test_tools.py
+++ b/Lib/test/test_tools.py
@@ -8,6 +8,7 @@
 import sys
 import imp
 import unittest
+from unittest import mock
 import sysconfig
 import tempfile
 from test import support
@@ -40,7 +41,7 @@
     # added for a script it should be added to the whitelist below.
 
     # scripts that have independent tests.
-    whitelist = ['reindent.py']
+    whitelist = ['reindent.py', 'pdeps.py', 'gprof2html']
     # scripts that can't be imported without running
     blacklist = ['make_ctype.py']
     # scripts that use windows-only modules
@@ -99,6 +100,28 @@
         self.pdeps.inverse({'a': []})
 
 
+class Gprof2htmlTests(unittest.TestCase):
+
+    def setUp(self):
+        path = os.path.join(scriptsdir, 'gprof2html.py')
+        self.gprof = imp.load_source('gprof2html', path)
+        oldargv = sys.argv
+        def fixup():
+            sys.argv = oldargv
+        self.addCleanup(fixup)
+        sys.argv = []
+
+    def test_gprof(self):
+        # Issue #14508: this used to fail with an NameError.
+        with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
+                tempfile.TemporaryDirectory() as tmpdir:
+            fn = os.path.join(tmpdir, 'abc')
+            open(fn, 'w').close()
+            sys.argv = ['gprof2html', fn]
+            self.gprof.main()
+        self.assertTrue(wmock.open.called)
+
+
 def test_main():
     support.run_unittest(*[obj for obj in globals().values()
                                if isinstance(obj, type)])
diff --git a/Tools/scripts/gprof2html.py b/Tools/scripts/gprof2html.py
--- a/Tools/scripts/gprof2html.py
+++ b/Tools/scripts/gprof2html.py
@@ -19,17 +19,19 @@
 </html>
 """
 
-def add_escapes(input):
-    for line in input:
-        yield cgi.escape(line)
+def add_escapes(filename):
+    with open(filename) as fp:
+        for line in fp:
+            yield cgi.escape(line)
+
 
 def main():
     filename = "gprof.out"
     if sys.argv[1:]:
         filename = sys.argv[1]
     outputfilename = filename + ".html"
-    input = add_escapes(file(filename))
-    output = file(outputfilename, "w")
+    input = add_escapes(filename)
+    output = open(outputfilename, "w")
     output.write(header % filename)
     for line in input:
         output.write(line)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list