|
9 | 9 | import collections
|
10 | 10 | import collections.abc
|
11 | 11 | import contextlib
|
12 |
| -import datetime |
13 |
| -import errno |
14 | 12 | import functools
|
15 | 13 | import glob
|
16 | 14 | import gzip
|
17 |
| -import io |
18 | 15 | import itertools
|
19 | 16 | import locale
|
20 | 17 | import numbers
|
@@ -725,45 +722,29 @@ def remove(self, o):
|
725 | 722 |
|
726 | 723 |
|
727 | 724 | def report_memory(i=0): # argument may go away
|
728 |
| - """return the memory consumed by process""" |
729 |
| - from subprocess import Popen, PIPE |
730 |
| - pid = os.getpid() |
731 |
| - if sys.platform == 'sunos5': |
| 725 | + """Return the memory consumed by the process.""" |
| 726 | + def call(command, os_name): |
732 | 727 | try:
|
733 |
| - a2 = Popen(['ps', '-p', '%d' % pid, '-o', 'osz'], |
734 |
| - stdout=PIPE).stdout.readlines() |
735 |
| - except OSError: |
| 728 | + return subprocess.check_output(command) |
| 729 | + except subprocess.CalledProcessError: |
736 | 730 | raise NotImplementedError(
|
737 |
| - "report_memory works on Sun OS only if " |
738 |
| - "the 'ps' program is found") |
739 |
| - 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()) |
740 | 739 | elif sys.platform == 'linux':
|
741 |
| - try: |
742 |
| - a2 = Popen(['ps', '-p', '%d' % pid, '-o', 'rss,sz'], |
743 |
| - stdout=PIPE).stdout.readlines() |
744 |
| - except OSError: |
745 |
| - raise NotImplementedError( |
746 |
| - "report_memory works on Linux only if " |
747 |
| - "the 'ps' program is found") |
748 |
| - mem = int(a2[1].split()[1]) |
| 740 | + lines = call(['ps', '-p', '%d' % pid, '-o', 'rss,sz'], 'Linux') |
| 741 | + mem = int(lines[1].split()[1]) |
749 | 742 | elif sys.platform == 'darwin':
|
750 |
| - try: |
751 |
| - a2 = Popen(['ps', '-p', '%d' % pid, '-o', 'rss,vsz'], |
752 |
| - stdout=PIPE).stdout.readlines() |
753 |
| - except OSError: |
754 |
| - raise NotImplementedError( |
755 |
| - "report_memory works on Mac OS only if " |
756 |
| - "the 'ps' program is found") |
757 |
| - 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]) |
758 | 745 | elif sys.platform == 'win32':
|
759 |
| - try: |
760 |
| - a2 = Popen(["tasklist", "/nh", "/fi", "pid eq %d" % pid], |
761 |
| - stdout=PIPE).stdout.read() |
762 |
| - except OSError: |
763 |
| - raise NotImplementedError( |
764 |
| - "report_memory works on Windows only if " |
765 |
| - "the 'tasklist' program is found") |
766 |
| - 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(',', '')) |
767 | 748 | else:
|
768 | 749 | raise NotImplementedError(
|
769 | 750 | "We don't have a memory monitor for %s" % sys.platform)
|
@@ -813,7 +794,6 @@ def print_cycles(objects, outstream=sys.stdout, show_progress=False):
|
813 | 794 | If True, print the number of objects reached as they are found.
|
814 | 795 | """
|
815 | 796 | import gc
|
816 |
| - from types import FrameType |
817 | 797 |
|
818 | 798 | def print_path(path):
|
819 | 799 | for i, step in enumerate(path):
|
@@ -854,7 +834,7 @@ def recurse(obj, start, all, current_path):
|
854 | 834 | # Don't go back through the original list of objects, or
|
855 | 835 | # through temporary references to the object, since those
|
856 | 836 | # are just an artifact of the cycle detector itself.
|
857 |
| - elif referent is objects or isinstance(referent, FrameType): |
| 837 | + elif referent is objects or isinstance(referent, types.FrameType): |
858 | 838 | continue
|
859 | 839 |
|
860 | 840 | # We haven't seen this object before, so recurse
|
@@ -2009,7 +1989,7 @@ def _warn_external(message, category=None):
|
2009 | 1989 | etc.).
|
2010 | 1990 | """
|
2011 | 1991 | frame = sys._getframe()
|
2012 |
| - for stacklevel in itertools.count(1): |
| 1992 | + for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable] |
2013 | 1993 | if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)",
|
2014 | 1994 | frame.f_globals["__name__"]):
|
2015 | 1995 | break
|
|
0 commit comments