summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGautam Chaudhuri <gautam.chaudhuri.1803@gmail.com>2021-08-15 11:29:05 (GMT)
committerGitHub <noreply@github.com>2021-08-15 11:29:05 (GMT)
commitad0a8a9c629a7a0fa306fbdf019be63c701a8028 (patch)
treeb9a3bb5d459ffa6934ba402c5c121448afebed64 /Lib
parented524b4569b1e4a166886c880018418d46284378 (diff)
downloadcpython-ad0a8a9c629a7a0fa306fbdf019be63c701a8028.zip
cpython-ad0a8a9c629a7a0fa306fbdf019be63c701a8028.tar.gz
cpython-ad0a8a9c629a7a0fa306fbdf019be63c701a8028.tar.bz2
bpo-16580: [doc] Add examples to int.to_bytes and int.from_bytes (GH-27760)
* added code equivs. for to_bytes and from_bytes Based on woparry's patch[1] from the relevant issue thread[2]. [1]: https://bugs.python.org/file30372/issue16580.patch [2]: https://bugs.python.org/issue16580 Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_long.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index c97842b..1de75bf 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -1103,6 +1103,13 @@ class LongTest(unittest.TestCase):
def test_to_bytes(self):
def check(tests, byteorder, signed=False):
+ def equivalent_python(n, length, byteorder, signed=False):
+ if byteorder == 'little':
+ order = range(length)
+ elif byteorder == 'big':
+ order = reversed(range(length))
+ return bytes((n >> i*8) & 0xff for i in order)
+
for test, expected in tests.items():
try:
self.assertEqual(
@@ -1113,6 +1120,18 @@ class LongTest(unittest.TestCase):
"failed to convert {0} with byteorder={1} and signed={2}"
.format(test, byteorder, signed)) from err
+ try:
+ self.assertEqual(
+ equivalent_python(
+ test, len(expected), byteorder, signed=signed),
+ expected
+ )
+ except Exception as err:
+ raise AssertionError(
+ "Code equivalent from docs is not equivalent for "
+ "conversion of {0} with byteorder byteorder={1} and "
+ "signed={2}".format(test, byteorder, signed)) from err
+
# Convert integers to signed big-endian byte arrays.
tests1 = {
0: b'\x00',
@@ -1202,6 +1221,18 @@ class LongTest(unittest.TestCase):
def test_from_bytes(self):
def check(tests, byteorder, signed=False):
+ def equivalent_python(byte_array, byteorder, signed=False):
+ if byteorder == 'little':
+ little_ordered = list(byte_array)
+ elif byteorder == 'big':
+ little_ordered = list(reversed(byte_array))
+
+ n = sum(b << i*8 for i, b in enumerate(little_ordered))
+ if signed and little_ordered and (little_ordered[-1] & 0x80):
+ n -= 1 << 8*len(little_ordered)
+
+ return n
+
for test, expected in tests.items():
try:
self.assertEqual(
@@ -1212,6 +1243,17 @@ class LongTest(unittest.TestCase):
"failed to convert {0} with byteorder={1!r} and signed={2}"
.format(test, byteorder, signed)) from err
+ try:
+ self.assertEqual(
+ equivalent_python(test, byteorder, signed=signed),
+ expected
+ )
+ except Exception as err:
+ raise AssertionError(
+ "Code equivalent from docs is not equivalent for "
+ "conversion of {0} with byteorder={1!r} and signed={2}"
+ .format(test, byteorder, signed)) from err
+
# Convert signed big-endian byte arrays to integers.
tests1 = {
b'': 0,