| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -641,6 +641,7 @@ PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteord | |||
| 641 | 641 | } else if (strcmp(order, "little") == 0) { | |
| 642 | 642 | little_endian = 1; | |
| 643 | 643 | } else { | |
| 644 | + Py_DECREF(pyint); | ||
| 644 | 645 | PyErr_SetString(PyExc_ValueError, "byteorder must be either 'little' or 'big'"); | |
| 645 | 646 | return NULL; | |
| 646 | 647 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -574,6 +574,7 @@ class int: | |||
| 574 | 574 | pass | |
| 575 | 575 | ||
| 576 | 576 | [case testIntToBytes] | |
| 577 | + import sys | ||
| 577 | 578 | from testutil import assertRaises | |
| 578 | 579 | def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes: | |
| 579 | 580 | return n.to_bytes(length, byteorder, signed=signed) | |
@@ -594,6 +595,19 @@ def test_to_bytes() -> None: | |||
| 594 | 595 | with assertRaises(OverflowError): | |
| 595 | 596 | to_bytes(128, 1, "big", True) | |
| 596 | 597 | ||
| 598 | + def test_invalid_byteorder_refcount() -> None: | ||
| 599 | + n = int("123456789012345678901234567890") | ||
| 600 | + before = sys.getrefcount(n) | ||
| 601 | + for _ in range(10): | ||
| 602 | + with assertRaises(ValueError): | ||
| 603 | + to_bytes(n, 16, "invalid") | ||
| 604 | + assert sys.getrefcount(n) == before | ||
| 605 | + # Keep n alive across both refcount checks. | ||
| 606 | + assert n == int("123456789012345678901234567890") | ||
| 607 | + | ||
| 608 | + [file sys/__init__.pyi] | ||
| 609 | + def getrefcount(n: object) -> int: ... | ||
| 610 | + | ||
| 597 | 611 | [case testBitLength] | |
| 598 | 612 | def bit_length(n: int) -> int: | |
| 599 | 613 | return n.bit_length() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments