From 5b7f0bb6a862725102b48211d91d8a54ff992f8f Mon Sep 17 00:00:00 2001 From: Enix Yu Date: Sat, 16 Jan 2016 16:11:34 +0800 Subject: [PATCH] fix python3 compatibility issue for using json/base64 --- github/Repository.py | 25 +++++++++++++++++-------- github/tests/Repository.py | 4 ++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/github/Repository.py b/github/Repository.py index 07272179..2f43bd78 100644 --- a/github/Repository.py +++ b/github/Repository.py @@ -29,7 +29,7 @@ # along with PyGithub. If not, see . # # # # ############################################################################## - +import sys import urllib import datetime from base64 import b64encode @@ -71,6 +71,8 @@ import github.StatsParticipation import github.StatsPunchCard import github.Stargazer +atLeastPython26 = sys.hexversion >= 0x02060000 +atLeastPython3 = sys.hexversion >= 0x03000000 class Repository(github.GithubObject.CompletableGithubObject): """ @@ -1225,7 +1227,7 @@ class Repository(github.GithubObject.CompletableGithubObject): :calls: `PUT /repos/:owner/:repo/contents/:path `_ :param path: string, (required), path of the file in the repository :param message: string, (required), commit message - :param content: bytes, (required), the actual data in the file + :param content: string, (required), the actual data in the file :param branch: string, (optional), branch to create the commit on. Defaults to the default branch of the repository :param committer: dict, (optional), if no information is given the authenticated user's information will be used. You must specify both a name and email. :param author: dict, (optional), if omitted this will be filled in with committer information. If passed, you must specify both a name and email. @@ -1237,8 +1239,8 @@ class Repository(github.GithubObject.CompletableGithubObject): 'path must be str/unicode object' assert isinstance(message, (str, unicode)), \ 'message must be str/unicode object' - assert isinstance(content, bytes), \ - 'content must be a byte object' + assert isinstance(content, (str, unicode)), \ + 'content must be a str/unicode object' assert branch is github.GithubObject.NotSet \ or isinstance(branch, (str, unicode)), \ 'branch must be a str/unicode object' @@ -1249,7 +1251,10 @@ class Repository(github.GithubObject.CompletableGithubObject): or isinstance(committer, github.InputGitAuthor), \ 'committer must be a github.InputGitAuthor object' - content = b64encode(content) + if atLeastPython3: + content = b64encode(content.encode('utf-8')).decode('utf-8') + else: + content = b64encode(content) put_parameters = {'message': message, 'content': content} if branch is not github.GithubObject.NotSet: @@ -1286,8 +1291,8 @@ class Repository(github.GithubObject.CompletableGithubObject): 'path must be str/unicode object' assert isinstance(message, (str, unicode)), \ 'message must be str/unicode object' - assert isinstance(content, bytes), \ - 'content must be a byte object' + assert isinstance(content, (str, unicode)), \ + 'content must be a str/unicode object' assert isinstance(sha, (str, unicode)), \ 'sha must be a str/unicode object' assert branch is github.GithubObject.NotSet \ @@ -1300,7 +1305,11 @@ class Repository(github.GithubObject.CompletableGithubObject): or isinstance(committer, github.InputGitAuthor), \ 'committer must be a github.InputGitAuthor object' - content = b64encode(content) + if atLeastPython3: + content = b64encode(content.encode('utf-8')).decode('utf-8') + else: + content = b64encode(content) + put_parameters = {'message': message, 'content': content, 'sha': sha} diff --git a/github/tests/Repository.py b/github/tests/Repository.py index 918fb445..1c3752a3 100644 --- a/github/tests/Repository.py +++ b/github/tests/Repository.py @@ -414,7 +414,7 @@ class Repository(Framework.TestCase): def testCreateFile(self): newFile = '/doc/testCreateUpdateDeleteFile.md' - content = bytes('Hello world'.encode('utf-8')) + content = 'Hello world' self.repo.create_file( path=newFile, message='Create file for testCreateFile', content=content, branch="master", committer=github.InputGitAuthor("Enix Yu", "enix223@163.com", "2016-01-15T16:13:30+12:00"), @@ -422,7 +422,7 @@ class Repository(Framework.TestCase): def testUpdateFile(self): updateFile = '/doc/testCreateUpdateDeleteFile.md' - content = bytes('Hello World'.encode('utf-8')) + content = 'Hello World' sha = self.repo.get_contents(updateFile).sha self.repo.update_file( path=updateFile, message='Update file for testUpdateFile', content=content, sha=sha,