@@ -722,45 +722,29 @@ def remove(self, o):
722722
723723
724724def report_memory (i = 0 ): # argument may go away
725- """return the memory consumed by process"""
726- from subprocess import Popen , PIPE
727- pid = os .getpid ()
728- if sys .platform == 'sunos5' :
725+ """Return the memory consumed by the process."""
726+ def call (command , os_name ):
729727 try :
730- a2 = Popen (['ps' , '-p' , '%d' % pid , '-o' , 'osz' ],
731- stdout = PIPE ).stdout .readlines ()
732- except OSError :
728+ return subprocess .check_output (command )
729+ except subprocess .CalledProcessError :
733730 raise NotImplementedError (
734- "report_memory works on Sun OS only if "
735- "the 'ps' program is found" )
736- mem = int (a2 [- 1 ].strip ())
731+ "report_memory works on %s only if "
732+ "the '%s' program is found" % (os_name , command [0 ])
733+ )
734+
735+ pid = os .getpid ()
736+ if sys .platform == 'sunos5' :
737+ lines = call (['ps' , '-p' , '%d' % pid , '-o' , 'osz' ], 'Sun OS' )
738+ mem = int (lines [- 1 ].strip ())
737739 elif sys .platform == 'linux' :
738- try :
739- a2 = Popen (['ps' , '-p' , '%d' % pid , '-o' , 'rss,sz' ],
740- stdout = PIPE ).stdout .readlines ()
741- except OSError :
742- raise NotImplementedError (
743- "report_memory works on Linux only if "
744- "the 'ps' program is found" )
745- mem = int (a2 [1 ].split ()[1 ])
740+ lines = call (['ps' , '-p' , '%d' % pid , '-o' , 'rss,sz' ], 'Linux' )
741+ mem = int (lines [1 ].split ()[1 ])
746742 elif sys .platform == 'darwin' :
747- try :
748- a2 = Popen (['ps' , '-p' , '%d' % pid , '-o' , 'rss,vsz' ],
749- stdout = PIPE ).stdout .readlines ()
750- except OSError :
751- raise NotImplementedError (
752- "report_memory works on Mac OS only if "
753- "the 'ps' program is found" )
754- mem = int (a2 [1 ].split ()[0 ])
743+ lines = call (['ps' , '-p' , '%d' % pid , '-o' , 'rss,vsz' ], 'Mac OS' )
744+ mem = int (lines [1 ].split ()[0 ])
755745 elif sys .platform == 'win32' :
756- try :
757- a2 = Popen (["tasklist" , "/nh" , "/fi" , "pid eq %d" % pid ],
758- stdout = PIPE ).stdout .read ()
759- except OSError :
760- raise NotImplementedError (
761- "report_memory works on Windows only if "
762- "the 'tasklist' program is found" )
763- mem = int (a2 .strip ().split ()[- 2 ].replace (',' , '' ))
746+ lines = call (["tasklist" , "/nh" , "/fi" , "pid eq %d" % pid ], 'Windows' )
747+ mem = int (lines .strip ().split ()[- 2 ].replace (',' , '' ))
764748 else :
765749 raise NotImplementedError (
766750 "We don't have a memory monitor for %s" % sys .platform )
@@ -810,7 +794,6 @@ def print_cycles(objects, outstream=sys.stdout, show_progress=False):
810794 If True, print the number of objects reached as they are found.
811795 """
812796 import gc
813- from types import FrameType
814797
815798 def print_path (path ):
816799 for i , step in enumerate (path ):
@@ -851,7 +834,7 @@ def recurse(obj, start, all, current_path):
851834 # Don't go back through the original list of objects, or
852835 # through temporary references to the object, since those
853836 # are just an artifact of the cycle detector itself.
854- elif referent is objects or isinstance (referent , FrameType ):
837+ elif referent is objects or isinstance (referent , types . FrameType ):
855838 continue
856839
857840 # We haven't seen this object before, so recurse
@@ -2006,7 +1989,7 @@ def _warn_external(message, category=None):
20061989 etc.).
20071990 """
20081991 frame = sys ._getframe ()
2009- for stacklevel in itertools .count (1 ):
1992+ for stacklevel in itertools .count (1 ): # lgtm[py/unused-loop-variable]
20101993 if not re .match (r"\A(matplotlib|mpl_toolkits)(\Z|\.)" ,
20111994 frame .f_globals ["__name__" ]):
20121995 break
0 commit comments