Python 3.7, 3.8 compatibility fix

This commit is contained in:
The MMGen Project 2022-08-16 20:35:49 +00:00
commit 6653cff8b0
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 15 additions and 3 deletions

View file

@ -191,6 +191,12 @@ def list_gen(*data):
yield i[0]
return list(gen())
def removeprefix(s,pfx): # workaround for pre-Python 3.9
return s[len(pfx):] if s.startswith(pfx) else s
def removesuffix(s,sfx): # workaround for pre-Python 3.9
return s[:len(sfx)] if s.endswith(sfx) else s
def remove_dups(iterable,edesc='element',desc='list',quiet=False,hide=False):
"""
Remove duplicate occurrences of iterable elements, preserving first occurrence

View file

@ -44,8 +44,12 @@ def exec_wrapper_write_traceback(e,exit_val):
cwd = os.path.abspath('.')
def fixup_fn(fn_in):
fn = fn_in.removeprefix(cwd+'/').removeprefix('test/overlay/tree/')
return (fn.removesuffix('_orig.py') + '.py') if fn.endswith('_orig.py') else fn
from mmgen.util import removeprefix,removesuffix
fn = removeprefix(removeprefix(fn_in,cwd+'/'),'test/overlay/tree/')
return removesuffix(fn,'_orig.py') + '.py' if fn.endswith('_orig.py') else fn
# Python 3.9:
# fn = fn_in.removeprefix(cwd+'/').removeprefix('test/overlay/tree/')
# return fn.removesuffix('_orig.py') + '.py' if fn.endswith('_orig.py') else fn
def gen_output():
yield 'Traceback (most recent call last):'

View file

@ -373,7 +373,9 @@ class CmdGroupMgr(object):
def gen():
for name,data in cls.cmd_group_in:
if name.startswith('subgroup.'):
sg_key = name.removeprefix('subgroup.')
from mmgen.util import removeprefix
sg_key = removeprefix(name,'subgroup.')
# sg_key = name.removeprefix('subgroup.') # Python 3.9
if sg_name in (None,sg_key):
for e in add_entries(
sg_key,