LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
wrappers.py
Go to the documentation of this file.
1 import warnings
2 import __builtin__
3 
4 from . import exceptionsLib
5 
6 registry = {}
7 
8 def register(cls):
9  """A Python decorator that adds a Python exception wrapper to the registry that maps C++ Exceptions
10  to their Python wrapper classes.
11  """
12  registry[cls.WrappedClass] = cls
13  return cls
14 
15 class ExceptionMeta(type):
16  """A metaclass for custom exception wrappers, which adds lookup of class attributes
17  by delegating to the Swig-generated wrapper.
18  """
19 
20  def __getattr__(self, name):
21  return getattr(self.WrappedClass, name)
22 
23 @register
24 class Exception(StandardError):
25  """The base class for Python-wrapped LSST C++ exceptions.
26  """
27 
28  __metaclass__ = ExceptionMeta
29 
30  # wrappers.py is an implementation detail, not a public namespace, so we pretend this is defined
31  # in the package for pretty-printing purposes
32  __module__ = "lsst.pex.exceptions"
33 
34  WrappedClass = exceptionsLib.Exception
35 
36  def __init__(self, arg, *args, **kwds):
37  if isinstance(arg, exceptionsLib.Exception):
38  cpp = arg
39  message = cpp.what()
40  else:
41  message = arg
42  cpp = self.WrappedClass(message, *args, **kwds)
43  super(Exception, self).__init__(message)
44  self.cpp = cpp
45 
46  def __getattr__(self, name):
47  return getattr(self.cpp, name)
48 
49  def __repr__(self):
50  return "%s('%s')" % (type(self).__name__, self.cpp.what())
51 
52  def __str__(self):
53  return self.cpp.asString()
54 
55 @register
57  WrappedClass = exceptionsLib.LogicError
58 
59 @register
61  WrappedClass = exceptionsLib.DomainError
62 
63 @register
65  WrappedClass = exceptionsLib.InvalidParameterError
66 
67 @register
69  WrappedClass = exceptionsLib.LengthError
70 
71 @register
73  WrappedClass = exceptionsLib.OutOfRangeError
74 
75 @register
76 class RuntimeError(Exception, __builtin__.RuntimeError):
77  WrappedClass = exceptionsLib.RuntimeError
78 
79 @register
81  WrappedClass = exceptionsLib.RangeError
82 
83 @register
84 class OverflowError(RuntimeError, __builtin__.OverflowError):
85  WrappedClass = exceptionsLib.OverflowError
86 
87 @register
88 class UnderflowError(RuntimeError, __builtin__.ArithmeticError):
89  WrappedClass = exceptionsLib.UnderflowError
90 
91 @register
92 class NotFoundError(Exception, __builtin__.LookupError):
93  WrappedClass = exceptionsLib.NotFoundError
94 
95 @register
96 class MemoryError(RuntimeError, __builtin__.MemoryError):
97  WrappedClass = exceptionsLib.MemoryError
98 
99 @register
100 class IoError(RuntimeError, __builtin__.IOError):
101  WrappedClass = exceptionsLib.IoError
102 
103 @register
104 class TypeError(RuntimeError, __builtin__.TypeError):
105  WrappedClass = exceptionsLib.TypeError
106 
107 @register
109  WrappedClass = exceptionsLib.TimeoutError
110 
111 def translate(cpp):
112  """Translate a C++ Exception instance to Python and return it."""
113  PyType = registry.get(type(cpp), None)
114  if PyType is None:
115  warnings.warn("Could not find appropriate Python type for C++ Exception")
116  PyType = Exception
117  return PyType(cpp)
118