kopia lustrzana https://github.com/biobootloader/wolverine
switch singleton example to have a bug in singleton implementation
rodzic
459a9c5ef2
commit
bb8c6f8e3e
|
@ -2,27 +2,26 @@
|
|||
import fire
|
||||
|
||||
"""
|
||||
Run With: with `wolverine examples/buggy_script_2.py "return_2"`
|
||||
Run With: with `python wolverine.py examples/buggy_script_2.py`
|
||||
Purpose: Fix singleton code bug in Python
|
||||
"""
|
||||
|
||||
class SingletonClass(object):
|
||||
def __new__(cls):
|
||||
if not hasattr(cls, 'instance'):
|
||||
cls.instance = super(SingletonClass, cls).__new__(cls)
|
||||
return cls.instance
|
||||
|
||||
def return_2():
|
||||
"""
|
||||
Always returns 2
|
||||
"""
|
||||
singleton = SingletonClass()
|
||||
new_singleton = SingletonClass()
|
||||
singleton.a = 1
|
||||
new_singleton.a = 2
|
||||
would_be_2 = (singleton.a + singleton.a)
|
||||
assert would_be_2 == 2
|
||||
return would_be_2
|
||||
def __new__(cls):
|
||||
cls.instance = super(SingletonClass, cls).__new__(cls)
|
||||
return cls.instance
|
||||
|
||||
def check_singleton_works():
|
||||
"""
|
||||
check that singleton pattern is working
|
||||
"""
|
||||
singleton = SingletonClass()
|
||||
new_singleton = SingletonClass()
|
||||
singleton.a = 1
|
||||
new_singleton.a = 2
|
||||
should_be_4 = (singleton.a + new_singleton.a)
|
||||
assert should_be_4 == 4
|
||||
|
||||
if __name__=="__main__":
|
||||
fire.Fire()
|
||||
fire.Fire(check_singleton_works)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue