Commit Graph

594 Commits

Author SHA1 Message Date
RUANG (James Roy) 8ff7efb46d
gh-126061: Add PyLong_IsPositive/Zero/Negative() functions (#126065)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-11-12 14:18:06 +01:00
Mark Shannon fa40922597
GH-126547: Pre-assign version numbers for a few common classes (GH-126551) 2024-11-08 16:44:44 +00:00
Victor Stinner 03775472cc
Use _PyLong_GetOne() and _PyLong_GetZero() in long_invmod() (#125044)
These functions cannot fail.
2024-10-07 19:54:42 +02:00
Victor Stinner 113b2d7583
gh-111178: Fix function signatures in longobject.c (#124895)
* Add _PyLong_CAST() macro.
* Move forward declarations to the top of longobject.c.
* Change long_add(), long_sub(), long_mul(), long_neg(),
  long_lshift(), long_abs() to take PyLongObject* and return
  PyLongObject*. Avoid CHECK_BINOP() test.
* Add long_add_method(), long_sub_method(), long_mul_method(),
  long_neg_method(), long_lshift_method(), and long_abs_method()
  which take PyObject* and return PyObject*. Implement CHECK_BINOP()
  test.
* Add long_lshift_int64() function.
* _PyLong_DivmodNear() calls long_lshift_int64(obj, 1) instead of
  long_lshift_obj(obj, one).
2024-10-02 17:41:19 +02:00
Serhiy Storchaka d08c788822
gh-123497: New limit for Python integers on 64-bit platforms (GH-123724)
Instead of be limited just by the size of addressable memory (2**63
bytes), Python integers are now also limited by the number of bits, so
the number of bit now always fit in a 64-bit integer.

Both limits are much larger than what might be available in practice,
so it doesn't affect users.

_PyLong_NumBits() and _PyLong_Frexp() are now always successful.
2024-09-29 10:40:20 +03:00
Serhiy Storchaka 32c7dbb2bc
gh-121485: Always use 64-bit integers for integers bits count (GH-121486)
Use 64-bit integers instead of platform specific size_t or Py_ssize_t
to represent the number of bits in Python integer.
2024-08-30 08:13:24 +03:00
Victor Stinner 4c6dca8292
gh-120389: Add PyLong_FromInt64() and PyLong_AsInt64() (#120390)
Add new functions to convert C <stdint.h> numbers from/to Python int:

* PyLong_FromInt32()
* PyLong_FromUInt32()
* PyLong_FromInt64()
* PyLong_FromUInt64()
* PyLong_AsInt32()
* PyLong_AsUInt32()
* PyLong_AsInt64()
* PyLong_AsUInt64()
2024-08-28 10:16:13 +00:00
Bénédikt Tran fe3e623562
gh-111178: Avoid calling long_hash from incompatible pointer type (GH-122972)
Make `long_hash` compatible with `hashfunc`.
2024-08-14 11:50:35 +02:00
Serhiy Storchaka 1a0c7b9ba4
gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) 2024-07-19 08:06:02 +00:00
Serhiy Storchaka 18015451d0
gh-121153: Fix some errors with use of _PyLong_CompactValue() (GH-121154)
* The result has type Py_ssize_t, not intptr_t.
* Type cast between unsigned and signdet integer types should be explicit.
* Downcasting should be explicit.
* Fix integer overflow check in sum().
2024-07-13 13:40:44 +03:00
Steve Dower 2894aa14f2
gh-121115: Skip __index__ in PyLong_AsNativeBytes by default (GH-121118) 2024-06-28 16:26:21 +01:00
Victor Stinner e51e880e75
gh-120593: Fix const qualifier in _PyLong_CompactValue() (#121053)
Remove the const qualifier of the argument of functions:

* _PyLong_IsCompact()
* _PyLong_CompactValue()

Py_TYPE() argument is not const.

Fix the compiler warning:

  Include/cpython/longintrepr.h: In function ‘_PyLong_CompactValue’:
  Include/pyport.h:19:31: error: cast discards ‘const’ qualifier from
  pointer target type [-Werror=cast-qual]
    (...)
  Include/cpython/longintrepr.h:133:30: note: in expansion of macro
  ‘Py_TYPE’
    assert(PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS));
2024-06-26 20:11:21 +02:00
Kirill Podoprigora 57ad769076
gh-120080: Accept ``None`` as a valid argument for direct call of the ``int.__round__`` (#120088)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-06-07 10:03:28 +02:00
Nikita Sobolev 1d4c2e4a87
gh-119057: Use better error messages for zero division (#119066) 2024-06-03 19:03:56 +03:00
Sergey B Kirpichev 61d3ab32da
gh-116560: Add PyLong_GetSign() public function (#116561)
Co-authored-by: Victor Stinner <vstinner@python.org>
2024-06-03 14:06:31 +02:00
Sergey B Kirpichev cd11ff12ac
gh-119613: Use C99+ functions instead of Py_IS_NAN/INFINITY/FINITE (#119619) 2024-05-29 09:51:19 +02:00
Ayato Hayashi 43807fec8d
Tiny fix: Update link for HAC algorithm (gh-118546) 2024-05-23 02:11:11 +00:00
Jens Hedegaard Nielsen 1539854c3d
fix formatting of literal in docstring of int.from_bytes and int.to_bytes (#117847) 2024-04-18 09:01:58 +00:00
Steve Dower 687616877b
gh-111140: PyLong_From/AsNativeBytes: Take *flags* rather than just *endianness* (GH-116053) 2024-04-05 16:21:16 +02:00
Nikita Sobolev eb947cdc13
gh-110819: Fix ‘kind’ may be used uninitialized warning in `longobject` (#116599) 2024-03-12 13:50:06 +03:00
Nikita Sobolev 465df8855e
gh-115827: Fix compile warning in `longobject.c` (#115828)
Objects/longobject.c:1186:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
2024-02-22 17:57:12 +00:00
Kirill Podoprigora 206f73dc5f
gh-115391: Fix compiler warning in `Objects/longobject.c` (GH-115368) 2024-02-13 20:49:13 +00:00
Steve Dower 7861dfd26a
gh-111140: Adds PyLong_AsNativeBytes and PyLong_FromNative[Unsigned]Bytes functions (GH-114886) 2024-02-12 20:13:13 +00:00
kcatss a571a2fd3f
gh-114050: Fix crash when more than two arguments are passed to int() (GH-114067)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
2024-01-18 13:27:44 +02:00
Mark Shannon 0887b9ce8b
GH-109369 Add vectorcall to `PyLong_Type` (GH-111642) 2023-11-02 15:07:47 +00:00
Victor Stinner 03c4080c71
gh-108765: Python.h no longer includes <ctype.h> (#108831)
Remove <ctype.h> in C files which don't use it; only sre.c and
_decimal.c still use it.

Remove _PY_PORT_CTYPE_UTF8_ISSUE code from pyport.h:

* Code added by commit b5047fd019
  in 2004 for MacOSX and FreeBSD.
* Test removed by commit 52ddaefb6b
  in 2007, since Python str type now uses locale independent
  functions like Py_ISALPHA() and Py_TOLOWER() and the Unicode
  database.

Modules/_sre/sre.c replaces _PY_PORT_CTYPE_UTF8_ISSUE with new
functions: sre_isalnum(), sre_tolower(), sre_toupper().

Remove unused includes:

* _localemodule.c: remove <stdio.h>.
* getargs.c: remove <float.h>.
* dynload_win.c: remove <direct.h>, it no longer calls _getcwd()
  since commit fb1f68ed7c (in 2001).
2023-09-03 18:54:27 +02:00
Victor Stinner be436e08b8
gh-108444: Add PyLong_AsInt() public function (#108445)
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
2023-08-24 23:55:30 +02:00
Brandt Bucher 05a824f294
GH-84436: Skip refcounting for known immortals (GH-107605) 2023-08-04 16:24:50 -07:00
Illia Volochii fc130c47da
gh-102509: Start initializing `ob_digit` of `_PyLongValue` (GH-102510) 2023-07-28 14:39:54 +01:00
Mark Shannon 93923793f6
GH-101291: Add low level, unstable API for pylong (GH-101685)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2023-05-21 14:45:48 +01:00
Eric Snow fdd878650d
gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (gh-104072)
Until now, we haven't been initializing nor finalizing the per-interpreter state properly.
2023-05-01 19:36:00 -06:00
Eric Snow 59bc36aacd
gh-84436: Immortalize in _PyStructSequence_InitBuiltinWithFlags() (gh-104054)
This also does some cleanup.
2023-05-01 15:08:34 -06:00
Eric Snow d2e2e53f73
gh-94673: Ensure Builtin Static Types are Readied Properly (gh-103940)
There were cases where we do unnecessary work for builtin static types. This also simplifies some work necessary for a per-interpreter GIL.
2023-04-27 16:19:43 -06:00
Eddie Elizondo ea2c001650
gh-84436: Implement Immortal Objects (gh-19474)
This is the implementation of PEP683

Motivation:

The PR introduces the ability to immortalize instances in CPython which bypasses reference counting. Tagging objects as immortal allows up to skip certain operations when we know that the object will be around for the entire execution of the runtime.

Note that this by itself will bring a performance regression to the runtime due to the extra reference count checks. However, this brings the ability of having truly immutable objects that are useful in other contexts such as immutable data sharing between sub-interpreters.
2023-04-22 13:39:37 -06:00
Mark Shannon 7559f5fda9
GH-101291: Rearrange the size bits in PyLongObject (GH-102464)
* Eliminate all remaining uses of Py_SIZE and Py_SET_SIZE on PyLongObject, adding asserts.

* Change layout of size/sign bits in longobject to support future addition of immortal ints and tagged medium ints.

* Add functions to hide some internals of long object, and for setting sign and digit count.

* Replace uses of IS_MEDIUM_VALUE macro with _PyLong_IsCompact().
2023-03-22 14:49:51 +00:00
Sergey B Kirpichev 4624987b29
gh-101825: Clarify that as_integer_ratio() output is always normalized (#101843)
Make docstrings for `as_integer_ratio` consistent across types, and document that
the returned pair is always normalized (coprime integers, with positive denominator).

---------

Co-authored-by: Owain Davies <116417456+OTheDev@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2023-02-27 19:11:28 +00:00
Mark Dickinson 39017e04b5
gh-101266: Fix __sizeof__ for subclasses of int (#101394)
Fix the behaviour of the `__sizeof__` method (and hence the results returned by `sys.getsizeof`) for subclasses of `int`. Previously, `int` subclasses gave identical results to the `int` base class, ignoring the presence of the instance dictionary.

<!-- gh-issue-number: gh-101266 -->
* Issue: gh-101266
<!-- /gh-issue-number -->
2023-02-05 10:02:53 +00:00
Mark Shannon c1b1f51cd1
GH-101291: Refactor the `PyLongObject` struct into object header and PyLongValue struct. (GH-101292) 2023-01-30 10:03:04 +00:00
Mark Dickinson 401fdf9c85
gh-101037: Fix potential memory underallocation for zeros of int subtypes (#101038)
This PR fixes object allocation in long_subtype_new to ensure that there's at least one digit in all cases, and makes sure that the value of that digit is copied over from the source long.

Needs backport to 3.11, but not any further: the change to require at least one digit was only introduced for Python 3.11.

Fixes #101037.
2023-01-21 10:23:59 +00:00
Ionite d7e7f79ca7
gh-100637: Fix int and bool __sizeof__ calculation to include the 1 element ob_digit array for 0 and False (#100663)
Fixes behaviour where int (and subtypes like bool) __sizeof__ under-reports true size as it did not take into account the size 1 `ob_digit` array for the zero int.

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2023-01-02 21:11:49 +00:00
Shantanu 3e46f9fe05
gh-100268: Add is_integer method to int (#100439)
This improves the lives of type annotation users of `float` - which type checkers implicitly treat as `int|float` because that is what most code actually wants. Before this change a `.is_integer()` method could not be assumed to exist on things annotated as `: float` due to the method not existing on both types.
2022-12-23 18:30:27 -08:00
Ned Batchelder 935ef59321
clarify the 4300-digit limit on int-str conversion (#100175) 2022-12-12 13:39:54 +02:00
Victor Stinner 5556d3e02c
gh-98724: Fix warnings on Py_SETREF() usage (#99781)
Cast argument to the expected type.
2022-11-26 00:30:37 +01:00
Victor Stinner 20d9749a0f
gh-99537: Use Py_SETREF() function in longobject C code (#99655)
Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);"
in longobject.c and _testcapi/long.c.
2022-11-22 13:04:19 +01:00
Victor Stinner 1960eb005e
gh-99300: Use Py_NewRef() in Objects/ directory (#99351)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
2022-11-10 23:40:31 +01:00
jonasdlindner ede6cb2615
Correct some typos in comments (GH-98194)
Automerge-Triggered-By: GH:AlexWaygood
2022-11-06 08:54:44 -08:00
Victor Stinner 387f72588d
gh-90716: Fix pylong_int_from_string() refleak (#99094)
Fix validated by:

    $ ./python -m test -R 3:3 test_int
    Tests result: SUCCESS
2022-11-04 14:24:10 +01:00
Gregory P. Smith 4c4b5ce2e5
gh-90716: bugfixes and more tests for _pylong. (#99073)
* 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.
2022-11-03 16:18:38 -07:00
Neil Schemenauer de6981680b
gh-90716: add _pylong.py module (#96673)
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
2022-10-25 22:00:50 -07:00
Eric Snow 9c8dde0fa5
gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418) 2022-10-19 13:27:46 -07:00