Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching TimeoutExpired exception in Python 3.3

Sorry if this is a newbie question, but I'm having trouble catching the timeout exception in Python 3.3, running on win7, e.g.

import subprocess
try:
   subprocess.call("ping -t localhost", timeout=3)
except TimeoutExpired:
    print("Timeout happened.\n")

The timeout works fine, and according to my pdb traceback, it says: "raise TimeoutExpired(self.args, timeout)"

yet "except TimeoutExpired:" doesn't catch it. Also, TimeoutExpired is not listed as a standard exception and comes out as a nameError.

If I try "except TimeoutError:" instead of "except TimeoutExpired:", I don't get the error message, but in pdb, I get:

"Uncaught exception"

and the print command does not get executed in any case.

Is this a bug, or am I doing something wrong?

like image 808
Sarge Gerbode Avatar asked Oct 14 '13 17:10

Sarge Gerbode


1 Answers

TimeoutExpired is not globally defined; use subprocess.TimeoutExpired instead.

like image 132
Peter Rowell Avatar answered Nov 07 '22 06:11

Peter Rowell