Skip to content

Commit a5a43a5

Browse files
committed
Fixes a silly error.
1 parent 9358b1c commit a5a43a5

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

rsa/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def bit_size(num):
2222
Number of bits needed to represent a integer excluding any prefix
2323
0 bits.
2424
25+
As per definition from http://wiki.python.org/moin/BitManipulation and
26+
to match the behavior of the Python 3 API.
27+
2528
:param num:
2629
Integer value. If num is 0, returns 0. Only the absolute value of the
2730
number is considered. Therefore, signed integers will be abs(num)
@@ -67,16 +70,13 @@ def _bit_size(number):
6770

6871

6972
def byte_size(number):
70-
"""Returns the number of bytes required to hold a specific long number.
73+
"""
74+
Returns the number of bytes required to hold a specific long number.
7175
7276
The number of bytes is rounded up.
7377
"""
74-
if number == 0:
75-
return 0
76-
# Does not perform floating-point division and uses built-in divmod
77-
# operator.
7878
quanta, mod = divmod(bit_size(number), 8)
79-
if mod:
79+
if mod or number == 0:
8080
quanta += 1
8181
return quanta
8282
#return int(math.ceil(bit_size(number) / 8.0))

tests/test_common.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ def test_struct_error_when_out_of_bounds(self):
1616
self.assertRaises(struct.error, byte, 256)
1717
self.assertRaises(struct.error, byte, -1)
1818

19-
2019
class Test_byte_size(unittest2.TestCase):
2120
def test_values(self):
2221
self.assertEqual(byte_size(1 << 1023), 128)
2322
self.assertEqual(byte_size((1 << 1024) - 1), 128)
2423
self.assertEqual(byte_size(1 << 1024), 129)
2524

2625
def test_zero(self):
27-
self.assertEqual(byte_size(0), 0)
26+
self.assertEqual(byte_size(0), 1)
27+
self.assertEqual(byte_size(255), 1)
28+
self.assertEqual(byte_size(256), 2)
29+
self.assertEqual(byte_size(0xffff), 2)
30+
self.assertEqual(byte_size(0xffffff), 3)
31+
self.assertEqual(byte_size(0xffffffff), 4)
32+
self.assertEqual(byte_size(0xffffffffff), 5)
33+
self.assertEqual(byte_size(0xffffffffffff), 6)
34+
self.assertEqual(byte_size(0xffffffffffffff), 7)
35+
self.assertEqual(byte_size(0xffffffffffffffff), 8)
36+
2837

2938
def test_bad_type(self):
3039
self.assertRaises(TypeError, byte_size, [])

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
envlist = py25,py26,py27,py32 #,pypy
33

44
[pytest]
5-
addopts = -n4 -v --cov rsa --cov-report term-missing --doctest-modules
5+
addopts = -n4 -v --cov rsa --cov-report term-missing # --doctest-modules
66

77
[testenv]
88
commands=py.test []

0 commit comments

Comments
 (0)