Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-102471, PEP 757: Add PyLong import and export API#121339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 54 commits intopython:mainfromvstinner:long_export
Dec 13, 2024
Merged
Changes from1 commit
Commits
Show all changes
54 commits
Select commitHold shift + click to select a range
f4fdbf2
gh-102471: Add PyLong import and export API
vstinnerJul 2, 2024
c2e568e
Add layout
vstinnerJul 23, 2024
f0d9525
Merge branch 'main' into long_export
vstinnerSep 3, 2024
b19764f
Rename word_endian to digits_order
vstinnerSep 3, 2024
6f7fd11
Replace Py_digit* type with void*
vstinnerSep 3, 2024
080e079
Add PyLongWriter_Discard() function
vstinnerSep 4, 2024
1a7902f
Fixes
vstinnerSep 5, 2024
b70a6dd
Use unsigned type for ndigits
vstinnerSep 5, 2024
07552a7
Remove again layout
vstinnerSep 5, 2024
0d0f942
Revert "Use unsigned type for ndigits"
vstinnerSep 6, 2024
762c33a
doc: adjust ndigits documentation
vstinnerSep 6, 2024
20be7a3
Update doc
vstinnerSep 13, 2024
d92bf1e
Make PyLong_DigitArray.obj private
vstinnerSep 16, 2024
b3b02a2
Remove reserved documentation
vstinnerSep 16, 2024
caca2d7
PyLong_FreeDigitArray() only clears _reserved
vstinnerSep 16, 2024
4221a49
Make PyLong_LAYOUT static
vstinnerSep 16, 2024
37b1d49
Add PyLong_AsDigitArray.value
vstinnerSep 16, 2024
d70a121
Inline PyLong_AsInt64() to avoid the exception
vstinnerSep 17, 2024
4aa25f6
Remove Py_digit type; update the doc
vstinnerSep 17, 2024
90973d4
Merge branch 'main' into long_export
vstinnerSep 17, 2024
5d3e224
Add long_asnativebytes() function
vstinnerSep 17, 2024
c7d7cb2
Remove reference to removed Py_digit type
vstinnerSep 17, 2024
a3d601a
Address Antoine's review
vstinnerSep 17, 2024
c049268
Merge branch 'main' into long_export
vstinnerSep 17, 2024
06b196b
Merge branch 'main' into long_export
skirpichevSep 18, 2024
3e8d296
Apply suggestions from code review
skirpichevSep 18, 2024
86c68c2
Merge branch 'main' into long_export
skirpichevSep 18, 2024
a8fd669
Revert "Add long_asnativebytes() function"
vstinnerSep 18, 2024
a04f9d0
Use PyLong_AsLongAndOverflow()
vstinnerSep 18, 2024
b2be94a
Try PyLong_AsLongLongAndOverflow() first
vstinnerSep 18, 2024
ca98ad1
Merge branch 'main' into long_export
vstinnerSep 18, 2024
167d75e
Update Doc/c-api/long.rst
vstinnerSep 19, 2024
5e53a5b
Sync implementation with PEP (#8)
skirpichevOct 16, 2024
c24789f
Merge branch 'main' into long_export
skirpichevNov 13, 2024
0422f9d
fix NL in Doc/c-api/long.rst (sorry, damn web editor)
skirpichevNov 13, 2024
a529a48
rename news
skirpichevNov 13, 2024
3db44f3
Address Erlend's review
vstinnerNov 13, 2024
1d2863e
Address Sergey's review
vstinnerNov 13, 2024
d663511
Merge branch 'main' into long_export
vstinnerNov 13, 2024
816798d
Merge branch 'main' into long_export
vstinnerNov 28, 2024
033bd65
Update documentation from PEP 757
vstinnerNov 28, 2024
36b87d4
Update Modules/_testcapi/long.c
vstinnerNov 28, 2024
94d852e
Sync implementation with PEP (#9)
skirpichevDec 9, 2024
a72ff83
Merge branch 'main' into long_export
vstinnerDec 9, 2024
53d584b
Cleanup
vstinnerDec 9, 2024
577598a
Update Doc/c-api/long.rst
vstinnerDec 9, 2024
b08cd55
Address Steve's review
vstinnerDec 9, 2024
eaebef3
Address Bénédikt's review
vstinnerDec 10, 2024
03248c7
Apply suggestions from code review
vstinnerDec 10, 2024
0a26f97
Address Steve's review
vstinnerDec 11, 2024
88a62fe
Add PyLong_Export to Doc/data/refcounts.dat
vstinnerDec 12, 2024
45517ab
Address Serhiy's review
vstinnerDec 12, 2024
92007d1
Address Petr's review
vstinnerDec 12, 2024
6d3cb80
Add PyLongWriter to Doc/data/refcounts.dat
vstinnerDec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Revert "Add long_asnativebytes() function"
This reverts commit5d3e224.
  • Loading branch information
@vstinner
vstinner committedSep 18, 2024
commita8fd66990491ab6d190a0e9bf648b05d332ee0f4
106 changes: 48 additions & 58 deletionsObjects/longobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1117,18 +1117,53 @@ _resolve_endianness(int *endianness)
return 0;
}


static Py_ssize_t
long_asnativebytes(PyLongObject *v, void* buffer, Py_ssize_t n,
int little_endian, int unsigned_buffer)
Py_ssize_t
PyLong_AsNativeBytes(PyObject* vv, void* buffer, Py_ssize_t n, int flags)
{
Py_ssize_t res = 0;
PyLongObject *v;
union {
Py_ssize_t v;
unsigned char b[sizeof(Py_ssize_t)];
} cv;
int do_decref = 0;
Py_ssize_t res = 0;

if (vv == NULL || n < 0) {
PyErr_BadInternalCall();
return -1;
}

int little_endian = flags;
if (_resolve_endianness(&little_endian) < 0) {
return -1;
}

if (PyLong_Check(vv)) {
v = (PyLongObject *)vv;
}
else if (flags != -1 && (flags & Py_ASNATIVEBYTES_ALLOW_INDEX)) {
v = (PyLongObject *)_PyNumber_Index(vv);
if (v == NULL) {
return -1;
}
do_decref = 1;
}
else {
PyErr_Format(PyExc_TypeError, "expect int, got %T", vv);
return -1;
}

if ((flags != -1 && (flags & Py_ASNATIVEBYTES_REJECT_NEGATIVE))
&& _PyLong_IsNegative(v)) {
PyErr_SetString(PyExc_ValueError, "Cannot convert negative int");
if (do_decref) {
Py_DECREF(v);
}
return -1;
}

if (_PyLong_IsCompact(v)) {
res = 0;
cv.v = _PyLong_CompactValue(v);
/* Most paths result in res = sizeof(compact value). Only the case
* where 0 < n < sizeof(compact value) do we need to check and adjust
Expand DownExpand Up@@ -1165,7 +1200,7 @@ long_asnativebytes(PyLongObject *v, void* buffer, Py_ssize_t n,
/* Positive values with the MSB set do not require an
* additional bit when the caller's intent is to treat them
* as unsigned. */
if (unsigned_buffer) {
if (flags == -1 || (flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER)) {
res = n;
} else {
res = n + 1;
Expand DownExpand Up@@ -1252,7 +1287,7 @@ long_asnativebytes(PyLongObject *v, void* buffer, Py_ssize_t n,
* as unsigned. */
unsigned char *b = (unsigned char *)buffer;
if (b[little_endian ? n - 1 : 0] & 0x80) {
if (unsigned_buffer) {
if (flags == -1 || (flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER)) {
res = n;
} else {
res = n + 1;
Expand All@@ -1261,54 +1296,6 @@ long_asnativebytes(PyLongObject *v, void* buffer, Py_ssize_t n,
}
}
}
return res;
}


Py_ssize_t
PyLong_AsNativeBytes(PyObject* vv, void* buffer, Py_ssize_t n, int flags)
{
PyLongObject *v;
int do_decref = 0;
Py_ssize_t res = 0;

if (vv == NULL || n < 0) {
PyErr_BadInternalCall();
return -1;
}

int little_endian = flags;
if (_resolve_endianness(&little_endian) < 0) {
return -1;
}

if (PyLong_Check(vv)) {
v = (PyLongObject *)vv;
}
else if (flags != -1 && (flags & Py_ASNATIVEBYTES_ALLOW_INDEX)) {
v = (PyLongObject *)_PyNumber_Index(vv);
if (v == NULL) {
return -1;
}
do_decref = 1;
}
else {
PyErr_Format(PyExc_TypeError, "expect int, got %T", vv);
return -1;
}

if ((flags != -1 && (flags & Py_ASNATIVEBYTES_REJECT_NEGATIVE))
&& _PyLong_IsNegative(v)) {
PyErr_SetString(PyExc_ValueError, "Cannot convert negative int");
if (do_decref) {
Py_DECREF(v);
}
return -1;
}

int unsigned_buffer = (flags == -1
|| (flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER));
res = long_asnativebytes(v, buffer, n, little_endian, unsigned_buffer);

if (do_decref) {
Py_DECREF(v);
Expand DownExpand Up@@ -6810,11 +6797,13 @@ PyLong_Export(PyObject *obj, PyLongExport *export_long)
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
return -1;
}
PyLongObject *self = (PyLongObject*)obj;

int64_t value;
Py_ssize_t bytes = long_asnativebytes(self, &value, sizeof(value),
PY_LITTLE_ENDIAN, 0);
int flags = Py_ASNATIVEBYTES_NATIVE_ENDIAN;
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, &value, sizeof(value), flags);
if (bytes < 0) {
return -1;
}

if ((size_t)bytes <= sizeof(value)) {
export_long->value = value;
Expand All@@ -6824,6 +6813,7 @@ PyLong_Export(PyObject *obj, PyLongExport *export_long)
export_long->_reserved = 0;
}
else {
PyLongObject *self = (PyLongObject*)obj;
export_long->value = 0;
export_long->negative = _PyLong_IsNegative(self);
export_long->ndigits = _PyLong_DigitCount(self);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp