@@ -1690,7 +1690,6 @@ def profile_code(
16901690 except SystemExit as se :
16911691 # Intercept sys.exit and propagate the error code.
16921692 exit_status = se .code if isinstance (se .code , int ) else 1
1693-
16941693 except KeyboardInterrupt :
16951694 # Cleanly handle keyboard interrupts (quits execution and dumps the profile).
16961695 print ("Scalene execution interrupted." , file = sys .stderr )
@@ -1704,7 +1703,7 @@ def profile_code(
17041703 if Scalene .__args .memory :
17051704 pywhere .disable_settrace ()
17061705 pywhere .depopulate_struct ()
1707-
1706+
17081707 # Leaving here in case of reversion
17091708 # sys.settrace(None)
17101709 stats = Scalene .__stats
@@ -1766,62 +1765,63 @@ def profile_code(
17661765 ):
17671766 return exit_status
17681767
1769- if Scalene .__args .web or Scalene .__args .html :
1770- profile_filename = Scalene .__profile_filename
1771- if Scalene .__args .outfile :
1772- profile_filename = os .path .join (
1773- os .path .dirname (Scalene .__args .outfile ),
1774- os .path .splitext (os .path .basename (Scalene .__args .outfile ))[0 ] + ".json"
1775- )
1776- generate_html (
1777- profile_fname = profile_filename ,
1778- output_fname = (
1779- Scalene .__profiler_html if not Scalene .__args .outfile
1780- else Scalene .__args .outfile
1781- ),
1768+ assert did_output
1769+ if Scalene .__args .web or Scalene .__args .html :
1770+ profile_filename = Scalene .__profile_filename
1771+ if Scalene .__args .outfile :
1772+ profile_filename = os .path .join (
1773+ os .path .dirname (Scalene .__args .outfile ),
1774+ os .path .splitext (os .path .basename (Scalene .__args .outfile ))[0 ] + ".json"
17821775 )
1783- if Scalene .in_jupyter ():
1784- from scalene .scalene_jupyter import ScaleneJupyter
1776+ generate_html (
1777+ profile_fname = profile_filename ,
1778+ output_fname = (
1779+ Scalene .__profiler_html if not Scalene .__args .outfile
1780+ else Scalene .__args .outfile
1781+ ),
1782+ )
1783+ if Scalene .in_jupyter ():
1784+ from scalene .scalene_jupyter import ScaleneJupyter
17851785
1786- port = ScaleneJupyter .find_available_port (8181 , 9000 )
1787- if not port :
1788- print (
1789- "Scalene error: could not find an available port." ,
1790- file = sys .stderr ,
1791- )
1792- else :
1793- ScaleneJupyter .display_profile (
1794- port , Scalene .__profiler_html
1795- )
1786+ port = ScaleneJupyter .find_available_port (8181 , 9000 )
1787+ if not port :
1788+ print (
1789+ "Scalene error: could not find an available port." ,
1790+ file = sys .stderr ,
1791+ )
17961792 else :
1797- if not Scalene .__args .no_browser :
1798- # Remove any interposition libraries from the environment before opening the browser.
1799- # See also scalene/scalene_preload.py
1800- old_dyld = os .environ .pop ("DYLD_INSERT_LIBRARIES" , "" )
1801- old_ld = os .environ .pop ("LD_PRELOAD" , "" )
1802- output_fname = (
1803- f"{ os .getcwd ()} { os .sep } { Scalene .__profiler_html } "
1804- )
1805- if Scalene .__pid == 0 :
1806- # Only open a browser tab for the parent.
1807- dir = os .path .dirname (__file__ )
1808- subprocess .Popen (
1809- [
1810- Scalene .__orig_python ,
1811- f"{ dir } { os .sep } launchbrowser.py" ,
1812- output_fname ,
1813- str (scalene .scalene_config .SCALENE_PORT ),
1814- ],
1815- stdout = subprocess .DEVNULL ,
1816- stderr = subprocess .DEVNULL ,
1817- )
1818- # Restore them.
1819- os .environ .update (
1820- {
1821- "DYLD_INSERT_LIBRARIES" : old_dyld ,
1822- "LD_PRELOAD" : old_ld ,
1823- }
1793+ ScaleneJupyter .display_profile (
1794+ port , Scalene .__profiler_html
1795+ )
1796+ else :
1797+ if not Scalene .__args .no_browser :
1798+ # Remove any interposition libraries from the environment before opening the browser.
1799+ # See also scalene/scalene_preload.py
1800+ old_dyld = os .environ .pop ("DYLD_INSERT_LIBRARIES" , "" )
1801+ old_ld = os .environ .pop ("LD_PRELOAD" , "" )
1802+ output_fname = (
1803+ f"{ os .getcwd ()} { os .sep } { Scalene .__profiler_html } "
1804+ )
1805+ if Scalene .__pid == 0 :
1806+ # Only open a browser tab for the parent.
1807+ dir = os .path .dirname (__file__ )
1808+ subprocess .Popen (
1809+ [
1810+ Scalene .__orig_python ,
1811+ f"{ dir } { os .sep } launchbrowser.py" ,
1812+ output_fname ,
1813+ str (scalene .scalene_config .SCALENE_PORT ),
1814+ ],
1815+ stdout = subprocess .DEVNULL ,
1816+ stderr = subprocess .DEVNULL ,
18241817 )
1818+ # Restore them.
1819+ os .environ .update (
1820+ {
1821+ "DYLD_INSERT_LIBRARIES" : old_dyld ,
1822+ "LD_PRELOAD" : old_ld ,
1823+ }
1824+ )
18251825
18261826 return exit_status
18271827
0 commit comments