Skip to content

gh-104399: Use newer libtommath APIs when necessary #104407

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Prepare the ``_tkinter`` module for building with Tcl 9.0 and future
libtommath by replacing usage of deprecated functions
:c:func:`mp_to_unsigned_bin_n` and :c:func:`mp_unsigned_bin_size`
when necessary.
23 changes: 21 additions & 2 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Copyright (C) 1994 Steen Lumholt.
#endif
#include <tclTomMath.h>

#if defined(TCL_WITH_EXTERNAL_TOMMATH) || (TCL_MAJOR_VERSION >= 9)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new API is available since 8.6.10. Would not be better to use it as soon as it is available? Potentially it can support larger integers.

Suggested change
#if defined(TCL_WITH_EXTERNAL_TOMMATH) || (TCL_MAJOR_VERSION >= 9)
#if defined(TCL_WITH_EXTERNAL_TOMMATH) || (TK_HEX_VERSION >= 0x0806020a)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with libtommath APIs nor aware what the exact limits are, but I would think that Tcl is more likely to be the limiting factor anyway, as even the deprecated libtommath functions can handle numbers exceeding what Tcl < 9 can usefully convert to and from (due to various 2**31-1 size limitations on strings, etc.).

Given how Tkinter requires the Tcl version but not the patchlevel to match at compile time and runtime (#104399 (comment)), it should be fine to enable the new functions on Tcl 8.7; this would also allow not having to check for TCL_WITH_EXTERNAL_TOMMATH in this case (although it still has to be checked for #103842).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

#define USE_DEPRECATED_TOMMATH_API 0
#else
#define USE_DEPRECATED_TOMMATH_API 1
#endif

#if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
#define HAVE_CREATEFILEHANDLER
#endif
Expand Down Expand Up @@ -1053,20 +1059,33 @@ static PyObject*
fromBignumObj(TkappObject *tkapp, Tcl_Obj *value)
{
mp_int bigValue;
mp_err err;
#if USE_DEPRECATED_TOMMATH_API
unsigned long numBytes;
#else
size_t numBytes;
#endif
unsigned char *bytes;
PyObject *res;

if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK)
return Tkinter_Error(tkapp);
#if USE_DEPRECATED_TOMMATH_API
numBytes = mp_unsigned_bin_size(&bigValue);
#else
numBytes = mp_ubin_size(&bigValue);
#endif
bytes = PyMem_Malloc(numBytes);
if (bytes == NULL) {
mp_clear(&bigValue);
return PyErr_NoMemory();
}
if (mp_to_unsigned_bin_n(&bigValue, bytes,
&numBytes) != MP_OKAY) {
#if USE_DEPRECATED_TOMMATH_API
err = mp_to_unsigned_bin_n(&bigValue, bytes, &numBytes);
#else
err = mp_to_ubin(&bigValue, bytes, numBytes, NULL);
#endif
if (err != MP_OKAY) {
mp_clear(&bigValue);
PyMem_Free(bytes);
return PyErr_NoMemory();
Expand Down