tools: Use raw strings for regexps in python scripts (fixes #3677)

Starting with Python 3.12, use of invalid escape sequences in strings
is reported as a SyntaxWarning and will become a SyntaxError at a
later point.

Regular expressions use the backslash character a lot, which result in
warnings of this kind. Python docs recommend to generally use raw
strings for this purpose.
This commit is contained in:
Jacobo Aragunde Pérez 2024-04-12 15:53:51 +00:00 committed by Marshall Greenblatt
parent 744a194a6e
commit 81a0648ee1
3 changed files with 42 additions and 42 deletions

View File

@ -133,11 +133,11 @@ class cef_api_hash:
def __parse_objects(self, content): def __parse_objects(self, content):
""" Returns array of objects in content file. """ """ Returns array of objects in content file. """
objects = [] objects = []
content = re.sub("//.*\n", "", content) content = re.sub(r"//.*\n", "", content)
# function declarations # function declarations
for m in re.finditer( for m in re.finditer(
"\nCEF_EXPORT\s+?.*?\s+?(\w+)\s*?\(.*?\)\s*?;", r"\nCEF_EXPORT\s+?.*?\s+?(\w+)\s*?\(.*?\)\s*?;",
content, content,
flags=re.DOTALL): flags=re.DOTALL):
object = {"name": m.group(1), "text": m.group(0).strip()} object = {"name": m.group(1), "text": m.group(0).strip()}
@ -145,7 +145,7 @@ class cef_api_hash:
# structs # structs
for m in re.finditer( for m in re.finditer(
"\ntypedef\s+?struct\s+?(\w+)\s+?\{.*?\}\s+?(\w+)\s*?;", r"\ntypedef\s+?struct\s+?(\w+)\s+?\{.*?\}\s+?(\w+)\s*?;",
content, content,
flags=re.DOTALL): flags=re.DOTALL):
object = {"name": m.group(2), "text": m.group(0).strip()} object = {"name": m.group(2), "text": m.group(0).strip()}
@ -153,12 +153,12 @@ class cef_api_hash:
# enums # enums
for m in re.finditer( for m in re.finditer(
"\ntypedef\s+?enum\s+?\{.*?\}\s+?(\w+)\s*?;", content, flags=re.DOTALL): r"\ntypedef\s+?enum\s+?\{.*?\}\s+?(\w+)\s*?;", content, flags=re.DOTALL):
object = {"name": m.group(1), "text": m.group(0).strip()} object = {"name": m.group(1), "text": m.group(0).strip()}
objects.append(object) objects.append(object)
# typedefs # typedefs
for m in re.finditer("\ntypedef\s+?.*?\s+(\w+);", content, flags=0): for m in re.finditer(r"\ntypedef\s+?.*?\s+(\w+);", content, flags=0):
object = {"name": m.group(1), "text": m.group(0).strip()} object = {"name": m.group(1), "text": m.group(0).strip()}
objects.append(object) objects.append(object)
@ -168,7 +168,7 @@ class cef_api_hash:
""" Grab defined CEF_STRING_TYPE_xxx """ """ Grab defined CEF_STRING_TYPE_xxx """
objects = [] objects = []
for m in re.finditer( for m in re.finditer(
"\n\s*?#\s*?define\s+?(CEF_STRING_TYPE_\w+)\s+?.*?\n", content, r"\n\s*?#\s*?define\s+?(CEF_STRING_TYPE_\w+)\s+?.*?\n", content,
flags=0): flags=0):
object = { object = {
"name": m.group(1), "name": m.group(1),
@ -179,8 +179,8 @@ class cef_api_hash:
def __prepare_text(self, text): def __prepare_text(self, text):
text = text.strip() text = text.strip()
text = re.sub("\s+", " ", text) text = re.sub(r"\s+", " ", text)
text = re.sub("\(\s+", "(", text) text = re.sub(r"\(\s+", "(", text)
return text return text
def __get_final_sig(self, objects, platform): def __get_final_sig(self, objects, platform):

View File

@ -273,7 +273,7 @@ def format_translation_includes(header, body):
result += '#include "libcef_dll/template_util.h"\n' result += '#include "libcef_dll/template_util.h"\n'
# identify what CppToC classes are being used # identify what CppToC classes are being used
p = re.compile('([A-Za-z0-9_]{1,})CppToC') p = re.compile(r'([A-Za-z0-9_]{1,})CppToC')
list = sorted(set(p.findall(body))) list = sorted(set(p.findall(body)))
for item in list: for item in list:
directory = '' directory = ''
@ -286,7 +286,7 @@ def format_translation_includes(header, body):
get_capi_name(item[3:], False)+'_cpptoc.h"\n' get_capi_name(item[3:], False)+'_cpptoc.h"\n'
# identify what CToCpp classes are being used # identify what CToCpp classes are being used
p = re.compile('([A-Za-z0-9_]{1,})CToCpp') p = re.compile(r'([A-Za-z0-9_]{1,})CToCpp')
list = sorted(set(p.findall(body))) list = sorted(set(p.findall(body)))
for item in list: for item in list:
directory = '' directory = ''
@ -355,21 +355,21 @@ def dict_to_str(dict):
# regex for matching comment-formatted attributes # regex for matching comment-formatted attributes
_cre_attrib = '/\*--cef\(([A-Za-z0-9_ ,=:\n]{0,})\)--\*/' _cre_attrib = r'/\*--cef\(([A-Za-z0-9_ ,=:\n]{0,})\)--\*/'
# regex for matching class and function names # regex for matching class and function names
_cre_cfname = '([A-Za-z0-9_]{1,})' _cre_cfname = r'([A-Za-z0-9_]{1,})'
# regex for matching class and function names including path separators # regex for matching class and function names including path separators
_cre_cfnameorpath = '([A-Za-z0-9_\/]{1,})' _cre_cfnameorpath = r'([A-Za-z0-9_\/]{1,})'
# regex for matching typedef value and name combination # regex for matching typedef value and name combination
_cre_typedef = '([A-Za-z0-9_<>:,\*\&\s]{1,})' _cre_typedef = r'([A-Za-z0-9_<>:,\*\&\s]{1,})'
# regex for matching function return value and name combination # regex for matching function return value and name combination
_cre_func = '([A-Za-z][A-Za-z0-9_<>:,\*\&\s]{1,})' _cre_func = r'([A-Za-z][A-Za-z0-9_<>:,\*\&\s]{1,})'
# regex for matching virtual function modifiers + arbitrary whitespace # regex for matching virtual function modifiers + arbitrary whitespace
_cre_vfmod = '([\sA-Za-z0-9_]{0,})' _cre_vfmod = r'([\sA-Za-z0-9_]{0,})'
# regex for matching arbitrary whitespace # regex for matching arbitrary whitespace
_cre_space = '[\s]{1,}' _cre_space = r'[\s]{1,}'
# regex for matching optional virtual keyword # regex for matching optional virtual keyword
_cre_virtual = '(?:[\s]{1,}virtual){0,1}' _cre_virtual = r'(?:[\s]{1,}virtual){0,1}'
# Simple translation types. Format is: # Simple translation types. Format is:
# 'cpp_type' : ['capi_type', 'capi_default_value'] # 'cpp_type' : ['capi_type', 'capi_default_value']
@ -437,11 +437,11 @@ def get_function_impls(content, ident, has_impl=True):
content = content.replace('NO_SANITIZE("cfi-icall")\n', '') content = content.replace('NO_SANITIZE("cfi-icall")\n', '')
# extract the functions # extract the functions
find_regex = '\n' + _cre_func + '\((.*?)\)([A-Za-z0-9_\s]{0,})' find_regex = r'\n' + _cre_func + r'\((.*?)\)([A-Za-z0-9_\s]{0,})'
if has_impl: if has_impl:
find_regex += '\{(.*?)\n\}' find_regex += r'\{(.*?)\n\}'
else: else:
find_regex += '(;)' find_regex += r'(;)'
p = re.compile(find_regex, re.MULTILINE | re.DOTALL) p = re.compile(find_regex, re.MULTILINE | re.DOTALL)
list = p.findall(content) list = p.findall(content)
@ -603,7 +603,7 @@ class obj_header:
data = data.replace("> >", ">>") data = data.replace("> >", ">>")
# extract global typedefs # extract global typedefs
p = re.compile('\ntypedef' + _cre_space + _cre_typedef + ';', p = re.compile(r'\ntypedef' + _cre_space + _cre_typedef + r';',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(data) list = p.findall(data)
if len(list) > 0: if len(list) > 0:
@ -617,7 +617,7 @@ class obj_header:
self.typedefs.append(obj_typedef(self, filename, value, alias)) self.typedefs.append(obj_typedef(self, filename, value, alias))
# extract global functions # extract global functions
p = re.compile('\n' + _cre_attrib + '\n' + _cre_func + '\((.*?)\)', p = re.compile(r'\n' + _cre_attrib + r'\n' + _cre_func + r'\((.*?)\)',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(data) list = p.findall(data)
if len(list) > 0: if len(list) > 0:
@ -631,17 +631,17 @@ class obj_header:
obj_function(self, filename, attrib, retval, argval, comment)) obj_function(self, filename, attrib, retval, argval, comment))
# extract includes # extract includes
p = re.compile('\n#include \"include/' + _cre_cfnameorpath + '.h') p = re.compile(r'\n#include \"include/' + _cre_cfnameorpath + r'.h')
includes = p.findall(data) includes = p.findall(data)
# extract forward declarations # extract forward declarations
p = re.compile('\nclass' + _cre_space + _cre_cfname + ';') p = re.compile(r'\nclass' + _cre_space + _cre_cfname + r';')
forward_declares = p.findall(data) forward_declares = p.findall(data)
# extract empty classes # extract empty classes
p = re.compile('\n' + _cre_attrib + '\nclass' + _cre_space + _cre_cfname + p = re.compile(r'\n' + _cre_attrib + r'\nclass' + _cre_space + _cre_cfname +
_cre_space + ':' + _cre_space + 'public' + _cre_virtual + _cre_space + r':' + _cre_space + r'public' + _cre_virtual +
_cre_space + _cre_cfname + _cre_space + '{};', _cre_space + _cre_cfname + _cre_space + r'{};',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(data) list = p.findall(data)
if len(list) > 0: if len(list) > 0:
@ -663,9 +663,9 @@ class obj_header:
data = p.sub('', data) data = p.sub('', data)
# extract classes # extract classes
p = re.compile('\n' + _cre_attrib + '\nclass' + _cre_space + _cre_cfname + p = re.compile(r'\n' + _cre_attrib + r'\nclass' + _cre_space + _cre_cfname +
_cre_space + ':' + _cre_space + 'public' + _cre_virtual + _cre_space + r':' + _cre_space + r'public' + _cre_virtual +
_cre_space + _cre_cfname + _cre_space + '{(.*?)\n};', _cre_space + _cre_cfname + _cre_space + r'{(.*?)\n};',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(data) list = p.findall(data)
if len(list) > 0: if len(list) > 0:
@ -852,7 +852,7 @@ class obj_class:
# extract typedefs # extract typedefs
p = re.compile( p = re.compile(
'\n' + _cre_space + 'typedef' + _cre_space + _cre_typedef + ';', r'\n' + _cre_space + r'typedef' + _cre_space + _cre_typedef + r';',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(body) list = p.findall(body)
@ -867,8 +867,8 @@ class obj_class:
self.typedefs.append(obj_typedef(self, filename, value, alias)) self.typedefs.append(obj_typedef(self, filename, value, alias))
# extract static functions # extract static functions
p = re.compile('\n' + _cre_space + _cre_attrib + '\n' + _cre_space + p = re.compile(r'\n' + _cre_space + _cre_attrib + r'\n' + _cre_space +
'static' + _cre_space + _cre_func + '\((.*?)\)', r'static' + _cre_space + _cre_func + r'\((.*?)\)',
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(body) list = p.findall(body)
@ -882,8 +882,8 @@ class obj_class:
# extract virtual functions # extract virtual functions
p = re.compile( p = re.compile(
'\n' + _cre_space + _cre_attrib + '\n' + _cre_space + 'virtual' + r'\n' + _cre_space + _cre_attrib + r'\n' + _cre_space + r'virtual' +
_cre_space + _cre_func + '\((.*?)\)' + _cre_vfmod, _cre_space + _cre_func + r'\((.*?)\)' + _cre_vfmod,
re.MULTILINE | re.DOTALL) re.MULTILINE | re.DOTALL)
list = p.findall(body) list = p.findall(body)
@ -1766,7 +1766,7 @@ class obj_analysis:
return {'result_type': 'structure', 'result_value': value} return {'result_type': 'structure', 'result_value': value}
# check for CEF reference pointers # check for CEF reference pointers
p = re.compile('^CefRefPtr<(.*?)>$', re.DOTALL) p = re.compile(r'^CefRefPtr<(.*?)>$', re.DOTALL)
list = p.findall(value) list = p.findall(value)
if len(list) == 1: if len(list) == 1:
return { return {
@ -1776,7 +1776,7 @@ class obj_analysis:
} }
# check for CEF owned pointers # check for CEF owned pointers
p = re.compile('^CefOwnPtr<(.*?)>$', re.DOTALL) p = re.compile(r'^CefOwnPtr<(.*?)>$', re.DOTALL)
list = p.findall(value) list = p.findall(value)
if len(list) == 1: if len(list) == 1:
return { return {
@ -1786,7 +1786,7 @@ class obj_analysis:
} }
# check for CEF raw pointers # check for CEF raw pointers
p = re.compile('^CefRawPtr<(.*?)>$', re.DOTALL) p = re.compile(r'^CefRawPtr<(.*?)>$', re.DOTALL)
list = p.findall(value) list = p.findall(value)
if len(list) == 1: if len(list) == 1:
return { return {

View File

@ -34,10 +34,10 @@ def MakeFileSegment(input, all_names):
# #define IDR_RESOURCE_NAME 12345 # #define IDR_RESOURCE_NAME 12345
# [1] See https://crbug.com/684788#c18 # [1] See https://crbug.com/684788#c18
regex = '#define\s([A-Za-z0-9_]{1,})\s+' regex = r'#define\s([A-Za-z0-9_]{1,})\s+'
if contents.find('ui::WhitelistedResource') > 0: if contents.find('ui::WhitelistedResource') > 0:
regex += '.*<' regex += r'.*<'
regex += '([0-9]{1,})' regex += r'([0-9]{1,})'
# identify the defines in the file # identify the defines in the file
p = re.compile(regex) p = re.compile(regex)