From 86d3df3fd4b494b197033b318efbcc07482f8b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:43:31 +0000 Subject: [PATCH 1/2] python: More fixes for Python 3.12 `SyntaxWarning: invalid escape sequence` --- gui/wxpython/gmodeler/model.py | 6 +++--- gui/wxpython/modules/mcalc_builder.py | 6 +++--- gui/wxpython/web_services/widgets.py | 2 +- python/grass/gunittest/gmodules.py | 2 +- python/grass/pygrass/modules/interface/module.py | 2 +- .../grass/pygrass/modules/interface/parameter.py | 2 +- python/grass/pygrass/vector/geometry.py | 14 +++++++------- python/grass/script/task.py | 2 +- .../grass/temporal/abstract_space_time_dataset.py | 4 ++-- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/gui/wxpython/gmodeler/model.py b/gui/wxpython/gmodeler/model.py index fb191ebe2b2..367599ab9e1 100644 --- a/gui/wxpython/gmodeler/model.py +++ b/gui/wxpython/gmodeler/model.py @@ -724,7 +724,7 @@ def Run(self, log, onDone, parent=None): # split condition # TODO: this part needs some better solution condVar, condText = map( - lambda x: x.strip(), re.split("\s* in \s*", cond) + lambda x: x.strip(), re.split(r"\s* in \s*", cond) ) pattern = re.compile("%" + condVar) # for vars()[condVar] in eval(condText): ? @@ -2578,7 +2578,7 @@ def _writeItem(self, item, ignoreBlock=True, variables={}): cond = pattern.sub(value, cond) if isinstance(item, ModelLoop): condVar, condText = map( - lambda x: x.strip(), re.split("\s* in \s*", cond) + lambda x: x.strip(), re.split(r"\s* in \s*", cond) ) cond = "%sfor %s in " % (" " * self.indent, condVar) if condText[0] == "`" and condText[-1] == "`": @@ -3324,7 +3324,7 @@ def _substituteVariable(self, string, variable, data): :return: modified string """ result = "" - ss = re.split("\w*(%" + variable + ")w*", string) + ss = re.split(r"\w*(%" + variable + ")w*", string) if not ss[0] and not ss[-1]: if data: diff --git a/gui/wxpython/modules/mcalc_builder.py b/gui/wxpython/modules/mcalc_builder.py index bbd77b07ff4..47c317fb937 100644 --- a/gui/wxpython/modules/mcalc_builder.py +++ b/gui/wxpython/modules/mcalc_builder.py @@ -581,7 +581,7 @@ def _getCommand(self): if self.overwrite.IsChecked(): overwrite = " --overwrite" seed_flag = seed = "" - if re.search(pattern="rand *\(.+\)", string=expr): + if re.search(pattern=r"rand *\(.+\)", string=expr): if self.randomSeed.IsChecked(): seed_flag = " -s" else: @@ -624,7 +624,7 @@ def _addSomething(self, what): self.text_mcalc.SetValue(newmcalcstr) if len(what) > 0: - match = re.search(pattern="\(.*\)", string=what) + match = re.search(pattern=r"\(.*\)", string=what) if match: position_offset += match.start() + 1 else: @@ -665,7 +665,7 @@ def OnMCalcRun(self, event): return seed_flag = seed = None - if re.search(pattern="rand *\(.+\)", string=expr): + if re.search(pattern=r"rand *\(.+\)", string=expr): if self.randomSeed.IsChecked(): seed_flag = "-s" else: diff --git a/gui/wxpython/web_services/widgets.py b/gui/wxpython/web_services/widgets.py index 43119415aa2..e1cf5e84dd8 100644 --- a/gui/wxpython/web_services/widgets.py +++ b/gui/wxpython/web_services/widgets.py @@ -1000,7 +1000,7 @@ def addlayer(layer, item): # self.ExpandAll(self.GetRootItem()) def GetSelectedLayers(self): - """Get selected layers/styles in LayersList + r"""Get selected layers/styles in LayersList :return: dict with these items: * 'name' : layer name used for request diff --git a/python/grass/gunittest/gmodules.py b/python/grass/gunittest/gmodules.py index be19ff7d4b8..c105fcfa12d 100644 --- a/python/grass/gunittest/gmodules.py +++ b/python/grass/gunittest/gmodules.py @@ -19,7 +19,7 @@ class SimpleModule(Module): - """Simple wrapper around pygrass.modules.Module to make sure that + r"""Simple wrapper around pygrass.modules.Module to make sure that run\_, finish\_, stdout and stderr are set correctly. >>> mapcalc = SimpleModule('r.mapcalc', expression='test_a = 1', diff --git a/python/grass/pygrass/modules/interface/module.py b/python/grass/pygrass/modules/interface/module.py index b8f49f5b888..57b4c235302 100644 --- a/python/grass/pygrass/modules/interface/module.py +++ b/python/grass/pygrass/modules/interface/module.py @@ -221,7 +221,7 @@ def __init__(self, nprocs=1): self._finished_modules = [] # Store all processed modules in a list def put(self, module): - """Put the next Module or MultiModule object in the queue + r"""Put the next Module or MultiModule object in the queue To run the Module objects in parallel the run\_ and finish\_ options of the Module must be set to False. diff --git a/python/grass/pygrass/modules/interface/parameter.py b/python/grass/pygrass/modules/interface/parameter.py index 8d58165d3ee..499bc4748e6 100644 --- a/python/grass/pygrass/modules/interface/parameter.py +++ b/python/grass/pygrass/modules/interface/parameter.py @@ -176,7 +176,7 @@ def __init__(self, xparameter=None, diz=None): try: # Check for integer ranges: "3-30" or float ranges: "0.0-1.0" isrange = re.match( - "(?P-*\d+.*\d*)*-(?P\d+.*\d*)*", diz["values"][0] + r"(?P-*\d+.*\d*)*-(?P\d+.*\d*)*", diz["values"][0] ) if isrange: mn, mx = isrange.groups() diff --git a/python/grass/pygrass/vector/geometry.py b/python/grass/pygrass/vector/geometry.py index f5d7cd3580e..81d9f3a8d78 100644 --- a/python/grass/pygrass/vector/geometry.py +++ b/python/grass/pygrass/vector/geometry.py @@ -24,8 +24,8 @@ LineDist = namedtuple("LineDist", "point dist spdist sldist") WKT = { - "POINT\((.*)\)": "point", # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)' - "LINESTRING\((.*)\)": "line", + r"POINT\((.*)\)": "point", # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)' + r"LINESTRING\((.*)\)": "line", } @@ -1135,7 +1135,7 @@ def from_wkt(self, wkt): .. """ - match = re.match("LINESTRING\((.*)\)", wkt) + match = re.match(r"LINESTRING\((.*)\)", wkt) if match: self.reset() for coord in match.groups()[0].strip().split(","): @@ -1682,7 +1682,7 @@ def isles(self, isles=None): @mapinfo_must_be_set def area(self): - """Returns area of area without areas of isles. + r"""Returns area of area without areas of isles. double Vect_get_area_area (const struct Map_info \*Map, int area) """ return libvect.Vect_get_area_area(self.c_mapinfo, self.id) @@ -1763,7 +1763,7 @@ def buffer( @mapinfo_must_be_set def boundaries(self, ilist=False): - """Creates list of boundaries for given area. + r"""Creates list of boundaries for given area. int Vect_get_area_boundaries(const struct Map_info \*Map, int area, struct ilist \*List) @@ -1802,7 +1802,7 @@ def cats(self, cats=None): return cats def get_first_cat(self): - """Find FIRST category of given field and area. + r"""Find FIRST category of given field and area. int Vect_get_area_cat(const struct Map_info \*Map, int area, int field) @@ -1828,7 +1828,7 @@ def contains_point(self, point, bbox=None): @mapinfo_must_be_set def perimeter(self): - """Calculate area perimeter. + r"""Calculate area perimeter. :return: double Vect_area_perimeter (const struct line_pnts \*Points) diff --git a/python/grass/script/task.py b/python/grass/script/task.py index 31102835db4..1c4e29f69a6 100644 --- a/python/grass/script/task.py +++ b/python/grass/script/task.py @@ -446,7 +446,7 @@ def convert_xml_to_utf8(xml_text): # modify: fetch encoding from the interface description text(xml) # e.g. - pattern = re.compile(b'<\?xml[^>]*\Wencoding="([^"]*)"[^>]*\?>') + pattern = re.compile(rb'<\?xml[^>]*\Wencoding="([^"]*)"[^>]*\?>') m = re.match(pattern, xml_text) if m is None: return xml_text.encode("utf-8") if xml_text else None diff --git a/python/grass/temporal/abstract_space_time_dataset.py b/python/grass/temporal/abstract_space_time_dataset.py index 2e46f06a6d3..34b8e19579d 100644 --- a/python/grass/temporal/abstract_space_time_dataset.py +++ b/python/grass/temporal/abstract_space_time_dataset.py @@ -1647,8 +1647,8 @@ def leading_zero(value): shortcut_identifier = leading_zero(self.semantic_label) if shortcut_identifier: where += ( - "{br} LIKE '{si}\_%' {esc} OR {br} LIKE '%\_{si}' {esc} OR " - "{br} LIKE '{orig}\_%' {esc} OR {br} LIKE '%\_{orig}' {esc}".format( + "{br} LIKE '{si}\\_%' {esc} OR {br} LIKE '%\\_{si}' {esc} OR " + "{br} LIKE '{orig}\\_%' {esc} OR {br} LIKE '%\\_{orig}' {esc}".format( br="semantic_label", si=shortcut_identifier, orig=self.semantic_label.upper(), From 2c082e9dfbddd0dcfd07c061edceeacf0f0332a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Fri, 5 Jan 2024 21:18:28 -0500 Subject: [PATCH 2/2] python: Use a safer regex for range checks --- python/grass/pygrass/modules/interface/parameter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/grass/pygrass/modules/interface/parameter.py b/python/grass/pygrass/modules/interface/parameter.py index 499bc4748e6..7ab67c186ad 100644 --- a/python/grass/pygrass/modules/interface/parameter.py +++ b/python/grass/pygrass/modules/interface/parameter.py @@ -176,7 +176,8 @@ def __init__(self, xparameter=None, diz=None): try: # Check for integer ranges: "3-30" or float ranges: "0.0-1.0" isrange = re.match( - r"(?P-*\d+.*\d*)*-(?P\d+.*\d*)*", diz["values"][0] + r"(?P-?(?:\d*\.)?\d+)?-(?P-?(?:\d*\.)?\d+)?", + diz["values"][0], ) if isrange: mn, mx = isrange.groups()