Skip to content

Commit 67dbbf9

Browse files
remove quotes from etag and use binary checksum to save a few bytes
1 parent 26b9cf7 commit 67dbbf9

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/pip/_internal/index/package_finder.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ def _try_load_http_cache_headers(
807807
checksum_path: Path,
808808
project_url: Link,
809809
headers: Dict[str, str],
810-
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
810+
) -> Tuple[Optional[str], Optional[str], Optional[bytes]]:
811811
etag: Optional[str] = None
812812
try:
813813
etag = etag_path.read_text()
@@ -817,7 +817,7 @@ def _try_load_http_cache_headers(
817817
etag_path,
818818
etag,
819819
)
820-
headers["If-None-Match"] = etag
820+
headers["If-None-Match"] = f'"{etag}"'
821821
except OSError as e:
822822
logger.debug("no etag found for url %s (%s)", project_url, str(e))
823823

@@ -834,9 +834,9 @@ def _try_load_http_cache_headers(
834834
except OSError as e:
835835
logger.debug("no date found for url %s (%s)", project_url, str(e))
836836

837-
checksum: Optional[str] = None
837+
checksum: Optional[bytes] = None
838838
try:
839-
checksum = checksum_path.read_text()
839+
checksum = checksum_path.read_bytes()
840840
logger.debug(
841841
"found checksum for url %s at %s: %s",
842842
project_url,
@@ -848,44 +848,53 @@ def _try_load_http_cache_headers(
848848

849849
return (etag, date, checksum)
850850

851-
@staticmethod
851+
_quoted_value = re.compile(r'^"([^"]*)"$')
852+
853+
@classmethod
854+
def _strip_quoted_value(cls, value: str) -> str:
855+
return cls._quoted_value.sub(r"\1", value)
856+
857+
@classmethod
852858
def _write_http_cache_info(
859+
cls,
853860
etag_path: Path,
854861
date_path: Path,
855862
checksum_path: Path,
856863
project_url: Link,
857864
index_response: IndexContent,
858865
prev_etag: Optional[str],
859-
prev_checksum: Optional[str],
860-
) -> Tuple[Optional[str], Optional[str], str, bool]:
866+
prev_checksum: Optional[bytes],
867+
) -> Tuple[Optional[str], Optional[str], bytes, bool]:
861868
hasher = sha256()
862869
hasher.update(index_response.content)
863-
new_checksum = hasher.hexdigest()
864-
checksum_path.write_text(new_checksum)
870+
new_checksum = hasher.digest()
871+
checksum_path.write_bytes(new_checksum)
865872
page_unmodified = new_checksum == prev_checksum
866873

867-
new_etag = index_response.etag
874+
new_etag: Optional[str] = index_response.etag
868875
if new_etag is None:
869876
logger.debug("no etag returned from fetch for url %s", project_url.url)
870877
try:
871878
etag_path.unlink()
872879
except OSError:
873880
pass
874-
elif new_etag != prev_etag:
875-
logger.debug(
876-
"etag for url %s updated from %s -> %s",
877-
project_url.url,
878-
prev_etag,
879-
new_etag,
880-
)
881-
etag_path.write_text(new_etag)
882881
else:
883-
logger.debug(
884-
"etag was unmodified for url %s (%s)", project_url.url, prev_etag
885-
)
886-
assert page_unmodified
882+
new_etag = cls._strip_quoted_value(new_etag)
883+
if new_etag != prev_etag:
884+
logger.debug(
885+
"etag for url %s updated from %s -> %s",
886+
project_url.url,
887+
prev_etag,
888+
new_etag,
889+
)
890+
etag_path.write_text(new_etag)
891+
else:
892+
logger.debug(
893+
"etag was unmodified for url %s (%s)", project_url.url, prev_etag
894+
)
895+
assert page_unmodified
887896

888-
new_date = index_response.date
897+
new_date: Optional[str] = index_response.date
889898
if new_date is None:
890899
logger.debug(
891900
"no date could be parsed from response for url %s", project_url

0 commit comments

Comments
 (0)