web.profiler does not work on Windows

Bug #325139 reported by asmo
2
Affects Status Importance Assigned to Milestone
web.py
Fix Released
Low
Anand Chitipothu

Bug Description

As discussed here, web.profiler fails to create temporary file on Windows.

http://groups.google.com/group/webpy/browse_thread/thread/3aabb8e66dc60093

Ben Corneu suggested this fix:

-----------
looks like it's a bug in utils.py. It uses
tempfile.NamedTemporaryFile() to generate a temp file. On Windows the
file cannot be opened a second time. Using tempfile.mkstemp() instead
works but you lose the guaranteed cleanup of the temp file.

http://docs.python.org/library/tempfile.html#tempfile.NamedTemporaryFile

...Whether the name can be used to open the file a second time, while
the named temporary file is still open, varies across platforms (it can
be so used on Unix; it cannot on Windows NT or later)....

here's my fix for it.

Line 733 of utils.py
    def __call__(self, *args): ##, **kw): kw unused
        import hotshot, hotshot.stats, tempfile, os ##, time already
imported
        temp = tempfile.mkstemp()
        os.close(temp[0])
        temp_filename = temp[1]
        prof = hotshot.Profile(temp_filename)
        stime = time.time()
        result = prof.runcall(self.func, *args)
        stime = time.time() - stime
        prof.close()
        import cStringIO
        out = cStringIO.StringIO()
        stats = hotshot.stats.load(temp_filename)
        #delete the temp file as son as we are done with it
        os.remove(temp_filename)
        stats.stream = out
        stats.strip_dirs()
        stats.sort_stats('time', 'calls')
        stats.print_stats(40)
        stats.print_callers()
        x = '\n\ntook '+ str(stime) + ' seconds\n'
        x += out.getvalue()
        return result, x
----------

Aaron Swartz (aaronsw)
Changed in webpy:
assignee: nobody → anandology
status: New → Confirmed
Changed in webpy:
importance: Undecided → Low
milestone: none → 0.33
Revision history for this message
Anand Chitipothu (anandology) wrote :
Changed in webpy:
status: Confirmed → Fix Committed
Changed in webpy:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.