Skip to content

Commit ded036c

Browse files
committed
Removed compatibility code for Python 2.7 and 3.4
1 parent 65a8105 commit ded036c

21 files changed

+37
-406
lines changed

rsa/_compat.py

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,12 @@
1616

1717
"""Python compatibility wrappers."""
1818

19-
from __future__ import absolute_import
20-
2119
import itertools
2220
import sys
2321
from struct import pack
2422

25-
MAX_INT = sys.maxsize
26-
MAX_INT64 = (1 << 63) - 1
27-
MAX_INT32 = (1 << 31) - 1
28-
MAX_INT16 = (1 << 15) - 1
29-
30-
PY2 = sys.version_info[0] == 2
31-
32-
# Determine the word size of the processor.
33-
if MAX_INT == MAX_INT64:
34-
# 64-bit processor.
35-
MACHINE_WORD_SIZE = 64
36-
elif MAX_INT == MAX_INT32:
37-
# 32-bit processor.
38-
MACHINE_WORD_SIZE = 32
39-
else:
40-
# Else we just assume 64-bit processor keeping up with modern times.
41-
MACHINE_WORD_SIZE = 64
42-
43-
if PY2:
44-
integer_types = (int, long)
45-
range = xrange
46-
zip = itertools.izip
47-
else:
48-
integer_types = (int, )
49-
range = range
50-
zip = zip
51-
52-
53-
def write_to_stdout(data):
54-
"""Writes bytes to stdout
55-
56-
:type data: bytes
57-
"""
58-
if PY2:
59-
sys.stdout.write(data)
60-
else:
61-
# On Py3 we must use the buffer interface to write bytes.
62-
sys.stdout.buffer.write(data)
63-
64-
65-
def is_bytes(obj):
66-
"""
67-
Determines whether the given value is a byte string.
68-
69-
:param obj:
70-
The value to test.
71-
:returns:
72-
``True`` if ``value`` is a byte string; ``False`` otherwise.
73-
"""
74-
return isinstance(obj, bytes)
7523

76-
77-
def is_integer(obj):
78-
"""
79-
Determines whether the given value is an integer.
80-
81-
:param obj:
82-
The value to test.
83-
:returns:
84-
``True`` if ``value`` is an integer; ``False`` otherwise.
85-
"""
86-
return isinstance(obj, integer_types)
87-
88-
89-
def byte(num):
24+
def byte(num):## XXX
9025
"""
9126
Converts a number between 0 and 255 (both inclusive) to a base-256 (byte)
9227
representation.
@@ -117,46 +52,4 @@ def xor_bytes(b1, b2):
11752
:returns:
11853
Bytes object, result of XOR operation.
11954
"""
120-
if PY2:
121-
return ''.join(byte(ord(x) ^ ord(y)) for x, y in zip(b1, b2))
122-
12355
return bytes(x ^ y for x, y in zip(b1, b2))
124-
125-
126-
def get_word_alignment(num, force_arch=64,
127-
_machine_word_size=MACHINE_WORD_SIZE):
128-
"""
129-
Returns alignment details for the given number based on the platform
130-
Python is running on.
131-
132-
:param num:
133-
Unsigned integral number.
134-
:param force_arch:
135-
If you don't want to use 64-bit unsigned chunks, set this to
136-
anything other than 64. 32-bit chunks will be preferred then.
137-
Default 64 will be used when on a 64-bit machine.
138-
:param _machine_word_size:
139-
(Internal) The machine word size used for alignment.
140-
:returns:
141-
4-tuple::
142-
143-
(word_bits, word_bytes,
144-
max_uint, packing_format_type)
145-
"""
146-
max_uint64 = 0xffffffffffffffff
147-
max_uint32 = 0xffffffff
148-
max_uint16 = 0xffff
149-
max_uint8 = 0xff
150-
151-
if force_arch == 64 and _machine_word_size >= 64 and num > max_uint32:
152-
# 64-bit unsigned integer.
153-
return 64, 8, max_uint64, "Q"
154-
elif num > max_uint16:
155-
# 32-bit unsigned integer
156-
return 32, 4, max_uint32, "L"
157-
elif num > max_uint8:
158-
# 16-bit unsigned integer.
159-
return 16, 2, max_uint16, "H"
160-
else:
161-
# 8-bit unsigned integer.
162-
return 8, 1, max_uint8, "B"

rsa/cli.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
These scripts are called by the executables defined in setup.py.
2020
"""
2121

22-
from __future__ import with_statement, print_function
23-
2422
import abc
2523
import sys
2624
from optparse import OptionParser
@@ -83,7 +81,7 @@ def keygen():
8381
outfile.write(data)
8482
else:
8583
print('Writing private key to stdout', file=sys.stderr)
86-
rsa._compat.write_to_stdout(data)
84+
sys.stdout.buffer.write(data)
8785

8886

8987
class CryptoOperation(object):
@@ -189,7 +187,7 @@ def write_outfile(self, outdata, outname):
189187
outfile.write(outdata)
190188
else:
191189
print('Writing output to stdout', file=sys.stderr)
192-
rsa._compat.write_to_stdout(outdata)
190+
sys.stdout.buffer.write(outdata)
193191

194192

195193
class EncryptOperation(CryptoOperation):

rsa/common.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from rsa._compat import zip
18-
1917
"""Common functionality shared by several modules."""
2018

2119

rsa/core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
mathematically on integers.
2121
"""
2222

23-
from rsa._compat import is_integer
24-
2523

2624
def assert_int(var, name):
27-
if is_integer(var):
25+
if isinstance(var, int):
2826
return
2927

3028
raise TypeError('%s should be an integer, not %s' % (name, var.__class__))

rsa/key.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import logging
3737
import warnings
3838

39-
from rsa._compat import range
4039
import rsa.prime
4140
import rsa.pem
4241
import rsa.common

rsa/machine_size.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

rsa/parallel.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@
2424
2525
"""
2626

27-
from __future__ import print_function
28-
2927
import multiprocessing as mp
3028

31-
from rsa._compat import range
3229
import rsa.prime
3330
import rsa.randnum
3431

rsa/pem.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818

1919
import base64
2020

21-
from rsa._compat import is_bytes, range
22-
2321

2422
def _markers(pem_marker):
2523
"""
2624
Returns the start and end PEM markers, as bytes.
2725
"""
2826

29-
if not is_bytes(pem_marker):
27+
if not isinstance(pem_marker, bytes):
3028
pem_marker = pem_marker.encode('ascii')
3129

3230
return (b'-----BEGIN ' + pem_marker + b'-----',
@@ -49,7 +47,7 @@ def load_pem(contents, pem_marker):
4947
"""
5048

5149
# We want bytes, not text. If it's text, it can be converted to ASCII bytes.
52-
if not is_bytes(contents):
50+
if not isinstance(contents, bytes):
5351
contents = contents.encode('ascii')
5452

5553
(pem_start, pem_end) = _markers(pem_marker)

rsa/pkcs1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import hashlib
3232
import os
3333

34-
from rsa._compat import range
3534
from rsa import common, transform, core
3635

3736
# ASN.1 codes that describe the hash algorithm used.

rsa/pkcs1_v2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
documentation is RFC 2437: https://tools.ietf.org/html/rfc2437
2121
"""
2222

23-
from rsa._compat import range
2423
from rsa import (
2524
common,
2625
pkcs1,

0 commit comments

Comments
 (0)