LSSTApplications  17.0+50,17.0+84,17.0+9,18.0.0+14,18.0.0+2,18.0.0+30,18.0.0+4,18.0.0+9,18.0.0-2-ge43143a+4,18.1.0-1-g0001055,18.1.0-1-g0896a44+6,18.1.0-1-g1349e88+4,18.1.0-1-g2505f39+3,18.1.0-1-g380d4d4+4,18.1.0-1-g5e4b7ea,18.1.0-1-g85f8cd4+3,18.1.0-1-g9a6769a+2,18.1.0-1-ga1a4c1a+2,18.1.0-1-gc037db8,18.1.0-1-gd55f500+1,18.1.0-1-ge10677a+3,18.1.0-10-g73b8679e+7,18.1.0-11-g311e899+3,18.1.0-12-g0d73a3591,18.1.0-12-gc95f69a+3,18.1.0-2-g000ad9a+3,18.1.0-2-g31c43f9+3,18.1.0-2-g9c63283+4,18.1.0-2-gdf0b915+4,18.1.0-2-gf03bb23,18.1.0-3-g2e29e3d+6,18.1.0-3-g52aa583+2,18.1.0-3-g9cb968e+3,18.1.0-4-gd2e8982+6,18.1.0-5-g510c42a+3,18.1.0-5-gaeab27e+4,18.1.0-6-gdda7f3e+6,18.1.0-7-g89824ecc+4,w.2019.32
LSSTDataManagementBasePackage
wrappers.py
Go to the documentation of this file.
1 # This file is part of pex_exceptions.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 __all__ = ["register", "ExceptionMeta", "Exception", "LogicError",
23  "DomainError", "InvalidParameterError", "LengthError",
24  "OutOfRangeError", "RuntimeError", "RangeError", "OverflowError",
25  "UnderflowError", "NotFoundError", "IoError", "TypeError",
26  "translate", "declare"]
27 
28 import warnings
29 
30 from . import exceptions
31 
32 registry = {}
33 
34 
35 def register(cls):
36  """A Python decorator that adds a Python exception wrapper to the registry that maps C++ Exceptions
37  to their Python wrapper classes.
38  """
39  registry[cls.WrappedClass] = cls
40  return cls
41 
42 
44  """A metaclass for custom exception wrappers, which adds lookup of class attributes
45  by delegating to the Swig-generated wrapper.
46  """
47 
48  def __getattr__(cls, name):
49  return getattr(cls.WrappedClass, name)
50 
51 
52 @register
53 class Exception(Exception, metaclass=ExceptionMeta):
54  """The base class for Python-wrapped LSST C++ exceptions.
55  """
56 
57  # wrappers.py is an implementation detail, not a public namespace, so we pretend this is defined
58  # in the package for pretty-printing purposes
59  __module__ = "lsst.pex.exceptions"
60 
61  WrappedClass = exceptions.Exception
62 
63  def __init__(self, arg, *args, **kwds):
64  if isinstance(arg, exceptions.Exception):
65  cpp = arg
66  message = cpp.what()
67  else:
68  message = arg
69  cpp = self.WrappedClass(message, *args, **kwds)
70  super(Exception, self).__init__(message)
71  self.cpp = cpp
72 
73  def __getattr__(self, name):
74  return getattr(self.cpp, name)
75 
76  def __repr__(self):
77  return "%s('%s')" % (type(self).__name__, self.cpp.what())
78 
79  def __str__(self):
80  return self.cpp.asString()
81 
82 
83 @register
85  WrappedClass = exceptions.LogicError
86 
87 
88 @register
90  WrappedClass = exceptions.DomainError
91 
92 
93 @register
96 
97 
98 @register
100  WrappedClass = exceptions.LengthError
101 
102 
103 @register
106 
107 
108 @register
110  WrappedClass = exceptions.RuntimeError
111 
112 
113 @register
115  WrappedClass = exceptions.RangeError
116 
117 
118 @register
121 
122 
123 @register
124 class UnderflowError(RuntimeError, ArithmeticError):
126 
127 
128 @register
129 class NotFoundError(Exception, LookupError):
131 
132 
133 @register
134 class IoError(RuntimeError, IOError):
135  WrappedClass = exceptions.IoError
136 
137 
138 @register
140  WrappedClass = exceptions.TypeError
141 
142 
143 def translate(cpp):
144  """Translate a C++ Exception instance to Python and return it."""
145  PyType = registry.get(type(cpp), None)
146  if PyType is None:
147  warnings.warn("Could not find appropriate Python type for C++ Exception")
148  PyType = Exception
149  return PyType(cpp)
150 
151 
152 def declare(module, exception_name, base, wrapped_class):
153  """Declare a new exception."""
154  setattr(module, exception_name, register(ExceptionMeta(exception_name, (base, ),
155  dict(WrappedClass=wrapped_class))))
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
Reports arguments outside the domain of an operation.
Definition: Runtime.h:57
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
Reports errors in external input/output operations.
Definition: Runtime.h:160
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition: Exception.cc:99
Reports when the result of an arithmetic operation is too large for the destination type...
Definition: Runtime.h:124
table::Key< int > type
Definition: Detector.cc:167
Reports when the result of an arithmetic operation is too small for the destination type...
Definition: Runtime.h:133
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
def __init__(self, arg, args, kwds)
Definition: wrappers.py:63
def declare(module, exception_name, base, wrapped_class)
Definition: wrappers.py:152
Reports invalid arguments.
Definition: Runtime.h:66
Reports errors from accepting an object of an unexpected or inappropriate type.
Definition: Runtime.h:167
Reports when the result of an operation cannot be represented by the destination type.
Definition: Runtime.h:115
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104