| 1 |
"""Python's standard exception class hierarchy. |
| 2 |
|
| 3 |
Before Python 1.5, the standard exceptions were all simple string objects. |
| 4 |
In Python 1.5, the standard exceptions were converted to classes organized |
| 5 |
into a relatively flat hierarchy. String-based standard exceptions were |
| 6 |
optional, or used as a fallback if some problem occurred while importing |
| 7 |
the exception module. With Python 1.6, optional string-based standard |
| 8 |
exceptions were removed (along with the -X command line flag). |
| 9 |
|
| 10 |
The class exceptions were implemented in such a way as to be almost |
| 11 |
completely backward compatible. Some tricky uses of IOError could |
| 12 |
potentially have broken, but by Python 1.6, all of these should have |
| 13 |
been fixed. As of Python 1.6, the class-based standard exceptions are |
| 14 |
now implemented in C, and are guaranteed to exist in the Python |
| 15 |
interpreter. |
| 16 |
|
| 17 |
Here is a rundown of the class hierarchy. The classes found here are |
| 18 |
inserted into both the exceptions module and the `built-in' module. It is |
| 19 |
recommended that user defined class based exceptions be derived from the |
| 20 |
`Exception' class, although this is currently not enforced. |
| 21 |
|
| 22 |
BaseException |
| 23 |
+-- SystemExit |
| 24 |
+-- KeyboardInterrupt |
| 25 |
+-- GeneratorExit |
| 26 |
+-- Exception |
| 27 |
+-- StopIteration |
| 28 |
+-- ArithmeticError |
| 29 |
| +-- FloatingPointError |
| 30 |
| +-- OverflowError |
| 31 |
| +-- ZeroDivisionError |
| 32 |
+-- AssertionError |
| 33 |
+-- AttributeError |
| 34 |
+-- BufferError |
| 35 |
+-- OSError |
| 36 |
| = EnvironmentError |
| 37 |
| = IOError |
| 38 |
| = WindowsError (Windows) |
| 39 |
| = VMSError (VMS) |
| 40 |
| +-- BlockingIOError |
| 41 |
| +-- ChildProcessError |
| 42 |
| +-- ConnectionError |
| 43 |
| | +-- BrokenPipeError |
| 44 |
| | +-- ConnectionAbortedError |
| 45 |
| | +-- ConnectionRefusedError |
| 46 |
| | +-- ConnectionResetError |
| 47 |
| +-- FileExistsError |
| 48 |
| +-- FileNotFoundError |
| 49 |
| +-- InterruptedError |
| 50 |
| +-- IsADirectoryError |
| 51 |
| +-- NotADirectoryError |
| 52 |
| +-- PermissionError |
| 53 |
| +-- ProcessLookupError |
| 54 |
| +-- TimeoutError |
| 55 |
+-- EOFError |
| 56 |
+-- ImportError |
| 57 |
+-- LookupError |
| 58 |
| +-- IndexError |
| 59 |
| +-- KeyError |
| 60 |
+-- MemoryError |
| 61 |
+-- NameError |
| 62 |
| +-- UnboundLocalError |
| 63 |
+-- ReferenceError |
| 64 |
+-- RuntimeError |
| 65 |
| +-- NotImplementedError |
| 66 |
| +-- RecursionError |
| 67 |
+-- StopAsyncIteration |
| 68 |
+-- SyntaxError |
| 69 |
| +-- IndentationError |
| 70 |
| +-- TabError |
| 71 |
+-- SystemError |
| 72 |
+-- TypeError |
| 73 |
+-- ValueError |
| 74 |
| +-- UnicodeError |
| 75 |
| +-- UnicodeDecodeError |
| 76 |
| +-- UnicodeEncodeError |
| 77 |
| +-- UnicodeTranslateError |
| 78 |
+-- Warning |
| 79 |
+-- DeprecationWarning |
| 80 |
+-- PendingDeprecationWarning |
| 81 |
+-- RuntimeWarning |
| 82 |
+-- SyntaxWarning |
| 83 |
+-- UserWarning |
| 84 |
+-- FutureWarning |
| 85 |
+-- ImportWarning |
| 86 |
+-- UnicodeWarning |
| 87 |
+-- BytesWarning |
| 88 |
+-- ResourceWarning |
| 89 |
+-- EncodingWarning |
| 90 |
""" |
| 91 |
import errno |
| 92 |
from pypy.interpreter.baseobjspace import W_Root |
| 93 |
from pypy.interpreter.typedef import ( |
| 94 |
TypeDef, GetSetProperty, |
| 95 |
descr_get_dict, descr_set_dict, descr_del_dict) |
| 96 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault |
| 97 |
from pypy.interpreter.error import OperationError, oefmt |
| 98 |
from pypy.interpreter.pytraceback import PyTraceback, check_traceback |
| 99 |
from rpython.rlib import rwin32, jit |
| 100 |
|
| 101 |
|
| 102 |
def readwrite_attrproperty_w(name, cls): |
| 103 |
def fget(space, obj): |
| 104 |
return getattr(obj, name) |
| 105 |
|
| 106 |
def fset(space, obj, w_val): |
| 107 |
setattr(obj, name, w_val) |
| 108 |
return GetSetProperty(fget, fset, cls=cls) |
| 109 |
|
| 110 |
|
| 111 |
class W_BaseException(W_Root): |
| 112 |
"""Superclass representing the base of the exception hierarchy.""" |
| 113 |
w_dict = None |
| 114 |
args_w = [] |
| 115 |
w_cause = None |
| 116 |
w_context = None |
| 117 |
w_traceback = None |
| 118 |
suppress_context = False |
| 119 |
|
| 120 |
def __init__(self, space): |
| 121 |
pass |
| 122 |
|
| 123 |
def descr_init(self, space, args_w): |
| 124 |
self.args_w = args_w |
| 125 |
|
| 126 |
def descr_str(self, space): |
| 127 |
lgt = len(self.args_w) |
| 128 |
if lgt == 0: |
| 129 |
return space.newtext('') |
| 130 |
elif lgt == 1: |
| 131 |
return space.str(self.args_w[0]) |
| 132 |
else: |
| 133 |
return space.str(space.newtuple(self.args_w)) |
| 134 |
|
| 135 |
def descr_repr(self, space): |
| 136 |
lgt = len(self.args_w) |
| 137 |
if lgt == 0: |
| 138 |
args_repr = b"()" |
| 139 |
elif lgt == 1: |
| 140 |
args_repr = (b"(" + |
| 141 |
space.utf8_w(space.repr(self.args_w[0])) + |
| 142 |
b")") |
| 143 |
else: |
| 144 |
args_repr = space.utf8_w( |
| 145 |
space.repr(space.newtuple(self.args_w))) |
| 146 |
clsname = self.getclass(space).getname(space) |
| 147 |
return space.newtext(clsname + args_repr) |
| 148 |
|
| 149 |
def __repr__(self): |
| 150 |
"""representation for debugging purposes""" |
| 151 |
return '%s(%s)' % (self.__class__.__name__, self.args_w) |
| 152 |
|
| 153 |
def descr_getargs(self, space): |
| 154 |
return space.newtuple(self.args_w) |
| 155 |
|
| 156 |
def descr_setargs(self, space, w_newargs): |
| 157 |
self.args_w = space.fixedview(w_newargs) |
| 158 |
|
| 159 |
def descr_delargs(self, space): |
| 160 |
raise oefmt(space.w_TypeError, |
| 161 |
"args may not be deleted") |
| 162 |
|
| 163 |
def descr_getcause(self, space): |
| 164 |
return self.w_cause |
| 165 |
|
| 166 |
def descr_setcause(self, space, w_newcause): |
| 167 |
if space.is_w(w_newcause, space.w_None): |
| 168 |
w_newcause = None |
| 169 |
elif not space.exception_is_valid_class_w(space.type(w_newcause)): |
| 170 |
raise oefmt(space.w_TypeError, |
| 171 |
"exception cause must be None or derive from " |
| 172 |
"BaseException") |
| 173 |
self.w_cause = w_newcause |
| 174 |
self.suppress_context = True |
| 175 |
|
| 176 |
def descr_delcause(self, space): |
| 177 |
raise oefmt(space.w_TypeError, |
| 178 |
"__cause__ may not be deleted") |
| 179 |
|
| 180 |
def descr_getcontext(self, space): |
| 181 |
return self.w_context |
| 182 |
|
| 183 |
def descr_setcontext(self, space, w_newcontext): |
| 184 |
if not (space.is_w(w_newcontext, space.w_None) or |
| 185 |
space.exception_is_valid_class_w(space.type(w_newcontext))): |
| 186 |
raise oefmt(space.w_TypeError, |
| 187 |
"exception context must be None or derive from " |
| 188 |
"BaseException") |
| 189 |
self.w_context = w_newcontext |
| 190 |
|
| 191 |
def descr_delcontext(self, space): |
| 192 |
raise oefmt(space.w_TypeError, |
| 193 |
"__context__ may not be deleted") |
| 194 |
|
| 195 |
def descr_gettraceback(self, space): |
| 196 |
tb = self.w_traceback |
| 197 |
if tb is not None and isinstance(tb, PyTraceback): |
| 198 |
|
| 199 |
tb.frame.mark_as_escaped() |
| 200 |
return tb |
| 201 |
|
| 202 |
def descr_settraceback(self, space, w_newtraceback): |
| 203 |
msg = '__traceback__ must be a traceback or None' |
| 204 |
if not space.is_w(w_newtraceback, space.w_None): |
| 205 |
w_newtraceback = check_traceback(space, w_newtraceback, msg) |
| 206 |
self.w_traceback = w_newtraceback |
| 207 |
|
| 208 |
def descr_deltraceback(self, space): |
| 209 |
raise oefmt(space.w_TypeError, |
| 210 |
"__traceback__ may not be deleted") |
| 211 |
|
| 212 |
def descr_getsuppresscontext(self, space): |
| 213 |
return space.newbool(self.suppress_context) |
| 214 |
|
| 215 |
def descr_setsuppresscontext(self, space, w_value): |
| 216 |
self.suppress_context = space.bool_w(w_value) |
| 217 |
|
| 218 |
def descr_delsuppresscontext(self, space): |
| 219 |
raise oefmt(space.w_TypeError, |
| 220 |
"__suppress_context__ may not be deleted") |
| 221 |
|
| 222 |
def getdict(self, space): |
| 223 |
if self.w_dict is None: |
| 224 |
self.w_dict = space.newdict(instance=True) |
| 225 |
return self.w_dict |
| 226 |
|
| 227 |
def setdict(self, space, w_dict): |
| 228 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 229 |
raise oefmt(space.w_TypeError, |
| 230 |
"setting exceptions's dictionary to a non-dict") |
| 231 |
self.w_dict = w_dict |
| 232 |
|
| 233 |
def descr_reduce(self, space): |
| 234 |
lst = [self.getclass(space), space.newtuple(self.args_w)] |
| 235 |
if self.w_dict is not None and space.is_true(self.w_dict): |
| 236 |
lst = lst + [self.w_dict] |
| 237 |
return space.newtuple(lst) |
| 238 |
|
| 239 |
def descr_setstate(self, space, w_dict): |
| 240 |
w_olddict = self.getdict(space) |
| 241 |
space.call_method(w_olddict, 'update', w_dict) |
| 242 |
|
| 243 |
def descr_with_traceback(self, space, w_traceback): |
| 244 |
self.descr_settraceback(space, w_traceback) |
| 245 |
return self |
| 246 |
|
| 247 |
def _cleanup_(self): |
| 248 |
raise Exception("Prebuilt instances of (subclasses of) BaseException " |
| 249 |
"must be avoided in Python 3.x. They have mutable " |
| 250 |
"attributes related to tracebacks, so whenever they " |
| 251 |
"are raised in the actual program they will " |
| 252 |
"accumulate more frames and never free them.") |
| 253 |
|
| 254 |
def descr_add_note(self, space, w_note): |
| 255 |
"""Exception.add_note(note) -- |
| 256 |
add a note to the exception""" |
| 257 |
if not space.isinstance_w(w_note, space.w_unicode): |
| 258 |
raise oefmt(space.w_TypeError, |
| 259 |
"note must be a str, not %T", |
| 260 |
w_note) |
| 261 |
w_dict = self.getdict(space) |
| 262 |
w_attr = space.newtext("__notes__") |
| 263 |
try: |
| 264 |
w_list = space.getitem(w_dict, w_attr) |
| 265 |
except OperationError as e: |
| 266 |
if not e.match(space, space.w_KeyError): |
| 267 |
raise |
| 268 |
space.setitem(w_dict, w_attr, space.newlist([w_note])) |
| 269 |
return |
| 270 |
if not space.isinstance_w(w_list, space.w_list): |
| 271 |
raise oefmt(space.w_TypeError, "Cannot add note: __notes__ is not a list") |
| 272 |
space.call_method(w_list, "append", w_note) |
| 273 |
|
| 274 |
def _new(cls, basecls=None): |
| 275 |
if basecls is None: |
| 276 |
basecls = cls |
| 277 |
def descr_new_base_exception(space, w_subtype, __args__): |
| 278 |
args_w, kwds_w = __args__.unpack() |
| 279 |
exc = space.allocate_instance(cls, w_subtype) |
| 280 |
basecls.__init__(exc, space) |
| 281 |
exc.args_w = args_w |
| 282 |
return exc |
| 283 |
descr_new_base_exception.func_name = 'descr_new_' + cls.__name__ |
| 284 |
return interp2app(descr_new_base_exception) |
| 285 |
|
| 286 |
W_BaseException.typedef = TypeDef( |
| 287 |
'BaseException', |
| 288 |
__doc__ = W_BaseException.__doc__, |
| 289 |
__new__ = _new(W_BaseException), |
| 290 |
__init__ = interp2app(W_BaseException.descr_init), |
| 291 |
__str__ = interp2app(W_BaseException.descr_str), |
| 292 |
__repr__ = interp2app(W_BaseException.descr_repr), |
| 293 |
__dict__ = GetSetProperty(descr_get_dict, descr_set_dict, descr_del_dict, |
| 294 |
cls=W_BaseException), |
| 295 |
__reduce__ = interp2app(W_BaseException.descr_reduce), |
| 296 |
__setstate__ = interp2app(W_BaseException.descr_setstate), |
| 297 |
with_traceback = interp2app(W_BaseException.descr_with_traceback), |
| 298 |
args = GetSetProperty(W_BaseException.descr_getargs, |
| 299 |
W_BaseException.descr_setargs, |
| 300 |
W_BaseException.descr_delargs), |
| 301 |
__cause__ = GetSetProperty(W_BaseException.descr_getcause, |
| 302 |
W_BaseException.descr_setcause, |
| 303 |
W_BaseException.descr_delcause), |
| 304 |
__context__ = GetSetProperty(W_BaseException.descr_getcontext, |
| 305 |
W_BaseException.descr_setcontext, |
| 306 |
W_BaseException.descr_delcontext), |
| 307 |
__suppress_context__ = GetSetProperty( |
| 308 |
W_BaseException.descr_getsuppresscontext, |
| 309 |
W_BaseException.descr_setsuppresscontext, |
| 310 |
W_BaseException.descr_delsuppresscontext), |
| 311 |
__traceback__ = GetSetProperty(W_BaseException.descr_gettraceback, |
| 312 |
W_BaseException.descr_settraceback, |
| 313 |
W_BaseException.descr_deltraceback), |
| 314 |
add_note = interp2app(W_BaseException.descr_add_note), |
| 315 |
) |
| 316 |
|
| 317 |
def _new_exception(name, base, docstring, **kwargs): |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
class W_Exc(base): |
| 325 |
__doc__ = docstring |
| 326 |
|
| 327 |
W_Exc.__name__ = 'W_' + name |
| 328 |
|
| 329 |
realbase = base.typedef.applevel_subclasses_base or base |
| 330 |
|
| 331 |
for k, v in kwargs.items(): |
| 332 |
kwargs[k] = interp2app(v.__get__(None, realbase)) |
| 333 |
W_Exc.typedef = TypeDef( |
| 334 |
name, |
| 335 |
base.typedef, |
| 336 |
__rpython_level_class__=W_Exc, |
| 337 |
__doc__ = W_Exc.__doc__, |
| 338 |
**kwargs |
| 339 |
) |
| 340 |
W_Exc.typedef.applevel_subclasses_base = realbase |
| 341 |
return W_Exc |
| 342 |
|
| 343 |
W_Exception = _new_exception('Exception', W_BaseException, |
| 344 |
"""Common base class for all non-exit exceptions.""") |
| 345 |
|
| 346 |
W_GeneratorExit = _new_exception('GeneratorExit', W_BaseException, |
| 347 |
"""Request that a generator exit.""") |
| 348 |
|
| 349 |
W_BufferError = _new_exception('BufferError', W_Exception, |
| 350 |
"""Buffer error.""") |
| 351 |
|
| 352 |
W_ValueError = _new_exception('ValueError', W_Exception, |
| 353 |
"""Inappropriate argument value (of correct type).""") |
| 354 |
|
| 355 |
|
| 356 |
class W_ImportError(W_Exception): |
| 357 |
"""Import can't find module, or can't find name in module.""" |
| 358 |
w_name = None |
| 359 |
w_path = None |
| 360 |
w_msg = None |
| 361 |
|
| 362 |
@jit.unroll_safe |
| 363 |
def descr_init(self, space, __args__): |
| 364 |
args_w, kwargs_w = __args__.unpack() |
| 365 |
w_name = kwargs_w.pop("name", None) |
| 366 |
w_path = kwargs_w.pop("path", None) |
| 367 |
if kwargs_w: |
| 368 |
for key in kwargs_w: |
| 369 |
raise oefmt(space.w_TypeError, |
| 370 |
"'%s' is an invalid keyword argument for ImportError", key) |
| 371 |
self.w_name = w_name if w_name is not None else space.w_None |
| 372 |
self.w_path = w_path if w_path is not None else space.w_None |
| 373 |
if len(args_w) == 1: |
| 374 |
self.w_msg = args_w[0] |
| 375 |
else: |
| 376 |
self.w_msg = space.w_None |
| 377 |
W_Exception.descr_init(self, space, args_w) |
| 378 |
|
| 379 |
def descr_reduce(self, space): |
| 380 |
lst = [self.getclass(space), space.newtuple(self.args_w)] |
| 381 |
if self.w_dict is not None and space.is_true(self.w_dict): |
| 382 |
w_dict = space.call_method(self.w_dict, "copy") |
| 383 |
else: |
| 384 |
w_dict = space.newdict() |
| 385 |
if not space.is_w(self.w_name, space.w_None): |
| 386 |
space.setitem(w_dict, space.newtext("name"), self.w_name) |
| 387 |
if not space.is_w(self.w_path, space.w_None): |
| 388 |
space.setitem(w_dict, space.newtext("path"), self.w_path) |
| 389 |
if space.is_true(w_dict): |
| 390 |
lst = [lst[0], lst[1], w_dict] |
| 391 |
return space.newtuple(lst) |
| 392 |
|
| 393 |
def descr_setstate(self, space, w_dict): |
| 394 |
self.w_name = space.call_method(w_dict, "pop", space.newtext("name"), space.w_None) |
| 395 |
self.w_path = space.call_method(w_dict, "pop", space.newtext("path"), space.w_None) |
| 396 |
w_olddict = self.getdict(space) |
| 397 |
space.call_method(w_olddict, 'update', w_dict) |
| 398 |
|
| 399 |
|
| 400 |
W_ImportError.typedef = TypeDef( |
| 401 |
'ImportError', |
| 402 |
W_Exception.typedef, |
| 403 |
__doc__ = W_ImportError.__doc__, |
| 404 |
__module__ = 'builtins', |
| 405 |
__new__ = _new(W_ImportError), |
| 406 |
__init__ = interp2app(W_ImportError.descr_init), |
| 407 |
__reduce__ = interp2app(W_ImportError.descr_reduce), |
| 408 |
__setstate__ = interp2app(W_ImportError.descr_setstate), |
| 409 |
name = readwrite_attrproperty_w('w_name', W_ImportError), |
| 410 |
path = readwrite_attrproperty_w('w_path', W_ImportError), |
| 411 |
msg = readwrite_attrproperty_w('w_msg', W_ImportError), |
| 412 |
) |
| 413 |
|
| 414 |
|
| 415 |
W_RuntimeError = _new_exception('RuntimeError', W_Exception, |
| 416 |
"""Unspecified run-time error.""") |
| 417 |
|
| 418 |
W_UnicodeError = _new_exception('UnicodeError', W_ValueError, |
| 419 |
"""Unicode related error.""") |
| 420 |
|
| 421 |
W_ModuleNotFoundError = _new_exception( |
| 422 |
'ModuleNotFoundError', W_ImportError, """Module not found.""" |
| 423 |
) |
| 424 |
|
| 425 |
|
| 426 |
class W_UnicodeTranslateError(W_UnicodeError): |
| 427 |
"""Unicode translation error.""" |
| 428 |
w_object = None |
| 429 |
w_start = None |
| 430 |
w_end = None |
| 431 |
w_reason = None |
| 432 |
|
| 433 |
def descr_init(self, space, w_object, w_start, w_end, w_reason): |
| 434 |
|
| 435 |
space.utf8_w(w_object) |
| 436 |
space.int_w(w_start) |
| 437 |
space.int_w(w_end) |
| 438 |
space.realtext_w(w_reason) |
| 439 |
|
| 440 |
self.w_object = w_object |
| 441 |
self.w_start = w_start |
| 442 |
self.w_end = w_end |
| 443 |
self.w_reason = w_reason |
| 444 |
W_BaseException.descr_init(self, space, [w_object, w_start, |
| 445 |
w_end, w_reason]) |
| 446 |
|
| 447 |
def descr_str(self, space): |
| 448 |
return space.appexec([self], r"""(self): |
| 449 |
if self.object is None: |
| 450 |
return "" |
| 451 |
if self.end == self.start + 1: |
| 452 |
badchar = ord(self.object[self.start]) |
| 453 |
if badchar <= 0xff: |
| 454 |
return "can't translate character '\\x%02x' in position %d: %s" % (badchar, self.start, self.reason) |
| 455 |
if badchar <= 0xffff: |
| 456 |
return "can't translate character '\\u%04x' in position %d: %s"%(badchar, self.start, self.reason) |
| 457 |
return "can't translate character '\\U%08x' in position %d: %s"%(badchar, self.start, self.reason) |
| 458 |
return "can't translate characters in position %d-%d: %s" % (self.start, self.end - 1, self.reason) |
| 459 |
""") |
| 460 |
|
| 461 |
W_UnicodeTranslateError.typedef = TypeDef( |
| 462 |
'UnicodeTranslateError', |
| 463 |
W_UnicodeError.typedef, |
| 464 |
__doc__ = W_UnicodeTranslateError.__doc__, |
| 465 |
__new__ = _new(W_UnicodeTranslateError), |
| 466 |
__init__ = interp2app(W_UnicodeTranslateError.descr_init), |
| 467 |
__str__ = interp2app(W_UnicodeTranslateError.descr_str), |
| 468 |
object = readwrite_attrproperty_w('w_object', W_UnicodeTranslateError), |
| 469 |
start = readwrite_attrproperty_w('w_start', W_UnicodeTranslateError), |
| 470 |
end = readwrite_attrproperty_w('w_end', W_UnicodeTranslateError), |
| 471 |
reason = readwrite_attrproperty_w('w_reason', W_UnicodeTranslateError), |
| 472 |
) |
| 473 |
|
| 474 |
W_LookupError = _new_exception('LookupError', W_Exception, |
| 475 |
"""Base class for lookup errors.""") |
| 476 |
|
| 477 |
def key_error_str(self, space): |
| 478 |
if len(self.args_w) == 0: |
| 479 |
return space.newtext('') |
| 480 |
elif len(self.args_w) == 1: |
| 481 |
return space.repr(self.args_w[0]) |
| 482 |
else: |
| 483 |
return space.str(space.newtuple(self.args_w)) |
| 484 |
|
| 485 |
W_KeyError = _new_exception('KeyError', W_LookupError, |
| 486 |
"""Mapping key not found.""", |
| 487 |
__str__ = key_error_str) |
| 488 |
|
| 489 |
|
| 490 |
class W_StopIteration(W_Exception): |
| 491 |
"""Signal the end from iterator.__next__().""" |
| 492 |
def __init__(self, space): |
| 493 |
self.w_value = space.w_None |
| 494 |
W_Exception.__init__(self, space) |
| 495 |
|
| 496 |
def descr_init(self, space, args_w): |
| 497 |
if len(args_w) > 0: |
| 498 |
self.w_value = args_w[0] |
| 499 |
W_Exception.descr_init(self, space, args_w) |
| 500 |
|
| 501 |
W_StopIteration.typedef = TypeDef( |
| 502 |
'StopIteration', |
| 503 |
W_Exception.typedef, |
| 504 |
__doc__ = W_StopIteration.__doc__, |
| 505 |
__module__ = 'builtins', |
| 506 |
__new__ = _new(W_StopIteration), |
| 507 |
__init__ = interp2app(W_StopIteration.descr_init), |
| 508 |
value = readwrite_attrproperty_w('w_value', W_StopIteration), |
| 509 |
) |
| 510 |
|
| 511 |
|
| 512 |
W_Warning = _new_exception('Warning', W_Exception, |
| 513 |
"""Base class for warning categories.""") |
| 514 |
|
| 515 |
W_PendingDeprecationWarning = _new_exception('PendingDeprecationWarning', |
| 516 |
W_Warning, |
| 517 |
"""Base class for warnings about features which will be deprecated in the future.""") |
| 518 |
|
| 519 |
class W_OSError(W_Exception): |
| 520 |
"""OS system call failed.""" |
| 521 |
|
| 522 |
def __init__(self, space): |
| 523 |
self.w_errno = None |
| 524 |
self.w_winerror = None |
| 525 |
self.w_strerror = None |
| 526 |
self.w_filename = None |
| 527 |
self.w_filename2 = None |
| 528 |
self.written = -1 |
| 529 |
W_BaseException.__init__(self, space) |
| 530 |
|
| 531 |
@staticmethod |
| 532 |
def _use_init(space, w_subtype): |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
if space.is_w(w_subtype, space.w_OSError): |
| 543 |
return False |
| 544 |
if ((space.getattr(w_subtype, space.newtext('__init__')) != |
| 545 |
space.getattr(space.w_OSError, space.newtext('__init__'))) and |
| 546 |
(space.getattr(w_subtype, space.newtext('__new__')) == |
| 547 |
space.getattr(space.w_OSError, space.newtext('__new__')))): |
| 548 |
return True |
| 549 |
return False |
| 550 |
|
| 551 |
@staticmethod |
| 552 |
def _parse_init_args(space, args_w): |
| 553 |
if 2 <= len(args_w) <= 5: |
| 554 |
w_errno = args_w[0] |
| 555 |
w_strerror = args_w[1] |
| 556 |
w_filename = None |
| 557 |
w_winerror = None |
| 558 |
w_filename2 = None |
| 559 |
if len(args_w) > 2: |
| 560 |
w_filename = args_w[2] |
| 561 |
if len(args_w) > 3: |
| 562 |
w_winerror = args_w[3] |
| 563 |
if len(args_w) > 4: |
| 564 |
w_filename2 = args_w[4] |
| 565 |
if rwin32.WIN32 and w_winerror: |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
try: |
| 573 |
winerror = space.int_w(w_winerror) |
| 574 |
except OperationError: |
| 575 |
w_winerror = None |
| 576 |
else: |
| 577 |
w_errno = space.newint( |
| 578 |
WINERROR_TO_ERRNO.get(winerror, DEFAULT_WIN32_ERRNO)) |
| 579 |
return w_errno, w_winerror, w_strerror, w_filename, w_filename2 |
| 580 |
return None, None, None, None, None |
| 581 |
|
| 582 |
@staticmethod |
| 583 |
def descr_new(space, w_subtype, __args__): |
| 584 |
args_w, kwds_w = __args__.unpack() |
| 585 |
w_errno = None |
| 586 |
w_winerror = None |
| 587 |
w_strerror = None |
| 588 |
w_filename = None |
| 589 |
w_filename2 = None |
| 590 |
if not W_OSError._use_init(space, w_subtype): |
| 591 |
if kwds_w: |
| 592 |
raise oefmt(space.w_TypeError, |
| 593 |
"OSError does not take keyword arguments") |
| 594 |
(w_errno, w_winerror, w_strerror, w_filename, w_filename2 |
| 595 |
) = W_OSError._parse_init_args(space, args_w) |
| 596 |
if (not space.is_none(w_errno) and |
| 597 |
space.is_w(w_subtype, space.gettypeobject(W_OSError.typedef))): |
| 598 |
try: |
| 599 |
errno = space.int_w(w_errno) |
| 600 |
except OperationError: |
| 601 |
pass |
| 602 |
else: |
| 603 |
try: |
| 604 |
subclass = ERRNO_MAP[errno] |
| 605 |
except KeyError: |
| 606 |
pass |
| 607 |
else: |
| 608 |
w_subtype = space.gettypeobject(subclass.typedef) |
| 609 |
exc = space.allocate_instance(W_OSError, w_subtype) |
| 610 |
W_OSError.__init__(exc, space) |
| 611 |
if not W_OSError._use_init(space, w_subtype): |
| 612 |
exc._init_error(space, args_w, w_errno, w_winerror, w_strerror, |
| 613 |
w_filename, w_filename2) |
| 614 |
return exc |
| 615 |
|
| 616 |
def descr_init(self, space, __args__): |
| 617 |
args_w, kwds_w = __args__.unpack() |
| 618 |
if not W_OSError._use_init(space, space.type(self)): |
| 619 |
|
| 620 |
return |
| 621 |
if kwds_w: |
| 622 |
raise oefmt(space.w_TypeError, |
| 623 |
"OSError does not take keyword arguments") |
| 624 |
(w_errno, w_winerror, w_strerror, w_filename, w_filename2 |
| 625 |
) = W_OSError._parse_init_args(space, args_w) |
| 626 |
self._init_error(space, args_w, w_errno, w_winerror, w_strerror, |
| 627 |
w_filename, w_filename2) |
| 628 |
|
| 629 |
def _init_error(self, space, args_w, w_errno, w_winerror, w_strerror, |
| 630 |
w_filename, w_filename2): |
| 631 |
W_BaseException.descr_init(self, space, args_w) |
| 632 |
self.w_errno = w_errno |
| 633 |
self.w_winerror = w_winerror |
| 634 |
self.w_strerror = w_strerror |
| 635 |
|
| 636 |
if not space.is_none(w_filename): |
| 637 |
if space.isinstance_w( |
| 638 |
self, space.gettypeobject(W_BlockingIOError.typedef)): |
| 639 |
try: |
| 640 |
self.written = space.int_w(w_filename) |
| 641 |
except OperationError: |
| 642 |
self.w_filename = w_filename |
| 643 |
if not space.is_none(w_filename2): |
| 644 |
self.w_filename2 = w_filename2 |
| 645 |
else: |
| 646 |
if not space.is_none(w_filename): |
| 647 |
self.w_filename = w_filename |
| 648 |
if not space.is_none(w_filename2): |
| 649 |
self.w_filename2 = w_filename2 |
| 650 |
|
| 651 |
|
| 652 |
self.args_w = [w_errno, w_strerror] |
| 653 |
|
| 654 |
|
| 655 |
def descr_reduce(self, space): |
| 656 |
extra = [] |
| 657 |
if self.w_filename: |
| 658 |
extra.append(self.w_filename) |
| 659 |
if self.w_filename2: |
| 660 |
extra.append(space.w_None) |
| 661 |
extra.append(self.w_filename2) |
| 662 |
lst = [self.getclass(space), space.newtuple(self.args_w + extra)] |
| 663 |
if self.w_dict is not None and space.is_true(self.w_dict): |
| 664 |
lst = lst + [self.w_dict] |
| 665 |
return space.newtuple(lst) |
| 666 |
|
| 667 |
def descr_str(self, space): |
| 668 |
if self.w_errno: |
| 669 |
errno = space.utf8_w(space.str(self.w_errno)) |
| 670 |
else: |
| 671 |
errno = b"" |
| 672 |
if self.w_strerror: |
| 673 |
strerror = space.utf8_w(space.str(self.w_strerror)) |
| 674 |
else: |
| 675 |
strerror = b"" |
| 676 |
if rwin32.WIN32 and self.w_winerror: |
| 677 |
winerror = space.utf8_w(space.str(self.w_winerror)) |
| 678 |
|
| 679 |
if self.w_filename: |
| 680 |
if self.w_filename2: |
| 681 |
return space.newtext(b"[WinError %s] %s: %s -> %s" % ( |
| 682 |
winerror, strerror, |
| 683 |
space.utf8_w(space.repr(self.w_filename)), |
| 684 |
space.utf8_w(space.repr(self.w_filename2)))) |
| 685 |
return space.newtext(b"[WinError %s] %s: %s" % ( |
| 686 |
winerror, strerror, |
| 687 |
space.utf8_w(space.repr(self.w_filename)))) |
| 688 |
return space.newtext(b"[WinError %s] %s" % ( |
| 689 |
winerror, strerror)) |
| 690 |
if self.w_filename: |
| 691 |
if self.w_filename2: |
| 692 |
return space.newtext(b"[Errno %s] %s: %s -> %s" % ( |
| 693 |
errno, strerror, |
| 694 |
space.utf8_w(space.repr(self.w_filename)), |
| 695 |
space.utf8_w(space.repr(self.w_filename2)))) |
| 696 |
return space.newtext(b"[Errno %s] %s: %s" % ( |
| 697 |
errno, strerror, |
| 698 |
space.utf8_w(space.repr(self.w_filename)))) |
| 699 |
if self.w_errno and self.w_strerror: |
| 700 |
return space.newtext(b"[Errno %s] %s" % ( |
| 701 |
errno, strerror)) |
| 702 |
return W_BaseException.descr_str(self, space) |
| 703 |
|
| 704 |
def descr_get_written(self, space): |
| 705 |
if self.written == -1: |
| 706 |
raise oefmt(space.w_AttributeError, "characters_written") |
| 707 |
return space.newint(self.written) |
| 708 |
|
| 709 |
def descr_set_written(self, space, w_written): |
| 710 |
self.written = space.int_w(w_written) |
| 711 |
|
| 712 |
def descr_del_written(self, space): |
| 713 |
if self.written == -1: |
| 714 |
raise oefmt(space.w_AttributeError, "characters_written") |
| 715 |
self.written = -1 |
| 716 |
|
| 717 |
|
| 718 |
if hasattr(rwin32, 'build_winerror_to_errno'): |
| 719 |
WINERROR_TO_ERRNO, DEFAULT_WIN32_ERRNO = rwin32.build_winerror_to_errno() |
| 720 |
else: |
| 721 |
WINERROR_TO_ERRNO, DEFAULT_WIN32_ERRNO = {}, 22 |
| 722 |
|
| 723 |
if rwin32.WIN32: |
| 724 |
_winerror_property = dict( |
| 725 |
winerror = readwrite_attrproperty_w('w_winerror', W_OSError), |
| 726 |
) |
| 727 |
else: |
| 728 |
_winerror_property = dict() |
| 729 |
|
| 730 |
|
| 731 |
W_OSError.typedef = TypeDef( |
| 732 |
'OSError', |
| 733 |
W_Exception.typedef, |
| 734 |
__doc__ = W_OSError.__doc__, |
| 735 |
__new__ = interp2app(W_OSError.descr_new), |
| 736 |
__reduce__ = interp2app(W_OSError.descr_reduce), |
| 737 |
__init__ = interp2app(W_OSError.descr_init), |
| 738 |
__str__ = interp2app(W_OSError.descr_str), |
| 739 |
errno = readwrite_attrproperty_w('w_errno', W_OSError), |
| 740 |
strerror = readwrite_attrproperty_w('w_strerror', W_OSError), |
| 741 |
filename = readwrite_attrproperty_w('w_filename', W_OSError), |
| 742 |
filename2= readwrite_attrproperty_w('w_filename2',W_OSError), |
| 743 |
characters_written = GetSetProperty(W_OSError.descr_get_written, |
| 744 |
W_OSError.descr_set_written, |
| 745 |
W_OSError.descr_del_written), |
| 746 |
**_winerror_property |
| 747 |
) |
| 748 |
|
| 749 |
W_BlockingIOError = _new_exception( |
| 750 |
"BlockingIOError", W_OSError, "I/O operation would block") |
| 751 |
W_ConnectionError = _new_exception( |
| 752 |
"ConnectionError", W_OSError, "Connection error.") |
| 753 |
W_ChildProcessError = _new_exception( |
| 754 |
"ChildProcessError", W_OSError, "Child process error.") |
| 755 |
W_BrokenPipeError = _new_exception( |
| 756 |
"BrokenPipeError", W_ConnectionError, "Broken pipe.") |
| 757 |
W_ConnectionAbortedError = _new_exception( |
| 758 |
"ConnectionAbortedError", W_ConnectionError, "Connection aborted.") |
| 759 |
W_ConnectionRefusedError = _new_exception( |
| 760 |
"ConnectionRefusedError", W_ConnectionError, "Connection refused.") |
| 761 |
W_ConnectionResetError = _new_exception( |
| 762 |
"ConnectionResetError", W_ConnectionError, "Connection reset.") |
| 763 |
W_FileExistsError = _new_exception( |
| 764 |
"FileExistsError", W_OSError, "File already exists.") |
| 765 |
W_FileNotFoundError = _new_exception( |
| 766 |
"FileNotFoundError", W_OSError, "File not found.") |
| 767 |
W_IsADirectoryError = _new_exception( |
| 768 |
"IsADirectoryError", W_OSError, "Operation doesn't work on directories.") |
| 769 |
W_NotADirectoryError = _new_exception( |
| 770 |
"NotADirectoryError", W_OSError, "Operation only works on directories.") |
| 771 |
W_InterruptedError = _new_exception( |
| 772 |
"InterruptedError", W_OSError, "Interrupted by signal.") |
| 773 |
W_PermissionError = _new_exception( |
| 774 |
"PermissionError", W_OSError, "Not enough permissions.") |
| 775 |
W_ProcessLookupError = _new_exception( |
| 776 |
"ProcessLookupError", W_OSError, "Process not found.") |
| 777 |
W_TimeoutError = _new_exception( |
| 778 |
"TimeoutError", W_OSError, "Timeout expired.") |
| 779 |
|
| 780 |
|
| 781 |
W_BytesWarning = _new_exception('BytesWarning', W_Warning, |
| 782 |
"""Mixing bytes and unicode""") |
| 783 |
|
| 784 |
W_DeprecationWarning = _new_exception('DeprecationWarning', W_Warning, |
| 785 |
"""Base class for warnings about deprecated features.""") |
| 786 |
|
| 787 |
W_ResourceWarning = _new_exception('ResourceWarning', W_Warning, |
| 788 |
"""Base class for warnings about resource usage.""") |
| 789 |
|
| 790 |
W_EncodingWarning = _new_exception('EncodingWarning', W_Warning, |
| 791 |
"""Base class for warnings about encodings.""") |
| 792 |
|
| 793 |
W_ArithmeticError = _new_exception('ArithmeticError', W_Exception, |
| 794 |
"""Base class for arithmetic errors.""") |
| 795 |
|
| 796 |
W_FloatingPointError = _new_exception('FloatingPointError', W_ArithmeticError, |
| 797 |
"""Floating point operation failed.""") |
| 798 |
|
| 799 |
W_ReferenceError = _new_exception('ReferenceError', W_Exception, |
| 800 |
"""Weak ref proxy used after referent went away.""") |
| 801 |
|
| 802 |
class W_NameError(W_Exception): |
| 803 |
"""Name not found globally.""" |
| 804 |
name = None |
| 805 |
|
| 806 |
def __init__(self, space): |
| 807 |
pass |
| 808 |
|
| 809 |
@unwrap_spec(w_name=WrappedDefault(None)) |
| 810 |
def descr_init(self, space, args_w, __kwonly__, w_name=None): |
| 811 |
self.args_w = args_w |
| 812 |
self.w_name = w_name |
| 813 |
|
| 814 |
|
| 815 |
W_NameError.typedef = TypeDef('NameError', W_Exception.typedef, |
| 816 |
__doc__ = W_NameError.__doc__, |
| 817 |
__new__ = _new(W_NameError), |
| 818 |
__init__ = interp2app(W_NameError.descr_init), |
| 819 |
name = readwrite_attrproperty_w('w_name', W_NameError), |
| 820 |
) |
| 821 |
|
| 822 |
class W_SyntaxError(W_Exception): |
| 823 |
"""Invalid syntax.""" |
| 824 |
|
| 825 |
def __init__(self, space): |
| 826 |
self.w_filename = space.w_None |
| 827 |
self.w_lineno = space.w_None |
| 828 |
self.w_offset = space.w_None |
| 829 |
self.w_text = space.w_None |
| 830 |
self.w_msg = space.w_None |
| 831 |
self.w_print_file_and_line = space.w_None |
| 832 |
self.w_end_lineno = space.w_None |
| 833 |
self.w_end_offset = space.w_None |
| 834 |
W_BaseException.__init__(self, space) |
| 835 |
|
| 836 |
def descr_init(self, space, args_w): |
| 837 |
if len(args_w) > 0: |
| 838 |
self.w_msg = args_w[0] |
| 839 |
if len(args_w) == 2: |
| 840 |
values_w = space.fixedview(args_w[1]) |
| 841 |
if len(values_w) < 4: |
| 842 |
raise oefmt(space.w_TypeError, |
| 843 |
"function missing required argument 'info[3]' (pos 4)") |
| 844 |
self.w_filename = values_w[0] |
| 845 |
self.w_lineno = values_w[1] |
| 846 |
self.w_offset = values_w[2] |
| 847 |
self.w_text = values_w[3] |
| 848 |
if len(values_w) == 5: |
| 849 |
raise oefmt(space.w_TypeError, "end_offset must be provided when end_lineno is provided") |
| 850 |
if len(values_w) == 6: |
| 851 |
self.w_end_lineno = values_w[4] |
| 852 |
self.w_end_offset = values_w[5] |
| 853 |
if len(values_w) > 6: |
| 854 |
raise oefmt(space.w_TypeError, "function takes at most 6 arguments (%s given)", str(len(values_w))) |
| 855 |
W_BaseException.descr_init(self, space, args_w) |
| 856 |
if self.w_text and space.isinstance_w(self.w_text, space.w_unicode): |
| 857 |
self._report_missing_parentheses(space) |
| 858 |
|
| 859 |
def descr_str(self, space): |
| 860 |
return space.appexec([self], """(self): |
| 861 |
if type(self.msg) is not str: |
| 862 |
return str(self.msg) |
| 863 |
|
| 864 |
lineno = None |
| 865 |
buffer = self.msg |
| 866 |
have_filename = type(self.filename) is str |
| 867 |
if type(self.lineno) is int: |
| 868 |
if (type(self.end_lineno) is int and |
| 869 |
self.end_lineno > self.lineno): |
| 870 |
lineno = 'lines %d-%d' % (self.lineno, self.end_lineno) |
| 871 |
else: |
| 872 |
lineno = 'line %d' % (self.lineno,) |
| 873 |
if have_filename: |
| 874 |
import os |
| 875 |
fname = os.path.basename(self.filename or "???") |
| 876 |
if lineno: |
| 877 |
buffer = "%s (%s, %s)" % (self.msg, fname, lineno) |
| 878 |
else: |
| 879 |
buffer ="%s (%s)" % (self.msg, fname) |
| 880 |
elif lineno: |
| 881 |
buffer = "%s (%s)" % (self.msg, lineno) |
| 882 |
return buffer |
| 883 |
""") |
| 884 |
|
| 885 |
|
| 886 |
def _report_missing_parentheses(self, space): |
| 887 |
if not space.text_w(self.w_msg).startswith("Missing parentheses in call to "): |
| 888 |
|
| 889 |
|
| 890 |
return |
| 891 |
text = space.utf8_w(self.w_text) |
| 892 |
if b'(' in text: |
| 893 |
|
| 894 |
return |
| 895 |
|
| 896 |
if self._check_for_legacy_statements(space, text, 0): |
| 897 |
return |
| 898 |
|
| 899 |
pos = text.find(b':') |
| 900 |
if pos < 0: |
| 901 |
return |
| 902 |
|
| 903 |
self._check_for_legacy_statements(space, text, pos+1) |
| 904 |
|
| 905 |
def _check_for_legacy_statements(self, space, text, start): |
| 906 |
|
| 907 |
while start < len(text) and text[start] == b' ': |
| 908 |
start += 1 |
| 909 |
|
| 910 |
if start == len(text): |
| 911 |
return False |
| 912 |
if start > 0: |
| 913 |
text = text[start:] |
| 914 |
|
| 915 |
if text.startswith(b"print "): |
| 916 |
self._set_legacy_print_statement_msg(space, text) |
| 917 |
return True |
| 918 |
|
| 919 |
if text.startswith(b"exec "): |
| 920 |
arg = text[len("exec"):].strip() |
| 921 |
if arg.endswith(";"): |
| 922 |
arg = arg[:-1].strip() |
| 923 |
suggestion = "exec(%s)" % arg |
| 924 |
compiler = space.createcompiler() |
| 925 |
try: |
| 926 |
compiler.compile(suggestion, '?', 'eval', 0) |
| 927 |
except OperationError: |
| 928 |
self.w_msg = space.newtext( |
| 929 |
"Missing parentheses in call to 'exec'") |
| 930 |
else: |
| 931 |
self.w_msg = space.newtext( |
| 932 |
"Missing parentheses in call to 'exec'. Did you mean %s?" % ( |
| 933 |
suggestion,)) |
| 934 |
return True |
| 935 |
return False |
| 936 |
|
| 937 |
def _set_legacy_print_statement_msg(self, space, text): |
| 938 |
text = text[len("print"):] |
| 939 |
text = text.strip() |
| 940 |
if text.endswith(";"): |
| 941 |
end = len(text) - 1 |
| 942 |
assert end >= 0 |
| 943 |
text = text[:end].strip() |
| 944 |
|
| 945 |
maybe_end = "" |
| 946 |
if text.endswith(","): |
| 947 |
maybe_end = " end=\" \"" |
| 948 |
|
| 949 |
suggestion = "print(%s%s)" % ( |
| 950 |
text, maybe_end) |
| 951 |
|
| 952 |
|
| 953 |
compiler = space.createcompiler() |
| 954 |
try: |
| 955 |
compiler.compile(suggestion, '?', 'eval', 0) |
| 956 |
except OperationError: |
| 957 |
pass |
| 958 |
else: |
| 959 |
self.w_msg = space.newtext( |
| 960 |
"Missing parentheses in call to 'print'. Did you mean %s?" % ( |
| 961 |
suggestion, )) |
| 962 |
|
| 963 |
|
| 964 |
W_SyntaxError.typedef = TypeDef( |
| 965 |
'SyntaxError', |
| 966 |
W_Exception.typedef, |
| 967 |
__new__ = _new(W_SyntaxError), |
| 968 |
__init__ = interp2app(W_SyntaxError.descr_init), |
| 969 |
__str__ = interp2app(W_SyntaxError.descr_str), |
| 970 |
__repr__ = interp2app(W_SyntaxError.descr_repr), |
| 971 |
__doc__ = W_SyntaxError.__doc__, |
| 972 |
msg = readwrite_attrproperty_w('w_msg', W_SyntaxError), |
| 973 |
filename = readwrite_attrproperty_w('w_filename', W_SyntaxError), |
| 974 |
lineno = readwrite_attrproperty_w('w_lineno', W_SyntaxError), |
| 975 |
offset = readwrite_attrproperty_w('w_offset', W_SyntaxError), |
| 976 |
text = readwrite_attrproperty_w('w_text', W_SyntaxError), |
| 977 |
print_file_and_line = readwrite_attrproperty_w('w_print_file_and_line', |
| 978 |
W_SyntaxError), |
| 979 |
end_lineno = readwrite_attrproperty_w('w_end_lineno', W_SyntaxError), |
| 980 |
end_offset = readwrite_attrproperty_w('w_end_offset', W_SyntaxError), |
| 981 |
) |
| 982 |
|
| 983 |
W_FutureWarning = _new_exception('FutureWarning', W_Warning, |
| 984 |
"""Base class for warnings about constructs that will change semantically in the future.""") |
| 985 |
|
| 986 |
class W_SystemExit(W_BaseException): |
| 987 |
"""Request to exit from the interpreter.""" |
| 988 |
|
| 989 |
def __init__(self, space): |
| 990 |
self.w_code = space.w_None |
| 991 |
W_BaseException.__init__(self, space) |
| 992 |
|
| 993 |
def descr_init(self, space, args_w): |
| 994 |
if len(args_w) == 1: |
| 995 |
self.w_code = args_w[0] |
| 996 |
elif len(args_w) > 1: |
| 997 |
self.w_code = space.newtuple(args_w) |
| 998 |
W_BaseException.descr_init(self, space, args_w) |
| 999 |
|
| 1000 |
W_SystemExit.typedef = TypeDef( |
| 1001 |
'SystemExit', |
| 1002 |
W_BaseException.typedef, |
| 1003 |
__new__ = _new(W_SystemExit), |
| 1004 |
__init__ = interp2app(W_SystemExit.descr_init), |
| 1005 |
__doc__ = W_SystemExit.__doc__, |
| 1006 |
code = readwrite_attrproperty_w('w_code', W_SystemExit) |
| 1007 |
) |
| 1008 |
|
| 1009 |
W_EOFError = _new_exception('EOFError', W_Exception, |
| 1010 |
"""Read beyond end of file.""") |
| 1011 |
|
| 1012 |
W_IndentationError = _new_exception('IndentationError', W_SyntaxError, |
| 1013 |
"""Improper indentation.""") |
| 1014 |
|
| 1015 |
W_TabError = _new_exception('TabError', W_IndentationError, |
| 1016 |
"""Improper mixture of spaces and tabs.""") |
| 1017 |
|
| 1018 |
W_ZeroDivisionError = _new_exception('ZeroDivisionError', W_ArithmeticError, |
| 1019 |
"""Second argument to a division or modulo operation was zero.""") |
| 1020 |
|
| 1021 |
W_SystemError = _new_exception('SystemError', W_Exception, |
| 1022 |
"""Internal error in the Python interpreter. |
| 1023 |
|
| 1024 |
Please report this to the Python maintainer, along with the traceback, |
| 1025 |
the Python version, and the hardware/OS platform and version.""") |
| 1026 |
|
| 1027 |
W_AssertionError = _new_exception('AssertionError', W_Exception, |
| 1028 |
"""Assertion failed.""") |
| 1029 |
|
| 1030 |
W_StopAsyncIteration = _new_exception('StopAsyncIteration', W_Exception, |
| 1031 |
"""Signal the end from iterator.__anext__().""") |
| 1032 |
|
| 1033 |
class W_UnicodeDecodeError(W_UnicodeError): |
| 1034 |
"""Unicode decoding error.""" |
| 1035 |
w_encoding = None |
| 1036 |
w_object = None |
| 1037 |
w_start = None |
| 1038 |
w_end = None |
| 1039 |
w_reason = None |
| 1040 |
|
| 1041 |
def descr_init(self, space, w_encoding, w_object, w_start, w_end, w_reason): |
| 1042 |
|
| 1043 |
if space.isinstance_w(w_object, space.w_bytearray): |
| 1044 |
w_bytes = space.newbytes(space.charbuf_w(w_object)) |
| 1045 |
else: |
| 1046 |
w_bytes = w_object |
| 1047 |
space.realtext_w(w_encoding) |
| 1048 |
space.bytes_w(w_bytes) |
| 1049 |
space.int_w(w_start) |
| 1050 |
space.int_w(w_end) |
| 1051 |
space.realtext_w(w_reason) |
| 1052 |
|
| 1053 |
self.w_encoding = w_encoding |
| 1054 |
self.w_object = w_bytes |
| 1055 |
self.w_start = w_start |
| 1056 |
self.w_end = w_end |
| 1057 |
self.w_reason = w_reason |
| 1058 |
W_BaseException.descr_init(self, space, [w_encoding, w_object, |
| 1059 |
w_start, w_end, w_reason]) |
| 1060 |
|
| 1061 |
def descr_str(self, space): |
| 1062 |
return space.appexec([self], """(self): |
| 1063 |
if self.object is None: |
| 1064 |
return "" |
| 1065 |
if self.end == self.start + 1: |
| 1066 |
return "'%s' codec can't decode byte 0x%02x in position %d: %s"%( |
| 1067 |
self.encoding, |
| 1068 |
self.object[self.start], self.start, self.reason) |
| 1069 |
return "'%s' codec can't decode bytes in position %d-%d: %s" % ( |
| 1070 |
self.encoding, self.start, self.end - 1, self.reason) |
| 1071 |
""") |
| 1072 |
|
| 1073 |
W_UnicodeDecodeError.typedef = TypeDef( |
| 1074 |
'UnicodeDecodeError', |
| 1075 |
W_UnicodeError.typedef, |
| 1076 |
__doc__ = W_UnicodeDecodeError.__doc__, |
| 1077 |
__new__ = _new(W_UnicodeDecodeError), |
| 1078 |
__init__ = interp2app(W_UnicodeDecodeError.descr_init), |
| 1079 |
__str__ = interp2app(W_UnicodeDecodeError.descr_str), |
| 1080 |
encoding = readwrite_attrproperty_w('w_encoding', W_UnicodeDecodeError), |
| 1081 |
object = readwrite_attrproperty_w('w_object', W_UnicodeDecodeError), |
| 1082 |
start = readwrite_attrproperty_w('w_start', W_UnicodeDecodeError), |
| 1083 |
end = readwrite_attrproperty_w('w_end', W_UnicodeDecodeError), |
| 1084 |
reason = readwrite_attrproperty_w('w_reason', W_UnicodeDecodeError), |
| 1085 |
) |
| 1086 |
|
| 1087 |
W_TypeError = _new_exception('TypeError', W_Exception, |
| 1088 |
"""Inappropriate argument type.""") |
| 1089 |
|
| 1090 |
W_IndexError = _new_exception('IndexError', W_LookupError, |
| 1091 |
"""Sequence index out of range.""") |
| 1092 |
|
| 1093 |
W_RuntimeWarning = _new_exception('RuntimeWarning', W_Warning, |
| 1094 |
"""Base class for warnings about dubious runtime behavior.""") |
| 1095 |
|
| 1096 |
W_KeyboardInterrupt = _new_exception('KeyboardInterrupt', W_BaseException, |
| 1097 |
"""Program interrupted by user.""") |
| 1098 |
|
| 1099 |
W_UserWarning = _new_exception('UserWarning', W_Warning, |
| 1100 |
"""Base class for warnings generated by user code.""") |
| 1101 |
|
| 1102 |
W_SyntaxWarning = _new_exception('SyntaxWarning', W_Warning, |
| 1103 |
"""Base class for warnings about dubious syntax.""") |
| 1104 |
|
| 1105 |
W_UnicodeWarning = _new_exception('UnicodeWarning', W_Warning, |
| 1106 |
"""Base class for warnings about Unicode related problems, mostly |
| 1107 |
related to conversion problems.""") |
| 1108 |
|
| 1109 |
W_ImportWarning = _new_exception('ImportWarning', W_Warning, |
| 1110 |
"""Base class for warnings about probable mistakes in module imports""") |
| 1111 |
|
| 1112 |
W_MemoryError = _new_exception('MemoryError', W_Exception, |
| 1113 |
"""Out of memory.""") |
| 1114 |
|
| 1115 |
W_UnboundLocalError = _new_exception('UnboundLocalError', W_NameError, |
| 1116 |
"""Local name referenced but not bound to a value.""") |
| 1117 |
|
| 1118 |
W_NotImplementedError = _new_exception('NotImplementedError', W_RuntimeError, |
| 1119 |
"""Method or function hasn't been implemented yet.""") |
| 1120 |
|
| 1121 |
W_RecursionError = _new_exception('RecursionError', W_RuntimeError, |
| 1122 |
"""Recursion limit exceeded.""") |
| 1123 |
|
| 1124 |
|
| 1125 |
class W_AttributeError(W_Exception): |
| 1126 |
"""Attribute not found.""" |
| 1127 |
name = None |
| 1128 |
obj = None |
| 1129 |
|
| 1130 |
def __init__(self, space): |
| 1131 |
pass |
| 1132 |
|
| 1133 |
@unwrap_spec(w_name=WrappedDefault(None), w_obj=WrappedDefault(None)) |
| 1134 |
def descr_init(self, space, args_w, __kwonly__, w_obj=None, w_name=None): |
| 1135 |
self.args_w = args_w |
| 1136 |
self.w_name = w_name |
| 1137 |
self.w_obj = w_obj |
| 1138 |
|
| 1139 |
|
| 1140 |
W_AttributeError.typedef = TypeDef('AttributeError', W_Exception.typedef, |
| 1141 |
__doc__ = W_AttributeError.__doc__, |
| 1142 |
__new__ = _new(W_AttributeError), |
| 1143 |
__init__ = interp2app(W_AttributeError.descr_init), |
| 1144 |
name = readwrite_attrproperty_w('w_name', W_AttributeError), |
| 1145 |
obj = readwrite_attrproperty_w('w_obj', W_AttributeError), |
| 1146 |
) |
| 1147 |
|
| 1148 |
W_OverflowError = _new_exception('OverflowError', W_ArithmeticError, |
| 1149 |
"""Result too large to be represented.""") |
| 1150 |
|
| 1151 |
class W_UnicodeEncodeError(W_UnicodeError): |
| 1152 |
"""Unicode encoding error.""" |
| 1153 |
w_encoding = None |
| 1154 |
w_object = None |
| 1155 |
w_start = None |
| 1156 |
w_end = None |
| 1157 |
w_reason = None |
| 1158 |
|
| 1159 |
def descr_init(self, space, w_encoding, w_object, w_start, w_end, w_reason): |
| 1160 |
|
| 1161 |
space.realtext_w(w_encoding) |
| 1162 |
space.realutf8_w(w_object) |
| 1163 |
space.int_w(w_start) |
| 1164 |
space.int_w(w_end) |
| 1165 |
space.realtext_w(w_reason) |
| 1166 |
|
| 1167 |
self.w_encoding = w_encoding |
| 1168 |
self.w_object = w_object |
| 1169 |
self.w_start = w_start |
| 1170 |
self.w_end = w_end |
| 1171 |
self.w_reason = w_reason |
| 1172 |
W_BaseException.descr_init(self, space, [w_encoding, w_object, |
| 1173 |
w_start, w_end, w_reason]) |
| 1174 |
|
| 1175 |
def descr_str(self, space): |
| 1176 |
return space.appexec([self], r"""(self): |
| 1177 |
if self.object is None: |
| 1178 |
return "" |
| 1179 |
if self.end == self.start + 1: |
| 1180 |
badchar = ord(self.object[self.start]) |
| 1181 |
if badchar <= 0xff: |
| 1182 |
return "'%s' codec can't encode character '\\x%02x' in position %d: %s"%( |
| 1183 |
self.encoding, badchar, self.start, self.reason) |
| 1184 |
if badchar <= 0xffff: |
| 1185 |
return "'%s' codec can't encode character '\\u%04x' in position %d: %s"%( |
| 1186 |
self.encoding, badchar, self.start, self.reason) |
| 1187 |
return "'%s' codec can't encode character '\\U%08x' in position %d: %s"%( |
| 1188 |
self.encoding, badchar, self.start, self.reason) |
| 1189 |
return "'%s' codec can't encode characters in position %d-%d: %s" % ( |
| 1190 |
self.encoding, self.start, self.end - 1, self.reason) |
| 1191 |
""") |
| 1192 |
|
| 1193 |
W_UnicodeEncodeError.typedef = TypeDef( |
| 1194 |
'UnicodeEncodeError', |
| 1195 |
W_UnicodeError.typedef, |
| 1196 |
__doc__ = W_UnicodeEncodeError.__doc__, |
| 1197 |
__new__ = _new(W_UnicodeEncodeError), |
| 1198 |
__init__ = interp2app(W_UnicodeEncodeError.descr_init), |
| 1199 |
__str__ = interp2app(W_UnicodeEncodeError.descr_str), |
| 1200 |
encoding = readwrite_attrproperty_w('w_encoding', W_UnicodeEncodeError), |
| 1201 |
object = readwrite_attrproperty_w('w_object', W_UnicodeEncodeError), |
| 1202 |
start = readwrite_attrproperty_w('w_start', W_UnicodeEncodeError), |
| 1203 |
end = readwrite_attrproperty_w('w_end', W_UnicodeEncodeError), |
| 1204 |
reason = readwrite_attrproperty_w('w_reason', W_UnicodeEncodeError), |
| 1205 |
) |
| 1206 |
|
| 1207 |
ERRNO_MAP = { |
| 1208 |
errno.EAGAIN: W_BlockingIOError, |
| 1209 |
errno.EALREADY: W_BlockingIOError, |
| 1210 |
errno.EINPROGRESS: W_BlockingIOError, |
| 1211 |
errno.EWOULDBLOCK: W_BlockingIOError, |
| 1212 |
errno.EPIPE: W_BrokenPipeError, |
| 1213 |
errno.ESHUTDOWN: W_BrokenPipeError, |
| 1214 |
errno.ECHILD: W_ChildProcessError, |
| 1215 |
errno.ECONNABORTED: W_ConnectionAbortedError, |
| 1216 |
errno.ECONNREFUSED: W_ConnectionRefusedError, |
| 1217 |
errno.ECONNRESET: W_ConnectionResetError, |
| 1218 |
errno.EEXIST: W_FileExistsError, |
| 1219 |
errno.ENOENT: W_FileNotFoundError, |
| 1220 |
errno.EISDIR: W_IsADirectoryError, |
| 1221 |
errno.ENOTDIR: W_NotADirectoryError, |
| 1222 |
errno.EINTR: W_InterruptedError, |
| 1223 |
errno.EACCES: W_PermissionError, |
| 1224 |
errno.EPERM: W_PermissionError, |
| 1225 |
errno.ESRCH: W_ProcessLookupError, |
| 1226 |
errno.ETIMEDOUT: W_TimeoutError, |
| 1227 |
} |