summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
Commit message (Collapse)AuthorAgeFilesLines
* gh-120417: Remove unused imports in tests (part 3) (#120631)Victor Stinner2024-06-171-1/+0
|
* gh-120080: Mark test_round_with_none_arg_direct_call as cpython_only (#120328)Kirill Podoprigora2024-06-111-0/+1
|
* gh-120080: Accept ``None`` as a valid argument for direct call of the ↵Kirill Podoprigora2024-06-071-0/+6
| | | | | ``int.__round__`` (#120088) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
* gh-119740: Remove deprecated trunc delegation (#119743)Mark Dickinson2024-06-021-94/+2
| | | | | | | | Remove the delegation of `int` to the `__trunc__` special method: `int` will now only delegate to `__int__` and `__index__` (in that order). `__trunc__` continues to exist, but its sole purpose is to support `math.trunc`. --------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* Try to repair oddball test bots timing out in test_int (#119166)Tim Peters2024-05-191-0/+8
| | | Various test bots (outside the ones GH normally runs) are timing out during test_int after ecd8664 (asymptotically faster str->int). Best guess is that they don't build the C _decimal module. So require that module in the most likely tests to time out then. Flying mostly blind, though!
* gh-118750: Asymptotically faster `int(string)` (#118751)Tim Peters2024-05-191-0/+79
| | | | | | | | | | | | | | | Asymptotically faster (O(n log n)) str->int for very large strings, leveraging the faster multiplication scheme in the C-coded `_decimal` when available. This is used instead of the current Karatsuba-limited method starting at 2 million digits. Lots of opportunity remains for fine-tuning. Good targets include changing BYTELIM, and possibly changing the internal output base (from 256 to a higher number of bytes). Doing this was substantial work, and many of the new lines are actually comments giving correctness proofs. The obvious approaches sticking to integers were too slow to be useful, so this is doing variable-precision decimal floating-point arithmetic. Much faster, but worst-possible rounding errors have to be wholly accounted for, using as little precision as possible. Special thanks to Serhiy Storchaka for asking many good questions in his code reviews! Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: sstandre <43125375+sstandre@users.noreply.github.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
* gh-118610: Centralize power caching in `_pylong.py` (#118611)Tim Peters2024-05-081-0/+12
| | | | | A new `compute_powers()` function computes all and only the powers of the base the various base-conversion functions need, as efficiently as reasonably possible (turns out that invoking `**`is needed at most once). This typically gives a few % speedup, but the primary point is to simplify the base-conversion functions, which no longer need their own, ad hoc, and less efficient power-caching schemes. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-118164: Break a loop between _pydecimal and _pylong and optimize int to ↵Serhiy Storchaka2024-05-051-10/+21
| | | | | | | | | | | | | | | | | | | | str conversion (GH-118483) For converting large ints to strings, CPython invokes a function in _pylong.py, which uses the decimal module to implement an asymptotically waaaaay sub-quadratic algorithm. But if the C decimal module isn't available, CPython uses _pydecimal.py instead. Which in turn frequently does str(int). If the int is very large, _pylong ends up doing the work, which in turn asks decimal to do "big" arithmetic, which in turn calls str(big_int), which in turn ... it can become infinite mutual recursion. This change introduces a different int->str function that doesn't use decimal. It's asymptotically worse, "Karatsuba time" instead of quadratic time, so still a huge improvement. _pylong switches to that when the C decimal isn't available. It is also used for not too large integers (less than 450_000 bits), where it is faster (up to 2 times for 30_000 bits) than the asymptotically better implementation that uses the C decimal. Co-authored-by: Tim Peters <tim.peters@gmail.com>
* gh-114911: Add CPUStopwatch test helper (GH-114912)Petr Viktorin2024-02-281-32/+26
| | | | | | A few of our tests measure the time of CPU-bound operation, mainly to avoid quadratic or worse behaviour. Add a helper to ignore GC and time spent in other processes.
* gh-114050: Fix crash when more than two arguments are passed to int() ↵kcatss2024-01-181-0/+1
| | | | | (GH-114067) Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
* Improve int test coverage (#104024)Shantanu2023-05-011-0/+20
| | | | | | | Following discussion in https://discuss.python.org/t/bug-in-int-42/26360/5 This tests some of the things documented in https://github.com/python/cpython/pull/100436 Co-authored-by: Gregory P. Smith <greg@krypto.org>
* gh-90716: bugfixes and more tests for _pylong. (#99073)Gregory P. Smith2022-11-031-0/+39
| | | | | | | | | * Properly decref on _pylong import error. * Improve the error message on _pylong TypeError. * Fix the assertion error in pydebug builds to be a TypeError. * Tie the return value comments together. These are minor followups to issues not caught among the reviewers on https://github.com/python/cpython/pull/96673.
* gh-90716: add _pylong.py module (#96673)Neil Schemenauer2022-10-261-0/+47
| | | | | | | | | | | | Add Python implementations of certain longobject.c functions. These use asymptotically faster algorithms that can be used for operations on integers with many digits. In those cases, the performance overhead of the Python implementation is not significant since the asymptotic behavior is what dominates runtime. Functions provided by this module should be considered private and not part of any public API. Co-author: Tim Peters <tim.peters@gmail.com> Co-author: Mark Dickinson <dickinsm@gmail.com> Co-author: Bjorn Martinsson
* gh-96512: Move int_max_str_digits setting to PyConfig (#96944)Gregory P. Smith2022-10-031-0/+20
| | | | | | | | | | | It had to live as a global outside of PyConfig for stable ABI reasons in the pre-3.12 backports. This removes the `_Py_global_config_int_max_str_digits` and gets rid of the equivalent field in the internal `struct _is PyInterpreterState` as code can just use the existing nested config struct within that. Adds tests to verify unique settings and configs in subinterpreters.
* gh-96710: Make the test timing more lenient for the int/str DoS regression ↵Gregory P. Smith2022-09-091-6/+8
| | | | | | | | | test. (#96717) A regression would still absolutely fail and even a flaky pass isn't harmful as it'd fail most of the time across our N system test runs. Windows has a low resolution timer and CI systems are prone to odd timing so this just gives more leeway to avoid flakiness.
* gh-95778: Correctly pre-check for int-to-str conversion (#96537)Mark Dickinson2022-09-041-0/+82
| | | | | | | | | | | | | | | | | | | | | | | | | | | Converting a large enough `int` to a decimal string raises `ValueError` as expected. However, the raise comes _after_ the quadratic-time base-conversion algorithm has run to completion. For effective DOS prevention, we need some kind of check before entering the quadratic-time loop. Oops! =) The quick fix: essentially we catch _most_ values that exceed the threshold up front. Those that slip through will still be on the small side (read: sufficiently fast), and will get caught by the existing check so that the limit remains exact. The justification for the current check. The C code check is: ```c max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10 ``` In GitHub markdown math-speak, writing $M$ for `max_str_digits`, $L$ for `PyLong_SHIFT` and $s$ for `size_a`, that check is: $$\left\lfloor\frac{M}{3L}\right\rfloor \le \left\lfloor\frac{s - 11}{10}\right\rfloor$$ From this it follows that $$\frac{M}{3L} < \frac{s-1}{10}$$ hence that $$\frac{L(s-1)}{M} > \frac{10}{3} > \log_2(10).$$ So $$2^{L(s-1)} > 10^M.$$ But our input integer $a$ satisfies $|a| \ge 2^{L(s-1)}$, so $|a|$ is larger than $10^M$. This shows that we don't accidentally capture anything _below_ the intended limit in the check. <!-- gh-issue-number: gh-95778 --> * Issue: gh-95778 <!-- /gh-issue-number --> Co-authored-by: Gregory P. Smith [Google LLC] <greg@krypto.org>
* gh-95778: CVE-2020-10735: Prevent DoS by very large int() (#96499)Gregory P. Smith2022-09-021-0/+114
| | | | | | | | | | | | | | | | Integer to and from text conversions via CPython's bignum `int` type is not safe against denial of service attacks due to malicious input. Very large input strings with hundred thousands of digits can consume several CPU seconds. This PR comes fresh from a pile of work done in our private PSRT security response team repo. Signed-off-by: Christian Heimes [Red Hat] <christian@python.org> Tons-of-polishing-up-by: Gregory P. Smith [Google] <greg@krypto.org> Reviews via the private PSRT repo via many others (see the NEWS entry in the PR). <!-- gh-issue-number: gh-95778 --> * Issue: gh-95778 <!-- /gh-issue-number --> I wrote up [a one pager for the release managers](https://docs.google.com/document/d/1KjuF_aXlzPUxTK4BMgezGJ2Pn7uevfX7g0_mvgHlL7Y/edit#). Much of that text wound up in the Issue. Backports PRs already exist. See the issue for links.
* bpo-44977: Deprecate delegation of int to __trunc__ (GH-31031)Zackery Spytz2022-02-031-9/+18
| | | | | Calling int(a) when type(a) implements __trunc__ but not __int__ or __index__ now raises a DeprecationWarning.
* bpo-37999: No longer use __int__ in implicit integer conversions. (GH-15636)Serhiy Storchaka2020-05-261-4/+1
| | | | Only __index__ should be used to make integer conversions lossless.
* bpo-20092. Use __index__ in constructors of int, float and complex. (GH-13108)Serhiy Storchaka2019-06-011-6/+60
|
* bpo-36048: Use __index__() instead of __int__() for implicit conversion if ↵Serhiy Storchaka2019-02-251-1/+2
| | | | | | available. (GH-11952) Deprecate using the __int__() method in implicit conversions of Python numbers to C integers.
* bpo-31619: Fixed a ValueError when convert a string with large number of ↵Serhiy Storchaka2017-10-031-0/+8
| | | | | underscores (#3827) to integer with binary base.
* bpo-29695: Remove bad keyword parameters in int(), bool(), float(), list() ↵Serhiy Storchaka2017-03-061-4/+4
| | | | and tuple(). (#518)
* bpo-29695: Deprecated using bad named keyword arguments in builtings: (#486)Serhiy Storchaka2017-03-051-2/+4
| | | | int(), bool(), float(), list() and tuple(). Specify the value as a positional argument instead.
* Issue #26331: Implement the parsing part of PEP 515.Brett Cannon2016-09-091-0/+21
| | | | Thanks to Georg Brandl for the patch.
* Issue #26984: int() now always returns an instance of exact int.Serhiy Storchaka2016-08-211-1/+4
|
* Issue #24731: Fixed crash on converting objects with special methodsSerhiy Storchaka2015-11-251-0/+7
|\ | | | | | | | | __bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly.
| * Issue #24731: Fixed crash on converting objects with special methodsSerhiy Storchaka2015-11-251-0/+7
| | | | | | | | | | __bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly.
* | Issue #24802: Merge null termination fixes from 3.4 into 3.5Martin Panter2015-11-071-10/+34
|\ \ | |/
| * Issue #24802: Copy bytes-like objects to null-terminated buffers if necessaryMartin Panter2015-11-071-10/+34
| | | | | | | | | | | | | | | | This avoids possible buffer overreads when int(), float(), compile(), exec() and eval() are passed bytes-like objects. Similar code is removed from the complex() constructor, where it was not reachable. Patch by John Leitch, Serhiy Storchaka and Martin Panter.
* | Issue #21741: Update 147 test modules to use test discovery.Zachary Ware2015-04-131-4/+1
|/ | | | | | | I have compared output between pre- and post-patch runs of these tests to make sure there's nothing missing and nothing broken, on both Windows and Linux. The only differences I found were actually tests that were previously *not* run.
* Issue #17576: Deprecation warning emitted now when __int__() or __index__()Serhiy Storchaka2013-12-111-25/+51
|\ | | | | | | | | return not int instance. Introduced _PyLong_FromNbInt() and refactored PyLong_As*() functions.
| * Issue #17576: Deprecation warning emitted now when __int__() or __index__()Serhiy Storchaka2013-12-111-25/+51
| | | | | | | | | | return not int instance. Introduced _PyLong_FromNbInt() and refactored PyLong_As*() functions.
* | Issue #16741: Fix an error reporting in int().Serhiy Storchaka2013-08-031-16/+31
|\ \ | |/
| * Issue #16741: Fix an error reporting in int().Serhiy Storchaka2013-08-031-16/+31
| |
* | Issue #17715: Merge fix from 3.3.Mark Dickinson2013-04-131-0/+6
|\ \ | |/
| * Issue #17715: Add missing NULL Check to PyNumber_Long.Mark Dickinson2013-04-131-0/+6
| |
* | Issue #16772: in int(x, base), non-integer bases must have an __index__ method.Mark Dickinson2013-01-271-0/+17
| |
* | Issue #16761: Raise TypeError when int() called with base argument only.Serhiy Storchaka2012-12-281-10/+2
|\ \ | |/
| * Issue #16761: Raise TypeError when int() called with base argument only.Serhiy Storchaka2012-12-281-10/+2
| |\
| | * Issue #16761: Raise TypeError when int() called with base argument only.Serhiy Storchaka2012-12-281-0/+8
| | |
* | | Issue #16793. Replace deprecated unittest asserts with modern counterparts.Serhiy Storchaka2012-12-271-10/+10
|\ \ \ | |/ /
| * | Issue #16793. Replace deprecated unittest asserts with modern counterparts.Serhiy Storchaka2012-12-271-10/+10
| | |
* | | Issue #16792: Mark small ints test as CPython-only.Serhiy Storchaka2012-12-271-4/+8
|\ \ \ | |/ /
| * | Issue #16792: Mark small ints test as CPython-only.Serhiy Storchaka2012-12-271-4/+8
| |\ \ | | |/
| | * Issue #16792: Mark small ints test as CPython-only.Serhiy Storchaka2012-12-271-4/+9
| | |
* | | Test for issue16772 and redoes the previous fix to accept __index__-awareGregory P. Smith2012-12-261-0/+24
|/ / | | | | | | objects as the base by using PyNumber_AsSsize_t similar to round().
* | Issue #16045: add more unit tests for built-in int()Andrew Svetlov2012-12-231-2/+42
| | | | | | | | Patch by Chris Jerdonek.
* | Issue #16060: Fix a double DECREF in int() implementation. Thanks Serhiy ↵Mark Dickinson2012-09-271-0/+12
|/ | | | Storchaka.
* Issue #10557: Fixed error messages from float() and other numericAlexander Belopolsky2010-12-041-1/+14
| | | | | | types. Added a new API function, PyUnicode_TransformDecimalToASCII(), which transforms non-ASCII decimal digits in a Unicode string to their ASCII equivalents.