lsst.pex.exceptions  14.0-1-g13ef843+8
wrappers.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 import warnings
3 import builtins
4 
5 from future.utils import with_metaclass
6 from . import exceptions
7 
8 
9 registry = {}
10 
11 
12 def register(cls):
13  """A Python decorator that adds a Python exception wrapper to the registry that maps C++ Exceptions
14  to their Python wrapper classes.
15  """
16  registry[cls.WrappedClass] = cls
17  return cls
18 
19 
20 class ExceptionMeta(type):
21  """A metaclass for custom exception wrappers, which adds lookup of class attributes
22  by delegating to the Swig-generated wrapper.
23  """
24 
25  def __getattr__(self, name):
26  return getattr(self.WrappedClass, name)
27 
28 
29 @register
30 class Exception(with_metaclass(ExceptionMeta, builtins.Exception)):
31  """The base class for Python-wrapped LSST C++ exceptions.
32  """
33 
34  # wrappers.py is an implementation detail, not a public namespace, so we pretend this is defined
35  # in the package for pretty-printing purposes
36  __module__ = "lsst.pex.exceptions"
37 
38  WrappedClass = exceptions.Exception
39 
40  def __init__(self, arg, *args, **kwds):
41  if isinstance(arg, exceptions.Exception):
42  cpp = arg
43  message = cpp.what()
44  else:
45  message = arg
46  cpp = self.WrappedClass(message, *args, **kwds)
47  super(Exception, self).__init__(message)
48  self.cpp = cpp
49 
50  def __getattr__(self, name):
51  return getattr(self.cpp, name)
52 
53  def __repr__(self):
54  return "%s('%s')" % (type(self).__name__, self.cpp.what())
55 
56  def __str__(self):
57  return self.cpp.asString()
58 
59 
60 @register
62  WrappedClass = exceptions.LogicError
63 
64 
65 @register
67  WrappedClass = exceptions.DomainError
68 
69 
70 @register
73 
74 
75 @register
77  WrappedClass = exceptions.LengthError
78 
79 
80 @register
83 
84 
85 @register
86 class RuntimeError(Exception, builtins.RuntimeError):
87  WrappedClass = exceptions.RuntimeError
88 
89 
90 @register
92  WrappedClass = exceptions.RangeError
93 
94 
95 @register
96 class OverflowError(RuntimeError, builtins.OverflowError):
97  WrappedClass = exceptions.OverflowError
98 
99 
100 @register
101 class UnderflowError(RuntimeError, builtins.ArithmeticError):
103 
104 
105 @register
106 class NotFoundError(Exception, builtins.LookupError):
108 
109 
110 @register
111 class MemoryError(RuntimeError, builtins.MemoryError):
112  WrappedClass = exceptions.MemoryError
113 
114 
115 @register
116 class IoError(RuntimeError, builtins.IOError):
117  WrappedClass = exceptions.IoError
118 
119 
120 @register
121 class TypeError(RuntimeError, builtins.TypeError):
122  WrappedClass = exceptions.TypeError
123 
124 
125 @register
127  WrappedClass = exceptions.TimeoutError
128 
129 
130 def translate(cpp):
131  """Translate a C++ Exception instance to Python and return it."""
132  PyType = registry.get(type(cpp), None)
133  if PyType is None:
134  warnings.warn("Could not find appropriate Python type for C++ Exception")
135  PyType = Exception
136  return PyType(cpp)
137 
138 
139 def declare(module, exception_name, base, wrapped_class):
140  """Declare a new exception."""
141  setattr(module, exception_name, register(ExceptionMeta(exception_name, (base, ),
142  dict(WrappedClass=wrapped_class))))
def __init__(self, arg, args, kwds)
Definition: wrappers.py:40
def declare(module, exception_name, base, wrapped_class)
Definition: wrappers.py:139