Skip to content

Commit 6705f66

Browse files
committed
Fix Stripe interception in Python 2.
Previously this failed with: AttributeError: 'NoneType' object has no attribute 'path' Seems to be because Python 2 loses all imports when we delete sys.modules['stripe'] to reset it, even for the code still running in that module (!). Tested with 2.7.15+. Now works both with that & Python 3.6.8.
1 parent 2aaa9bb commit 6705f66

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

overrides/pythonpath/stripe.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import sys, os
2-
31
try:
42
import importlib
53
if not hasattr(importlib, 'reload'):
@@ -9,20 +7,24 @@
97
importlib = imp
108

119
def _load_real_stripe():
10+
# Importing this at the top level breaks after deleting sys.modules['stripe'],
11+
# in Python 2. Some interesting internal issues there, but this works in Py 2 & 3.
12+
import sys, os
13+
1214
override_path = os.path.dirname(os.path.abspath(__file__))
1315

1416
original_sys_path = list(sys.path)
17+
1518
sys.path = [p for p in sys.path if p != override_path]
1619
del sys.modules['stripe']
1720
import stripe
18-
sys.path = original_sys_path
1921

20-
def _inject_http_toolkit_certificate():
21-
import stripe
22-
stripe.ca_bundle_path = os.environ['SSL_CERT_FILE']
22+
sys.path = original_sys_path
2323

2424
_load_real_stripe()
25-
_inject_http_toolkit_certificate()
25+
26+
import stripe, os
27+
stripe.ca_bundle_path = os.environ['SSL_CERT_FILE']
2628

2729
# Re-export all public fields from Stripe
2830
from stripe import *

0 commit comments

Comments
 (0)