Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug:the call __init__ failed #103

Closed
wants to merge 1 commit into from
Closed

bug:the call __init__ failed #103

wants to merge 1 commit into from

Conversation

linxingjun
Copy link

bug:Calling a load_state method after inheriting the kademlia.network.Server class causes the inherited and modified init function to fail to act on the return value of the load_state method
fixed:Get the inherited class name and call the constructor via eval().

@linxingjun linxingjun changed the title Update network.py bug:the call __init__ failed May 1, 2023
@@ -220,7 +220,8 @@ async def load_state(cls, fname, port, interface='0.0.0.0'):
log.info("Loading state from %s", fname)
with open(fname, 'rb') as file:
data = pickle.load(file)
svr = Server(data['ksize'], data['alpha'], data['id'])
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not work?

Suggested change
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])")
svr = cls(data['ksize'], data['alpha'], data['id'])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my English is poor, so my responses and these are all machine translation
When you inherit this class, because the referenced constructor is still the inherited class
So if you modify the constructor of the inherited class, the modified constructor will not work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not work?

Suggested change
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])")
svr = eval(cls.__class__.__name__+"(data['ksize'], data['alpha'], data['id'])")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first version, I overlooked that there may not be name properties, modified here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class test1:
    def __init__(self):
          print("test1")
    def save(self):
          test1()
class test2:
    def __init__(self):
          print("test2")
    def save(self):
          eval(self.__class__.__name__+"()")
class test3(test1):
    def __init__(self):
          print("test3")
class test4(test2):
    def __init__(self):
          print("test4")
test1()
a=test2()
a.save()
a=test3()
a.save()
a=test4()
a.save()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not work?

Hope you can figure it out with this example

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not work?

And your changes are incomplete, because there is a possibility that the error will be reported because there is no call

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think your example matches this scenario - the load_state method is a @classmethod. Here's an example that I fits a bit better, and demonstrates that even a class that extends the base Server can be successfully instantiated in the load_state:

class Server:
    def __init__(self, details):
        self.details = details

    @classmethod
    def load_state(cls, details):
        svr = cls(details)
        return svr

class ExtendingServer(Server):
    def test(self):
        print('hello %s' % self.details)

s = ExtendingServer.load_state('there')
s.test()

When this code was written, it wasn't assumed that anyone would be extending Server - but I don't mind supporting that potential case.

@bmuller
Copy link
Owner

bmuller commented May 7, 2023

Closed in favor of ff19ddb

@bmuller bmuller closed this May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants