|
| 1 | +# Copyright (c) 2002-2011 IronPort Systems and Cisco Systems |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +# $Header: //prod/main/ap/aplib/aplib/aplib.oserrors.pyx#2 $ |
| 22 | + |
| 23 | +"""OSError mapping. |
| 24 | +
|
| 25 | +This module maps OSErrors to specific, catchable exceptions. For example, an |
| 26 | +OSError with an errno of ENOENT will be raised as the ENOENT exception. All |
| 27 | +exceptions derive from OSError, so it is compatible with regular OSError |
| 28 | +handling. |
| 29 | +""" |
| 30 | + |
| 31 | +include "python.pxi" |
| 32 | + |
| 33 | +import errno |
| 34 | +import new |
| 35 | +import sys |
| 36 | +cimport libc |
| 37 | + |
| 38 | +err_map = {} |
| 39 | + |
| 40 | +def map_exception(e): |
| 41 | + """Raise an errno-specific exception. |
| 42 | +
|
| 43 | + :Parameters: |
| 44 | + - `e`: The OSError instance. |
| 45 | + """ |
| 46 | + # Ignoring e.filename since it seems less than useful to me. |
| 47 | + try: |
| 48 | + raise err_map[e.errno](e.errno, e.strerror) |
| 49 | + except KeyError: |
| 50 | + raise e |
| 51 | + |
| 52 | +def raise_oserror(int error_number): |
| 53 | + """Raise an OSError exception by errno. |
| 54 | +
|
| 55 | + :Parameters: |
| 56 | + - `error_number`: The errno value to raise. |
| 57 | + """ |
| 58 | + map_exception(OSError(error_number, libc.strerror(error_number))) |
| 59 | + |
| 60 | +__m = sys.modules['aplib.oserrors'] |
| 61 | +__g = __m.__dict__ |
| 62 | +for errcode, errname in errno.errorcode.items(): |
| 63 | + # Not sure why __module__ is not getting set properly. Python looks at |
| 64 | + # globals() to fine __name__ to set the value, and it appears to be |
| 65 | + # set to __main__ for some odd reason. |
| 66 | + c = new.classobj(errname, (OSError,), {'__module__': 'aplib.oserrors'}) |
| 67 | + err_map[errcode] = c |
| 68 | + __g[errname] = c |
0 commit comments