Skip to content

Commit 2a0552a

Browse files
committed
Replace complex certifi patch with a more targetted requests patch
This should have the same final functionality, with a cleaner patch to requests instead of an exception-based complex patch to certifi.
1 parent 8375281 commit 2a0552a

File tree

4 files changed

+38
-49
lines changed

4 files changed

+38
-49
lines changed

src/pip/_vendor/certifi/core.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,7 @@
88
import types
99
from typing import Union
1010

11-
12-
class _PipPatchedCertificate(Exception):
13-
pass
14-
15-
1611
try:
17-
# Return a certificate file on disk for a standalone pip zipapp running in
18-
# an isolated build environment to use. Passing --cert to the standalone
19-
# pip does not work since requests calls where() unconditionally on import.
20-
_PIP_STANDALONE_CERT = os.environ.get("_PIP_STANDALONE_CERT")
21-
if _PIP_STANDALONE_CERT:
22-
def where():
23-
return _PIP_STANDALONE_CERT
24-
raise _PipPatchedCertificate()
25-
2612
from importlib.resources import path as get_path, read_text
2713

2814
_CACERT_CTX = None
@@ -52,8 +38,6 @@ def where() -> str:
5238

5339
return _CACERT_PATH
5440

55-
except _PipPatchedCertificate:
56-
pass
5741

5842
except ImportError:
5943
Package = Union[types.ModuleType, str]
@@ -81,4 +65,4 @@ def where() -> str:
8165

8266

8367
def contents() -> str:
84-
return read_text("certifi", "cacert.pem", encoding="ascii")
68+
return read_text("pip._vendor.certifi", "cacert.pem", encoding="ascii")

src/pip/_vendor/requests/certs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
environment, you can change the definition of where() to return a separately
1212
packaged CA bundle.
1313
"""
14-
from pip._vendor.certifi import where
14+
15+
import os
16+
17+
if "_PIP_STANDALONE_CERT" not in os.environ:
18+
from pip._vendor.certifi import where
19+
else:
20+
def where():
21+
return os.environ["_PIP_STANDALONE_CERT"]
1522

1623
if __name__ == "__main__":
1724
print(where())

tools/vendoring/patches/certifi.patch

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
diff --git a/src/pip/_vendor/certifi/core.py b/src/pip/_vendor/certifi/core.py
2-
index 497d938..f34045b 100644
2+
index 497d938d0..60ad982c6 100644
33
--- a/src/pip/_vendor/certifi/core.py
44
+++ b/src/pip/_vendor/certifi/core.py
5-
@@ -8,7 +8,21 @@ import os
6-
import types
7-
from typing import Union
8-
9-
+
10-
+class _PipPatchedCertificate(Exception):
11-
+ pass
12-
+
13-
+
14-
try:
15-
+ # Return a certificate file on disk for a standalone pip zipapp running in
16-
+ # an isolated build environment to use. Passing --cert to the standalone
17-
+ # pip does not work since requests calls where() unconditionally on import.
18-
+ _PIP_STANDALONE_CERT = os.environ.get("_PIP_STANDALONE_CERT")
19-
+ if _PIP_STANDALONE_CERT:
20-
+ def where():
21-
+ return _PIP_STANDALONE_CERT
22-
+ raise _PipPatchedCertificate()
23-
+
24-
from importlib.resources import path as get_path, read_text
25-
26-
_CACERT_CTX = None
27-
@@ -33,11 +47,13 @@ try:
5+
@@ -33,7 +33,7 @@ def where() -> str:
286
# We also have to hold onto the actual context manager, because
297
# it will do the cleanup whenever it gets garbage collected, so
308
# we will also store that at the global level as well.
319
- _CACERT_CTX = get_path("certifi", "cacert.pem")
3210
+ _CACERT_CTX = get_path("pip._vendor.certifi", "cacert.pem")
3311
_CACERT_PATH = str(_CACERT_CTX.__enter__())
34-
12+
3513
return _CACERT_PATH
36-
37-
+except _PipPatchedCertificate:
38-
+ pass
39-
40-
except ImportError:
41-
Package = Union[types.ModuleType, str]
14+
@@ -65,4 +65,4 @@ def where() -> str:
15+
16+
17+
def contents() -> str:
18+
- return read_text("certifi", "cacert.pem", encoding="ascii")
19+
+ return read_text("pip._vendor.certifi", "cacert.pem", encoding="ascii")

tools/vendoring/patches/requests.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,23 @@ index 8fbcd656..c5e9c19e 100644
123123

124124
try:
125125
import chardet
126+
diff --git a/src/pip/_vendor/requests/certs.py b/src/pip/_vendor/requests/certs.py
127+
index 2743144b9..38696a1fb 100644
128+
--- a/src/pip/_vendor/requests/certs.py
129+
+++ b/src/pip/_vendor/requests/certs.py
130+
@@ -11,7 +11,14 @@
131+
environment, you can change the definition of where() to return a separately
132+
packaged CA bundle.
133+
"""
134+
-from certifi import where
135+
+
136+
+import os
137+
+
138+
+if "_PIP_STANDALONE_CERT" not in os.environ:
139+
+ from certifi import where
140+
+else:
141+
+ def where():
142+
+ return os.environ["_PIP_STANDALONE_CERT"]
143+
144+
if __name__ == "__main__":
145+
print(where())

0 commit comments

Comments
 (0)