diff --git a/gui/wxpython/iscatt/plots.py b/gui/wxpython/iscatt/plots.py index f9cad136314..e2e3d632a36 100644 --- a/gui/wxpython/iscatt/plots.py +++ b/gui/wxpython/iscatt/plots.py @@ -45,7 +45,7 @@ 'The Scatterplot Tool needs the "matplotlib" ' "(python-matplotlib) package to be installed. {0}" ).format(e) - ) + ) from e import grass.script as gs from grass.pydispatch.signal import Signal diff --git a/gui/wxpython/timeline/frame.py b/gui/wxpython/timeline/frame.py index 0330adbde3d..e160b77b44a 100644 --- a/gui/wxpython/timeline/frame.py +++ b/gui/wxpython/timeline/frame.py @@ -43,7 +43,7 @@ "(python-matplotlib and on some systems also python-matplotlib-wx) " "package(s) to be installed. {}" ).format(e) - ) + ) from e import grass.script as gs diff --git a/gui/wxpython/tplot/frame.py b/gui/wxpython/tplot/frame.py index 4a1cd9f1ca6..beaa48c663c 100755 --- a/gui/wxpython/tplot/frame.py +++ b/gui/wxpython/tplot/frame.py @@ -46,7 +46,7 @@ 'The Temporal Plot Tool needs the "matplotlib" ' "(python-matplotlib) package to be installed. {0}" ).format(e) - ) + ) from e import grass.temporal as tgis diff --git a/python/grass/pygrass/modules/interface/module.py b/python/grass/pygrass/modules/interface/module.py index b9c298b4a6f..274b663e7af 100644 --- a/python/grass/pygrass/modules/interface/module.py +++ b/python/grass/pygrass/modules/interface/module.py @@ -555,7 +555,7 @@ def __init__(self, cmd, *args, **kargs): except OSError as e: print("OSError error({0}): {1}".format(e.errno, e.strerror)) str_err = "Error running: `%s --interface-description`." - raise GrassError(str_err % self.name) + raise GrassError(str_err % self.name) from e # get the xml of the module self.xml = get_cmd_xml.communicate()[0] # transform and parse the xml into an Element class: diff --git a/python/grass/pygrass/raster/category.py b/python/grass/pygrass/raster/category.py index 2cee672ed32..1480dda21e6 100644 --- a/python/grass/pygrass/raster/category.py +++ b/python/grass/pygrass/raster/category.py @@ -109,8 +109,8 @@ def _chk_index(self, index): if type(index) == str: try: index = self.labels().index(index) - except ValueError: - raise KeyError(index) + except ValueError as error: + raise KeyError(index) from error return index def _chk_value(self, value): diff --git a/python/grass/pygrass/rpc/base.py b/python/grass/pygrass/rpc/base.py index 38cf48c1581..133cbbc2205 100644 --- a/python/grass/pygrass/rpc/base.py +++ b/python/grass/pygrass/rpc/base.py @@ -173,7 +173,9 @@ def safe_receive(self, message): except (EOFError, OSError, FatalError) as e: # The pipe was closed by the checker thread because # the server process was killed - raise FatalError("Exception raised: " + str(e) + " Message: " + message) + raise FatalError( + "Exception raised: " + str(e) + " Message: " + message + ) from e def stop(self): """Stop the check thread, the libgis server and close the pipe diff --git a/python/grass/pygrass/vector/table.py b/python/grass/pygrass/vector/table.py index dec3ffbba08..54ff27b4025 100644 --- a/python/grass/pygrass/vector/table.py +++ b/python/grass/pygrass/vector/table.py @@ -859,10 +859,10 @@ def connection(self): return psycopg2.connect(db) except ImportError: er = "You need to install psycopg2 to connect with this table." - raise ImportError(er) + raise ImportError(er) from None else: str_err = "Driver is not supported yet, pleas use: sqlite or pg" - raise TypeError(str_err) + raise TypeError(str_err) from None def table(self): """Return a Table object. @@ -1204,7 +1204,7 @@ def execute(self, sql_code=None, cursor=None, many=False, values=None): "The SQL statement is not correct:\n%r,\n" "values: %r,\n" "SQL error: %s" % (sqlc, values, str(exc)) - ) + ) from exc def exist(self, cursor=None): """Return True if the table already exist in the DB, False otherwise diff --git a/python/grass/script/core.py b/python/grass/script/core.py index b22815bafb6..1c507ab91c8 100644 --- a/python/grass/script/core.py +++ b/python/grass/script/core.py @@ -867,8 +867,8 @@ def _parse_opts(lines): break try: var, val = line.split(b"=", 1) - except ValueError: - raise SyntaxError("invalid output from g.parser: {}".format(line)) + except ValueError as err: + raise SyntaxError("invalid output from g.parser: {}".format(line)) from err try: var = decode(var) val = decode(val) @@ -877,7 +877,7 @@ def _parse_opts(lines): "invalid output from g.parser ({error}): {line}".format( error=error, line=line ) - ) + ) from error if var.startswith("flag_"): flags[var[5:]] = bool(int(val)) elif var.startswith("opt_"): @@ -1888,7 +1888,7 @@ def _set_location_description(path, location, text): fd.write(os.linesep) fd.close() except OSError as e: - raise ScriptError(repr(e)) + raise ScriptError(repr(e)) from e def _create_location_xy(database, location): @@ -1939,7 +1939,7 @@ def _create_location_xy(database, location): os.chdir(cur_dir) except OSError as e: - raise ScriptError(repr(e)) + raise ScriptError(repr(e)) from e # interface to g.version diff --git a/python/grass/script/task.py b/python/grass/script/task.py index 9ab76eedcb8..c9efe248e8e 100644 --- a/python/grass/script/task.py +++ b/python/grass/script/task.py @@ -502,7 +502,7 @@ def get_interface_description(cmd): "Unable to fetch interface description for command '<{cmd}>'." "\n\nDetails: <{det}>" ).format(cmd=cmd, det=e) - ) + ) from e desc = convert_xml_to_utf8(cmdout) return desc.replace( @@ -530,7 +530,7 @@ def parse_interface(name, parser=processTask, blackList=None): _("Cannot parse interface description of<{name}> module: {error}").format( name=name, error=error ) - ) + ) from error task = parser(tree, blackList=blackList).get_task() # if name from interface is different than the originally # provided name, then the provided name is likely a full path needed diff --git a/python/grass/script/vector.py b/python/grass/script/vector.py index 4a57b5db07b..006e54c030b 100644 --- a/python/grass/script/vector.py +++ b/python/grass/script/vector.py @@ -423,7 +423,7 @@ def vector_what( try: ret = read_command("v.what", env=env, **cmdParams).strip() except CalledModuleError as e: - raise ScriptError(e.msg) + raise ScriptError(e.msg) from e data = [] if not ret: @@ -450,10 +450,10 @@ def vector_what( try: result = json.loads(ret, **kwargs) - except ValueError: + except ValueError as err: raise ScriptError( _("v.what output is not valid JSON format:\n {ret}").format(ret=ret) - ) + ) from err if multiple: for vmap in result["Maps"]: diff --git a/python/grass/semantic_label/reader.py b/python/grass/semantic_label/reader.py index 1da67aff7f9..ecd152271c2 100644 --- a/python/grass/semantic_label/reader.py +++ b/python/grass/semantic_label/reader.py @@ -40,7 +40,7 @@ def _read_config(self): except json.decoder.JSONDecodeError as e: raise SemanticLabelReaderError( "Unable to parse '{}': {}".format(json_file, e) - ) + ) from e # check if configuration is valid self._check_config(config) @@ -116,7 +116,9 @@ def print_info(self, shortcut=None, band=None, semantic_label=None, extended=Fal if shortcut and re.match(shortcut, item["shortcut"]) is None: continue except re.error as e: - raise SemanticLabelReaderError("Invalid pattern: {}".format(e)) + raise SemanticLabelReaderError( + "Invalid pattern: {}".format(e) + ) from e found = True if band and band not in item["bands"]: diff --git a/python/grass/utils/download.py b/python/grass/utils/download.py index f6b734b7291..46e6d75527f 100644 --- a/python/grass/utils/download.py +++ b/python/grass/utils/download.py @@ -107,7 +107,7 @@ def extract_zip(name, directory, tmpdir): extract_dir=extract_dir, target_dir=directory, files=files ) except zipfile.BadZipfile as error: - raise DownloadError(_("ZIP file is unreadable: {0}").format(error)) + raise DownloadError(_("ZIP file is unreadable: {0}").format(error)) from error # modified copy from g.extension @@ -167,9 +167,9 @@ def download_and_extract(source, reporthook=None): url=source, code=err, ), - ) - except URLError: - raise DownloadError(url_error_message.format(url=source)) + ) from err + except URLError as e: + raise DownloadError(url_error_message.format(url=source)) from e if headers.get("content-type", "") != "application/zip": raise DownloadError( _( @@ -188,9 +188,9 @@ def download_and_extract(source, reporthook=None): url=source, code=err, ), - ) - except URLError: - raise DownloadError(url_error_message.format(url=source)) + ) from err + except URLError as e: + raise DownloadError(url_error_message.format(url=source)) from e extract_tar(name=archive_name, directory=directory, tmpdir=tmpdir) else: # probably programmer error diff --git a/scripts/r.in.wms/wms_cap_parsers.py b/scripts/r.in.wms/wms_cap_parsers.py index 5435fe29591..9467bb9c297 100644 --- a/scripts/r.in.wms/wms_cap_parsers.py +++ b/scripts/r.in.wms/wms_cap_parsers.py @@ -41,17 +41,17 @@ def __init__(self, cap_file): if is_file: try: etree.ElementTree.__init__(self, file=cap_file) - except ParseError: - raise ParseError(_("Unable to parse XML file")) + except ParseError as pe: + raise ParseError(_("Unable to parse XML file")) from pe except OSError as error: raise ParseError( _("Unable to open XML file '%s'.\n%s\n" % (cap_file, error)) - ) + ) from error else: try: etree.ElementTree.__init__(self, element=etree.fromstring(cap_file)) - except ParseError: - raise ParseError(_("Unable to parse XML file")) + except ParseError as pe: + raise ParseError(_("Unable to parse XML file")) from pe if self.getroot() is None: raise ParseError(_("Root node was not found."))