]> granicus.if.org Git - python/commitdiff
Issue #4769: Fix main() function of the base64 module, use sys.stdin.buffer and
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 25 May 2010 21:12:34 +0000 (21:12 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 25 May 2010 21:12:34 +0000 (21:12 +0000)
sys.stdout.buffer (instead of sys.stdin and sys.stdout) to use the bytes API

Lib/base64.py
Lib/test/test_base64.py
Misc/NEWS

index a8d9f21136f9d1a59f2b35fbf76e5c6d55e5ca7f..20ed67dc1acad56573b64180e897381e4007a45d 100755 (executable)
@@ -383,9 +383,9 @@ def main():
         if o == '-u': func = decode
         if o == '-t': test(); return
     if args and args[0] != '-':
-        func(open(args[0], 'rb'), sys.stdout)
+        func(open(args[0], 'rb'), sys.stdout.buffer)
     else:
-        func(sys.stdin, sys.stdout)
+        func(sys.stdin.buffer, sys.stdout.buffer)
 
 
 def test():
index fadee6df6789cab506c021ef4f4945e37abb2ff6..7ff0db5cd04ca13b88edee4eb862f178a941f254 100644 (file)
@@ -2,6 +2,8 @@ import unittest
 from test import support
 import base64
 import binascii
+import sys
+import subprocess
 
 
 \f
@@ -207,6 +209,38 @@ class BaseXYTestCase(unittest.TestCase):
         self.assertTrue(issubclass(binascii.Error, ValueError))
 
 
+\f
+class TestMain(unittest.TestCase):
+    def get_output(self, *args, **options):
+        args = (sys.executable, '-m', 'base64') + args
+        return subprocess.check_output(args, **options)
+
+    def test_encode_decode(self):
+        output = self.get_output('-t')
+        self.assertSequenceEqual(output.splitlines(), (
+            b"b'Aladdin:open sesame'",
+            br"b'QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n'",
+            b"b'Aladdin:open sesame'",
+        ))
+
+    def test_encode_file(self):
+        with open(support.TESTFN, 'wb') as fp:
+            fp.write(b'a\xffb\n')
+
+        output = self.get_output('-e', support.TESTFN)
+        self.assertEquals(output.rstrip(), b'Yf9iCg==')
+
+        with open(support.TESTFN, 'rb') as fp:
+            output = self.get_output('-e', stdin=fp)
+        self.assertEquals(output.rstrip(), b'Yf9iCg==')
+
+    def test_decode(self):
+        with open(support.TESTFN, 'wb') as fp:
+            fp.write(b'Yf9iCg==')
+        output = self.get_output('-d', support.TESTFN)
+        self.assertEquals(output, b'a\xffb\n')
+
+
 \f
 def test_main():
     support.run_unittest(__name__)
index ecf5744ebc7cbe813f1592597742c0ae2d8751d7..5d20ca43c5c726acccaf5a297b69b6ad7585987f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -392,6 +392,10 @@ C-API
 Library
 -------
 
+- Issue #4769: Fix main() function of the base64 module, use sys.stdin.buffer
+  and sys.stdout.buffer (instead of sys.stdin and sys.stdout) to use the bytes
+  API
+
 - Issue #8770: now sysconfig displays information when it's called as
   a script. Initial idea by Sridhar Ratnakumar.