Commits

Sybren Stüvel  committed 389e0b3

Fixed compatibilty of pyrsa-priv2pub with Python 3

  • Participants
  • Parent commits 1c605c1

Comments (0)

Files changed (1)

 
 '''Utility functions.'''
 
-from __future__ import with_statement
+from __future__ import with_statement, print_function
 
 import sys
 from optparse import OptionParser
 
     # Read the input data
     if cli.infilename:
-        print >>sys.stderr, 'Reading private key from %s in %s format' % \
-            (cli.infilename, cli.inform)
-        with open(cli.infilename) as infile:
+        print('Reading private key from %s in %s format' % \
+            (cli.infilename, cli.inform), file=sys.stderr)
+        with open(cli.infilename, 'rb') as infile:
             in_data = infile.read()
     else:
-        print >>sys.stderr, 'Reading private key from stdin in %s format' % \
-            cli.inform
-        in_data = sys.stdin.read()
+        print('Reading private key from stdin in %s format' % cli.inform,
+              file=sys.stderr)
+        in_data = sys.stdin.read().encode('ascii')
+
+    assert type(in_data) == bytes, type(in_data)
 
 
     # Take the public fields and create a public key
     out_data = pub_key.save_pkcs1(cli.outform)
 
     if cli.outfilename:
-        print >>sys.stderr, 'Writing public key to %s in %s format' % \
-            (cli.outfilename, cli.outform)
-        with open(cli.outfilename, 'w') as outfile:
+        print('Writing public key to %s in %s format' % \
+            (cli.outfilename, cli.outform), file=sys.stderr)
+        with open(cli.outfilename, 'wb') as outfile:
             outfile.write(out_data)
     else:
-        print >>sys.stderr, 'Writing public key to stdout in %s format' % \
-            cli.outform
-        sys.stdout.write(out_data)
+        print('Writing public key to stdout in %s format' % cli.outform,
+              file=sys.stderr)
+        sys.stdout.write(out_data.decode('ascii'))