From 796cc8b9eaf6c0024ecd0c0d2e13e92416df1c22 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sat, 21 Sep 2013 22:47:10 +0200 Subject: [PATCH] Make perf attributes depending on the resource module optional. This makes the module usable on Python implementations, such as Jython, that lack the resource module. --- pdm/perf.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pdm/perf.py b/pdm/perf.py index f3a3e70..8326884 100644 --- a/pdm/perf.py +++ b/pdm/perf.py @@ -45,7 +45,7 @@ It contains two named PERF objects: - platform -- An attribute returning the Python build platform. """ -import os, sys, resource, time, socket, threading +import os, sys, time, socket, threading __all__ = ["attrinfo", "simpleattr", "valueattr", "eventobj", "staticdir", "event", "procevent", "startevent", @@ -232,16 +232,21 @@ class finishevent(procevent): sysres = staticdir() itime = time.time() -ires = resource.getrusage(resource.RUSAGE_SELF) -def ct(): - ru = resource.getrusage(resource.RUSAGE_SELF) - return (ru.ru_utime - ires.ru_utime) + (ru.ru_stime - ires.ru_stime) sysres["realtime"] = simpleattr(func = lambda: time.time() - itime) -sysres["cputime"] = simpleattr(func = ct) -sysres["utime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_utime - ires.ru_utime) -sysres["stime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_stime - ires.ru_stime) -sysres["maxrss"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) -sysres["rusage"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF)) +try: + import resource +except ImportError: + pass +else: + ires = resource.getrusage(resource.RUSAGE_SELF) + def ct(): + ru = resource.getrusage(resource.RUSAGE_SELF) + return (ru.ru_utime - ires.ru_utime) + (ru.ru_stime - ires.ru_stime) + sysres["cputime"] = simpleattr(func = ct) + sysres["utime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_utime - ires.ru_utime) + sysres["stime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_stime - ires.ru_stime) + sysres["maxrss"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) + sysres["rusage"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF)) sysinfo = staticdir() sysinfo["pid"] = simpleattr(func = os.getpid) -- 2.11.0