From ad82438c448597b0c6470eef697a2c1b5f721ef1 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Sat, 6 Jan 2024 18:08:46 +0100 Subject: [PATCH] Use regex literals for re.* functions --- miasm/__init__.py | 6 +++--- miasm/arch/x86/arch.py | 2 +- miasm/core/cpu.py | 4 ++-- miasm/core/graph.py | 2 +- miasm/core/sembuilder.py | 4 ++-- miasm/core/utils.py | 2 +- miasm/ir/ir.py | 2 +- miasm/os_dep/linux/environment.py | 4 ++-- test/analysis/depgraph.py | 4 ++-- test/arch/mep/asm/ut_helpers_asm.py | 2 +- 10 files changed, 16 insertions(+), 16 deletions(-) diff --git a/miasm/__init__.py b/miasm/__init__.py index 417a62685..309a1ae7e 100644 --- a/miasm/__init__.py +++ b/miasm/__init__.py @@ -40,13 +40,13 @@ def _version_from_git_describe(): if process.returncode == 0: tag = out.decode().strip() - match = re.match('^v?(.+?)-(\\d+)-g[a-f0-9]+$', tag) + match = re.match(r'^v?(.+?)-(\d+)-g[a-f0-9]+$', tag) if match: # remove the 'v' prefix and add a '.devN' suffix return '%s.dev%s' % (match.group(1), match.group(2)) else: # just remove the 'v' prefix - return re.sub('^v', '', tag) + return re.sub(r'^v', '', tag) else: raise subprocess.CalledProcessError(process.returncode, err) @@ -71,7 +71,7 @@ def _version(): # See 'man gitattributes' for more details. git_archive_id = '$Format:%h %d$' sha1 = git_archive_id.strip().split()[0] - match = re.search('tag:(\\S+)', git_archive_id) + match = re.search(r'tag:(\S+)', git_archive_id) if match: return "git-archive.dev" + match.group(1) elif sha1: diff --git a/miasm/arch/x86/arch.py b/miasm/arch/x86/arch.py index dabd0c82b..c5ff9b631 100644 --- a/miasm/arch/x86/arch.py +++ b/miasm/arch/x86/arch.py @@ -428,7 +428,7 @@ def offsize(p): def get_prefix(s): - g = re.search('(\S+)(\s+)', s) + g = re.search(r'(\S+)(\s+)', s) if not g: return None, s prefix, b = g.groups() diff --git a/miasm/core/cpu.py b/miasm/core/cpu.py index 7a1cacff2..dae93bf9c 100644 --- a/miasm/core/cpu.py +++ b/miasm/core/cpu.py @@ -408,7 +408,7 @@ def cb_op_mul(tokens): def isbin(s): - return re.match('[0-1]+$', s) + return re.match(r'[0-1]+$', s) def int2bin(i, l): @@ -1301,7 +1301,7 @@ def dis(cls, bs_o, mode_o = None, offset=0): @classmethod def fromstring(cls, text, loc_db, mode = None): global total_scans - name = re.search('(\S+)', text).groups() + name = re.search(r'(\S+)', text).groups() if not name: raise ValueError('cannot find name', text) name = name[0] diff --git a/miasm/core/graph.py b/miasm/core/graph.py index 0dfd7e6ac..e680894cd 100644 --- a/miasm/core/graph.py +++ b/miasm/core/graph.py @@ -20,7 +20,7 @@ def __init__(self): # N -> Nodes N2 with a edge (N2 -> N) self._nodes_pred = {} - self.escape_chars = re.compile('[' + re.escape('{}') + '&|<>' + ']') + self.escape_chars = re.compile(r'[\{\}&|<>]') def __repr__(self): diff --git a/miasm/core/sembuilder.py b/miasm/core/sembuilder.py index 244706562..9843ee6a8 100644 --- a/miasm/core/sembuilder.py +++ b/miasm/core/sembuilder.py @@ -22,8 +22,8 @@ class MiasmTransformer(ast.NodeTransformer): """ # Parsers - parse_integer = re.compile("^i([0-9]+)$") - parse_mem = re.compile("^mem([0-9]+)$") + parse_integer = re.compile(r"^i([0-9]+)$") + parse_mem = re.compile(r"^mem([0-9]+)$") # Visitors def visit_Call(self, node): diff --git a/miasm/core/utils.py b/miasm/core/utils.py index 41bf78c1d..eb170576d 100644 --- a/miasm/core/utils.py +++ b/miasm/core/utils.py @@ -26,7 +26,7 @@ COLOR_MNEMO = "blue1" -ESCAPE_CHARS = re.compile('[' + re.escape('{}') + '&|<>' + ']') +ESCAPE_CHARS = re.compile(r'[\{\}&|<>]') def set_html_text_color(text, color): return '%s' % (color, text) diff --git a/miasm/ir/ir.py b/miasm/ir/ir.py index e9b86899f..d26c5d1d5 100644 --- a/miasm/ir/ir.py +++ b/miasm/ir/ir.py @@ -48,7 +48,7 @@ def _expr_loc_to_symb(expr, loc_db): return m2_expr.ExprId(name, expr.size) -ESCAPE_CHARS = re.compile('[' + re.escape('{}') + '&|<>' + ']') +ESCAPE_CHARS = re.compile(r'[\{\}&|<>]') class TranslatorHtml(Translator): __LANG__ = "custom_expr_color" diff --git a/miasm/os_dep/linux/environment.py b/miasm/os_dep/linux/environment.py index 808fc847d..3ba4382f3 100644 --- a/miasm/os_dep/linux/environment.py +++ b/miasm/os_dep/linux/environment.py @@ -13,7 +13,7 @@ from miasm.jitter.csts import PAGE_READ, PAGE_WRITE -REGEXP_T = type(re.compile('')) +REGEXP_T = type(re.compile(r'')) StatInfo = namedtuple("StatInfo", [ "st_dev", "st_ino", "st_nlink", "st_mode", "st_uid", "st_gid", "st_rdev", @@ -262,7 +262,7 @@ def _convert_re(expr): expr.flags, exc_info=True, ) - return re.compile('$X') + return re.compile(r'$X') return expr # Remove '../', etc. diff --git a/test/analysis/depgraph.py b/test/analysis/depgraph.py index 57a73a5f6..9760e7179 100644 --- a/test/analysis/depgraph.py +++ b/test/analysis/depgraph.py @@ -108,7 +108,7 @@ def get_out_regs(self, _): def bloc2graph(irgraph, label=False, lines=True): """Render dot graph of @blocks""" - escape_chars = re.compile('[' + re.escape('{}') + ']') + escape_chars = re.compile(r'[\{\}]') label_attr = 'colspan="2" align="center" bgcolor="grey"' edge_attr = 'label = "%s" color="%s" style="bold"' td_attr = 'align="left"' @@ -179,7 +179,7 @@ def bloc2graph(irgraph, label=False, lines=True): def dg2graph(graph, label=False, lines=True): """Render dot graph of @blocks""" - escape_chars = re.compile('[' + re.escape('{}') + ']') + escape_chars = re.compile(r'[\{\}]') label_attr = 'colspan="2" align="center" bgcolor="grey"' edge_attr = 'label = "%s" color="%s" style="bold"' td_attr = 'align="left"' diff --git a/test/arch/mep/asm/ut_helpers_asm.py b/test/arch/mep/asm/ut_helpers_asm.py index 9f6dc5c20..2ebd0622a 100644 --- a/test/arch/mep/asm/ut_helpers_asm.py +++ b/test/arch/mep/asm/ut_helpers_asm.py @@ -27,7 +27,7 @@ def check_instruction(mn_str, mn_hex, multi=None, offset=0): """Try to disassemble and assemble this instruction""" # Rename objdump registers names - mn_str = re.sub("\$([0-9]+)", lambda m: "R"+m.group(1), mn_str) + mn_str = re.sub(r"\$([0-9]+)", lambda m: "R"+m.group(1), mn_str) mn_str = mn_str.replace("$", "") # Disassemble