-
Notifications
You must be signed in to change notification settings - Fork 94
Replace setattr and getattr calls with __dict__ manipulations #905
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
Conversation
|
Sorry - looks like I accidentally removed myself from the watchers for this repo (or have been removed somehow), so I missed this. I'm not quite sure about this - this part has been taken from the mox project... So I'm inclined to merge this after the failing test is fixed. Also please add an entry to the release notes. |
|
@mrbean-bremen I could always define separate class AccessMethod(Enum):
GETATTR = auto()
DICT = auto()
SLOT = auto()
def get_attribute_safely(obj, attr_name):
try:
attr_value = getattr(obj, attr_name)
access_method = AccessMethod.GETATTR
except AttributeError as e:
if hasattr(obj, '__dict__') and attr_name in obj.__dict__:
attr_value = obj.__dict__[attr_name]
access_method = AccessMethod.DICT
elif hasattr(obj, '__slot__') and attr_name in obj.__slot__:
attr_value = obj.__slot__[attr_name]
access_method = AccessMethod.SLOT
else:
raise e
return attr_value, access_methodallowing us to store the I think that would cover all possible edge cases and be quite safe, unless I'm missing something. On the other hand, if you think that the current approach is safe enough, that works, too. |
I like this. It may be a bit overkill, but it would not change the current behavior and performance in almost all cases, and as you wrote, we'd be on the secure side. |
|
Ok, I thought a bit more about this, and I think that your original approach might be sufficient, given that we are looking for specific file system functions that are certainly not slots. It may also have a performance benefit (though I still have to measure that). So, please ignore my last comment - if you add an entry to the release notes mentioning the change and what it fixes, I will merge this as is, thanks! |
|
Woops, missed your comment there. Everything should be reverted now. This is probably for the best anyway – I was working on writing unit tests for all the |
mrbean-bremen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good (apart from that typo that pre-commit found)!
|
Ahh, "compatibility" always gets me. Fixed! |
…, __dict__ or __slots__" This reverts commit 0052919.
ad5d515 to
7a3566c
Compare
|
Sorry I didn't reply earlier. Why is setting a default to |
|
Or, calling |
It is usually working, but there are cases where it is overwritten and may cause problems/unintended side effects (in this case, with One unintended consequence is also a noticable performance improvement, at least in Python versions before 3.12 (in 3.12 the performance of As always, I may have overlooked something, of course - crossing fingers that no regression bugs are incoming after the release... |
|
|
Ok, didn'y know that.
True, though I'm not sure if |
Describe the changes
Change the
setattrandgetattrcalls inpyfakefs/mox3_stubout.pyto directly access the objects__dict__attribute. I did this because I was having trouble using pyfakefs in a large codebase that also used PyTorch – which I tracked down to custom__setattr__methods found here (among other places).I saw the comment in
pyfakefs/mox3_stubout.pymentioning that a switch to manipulating the__dict__attribute directly had been considered, and that it would even resolve the special case with static methods losing theirstaticmethodwrapper when accessing them withgetattr, so I went ahead and made that change throughout the file.I'm not sure if this will break pyfakefs for other use cases, but it fixed it for me, so I figured I'd make a pull request :)
Tasks