1 """Automatic error checking for GL calls.
2 """
3
4 import re
7 """Exception representing an error within OpenGL.
8 """
9
10 - def __init__(self, error, result, func, arguments):
11 self.error = error
12 self.result = result
13 self.func = func
14 self.arguments = arguments
15
16 @staticmethod
18 """Lookup the error message for the given error number.
19 """
20
21 try:
22 from glitter.raw import glu
23 return glu.string_at(glu.gluErrorString(error))
24 except (ImportError, AttributeError):
25 return "error #%d" % error
26
28 return "GLError: %s in %s(%s)" % (GLError.string_from_id(self.error), self.func.__name__, ", ".join(repr(x) for x in self.arguments))
29
32
33 -def set_error_check(errcheck_func=Ellipsis, errcheck_ok=Ellipsis, name_re="^gl[A-Z].*$", d=None):
34 """Add error handlers to OpenGL functions.
35
36 The C{errcheck} attribute of all items in a dictionary C{d} having such an
37 attribute and matching C{name_re} will be set to a function that calls
38 C{errcheck_func} and raises L{GLError} if the return value is unequal to
39 C{errcheck_ok}. By default, C{d} is C{glitter.raw.__dict__}.
40
41 Defaults for C{errcheck_func} and C{errcheck_ok} are C{glGetError} and
42 C{GL_NO_ERROR}, respectively. If C{errcheck_func} is C{None}, error
43 checking will be disabled.
44 """
45
46 if d is None:
47 from glitter import raw
48 d = raw.__dict__
49
50 if errcheck_func is Ellipsis:
51 from glitter.raw import gl
52 errcheck_func = gl.glGetError
53
54 if errcheck_func is not None and errcheck_ok is Ellipsis:
55 from glitter.raw import gl
56 errcheck_ok = gl.GL_NO_ERROR
57
58 def errcheck(result, func, arguments):
59 error = errcheck_func()
60 if error == errcheck_ok:
61 return result
62 else:
63 raise GLError(error, result, func, arguments)
64
65 for key, value in d.items():
66 if re.match(name_re, key) and value is not errcheck_func and hasattr(value, "errcheck"):
67 value.errcheck = errcheck
68
69 __all__ = ["GLError", "set_error_check"]
70