Browse Source

MMGenObject.pformat(): handle objects that contain their parent

MMGen 5 years ago
parent
commit
6f1ee739b4
2 changed files with 6 additions and 4 deletions
  1. 5 3
      mmgen/obj.py
  2. 1 1
      test/unit_tests_d/ut_subseed.py

+ 5 - 3
mmgen/obj.py

@@ -50,7 +50,7 @@ class MMGenObject(object):
 	# Pretty-print any object subclassed from MMGenObject, recursing into sub-objects - WIP
 	def pmsg(self): print(self.pformat())
 	def pdie(self): print(self.pformat()); sys.exit(0)
-	def pformat(self,lvl=0):
+	def pformat(self,lvl=0,id_list=[]):
 		scalars = (str,int,float,Decimal)
 		def do_list(out,e,lvl=0,is_dict=False):
 			out.append('\n')
@@ -59,7 +59,7 @@ class MMGenObject(object):
 				if is_dict:
 					out.append('{s}{:<{l}}'.format(i,s=' '*(4*lvl+8),l=10,l2=8*(lvl+1)+8))
 				if hasattr(el,'pformat'):
-					out.append('{:>{l}}{}'.format('',el.pformat(lvl=lvl+1),l=(lvl+1)*8))
+					out.append('{:>{l}}{}'.format('',el.pformat(lvl=lvl+1,id_list=id_list+[id(self)]),l=(lvl+1)*8))
 				elif type(el) in scalars:
 					if isList(e):
 						out.append('{:>{l}}{:16}\n'.format('',repr(el),l=lvl*8))
@@ -90,6 +90,8 @@ class MMGenObject(object):
 # 		print self.keys()
 
 		out = ['<{}>{}\n'.format(type(self).__name__,' '+repr(self) if isScalar(self) else '')]
+		if id(self) in id_list:
+			return out[-1].rstrip() + ' [RECURSION]\n'
 		if isList(self) or isDict(self):
 			do_list(out,self,lvl=lvl,is_dict=isDict(self))
 
@@ -102,7 +104,7 @@ class MMGenObject(object):
 				out.append('{:>{l}}{:<10} {:16}'.format('',k,'<'+type(e).__name__+'>',l=(lvl*8)+4))
 				do_list(out,e,lvl=lvl,is_dict=isDict(e))
 			elif hasattr(e,'pformat') and type(e) != type:
-				out.append('{:>{l}}{:10} {}'.format('',k,e.pformat(lvl=lvl+1),l=(lvl*8)+4))
+				out.append('{:>{l}}{:10} {}'.format('',k,e.pformat(lvl=lvl+1,id_list=id_list+[id(self)]),l=(lvl*8)+4))
 			else:
 				out.append('{:>{l}}{:<10} {:16} {}\n'.format(
 					'',k,'<'+type(e).__name__+'>',repr(e),l=(lvl*8)+4))

+ 1 - 1
test/unit_tests_d/ut_subseed.py

@@ -53,7 +53,7 @@ class subseed(object):
 				seed2.subseeds.generate(10)
 				assert len(ss2_list) == 10, len(ss2_list)
 
-# 				assert seed.pformat() == seed2.pformat() # TODO: deal with recursion in pformat()
+				assert seed.pformat() == seed2.pformat()
 
 				s = seed.subseeds.format(1,g.subseeds)
 				s_lines = s.strip().split('\n')