Remove more Python version specific code (#1193)

Remove a bunch of other call sites that behaved differently between
Python 2 and 3, massively cleaning up a few messy methods.
This commit is contained in:
Steve Kowalik
2019-08-28 15:24:54 +10:00
committed by GitHub
parent 7bac694ae5
commit a0f01cf9cf
9 changed files with 44 additions and 94 deletions
+3 -1
View File
@@ -30,6 +30,8 @@
# #
################################################################################
import json
class GithubException(Exception):
"""
@@ -59,7 +61,7 @@ class GithubException(Exception):
return self.__data
def __str__(self):
return str(self.status) + " " + str(self.data)
return "{status} {data}".format(status=self.status, data=json.dumps(self.data))
class BadCredentialsException(GithubException):
+7 -9
View File
@@ -33,7 +33,6 @@
################################################################################
from __future__ import absolute_import
import sys
import datetime
from operator import itemgetter
@@ -41,8 +40,6 @@ from . import GithubException
from . import Consts
import six
atLeastPython3 = sys.hexversion >= 0x03000000
class _NotSetType:
def __repr__(self):
@@ -233,13 +230,14 @@ class GithubObject(object):
def format_params(params):
items = list(params.items())
for k, v in sorted(items, key=itemgetter(0), reverse=True):
isText = isinstance(v, (str, six.text_type))
if isText and not atLeastPython3:
v = v.encode('utf-8')
yield '{k}="{v}"'.format(k=k, v=v) if isText else '{k}={v}'.format(k=k, v=v)
return '{class_name}({params})'.format(
if isinstance(v, bytes):
v = v.decode('utf-8')
if isinstance(v, six.text_type):
v = u'"{v}"'.format(v=v)
yield u'{k}={v}'.format(k=k, v=v)
return u'{class_name}({params})'.format(
class_name=self.__class__.__name__,
params=", ".join(list(format_params(params)))
params=u", ".join(list(format_params(params)))
)
+1 -4
View File
@@ -53,7 +53,6 @@ import datetime
import pickle
import time
import sys
import requests
import jwt
import urllib3
@@ -78,8 +77,6 @@ from . import Invitation
from . import Consts
import six
atLeastPython3 = sys.hexversion >= 0x03000000
DEFAULT_BASE_URL = "https://api.github.com"
DEFAULT_STATUS_URL = "https://status.github.com"
# As of 2018-05-17, Github imposes a 10s limit for completion of API requests.
@@ -731,7 +728,7 @@ class GithubIntegration(object):
algorithm="RS256"
)
if atLeastPython3:
if isinstance(encrypted, bytes):
encrypted = encrypted.decode('utf-8')
return encrypted
+11 -24
View File
@@ -86,10 +86,10 @@
################################################################################
from __future__ import absolute_import
import sys
import six.moves.urllib.parse
import datetime
from base64 import b64encode
import collections
import github.GithubObject
import github.PaginatedList
@@ -137,8 +137,6 @@ import github.View
from . import Consts
import six
atLeastPython3 = sys.hexversion >= 0x03000000
class Repository(github.GithubObject.CompletableGithubObject):
"""
@@ -1647,14 +1645,9 @@ class Repository(github.GithubObject.CompletableGithubObject):
or isinstance(committer, github.InputGitAuthor), \
'committer must be a github.InputGitAuthor object'
if atLeastPython3:
if isinstance(content, str):
content = content.encode('utf-8')
content = b64encode(content).decode('utf-8')
else:
if isinstance(content, six.text_type):
content = content.encode('utf-8')
content = b64encode(content)
content = b64encode(bytearray(content, 'utf-8'))
if isinstance(content, bytes):
content = content.decode('utf-8')
put_parameters = {'message': message, 'content': content}
if branch is not github.GithubObject.NotSet:
@@ -1709,14 +1702,9 @@ class Repository(github.GithubObject.CompletableGithubObject):
or isinstance(committer, github.InputGitAuthor), \
'committer must be a github.InputGitAuthor object'
if atLeastPython3:
if isinstance(content, str):
content = content.encode('utf-8')
content = b64encode(content).decode('utf-8')
else:
if isinstance(content, six.text_type):
content = content.encode('utf-8')
content = b64encode(content)
content = b64encode(bytearray(content, 'utf-8'))
if isinstance(content, bytes):
content = content.decode('utf-8')
put_parameters = {'message': message, 'content': content,
'sha': sha}
@@ -2789,11 +2777,10 @@ class Repository(github.GithubObject.CompletableGithubObject):
assert isinstance(callback, (str, six.text_type)), callback
assert secret is github.GithubObject.NotSet or isinstance(secret, (str, six.text_type)), secret
post_parameters = {
"hub.mode": mode,
"hub.topic": "https://github.com/" + self.full_name + "/events/" + event,
"hub.callback": callback,
}
post_parameters = collections.OrderedDict()
post_parameters["hub.callback"] = callback
post_parameters["hub.topic"] = "https://github.com/" + self.full_name + "/events/" + event
post_parameters["hub.mode"] = mode
if secret is not github.GithubObject.NotSet:
post_parameters["hub.secret"] = secret
-5
View File
@@ -60,7 +60,6 @@ import mimetypes
import os
import re
import requests
import sys
import time
import six.moves.urllib.parse
from io import IOBase
@@ -69,8 +68,6 @@ from . import Consts
from . import GithubException
import six
atLeastPython3 = sys.hexversion >= 0x03000000
class RequestsResponse:
# mimic the httplib response object
@@ -458,8 +455,6 @@ class Requester:
def __createConnection(self):
kwds = {}
if not atLeastPython3: # pragma no branch (Branch useful only with Python 3)
kwds["strict"] = True # Useless in Python3, would generate a deprecation warning
kwds["timeout"] = self.__timeout
kwds["verify"] = self.__verify