Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.

Commit 45622ad

Browse files
committed
Create PY2 constant to simplify compatibility decisions
1 parent 9f57740 commit 45622ad

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

rsa/_compat.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
MAX_INT32 = (1 << 31) - 1
2727
MAX_INT16 = (1 << 15) - 1
2828

29+
PY2 = sys.version_info[0] == 2
30+
2931
# Determine the word size of the processor.
3032
if MAX_INT == MAX_INT64:
3133
# 64-bit processor.
@@ -37,19 +39,20 @@
3739
# Else we just assume 64-bit processor keeping up with modern times.
3840
MACHINE_WORD_SIZE = 64
3941

40-
# Range generator.
41-
try:
42-
# < Python3
42+
if PY2:
43+
integer_types = (int, long)
4344
range = xrange
44-
except NameError:
45-
# Python3
45+
else:
46+
integer_types = (int, )
4647
range = range
4748

48-
# ``long`` is no more. Do type detection using this instead.
49-
try:
50-
integer_types = (int, long)
51-
except NameError:
52-
integer_types = (int,)
49+
50+
def write_to_stdout(data):
51+
if PY2:
52+
sys.stdout.write(data)
53+
else:
54+
# On Py3 we must use the buffer interface to write bytes.
55+
sys.stdout.buffer.write(data)
5356

5457

5558
def is_bytes(obj):

rsa/cli.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ def keygen():
8383
outfile.write(data)
8484
else:
8585
print('Writing private key to stdout', file=sys.stderr)
86-
if sys.version_info[0] >= 3:
87-
# on Py3 we must use the buffer interface to write bytes.
88-
sys.stdout.buffer.write(data)
89-
else:
90-
sys.stdout.write(data)
86+
rsa._compat.write_to_stdout(data)
9187

9288

9389
class CryptoOperation(object):
@@ -193,11 +189,7 @@ def write_outfile(self, outdata, outname):
193189
outfile.write(outdata)
194190
else:
195191
print('Writing output to stdout', file=sys.stderr)
196-
if sys.version_info[0] >= 3:
197-
# on Py3 we must use the buffer interface to write bytes.
198-
sys.stdout.buffer.write(outdata)
199-
else:
200-
sys.stdout.write(outdata)
192+
rsa._compat.write_to_stdout(outdata)
201193

202194

203195
class EncryptOperation(CryptoOperation):

tests/test_cli.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@
1515
import rsa
1616
import rsa.cli
1717
import rsa.util
18+
from rsa._compat import PY2
1819

19-
if sys.version_info[0] < 3:
20-
def make_buffer():
20+
21+
def make_buffer():
22+
if PY2:
2123
return BytesIO()
24+
buf = StringIO()
25+
buf.buffer = BytesIO()
26+
return buf
2227

2328

24-
def get_bytes_out(out):
25-
# Python 2.x writes 'str' to stdout:
29+
def get_bytes_out(out):
30+
if PY2:
31+
# Python 2.x writes 'str' to stdout
2632
return out.getvalue()
27-
else:
28-
def make_buffer():
29-
buf = StringIO()
30-
buf.buffer = BytesIO()
31-
return buf
32-
33-
34-
def get_bytes_out(out):
35-
# Python 3.x writes 'bytes' to stdout.buffer:
36-
return out.buffer.getvalue()
33+
# Python 3.x writes 'bytes' to stdout.buffer
34+
return out.buffer.getvalue()
3735

3836

3937
@contextmanager

tests/test_compat.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import unittest
1818
import struct
19-
import sys
2019

2120
from rsa._compat import byte, is_bytes, range
2221

0 commit comments

Comments
 (0)