aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTay Ray Chuan <rctay89@gmail.com>2016-04-20 16:38:19 +0000
committerTay Ray Chuan <rctay89@gmail.com>2016-04-28 14:49:10 +0000
commit3146c92075eda90b6d1eeec62df1972b14954252 (patch)
tree428a0b852269d2a4f36344e164963be615e4e569
parent133b9ff999d46d50756358e7b8e9f0bc039c67dd (diff)
downloadgoogle-api-python-client-3146c92075eda90b6d1eeec62df1972b14954252.tar.gz
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`).
-rw-r--r--googleapiclient/http.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index ed074cb5c..f9d6a557e 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -38,11 +38,18 @@ import mimetypes
import os
import random
import socket
-import ssl
import sys
import time
import uuid
+# TODO(issue 221): Remove this conditional import jibbajabba.
+try:
+ import ssl
+except ImportError:
+ _ssl_SSLError = object()
+else:
+ _ssl_SSLError = ssl.SSLError
+
from email.generator import Generator
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
@@ -146,7 +153,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
exception = None
resp, content = http.request(uri, method, *args, **kwargs)
# Retry on SSL errors and socket timeout errors.
- except ssl.SSLError as ssl_error:
+ except _ssl_SSLError as ssl_error:
exception = ssl_error
except socket.error as socket_error:
# errno's contents differ by platform, so we have to match by name.