Python - except (OSError, e) - no longer work in 3.3.3?

The following actions worked on Python 3.X and did not tear in 3.3.3; they cannot find what has changed in the documents.

import os

def pid_alive(pid):
    pid = int(pid)
    if pid < 0:
        return False
    try:
        os.kill(pid, 0)
    except (OSError, e):
        return e.errno == errno.EPERM
    else:
        return True

I tried different options for the exception line, for example except OSError as e:, but then it errno.EPERMbreaks, etc.

Any quick pointers?

+4
source share
1 answer

An expression except (OSError, e)never worked in Python, and not the way you think it works. This expression captures two types of exceptions; OSErroror to what is global e. Your code breaks when there is no global name e.

Python 3 Python 2.6 :

except OSError as e:

Python 2 :

except OSError, e:

:

except (OSError, ValueError), e:

. , .

Python 2.6 , . PEP 3110 - Python 3000 2.6 What New.

errno.EPERM; errno, NameError.

+21

All Articles