2023-04-12 02:02:39 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import fire
|
|
|
|
|
|
|
|
"""
|
|
|
|
Run With: with `wolverine examples/buggy_script_2.py "return_2"`
|
|
|
|
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
|
2023-04-20 12:01:59 +00:00
|
|
|
would_be_2 = (singleton.a + singleton.a)
|
|
|
|
assert would_be_2 == 2
|
|
|
|
return would_be_2
|
2023-04-12 02:02:39 +00:00
|
|
|
|
|
|
|
if __name__=="__main__":
|
2023-04-20 12:01:59 +00:00
|
|
|
fire.Fire()
|