From e79dd520e390b48b28c475a32aba8a6550eec499 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 16 Mar 2014 10:01:47 -0700 Subject: [PATCH] Fix ContentFile.decoded_content for Python 3.0 to 3.2 --- github/ContentFile.py | 10 +++++++++- github/tests/ContentFile.py | 11 ++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/github/ContentFile.py b/github/ContentFile.py index 8e7dbb01..5f3ec960 100644 --- a/github/ContentFile.py +++ b/github/ContentFile.py @@ -25,11 +25,15 @@ ################################################################################ import base64 +import sys import github.GithubObject import github.Repository +atLeastPython3 = sys.hexversion >= 0x03000000 + + class ContentFile(github.GithubObject.CompletableGithubObject): """ This class represents ContentFiles as returned for example by http://developer.github.com/v3/todo @@ -46,7 +50,11 @@ class ContentFile(github.GithubObject.CompletableGithubObject): @property def decoded_content(self): assert self.encoding == "base64", "unsupported encoding: %s" % self.encoding - return base64.b64decode(self.content) + if atLeastPython3: + content = bytearray(self.content, "utf-8") # pragma no cover (covered by tests with Python 3.2) + else: + content = self.content + return base64.b64decode(content) @property def encoding(self): diff --git a/github/tests/ContentFile.py b/github/tests/ContentFile.py index 9b1b4c3a..b22f7c4c 100644 --- a/github/tests/ContentFile.py +++ b/github/tests/ContentFile.py @@ -23,16 +23,11 @@ # # ################################################################################ -import base64 -import sys - import Framework import github import datetime -atLeastPython3 = sys.hexversion >= 0x03000000 - class ContentFile(Framework.TestCase): def setUp(self): @@ -45,8 +40,6 @@ class ContentFile(Framework.TestCase): self.assertEqual(self.file.size, 7531) self.assertEqual(self.file.name, "ReadMe.md") self.assertEqual(self.file.path, "ReadMe.md") - if atLeastPython3: - self.assertEqual(len(base64.b64decode(bytearray(self.file.content, "utf-8"))), 7531) # pragma no cover (Covered with Python 3) - else: - self.assertEqual(len(base64.b64decode(self.file.content)), 7531) + self.assertEqual(len(self.file.content), 10212) + self.assertEqual(len(self.file.decoded_content), 7531) self.assertEqual(self.file.sha, "5628799a7d517a4aaa0c1a7004d07569cd154df0")