This hides unwanted implementation details from tracebacks.
try:
value = next(it)
except StopIteration:
- raise KeyError
+ raise KeyError from None
self.discard(value)
return value
try:
key = next(iter(self))
except StopIteration:
- raise KeyError
+ raise KeyError from None
value = self[key]
del self[key]
return key, value
try:
itemref = self.data.pop()
except KeyError:
- raise KeyError('pop from empty WeakSet')
+ raise KeyError('pop from empty WeakSet') from None
item = itemref()
if item is not None:
return item
try:
return struct.unpack('>l', file.read(4))[0]
except struct.error:
- raise EOFError
+ raise EOFError from None
def _read_ulong(file):
try:
return struct.unpack('>L', file.read(4))[0]
except struct.error:
- raise EOFError
+ raise EOFError from None
def _read_short(file):
try:
return struct.unpack('>h', file.read(2))[0]
except struct.error:
- raise EOFError
+ raise EOFError from None
def _read_ushort(file):
try:
return struct.unpack('>H', file.read(2))[0]
except struct.error:
- raise EOFError
+ raise EOFError from None
def _read_string(file):
length = ord(file.read(1))
except OSError as err:
raise OSError(err.errno, 'error while attempting '
'to bind on address %r: %s'
- % (sa, err.strerror.lower()))
+ % (sa, err.strerror.lower())) from None
completed = True
finally:
if not completed:
try:
number = int(arg)
except ValueError:
- raise ValueError('Non-numeric breakpoint number %s' % arg)
+ raise ValueError('Non-numeric breakpoint number %s' % arg) from None
try:
bp = Breakpoint.bpbynumber[number]
except IndexError:
- raise ValueError('Breakpoint number %d out of range' % number)
+ raise ValueError('Breakpoint number %d out of range' % number) from None
if bp is None:
raise ValueError('Breakpoint %d already deleted' % number)
return bp
try:
self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0]
except struct.error:
- raise EOFError
+ raise EOFError from None
if inclheader:
self.chunksize = self.chunksize - 8 # subtract header
self.size_read = 0
sectiondict = self._sections[section]
except KeyError:
if section != self.default_section:
- raise NoSectionError(section)
+ raise NoSectionError(section) from None
# Update with the entry specific variables
vardict = {}
if vars:
except AttributeError:
if getattr(self, "__slots__", None):
raise TypeError("a class that defines __slots__ without "
- "defining __getstate__ cannot be pickled")
+ "defining __getstate__ cannot be pickled") from None
try:
dict = self.__dict__
except AttributeError:
tag = {1: "- ", 2: "+ "}[int(which)]
except KeyError:
raise ValueError('unknown delta choice (must be 1 or 2): %r'
- % which)
+ % which) from None
prefixes = (" ", tag)
for line in delta:
if line[:2] in prefixes:
try:
tb = sys.last_traceback
except AttributeError:
- raise RuntimeError("no last traceback to disassemble")
+ raise RuntimeError("no last traceback to disassemble") from None
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti, file=file)
try:
esc = s[end]
except IndexError:
- raise JSONDecodeError("Unterminated string starting at", s, begin)
+ raise JSONDecodeError("Unterminated string starting at",
+ s, begin) from None
# If not a unicode escape sequence, must be in the lookup table
if esc != 'u':
try:
try:
nextchar = string[idx]
except IndexError:
- raise StopIteration(idx)
+ raise StopIteration(idx) from None
if nextchar == '"':
return parse_string(string, idx + 1, strict)
try:
root = self.driver.parse_tokens(tokens, debug=debug)
except parse.ParseError as e:
- raise PatternSyntaxError(str(e))
+ raise PatternSyntaxError(str(e)) from None
if with_tree:
return self.compile_node(root), root
else:
try:
i = int(hexes, 16)
except ValueError:
- raise ValueError("invalid hex string escape ('\\%s')" % tail)
+ raise ValueError("invalid hex string escape ('\\%s')" % tail) from None
else:
try:
i = int(tail, 8)
except ValueError:
- raise ValueError("invalid octal string escape ('\\%s')" % tail)
+ raise ValueError("invalid octal string escape ('\\%s')" % tail) from None
return chr(i)
def evalString(s):
try:
fix_class = getattr(mod, class_name)
except AttributeError:
- raise FixerError("Can't find %s.%s" % (fix_name, class_name))
+ raise FixerError("Can't find %s.%s" % (fix_name, class_name)) from None
fixer = fix_class(self.options, self.fixer_log)
if fixer.explicit and self.explicit is not True and \
fix_mod_path not in self.explicit:
else:
return language + '.' + encoding
except (TypeError, ValueError):
- raise TypeError('Locale must be None, a string, or an iterable of two strings -- language code, encoding.')
+ raise TypeError('Locale must be None, a string, or an iterable of '
+ 'two strings -- language code, encoding.') from None
def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
try:
return self._toc[key]
except KeyError:
- raise KeyError('No message with key: %s' % key)
+ raise KeyError('No message with key: %s' % key) from None
# This method is for backward compatibility only.
def next(self):
try:
return self._toc[key]
except KeyError:
- raise KeyError('No message with key: %s' % key)
+ raise KeyError('No message with key: %s' % key) from None
def _append_message(self, message):
"""Append message to mailbox and return (start, stop) offsets."""
try:
self._date = float(date)
except ValueError:
- raise TypeError("can't convert to float: %s" % date)
+ raise TypeError("can't convert to float: %s" % date) from None
def get_info(self):
"""Get the message's "info" as a string."""
try:
ctx = _concrete_contexts[method]
except KeyError:
- raise ValueError('cannot find context for %r' % method)
+ raise ValueError('cannot find context for %r' % method) from None
ctx._check_available()
return ctx
item = self._items.popleft()
except IndexError:
if self._index == self._length:
- raise StopIteration
+ raise StopIteration from None
self._cond.wait(timeout)
try:
item = self._items.popleft()
except IndexError:
if self._index == self._length:
- raise StopIteration
- raise TimeoutError
+ raise StopIteration from None
+ raise TimeoutError from None
success, value = item
if success:
try:
return self._sentinel
except AttributeError:
- raise ValueError("process not started")
+ raise ValueError("process not started") from None
def __repr__(self):
if self is _current_process:
try:
file = os.path.join(os.environ['HOME'], ".netrc")
except KeyError:
- raise OSError("Could not find .netrc: $HOME is not set")
+ raise OSError("Could not find .netrc: $HOME is not set") from None
self.hosts = {}
self.macros = {}
with open(file) as fp:
try:
[resp_num, path] = resp.split()
except ValueError:
- raise NNTPReplyError(resp)
+ raise NNTPReplyError(resp) from None
else:
return resp, path
obj = getattr(obj, subpath)
except AttributeError:
raise AttributeError("Can't get attribute {!r} on {!r}"
- .format(name, obj))
+ .format(name, obj)) from None
return obj, parent
def whichmodule(obj, name):
except (ImportError, KeyError, AttributeError):
raise PicklingError(
"Can't pickle %r: it's not found as %s.%s" %
- (obj, module_name, name))
+ (obj, module_name, name)) from None
else:
if obj2 is not obj:
raise PicklingError(
except UnicodeEncodeError:
raise PicklingError(
"can't pickle global identifier '%s.%s' using "
- "pickle protocol %i" % (module, name, self.proto))
+ "pickle protocol %i" % (module, name, self.proto)) from None
self.memoize(obj)
try:
format_info = _ARCHIVE_FORMATS[format]
except KeyError:
- raise ValueError("unknown archive format '%s'" % format)
+ raise ValueError("unknown archive format '%s'" % format) from None
func = format_info[0]
for arg, val in format_info[1]:
try:
format_info = _UNPACK_FORMATS[format]
except KeyError:
- raise ValueError("Unknown unpack format '{0}'".format(format))
+ raise ValueError("Unknown unpack format '{0}'".format(format)) from None
func = format_info[1]
func(filename, extract_dir, **dict(format_info[2]))
try:
return s.format(**os.environ)
except KeyError as var:
- raise AttributeError('{%s}' % var)
+ raise AttributeError('{%s}' % var) from None
def _extend_dict(target_dict, other_dict):
target_keys = target_dict.keys()
if lineno < 0:
raise ValueError
except (ValueError, OverflowError):
- raise _OptionError("invalid lineno %r" % (lineno,))
+ raise _OptionError("invalid lineno %r" % (lineno,)) from None
else:
lineno = 0
filterwarnings(action, message, category, module, lineno)
try:
cat = eval(category)
except NameError:
- raise _OptionError("unknown warning category: %r" % (category,))
+ raise _OptionError("unknown warning category: %r" % (category,)) from None
else:
i = category.rfind(".")
module = category[:i]
try:
m = __import__(module, None, None, [klass])
except ImportError:
- raise _OptionError("invalid module name: %r" % (module,))
+ raise _OptionError("invalid module name: %r" % (module,)) from None
try:
cat = getattr(m, klass)
except AttributeError:
- raise _OptionError("unknown warning category: %r" % (category,))
+ raise _OptionError("unknown warning category: %r" % (category,)) from None
if not issubclass(cat, Warning):
raise _OptionError("invalid warning category: %r" % (category,))
return cat
settings = self._settings[(_name_xform(name), state)]
except KeyError:
raise xml.dom.NotSupportedErr(
- "unsupported feature: %r" % (name,))
+ "unsupported feature: %r" % (name,)) from None
else:
for name, value in settings:
setattr(self._options, name, value)
raise KeyError
yield token[0], "{%s}%s" % (namespaces[prefix], uri)
except KeyError:
- raise SyntaxError("prefix %r not found in prefix map" % prefix)
+ raise SyntaxError("prefix %r not found in prefix map" % prefix) from None
else:
yield token
try:
return subsystem_details[subsystem][:2]
except KeyError:
- raise ValueError("The subsystem %s is not known" % subsystem)
+ raise ValueError("The subsystem %s is not known" % subsystem) from None
def makemakefile(outfp, vars, files, target):
try:
return self.__byrgb[rgbtuple]
except KeyError:
- raise BadColor(rgbtuple)
+ raise BadColor(rgbtuple) from None
def find_byname(self, name):
"""Return (red, green, blue) for name"""
try:
return self.__byname[name]
except KeyError:
- raise BadColor(name)
+ raise BadColor(name) from None
def nearest(self, red, green, blue):
"""Return the name of color nearest (red, green, blue)"""
try:
name, aliases = self.__byrgb[(red, green, blue)]
except KeyError:
- raise BadColor((red, green, blue))
+ raise BadColor((red, green, blue)) from None
return [name] + aliases
\f