Home | History | Annotate | Download | only in python-yasm
      1 # Python bindings for Yasm: Pyrex input file for errwarn.h
      2 #
      3 #  Copyright (C) 2006  Peter Johnson
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions
      7 # are met:
      8 # 1. Redistributions of source code must retain the above copyright
      9 #    notice, this list of conditions and the following disclaimer.
     10 # 2. Redistributions in binary form must reproduce the above copyright
     11 #    notice, this list of conditions and the following disclaimer in the
     12 #    documentation and/or other materials provided with the distribution.
     13 #
     14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
     15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
     18 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     24 # POSSIBILITY OF SUCH DAMAGE.
     25 
     26 class YasmError(Exception): pass
     27 
     28 cdef int __error_check() except 1:
     29     cdef yasm_error_class errclass
     30     cdef unsigned long xrefline
     31     cdef char *errstr, *xrefstr
     32 
     33     # short path for the common case
     34     if not <int>yasm_error_occurred():
     35         return 0
     36 
     37     # look up our preferred python error, fall back to YasmError
     38     # Order matters here. Go from most to least specific within a class
     39     if yasm_error_matches(YASM_ERROR_ZERO_DIVISION):
     40         exception = ZeroDivisionError
     41     # Enable these once there are tests that need them.
     42     #elif yasm_error_matches(YASM_ERROR_OVERFLOW):
     43     #   exception = OverflowError
     44     #elif yasm_error_matches(YASM_ERROR_FLOATING_POINT):
     45     #   exception = FloatingPointError
     46     #elif yasm_error_matches(YASM_ERROR_ARITHMETIC):
     47     #   exception = ArithmeticError
     48     #elif yasm_error_matches(YASM_ERROR_ASSERTION):
     49     #   exception = AssertionError
     50     #elif yasm_error_matches(YASM_ERROR_VALUE):
     51     #   exception = ValueError # include notabs, notconst, toocomplex
     52     #elif yasm_error_matches(YASM_ERROR_IO):
     53     #   exception = IOError
     54     #elif yasm_error_matches(YASM_ERROR_NOT_IMPLEMENTED):
     55     #   exception = NotImplementedError
     56     #elif yasm_error_matches(YASM_ERROR_TYPE):
     57     #   exception = TypeError
     58     #elif yasm_error_matches(YASM_ERROR_SYNTAX):
     59     #   exception = SyntaxError #include parse
     60     else:
     61         exception = YasmError
     62 
     63     # retrieve info (clears error)
     64     yasm_error_fetch(&errclass, &errstr, &xrefline, &xrefstr)
     65 
     66     if xrefline and xrefstr:
     67         PyErr_Format(exception, "%s: %d: %s", errstr, xrefline, xrefstr)
     68     else:
     69         PyErr_SetString(exception, errstr)
     70 
     71     if xrefstr: free(xrefstr)
     72     free(errstr)
     73     return 1
     74