Repository navigation
Use realpath in PackageDescriptor.__hash__ (#478) - #752
Open
paneendrakumar0 wants to merge 1 commit into
Open
paneendrakumar0 wants to merge 1 commit into
paneendrakumar0 wants to merge 1 commit into
Conversation
Both 'name' and 'type' attributes of PackageDescriptor are mutable and can be modified during package identification or augmentation. Computing __hash__ from mutable attributes causes the hash value to change after an instance has been added to a set or dict, resulting in hash table corruption and membership check failures. Compute __hash__ using os.path.realpath(str(self.path)), which is immutable over the lifetime of the descriptor and consistent with the realpath equality check in __eq__. Signed-off-by: paneendrakumar0 <paneendra100@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #478.
Problem
Both the
nameandtypeattributes ofPackageDescriptorare mutable and may be modified after instantiation (for example, during package identification or augmentation). Because__hash__previously computedhash((self.type, self.name)), mutating either attribute changes the instance's hash value while it resides in asetordict, corrupting the hash table buckets and causing subsequent membership checks or removal operations to fail:Solution
__hash__usingos.path.realpath(str(self.path)). The package path is set upon construction and does not change during the descriptor's lifetime.__eq__verifies(self.type, self.name) == (other.type, other.name)andrealpath(self) == realpath(other)) will always yield identical hashes.test_magic_methodsverifying that mutations tonameortypepreserve hash value and set membership, and that descriptors pointing to the same realpath compare equal and hash identically.