Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SG-29997 Removing Python 2 code #345

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions shotgun_api3/lib/httplib2/__init__.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to edit httplib2. This is third-party we bundle. But can we update to a newer version if we drop py2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it was manually updated by #202 so we might want to clean up (or simply update to a newer version)

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from .. import six

# Define all here to keep linters happy. It should be overwritten by the code
# below, but if in the future __all__ is not defined in httplib2 this will keep
# things from breaking.
Expand All @@ -9,24 +7,13 @@
# current python version. httplib2 supports python 2/3 by forking the code rather
# than with a single cross-compatible module. Rather than modify third party code,
# we'll just import the appropriate branch here.
if six.PY3:
# Generate ssl_error_classes
import ssl as __ssl
ssl_error_classes = (__ssl.SSLError, __ssl.CertificateError)
del __ssl

# get the python3 fork of httplib2
from . import python3 as __httplib2_compat


else:
# Generate ssl_error_classes
from .python2 import SSLHandshakeError as __SSLHandshakeError # TODO: shouldn't rely on this. not public
ssl_error_classes = (__SSLHandshakeError,)
del __SSLHandshakeError
# Generate ssl_error_classes
import ssl as __ssl
ssl_error_classes = (__ssl.SSLError, __ssl.CertificateError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep this variable or can we just replace the code where ssl_error_classes is used by __ssl.SSLError, __ssl.CertificateError?

del __ssl

# get the python2 fork of httplib2
from . import python2 as __httplib2_compat
# get the python3 fork of httplib2
from . import python3 as __httplib2_compat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have a "python3" folder in there? Can we simplify how we bundle httplib2 in this repo?


# Import all of the httplib2 module. Note that we can't use a star import because
# we need to import *everything*, not just what exists in __all__.
Expand Down
70 changes: 53 additions & 17 deletions shotgun_api3/lib/sgsix.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,24 @@
"""

# This module contains addtional functions and variables to supplement the six
# module for python 2/3 compatibility.
# module.

from . import six
import io
import sys

# For python 3, the `file` type no longer exists, and open() returns an
# io.IOBase instance. We add file_types to allow comparison across python
# versions. See https://stackoverflow.com/questions/36321030#36321030
#
# This means that to test if a variable contains a file in both Python 2 and 3
# you can use an isinstance test like:
# isinstance(value, sgsix.file_types)
if six.PY3:
file_types = (io.IOBase, )
else:
file_types = (file, io.IOBase) # noqa warning for undefined `file` in python 3
# This means that to test if a variable contains a file in Python3

file_types = (io.IOBase, )

# For python-api calls that result in an SSL error, the exception raised is
# different on Python 2 and 3. Store the approriate exception class in a
# variable to allow easier exception handling across Python 2/3.
if six.PY3:
import ssl
ShotgunSSLError = ssl.SSLError
else:
from .httplib2 import SSLHandshakeError
ShotgunSSLError = SSLHandshakeError
# different on Python 3. Store the approriate exception class in a
# variable to allow easier exception handling across Python 3.
import ssl
ShotgunSSLError = ssl.SSLError


def normalize_platform(platform, python2=True):
Expand Down Expand Up @@ -85,3 +76,48 @@ def normalize_platform(platform, python2=True):
# sgsix.platform will mimick the python2 sys.platform behavior to ensure
# compatibility with existing comparisons and dict keys.
platform = normalize_platform(sys.platform)


def ensure_binary(s, encoding='utf-8', errors='strict'):
"""
Coerce **s** to bytes.

For Python 3:
- `str` -> encoded to `bytes`
- `bytes` -> `bytes`
"""
if isinstance(s, str):
return s.encode(encoding, errors)
elif isinstance(s, bytes):
return s
else:
raise TypeError(f"not expecting type '{type(s)}'")


def ensure_text(s, encoding='utf-8', errors='strict'):
"""Coerce *s* to str.

For Python 3:
- `str` -> `str`
- `bytes` -> decoded to `str`
"""
if isinstance(s, bytes):
return s.decode(encoding, errors)
elif isinstance(s, str):
return s
else:
raise TypeError(f"not expecting type '{type(s)}'")


def ensure_str(s, encoding='utf-8', errors='strict'):
"""Coerce *s* to `str`.

For Python 3:
- `str` -> `str`
- `bytes` -> decoded to `str`
"""
if not isinstance(s, (str, bytes)):
raise TypeError(f"not expecting type '{type(s)}'")
if isinstance(s, bytes):
s = s.decode(encoding, errors)
return s
Loading