Skip to content

Commit

Permalink
src/sage/doctest/util.py: put "import" inside the "try"
Browse files Browse the repository at this point in the history
We're importing psutil and trying to catch the ImportError if one is
raised. But for that to work, it helps to put "import" inside of the
"try" block.
  • Loading branch information
orlitzky committed Oct 16, 2024
1 parent 993327b commit 7aa6be8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/sage/doctest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,18 @@ def _quick_cputime(self, expect_objects):
# installed as a transitive dependency (ipython
# needs it), but it isn't explicitly listed as
# a dependency of sagelib.
from psutil import NoSuchProcess, Process, ZombieProcess
try:
cputime += sum(Process(S.pid()).cpu_times()[0:2])
except (ImportError, ValueError, NoSuchProcess, ZombieProcess):
# ImportError: no psutil
# ValueError: invalid (e.g. negative) PID
# NoSuchProcess: it's gone
# ZombieProcess: PID refers to a zombie
from psutil import (NoSuchProcess,
Process,
ZombieProcess)
try:
cputime += sum(Process(S.pid()).cpu_times()[0:2])
except (ValueError, NoSuchProcess, ZombieProcess):
# ValueError: invalid (e.g. negative) PID
# NoSuchProcess: it's gone
# ZombieProcess: PID refers to a zombie
pass
except ImportError:
pass

return cputime
Expand Down

0 comments on commit 7aa6be8

Please sign in to comment.