@@ -81,7 +81,7 @@ def do_stuff(self):
8181self .increment () # <- will fail and rollback
8282
8383
84- if __name__ == '__main__' :
84+ def main () :
8585num_obj = NumObj (- 1 )
8686print (num_obj )
8787
@@ -114,30 +114,34 @@ def do_stuff(self):
114114traceback .print_exc (file = sys .stdout )
115115print (num_obj )
116116
117-
118- ### OUTPUT ###
119- # <NumObj: -1>
120- # <NumObj: 0>
121- # <NumObj: 1>
122- # <NumObj: 2>
123- # -- committed
124- # <NumObj: 3>
125- # <NumObj: 4>
126- # <NumObj: 5>
127- # -- rolled back
128- # <NumObj: 2>
129- # -- now doing stuff ...
130- # -> doing stuff failed!
131- # Traceback (most recent call last):
132- # File "memento.py", line 97, in <module>
133- # num_obj.do_stuff()
134- # File "memento.py", line 52, in transaction
135- # raise e
136- # File "memento.py", line 49, in transaction
137- # return self.method(obj, *args, **kwargs)
138- # File "memento.py", line 70, in do_stuff
139- # self.increment() # <- will fail and rollback
140- # File "memento.py", line 65, in increment
141- # self.value += 1
142- # TypeError: Can't convert 'int' object to str implicitly
143- # <NumObj: 2>
117+ if __name__ == '__main__' :
118+ main ()
119+
120+
121+ OUTPUT = """
122+ <NumObj: -1>
123+ <NumObj: 0>
124+ <NumObj: 1>
125+ <NumObj: 2>
126+ -- committed
127+ <NumObj: 3>
128+ <NumObj: 4>
129+ <NumObj: 5>
130+ -- rolled back
131+ <NumObj: 2>
132+ -- now doing stuff ...
133+ -> doing stuff failed!
134+ Traceback (most recent call last):
135+ File "patterns/behavioral/memento.py", line 108, in main
136+ num_obj.do_stuff()
137+ File "patterns/behavioral/memento.py", line 63, in transaction
138+ raise e
139+ File "patterns/behavioral/memento.py", line 60, in transaction
140+ return self.method(obj, *args, **kwargs)
141+ File "patterns/behavioral/memento.py", line 81, in do_stuff
142+ self.increment() # <- will fail and rollback
143+ File "patterns/behavioral/memento.py", line 76, in increment
144+ self.value += 1
145+ TypeError: can only concatenate str (not "int") to str
146+ <NumObj: 2>
147+ """
0 commit comments