Skip to content

Commit 3146c92

Browse files
committed
googleapiclient.http: guard when importing ssl
Fix #191 by adding a guard when importing `ssl`, and replace all direct references to `SSLError` (the sole member of `ssl` being used) with a shim, `_ssl_SSLError`. It is prefixed with a '_' to caution users against relying on the name when importing `googleclient.http`. `httplib2` also takes a similar approach (see `ssl_SSLError` in `httplib2/__init__.py`).
1 parent 133b9ff commit 3146c92

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

googleapiclient/http.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@
3838
import os
3939
import random
4040
import socket
41-
import ssl
4241
import sys
4342
import time
4443
import uuid
4544

45+
# TODO(issue 221): Remove this conditional import jibbajabba.
46+
try:
47+
import ssl
48+
except ImportError:
49+
_ssl_SSLError = object()
50+
else:
51+
_ssl_SSLError = ssl.SSLError
52+
4653
from email.generator import Generator
4754
from email.mime.multipart import MIMEMultipart
4855
from email.mime.nonmultipart import MIMENonMultipart
@@ -146,7 +153,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
146153
exception = None
147154
resp, content = http.request(uri, method, *args, **kwargs)
148155
# Retry on SSL errors and socket timeout errors.
149-
except ssl.SSLError as ssl_error:
156+
except _ssl_SSLError as ssl_error:
150157
exception = ssl_error
151158
except socket.error as socket_error:
152159
# errno's contents differ by platform, so we have to match by name.

0 commit comments

Comments
 (0)