Debian Codegraph @

Pyro5/errors.py

raw
1 """
2 Definition of the various exceptions that are used in Pyro.
3
4 Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]).
5 """
6
7 import sys
8 import linecache
9 import traceback
10 from . import config
11
12
13 class PyroError(Exception):
14 """Generic base of all Pyro-specific errors."""
15 pass
16
17
18 class CommunicationError(PyroError):
19 """Base class for the errors related to network communication problems."""
20 pass
21
22
23 class ConnectionClosedError(CommunicationError):
24 """The connection was unexpectedly closed."""
25 pass
26
27
28 class TimeoutError(CommunicationError):
29 """
30 A call could not be completed within the set timeout period,
31 or the network caused a timeout.
32 """
33 pass
34
35
36 class ProtocolError(CommunicationError):
37 """Pyro received a message that didn't match the active Pyro network protocol, or there was a protocol related error."""
38 pass
39
40
41 class MessageTooLargeError(ProtocolError):
42 """Pyro received a message or was trying to send a message that exceeds the maximum message size as configured."""
43 pass
44
45
46 class NamingError(PyroError):
47 """There was a problem related to the name server or object names."""
48 pass
49
50
51 class DaemonError(PyroError):
52 """The Daemon encountered a problem."""
53 pass
54
55
56 class SecurityError(PyroError):
57 """A security related error occurred."""
58 pass
59
60
61 class SerializeError(ProtocolError):
62 """Something went wrong while (de)serializing data."""
63 pass
64
65
66 def get_pyro_traceback(ex_type=None, ex_value=None, ex_tb=None):
67 """Returns a list of strings that form the traceback information of a
68 Pyro exception. Any remote Pyro exception information is included.
69 Traceback information is automatically obtained via ``sys.exc_info()`` if
70 you do not supply the objects yourself."""
71
72 def formatRemoteTraceback(remote_tb_lines):
73 result = [" +--- This exception occured remotely (Pyro) - Remote traceback:"]
74 for line in remote_tb_lines:
75 if line.endswith("\n"):
76 line = line[:-1]
77 lines = line.split("\n")
78 for line2 in lines:
79 result.append("\n | ")
80 result.append(line2)
81 result.append("\n +--- End of remote traceback\n")
82 return result
83
84 try:
85 if ex_type is not None and ex_value is None and ex_tb is None:
86 if type(ex_type) is not type:
87 raise TypeError("invalid argument: ex_type should be an exception type, or just supply no arguments at all")
88 if ex_type is None and ex_tb is None:
89 ex_type, ex_value, ex_tb = sys.exc_info()
90
91 remote_tb = getattr(ex_value, "_pyroTraceback", None)
92 local_tb = format_traceback(ex_type, ex_value, ex_tb, config.DETAILED_TRACEBACK)
93 if remote_tb:
94 remote_tb = formatRemoteTraceback(remote_tb)
95 return local_tb + remote_tb
96 else:
97 # hmm. no remote tb info, return just the local tb.
98 return local_tb
99 finally:
100 # clean up cycle to traceback, to allow proper GC
101 del ex_type, ex_value, ex_tb
102
103
104 def format_traceback(ex_type=None, ex_value=None, ex_tb=None, detailed=False):
105 """Formats an exception traceback. If you ask for detailed formatting,
106 the result will contain info on the variables in each stack frame.
107 You don't have to provide the exception info objects, if you omit them,
108 this function will obtain them itself using ``sys.exc_info()``."""
109 if ex_type is None and ex_tb is None:
110 ex_type, ex_value, ex_tb = sys.exc_info()
111 if detailed:
112 def makeStrValue(value):
113 try:
114 return repr(value)
115 except Exception:
116 try:
117 return str(value)
118 except Exception:
119 return "<ERROR>"
120
121 try:
122 result = ["-" * 52 + "\n",
123 " EXCEPTION %s: %s\n" % (ex_type, ex_value),
124 " Extended stacktrace follows (most recent call last)\n"]
125 skipLocals = True # don't print the locals of the very first stack frame
126 while ex_tb:
127 frame = ex_tb.tb_frame
128 sourceFileName = frame.f_code.co_filename
129 if "self" in frame.f_locals:
130 location = "%s.%s" % (frame.f_locals["self"].__class__.__name__, frame.f_code.co_name)
131 else:
132 location = frame.f_code.co_name
133 result.append("-" * 52 + "\n")
134 result.append("File \"%s\", line %d, in %s\n" % (sourceFileName, ex_tb.tb_lineno, location))
135 result.append("Source code:\n")
136 result.append(" " + linecache.getline(sourceFileName, ex_tb.tb_lineno).strip() + "\n")
137 if not skipLocals:
138 names = set()
139 names.update(getattr(frame.f_code, "co_varnames", ()))
140 names.update(getattr(frame.f_code, "co_names", ()))
141 names.update(getattr(frame.f_code, "co_cellvars", ()))
142 names.update(getattr(frame.f_code, "co_freevars", ()))
143 result.append("Local values:\n")
144 for name2 in sorted(names):
145 if name2 in frame.f_locals:
146 value = frame.f_locals[name2]
147 result.append(" %s = %s\n" % (name2, makeStrValue(value)))
148 if name2 == "self":
149 # print the local variables of the class instance
150 for name3, value in vars(value).items():
151 result.append(" self.%s = %s\n" % (name3, makeStrValue(value)))
152 skipLocals = False
153 ex_tb = ex_tb.tb_next
154 result.append("-" * 52 + "\n")
155 result.append(" EXCEPTION %s: %s\n" % (ex_type, ex_value))
156 result.append("-" * 52 + "\n")
157 return result
158 except Exception:
159 return ["-" * 52 + "\nError building extended traceback!!! :\n",
160 "".join(traceback.format_exception(*sys.exc_info())) + '-' * 52 + '\n',
161 "Original Exception follows:\n",
162 "".join(traceback.format_exception(ex_type, ex_value, ex_tb))]
163 else:
164 # default traceback format.
165 return traceback.format_exception(ex_type, ex_value, ex_tb)
166
167
168 def excepthook(ex_type, ex_value, ex_tb):
169 """An exception hook you can use for ``sys.excepthook``, to automatically print remote Pyro tracebacks"""
170 tb = "".join(get_pyro_traceback(ex_type, ex_value, ex_tb))
171 sys.stderr.write(tb)