Browse Source

Python 3.7, 3.8 compatibility fix

The MMGen Project 2 years ago
parent
commit
6653cff8b0
3 changed files with 15 additions and 3 deletions
  1. 6 0
      mmgen/util.py
  2. 6 2
      scripts/exec_wrapper.py
  3. 3 1
      test/test.py

+ 6 - 0
mmgen/util.py

@@ -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

+ 6 - 2
scripts/exec_wrapper.py

@@ -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):'

+ 3 - 1
test/test.py

@@ -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,