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

Update _hash when unpickling Tag() #860

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/packaging/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sysconfig
from importlib.machinery import EXTENSION_SUFFIXES
from typing import (
Any,
Iterable,
Iterator,
Sequence,
Expand Down Expand Up @@ -92,6 +93,13 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"<{self} @ {id(self)}>"

def __setstate__(self, state: tuple[dict[str, Any], dict[str, Any]]) -> None:
# cached _hash is wrong when unpickling
_, slots = state
for slot in slots:
setattr(self, slot, slots[slot])
self._hash = hash((self._interpreter, self._abi, self._platform))


dholth marked this conversation as resolved.
Show resolved Hide resolved
def parse_tag(tag: str) -> frozenset[Tag]:
"""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import importlib
import os
import pathlib
import pickle
import platform
import struct
import sys
Expand Down Expand Up @@ -1490,3 +1491,9 @@ def _calcsize(fmt):
monkeypatch.setattr(struct, "calcsize", _calcsize)
importlib.reload(tags)
assert tags._32_BIT_INTERPRETER == expected


def test_pickle():
# Make sure equality works between a pickle/unpickle round trip.
tag = tags.Tag("py3", "none", "any")
assert pickle.loads(pickle.dumps(tag)) == tag
Loading