-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
fix: get correct source from redefined class #704
base: master
Are you sure you want to change the base?
Conversation
I use current main branch code to run tests on Python 3.9.9 on my local linux machine and get the errors. That confirms these errors are not introduced by this PR. |
Here's a bit of a out-of the box thought regarding this PR. What if you were to pickle the class, then get the source from the pickled instance? |
I don’t quite understand the scenario you're describing. Do you have any test cases? What exactly do you mean by 'get the source from the pickled instance'? I've never performed such an operation before, so I'm not entirely clear on the situation you're referring to. |
I find when wrap |
See: #243 for an example of serializing class definition with the instance |
I looked into the issue you mentioned and tested a small example: import dill
class Foo(object):
y = 1
_Foo_bits = dill.dumps(Foo)
class Foo(object):
z = 1
_Foo = dill.loads(_Foo_bits)
print(dill.source.getsource(_Foo))
print(_Foo.y) I observed that the current code returns:
Are you expecting it to return:
instead? |
import dill
import inspect
class Foo(object):
def foo(self):
return self.x
x = 1
class Foo(object):
def bar(self):
return self.y
y = 1
_Foo_bits = dill.dumps(Foo)
class Foo(object):
def foo(self):
return self.z
z = 1
_Foo = dill.loads(_Foo_bits)
print(dill.source.getsource(_Foo))
print(inspect.getsource(_Foo))
print(_Foo.y)
f = _Foo()
print(f.bar()) The result is class Foo(object):
def foo(self):
return self.z
z = 1
class Foo(object):
def foo(self):
return self.x
x = 1
1
1 I guess in this situation, the only solution is to save the code when pickling the class object. I can't think of any other approaches. There might be another complex solution - we could first locate the invocation points and analyze the source file's AST to trace the origin of class variables. However, this would inevitably lead to another issue: if the variable is imported from other modules, there's no way to trace back the variable's origin just analyzing one file's AST. |
What I was alluding to from that issue is that |
Is there a way to extract the code from a serialized class object? I didn't find any API in |
No, there isn't a way to do it currently, or I would have pointed you at it. My point was, if it can be done, you don't have to rely on searching the entire buffer. If you use |
Summary
Fixes #703
Checklist
Documentation and Tests
python tests/__main__.py
, and pass.Release Management