-
Notifications
You must be signed in to change notification settings - Fork 23
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
[Question] How does theone
return the Python object?
#31
Comments
theone
return the Python object?theone
return the Python object?
Hmm... so the things such as NyNodeSet is just, think of it like a python set, except instead of equality being governed by the object's defined Because nodeset contains objects, it has to keep the objects alive. This is where Things like Since you're looking at
Lines 175 to 195 in a1bd557
Lines 340 to 367 in a1bd557
Lines 889 to 922 in a1bd557
The important thing here is that it creates and returns a mutnodeset (a mutable nodeset) that contains everything it saw during the traversal. Then Line 58 in a1bd557
Line 396 in a1bd557
Lines 2214 to 2215 in a1bd557
Here Lines 1526 to 1538 in a1bd557
If the size of the set is 1, then it gets the single element of the set via And Lines 624 to 630 in a1bd557
So tl;dr: in Does this answer how it works? |
Hi @zhuyifei1999, Thank you for your thorough explanation. This is definitely very helpful! I was able to follow the exact control flow when debugging with PyCharm.
This makes sense! To confirm: when adding an object by calling Lines 599 to 619 in a1bd557
The core functionality that adds the object is actually this: Line 604 in a1bd557
where it adds the object's adjusted address to the bitset (or nodes , depending on whether the set is immutable or not) within the NyNodeSetObject struct:Lines 325 to 329 in a1bd557
Am I correct? When retrieving the object from Lines 640 to 641 in a1bd557
somehow calls some C code to get the object from the address? I am sorry, this is where I get a bit fuzzy. I presume the following C function is triggered by some function(s) when self._node in the code above is called?Lines 331 to 335 in a1bd557
Thank you! |
Immutable one cannot be added.
No, custom C code is completely uninvolved here. This corresponds to Lines 624 to 630 in a1bd557
In
In
How |
OK. I think I see what is going on here. I believe, correct me if I am wrong, that the "trick" here is: Lines 2214 to 2215 in a1bd557
It is when self.immnodeset() is called where we store actual objects into the immutable node set.I see that when a copy is made: Lines 135 to 151 in a1bd557
NyNodeSet_iterate() would call mutnodeset_iterate_visit() to dereference the address from bitno , which is stored in mutable node set's bitset , and get the object. The object is then stored in nodes :Lines 8 to 16 in a1bd557
I guess my last question is, what is stored in Thanks! |
This you can answer with GDB:
The typedef struct {
PyObject_VAR_HEAD
/* ob_item contains space for 'ob_size' elements.
Items must normally not be NULL, except during construction when
the tuple is not yet visible outside the function that builds it. */
PyObject *ob_item[1];
} PyTupleObject; |
I am not sure if you actually answered my question. I am aware that |
Don't understand your question. I'm referring to the declaration In other words, the assumption " This might help you: https://cdecl.org/?q=int+*nodes%5B1%5D%3B Edit: I resized I was skipping too fast in my thoughts in my answer above. Sorry, |
My bad! It all makes sense! I need to brush up on my C syntax! Thank you! |
I am trying to understand the inner workings of guppy. I am curious how the following method in
IdentitySetSingleton
actually works to return the actual Python object:Looking through the C extension code in hv.c, I see in
hv_heap()
function that it callshv_heap_rec()
which in turn callsNyNodeSet_setobj()
. InNyNodeSet_setobj()
,Py_INCREF()
is called to increase the reference count of eachobj
. But in the code, I don't see whereobj
is stored in a node?Thank you for your explanation.
The text was updated successfully, but these errors were encountered: