Skip to content

Commit b9f7ecb

Browse files
authored
Rename/move strip_prefix -> removeprefix. NFC (emscripten-core#19723)
This matches the name of the of the python3.9 builtin.
1 parent 2cf0dc0 commit b9f7ecb

7 files changed

Lines changed: 32 additions & 35 deletions

File tree

emcc.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from tools import colored_logger, diagnostics, building
4646
from tools.shared import unsuffixed, unsuffixed_basename, WINDOWS, safe_copy
4747
from tools.shared import run_process, read_and_preprocess, exit_with_error, DEBUG
48-
from tools.shared import do_replace, strip_prefix
48+
from tools.shared import do_replace
4949
from tools.response_file import substitute_response_files
5050
from tools.minimal_runtime_shell import generate_minimal_runtime_html
5151
import tools.line_endings
@@ -55,7 +55,7 @@
5555
from tools import config
5656
from tools import cache
5757
from tools.settings import user_settings, settings, MEM_SIZE_SETTINGS, COMPILE_TIME_SETTINGS
58-
from tools.utils import read_file, write_file, read_binary, delete_file
58+
from tools.utils import read_file, write_file, read_binary, delete_file, removeprefix
5959

6060
logger = logging.getLogger('emcc')
6161

@@ -439,7 +439,7 @@ def apply_user_settings():
439439

440440
filename = None
441441
if value and value[0] == '@':
442-
filename = strip_prefix(value, '@')
442+
filename = removeprefix(value, '@')
443443
if not os.path.exists(filename):
444444
exit_with_error('%s: file not found parsing argument: %s=%s' % (filename, key, value))
445445
value = read_file(filename).strip()
@@ -774,7 +774,7 @@ def is_dash_s_for_emcc(args, i):
774774
return False
775775
arg = args[i + 1]
776776
else:
777-
arg = strip_prefix(args[i], '-s')
777+
arg = removeprefix(args[i], '-s')
778778
arg = arg.split('=')[0]
779779
return arg.isidentifier() and arg.isupper()
780780

@@ -834,7 +834,7 @@ def process_dynamic_libs(dylibs, lib_dirs):
834834
# EM_JS function are exports with a special prefix. We need to strip
835835
# this prefix to get the actaul symbol name. For the main module, this
836836
# is handled by extract_metadata.py.
837-
exports = [shared.maybe_strip_prefix(e, '__em_js__') for e in exports]
837+
exports = [utils.removeprefix(e, '__em_js__') for e in exports]
838838
settings.SIDE_MODULE_EXPORTS.extend(sorted(exports))
839839

840840
imports = webassembly.get_imports(dylib)
@@ -874,7 +874,7 @@ def parse_s_args(args):
874874
key = args[i + 1]
875875
args[i + 1] = ''
876876
else:
877-
key = strip_prefix(args[i], '-s')
877+
key = removeprefix(args[i], '-s')
878878
args[i] = ''
879879

880880
# If not = is specified default to 1
@@ -1374,7 +1374,7 @@ def normalize_boolean_setting(name, value):
13741374
# and we can't just flip them, so leave them as-is to be
13751375
# handled in a special way later)
13761376
if name.startswith('NO_') and value in ('0', '1'):
1377-
name = strip_prefix(name, 'NO_')
1377+
name = removeprefix(name, 'NO_')
13781378
value = str(1 - int(value))
13791379
return name, value
13801380

@@ -1490,7 +1490,7 @@ def phase_setup(options, state, newargs):
14901490
# For shared libraries that are neither bitcode nor wasm, assuming its local native
14911491
# library and attempt to find a library by the same name in our own library path.
14921492
# TODO(sbc): Do we really need this feature? See test_other.py:test_local_link
1493-
libname = strip_prefix(get_library_basename(arg), 'lib')
1493+
libname = removeprefix(get_library_basename(arg), 'lib')
14941494
flag = '-l' + libname
14951495
diagnostics.warning('map-unrecognized-libraries', f'unrecognized file type: `{arg}`. Mapping to `{flag}` and hoping for the best')
14961496
add_link_flag(state, i, flag)
@@ -2963,7 +2963,7 @@ def get_language_mode(args):
29632963
return_next = True
29642964
continue
29652965
if item.startswith('-x'):
2966-
return strip_prefix(item, '-x')
2966+
return removeprefix(item, '-x')
29672967
return ''
29682968

29692969
language_mode = get_language_mode(newargs)
@@ -3371,7 +3371,7 @@ def consume_arg_file():
33713371

33723372
if arg.startswith('-O'):
33733373
# Let -O default to -O2, which is what gcc does.
3374-
requested_level = strip_prefix(arg, '-O') or '2'
3374+
requested_level = removeprefix(arg, '-O') or '2'
33753375
if requested_level == 's':
33763376
requested_level = 2
33773377
settings.SHRINK_LEVEL = 1
@@ -3432,7 +3432,7 @@ def consume_arg_file():
34323432
settings.DEBUG_LEVEL = max(1, settings.DEBUG_LEVEL)
34333433
elif arg.startswith('-g'):
34343434
options.requested_debug = arg
3435-
requested_level = strip_prefix(arg, '-g') or '3'
3435+
requested_level = removeprefix(arg, '-g') or '3'
34363436
if is_int(requested_level):
34373437
# the -gX value is the debug level (-g1, -g2, etc.)
34383438
settings.DEBUG_LEVEL = validate_arg_level(requested_level, 4, 'invalid debug level: ' + arg)
@@ -3610,7 +3610,7 @@ def consume_arg_file():
36103610
elif arg == '-frtti':
36113611
settings.USE_RTTI = 1
36123612
elif arg.startswith('-jsD'):
3613-
key = strip_prefix(arg, '-jsD')
3613+
key = removeprefix(arg, '-jsD')
36143614
if '=' in key:
36153615
key, value = key.split('=')
36163616
else:
@@ -3626,7 +3626,7 @@ def consume_arg_file():
36263626
elif check_arg('-o'):
36273627
options.output_file = consume_arg()
36283628
elif arg.startswith('-o'):
3629-
options.output_file = strip_prefix(arg, '-o')
3629+
options.output_file = removeprefix(arg, '-o')
36303630
newargs[i] = ''
36313631
elif arg == '-mllvm':
36323632
# Ignore the next argument rather than trying to parse it. This is needed
@@ -4191,7 +4191,7 @@ def process_libraries(state, linker_inputs):
41914191
if not flag.startswith('-l'):
41924192
new_flags.append((i, flag))
41934193
continue
4194-
lib = strip_prefix(flag, '-l')
4194+
lib = removeprefix(flag, '-l')
41954195

41964196
logger.debug('looking for library "%s"', lib)
41974197
js_libs, native_lib = building.map_to_js_libs(lib)

emscripten.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
from tools import utils
2727
from tools import webassembly
2828
from tools import extract_metadata
29-
from tools.utils import exit_with_error, path_from_root
29+
from tools.utils import exit_with_error, path_from_root, removeprefix
3030
from tools.shared import DEBUG, asmjs_mangle
31-
from tools.shared import treat_as_user_function, strip_prefix
31+
from tools.shared import treat_as_user_function
3232
from tools.settings import settings
3333

3434
logger = logging.getLogger('emscripten')
@@ -618,7 +618,7 @@ def create_em_js(metadata):
618618
args = []
619619
else:
620620
args = args.split(',')
621-
arg_names = [arg.split()[-1].replace("*", "") for arg in args if arg]
621+
arg_names = [arg.split()[-1].replace('*', '') for arg in args if arg]
622622
args = ','.join(arg_names)
623623
func = f'function {name}({args}) {body}'
624624
if (settings.MAIN_MODULE or settings.ASYNCIFY == 2) and name in metadata.emJsFuncTypes:
@@ -847,7 +847,7 @@ def create_invoke_wrappers(metadata):
847847
"""Asm.js-style exception handling: invoke wrapper generation."""
848848
invoke_wrappers = ''
849849
for invoke in metadata.invokeFuncs:
850-
sig = strip_prefix(invoke, 'invoke_')
850+
sig = removeprefix(invoke, 'invoke_')
851851
invoke_wrappers += '\n' + js_manipulation.make_invoke(sig) + '\n'
852852
return invoke_wrappers
853853

tools/extract_metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
from typing import List, Dict
88

9-
from . import webassembly
9+
from . import webassembly, utils
1010
from .webassembly import OpCode, AtomicOpCode, MemoryOpCode
1111
from .shared import exit_with_error
1212
from .settings import settings
@@ -306,7 +306,7 @@ def extract_metadata(filename):
306306
export_map = {e.name: e for e in exports}
307307
for e in exports:
308308
if e.kind == webassembly.ExternType.GLOBAL and e.name.startswith('__em_js__'):
309-
name = e.name[len('__em_js__'):]
309+
name = utils.removeprefix(e.name, '__em_js__')
310310
globl = module.get_global(e.index)
311311
string_address = get_global_value(globl)
312312
em_js_funcs[name] = get_string_at(module, string_address)

tools/shared.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,18 +668,6 @@ def unsuffixed_basename(name):
668668
return os.path.basename(unsuffixed(name))
669669

670670

671-
def strip_prefix(string, prefix):
672-
assert string.startswith(prefix)
673-
return string[len(prefix):]
674-
675-
676-
def maybe_strip_prefix(string, prefix):
677-
if string.startswith(prefix):
678-
return string[len(prefix):]
679-
else:
680-
return string
681-
682-
683671
def make_writable(filename):
684672
assert os.path.isfile(filename)
685673
old_mode = stat.S_IMODE(os.stat(filename).st_mode)

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def get_link_flag(self):
431431
return fullpath
432432
# For libraries (.a) files, we pass the abbreviated `-l` form.
433433
base = shared.unsuffixed_basename(fullpath)
434-
return '-l' + shared.strip_prefix(base, 'lib')
434+
return '-l' + utils.removeprefix(base, 'lib')
435435

436436
def get_files(self):
437437
"""

tools/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def safe_ensure_dirs(dirname):
2929
os.makedirs(dirname, exist_ok=True)
3030

3131

32+
# TODO(sbc): Replace with str.removeprefix once we update to python3.9
33+
def removeprefix(string, prefix):
34+
if string.startswith(prefix):
35+
return string[len(prefix):]
36+
return string
37+
38+
3239
@contextlib.contextmanager
3340
def chdir(dir):
3441
"""A context manager that performs actions in the given directory."""

tools/wasm-sourcemap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
__rootdir__ = os.path.dirname(__scriptdir__)
2525
sys.path.insert(0, __rootdir__)
2626

27+
from tools import utils
28+
2729
logger = logging.getLogger('wasm-sourcemap')
2830

2931

@@ -62,9 +64,9 @@ def resolve(self, name):
6264
for p in self.prefixes:
6365
if name.startswith(p['prefix']):
6466
if p['replacement'] is None:
65-
result = name[len(p['prefix'])::]
67+
result = utils.removeprefix(name, p['prefix'])
6668
else:
67-
result = p['replacement'] + name[len(p['prefix'])::]
69+
result = p['replacement'] + utils.removeprefix(name, p['prefix'])
6870
break
6971
self.cache[name] = result
7072
return result

0 commit comments

Comments
 (0)