This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PYLONG_BITS_IN_DIGIT differs between MinGW and MSVC
Type: compile error Stage: patch review
Components: Extension Modules, Windows Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Beier Liu, mark.dickinson, paul.moore, scoder, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2018-10-21 08:22 by scoder, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 19326 open Beier Liu, 2021-10-20 13:48
Messages (9)
msg328197 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2018-10-21 08:22
I see reports from Cython users on Windows-64 that extension modules that use "longintrepr.h" get miscompiled by MinGW. A failing setup looks as follows:

Stock 64 bit CPython on Windows, e.g.
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]

MinGW uses this compile time setup, i.e. 15 bit digits:
PyLong_BASE  0x8000
PyLong_MASK  7FFF
PyLong_SHIFT  15
sizeof(digit)  2
sizeof(sdigit)  2

Whereas CPython reports the following, indicating that it was in fact built with 30 bit digits:
sys.getsize(1, 2**14, 2**15, 2**29, 2**30, 2**63, 2**64)  (28, 28, 28, 28, 32, 36, 36)

I'm not sure if this also applies to Py2.7, but I don't think the PyLong implementations differ in this regard.

The compile time PyLong digit size is not remembered by the CPython installation and instead determined on the fly by the extension compiler. It seems that the MSVC build of the stock CPython packages differs from what MinGW decides here. This renders MinGW unusable for code that uses "longintrepr.h", which Cython does by default in order to accelerate its unpacking of PyLong values.

Is there a reason why CPython cannot store its compile time value for the PYLONG_BITS_IN_DIGIT setting somewhere?
msg328198 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2018-10-21 08:29
Some more information. CPython uses this code snippet to decide on the PyLong digit size:

#ifndef PYLONG_BITS_IN_DIGIT
#if SIZEOF_VOID_P >= 8
#define PYLONG_BITS_IN_DIGIT 30
#else
#define PYLONG_BITS_IN_DIGIT 15
#endif
#endif

In MinGW, "SIZEOF_VOID_P" is 4, whereas "sizeof(void*)" resolves to 8.
msg328202 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-10-21 09:49
> Is there a reason why CPython cannot store its compile time value for the PYLONG_BITS_IN_DIGIT setting somewhere?

There's PyLong_GetInfo [1], and at Python level, the corresponding sys.long_info (Python 2) / sys.int_info (Python 3). Does that help?





[1] https://github.com/python/cpython/blob/b2e2025941f6a4fdb716bd141d31acf720353d21/Objects/longobject.c#L5578-L5595
msg328203 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-21 09:50
At runtime this information is available as sys.int_info.bits_per_digit.

> In MinGW, "SIZEOF_VOID_P" is 4, whereas "sizeof(void*)" resolves to 8.

Looks like a misconfiguration. I suppose this will cause more issues than just wrong PYLONG_BITS_IN_DIGIT.
msg328204 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2018-10-21 11:31
> There's PyLong_GetInfo ...

Thanks, I understand that this information can be found at runtime. However, in order to correctly compile C code against a given CPython runtime, there needs to be a guarantee that extension module builds use the same configuration as the CPython build.

> Misconfiguration

I searched some more, and it looks like this was already reported almost ten years ago in issue4709.

The work-around there is to pass -DMS_WIN64, which apparently is not defined by MinGW but required by the CPython headers.
msg328206 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-21 12:13
That variable should be inferred somewhere, either in PC/pyconfig.h or pyport.h. If there's a way to also infer it on 64-bit MinGW then we would consider a patch for that.
msg328341 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2018-10-23 20:18
The relevant macro seems to be "__MINGW64__". I have neither a Windows environment nor MinGW-64 for testing, but the logic should be the same as for the "_WIN64" macro, i.e.

#if defined(_WIN64) || defined(__MINGW64__)
#define MS_WIN64
#endif

And then there's probably also something to do to record the compiler version in the long CPython version string, etc. I also can't say if MinGW-64 sets the "MS_WIN32" macro. Having "MS_WIN64" without "MS_WIN32" might be unexpected for some code.
msg404733 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-10-22 09:17
This should probably be a separate issue, but I wonder whether the 15-bit digit option has value any more. Should we just drop that option and always use 30-bit digits? 

30-bit digits were introduced at a time when we couldn't rely on a 64-bit integer type always being available,  so we still needed to keep the 15-bit fallback option. But I think that's no longer the case.
msg409034 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-12-22 13:16
> This should probably be a separate issue,

Specifically, issue 45569.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79218
2021-12-22 13:16:07mark.dickinsonsetmessages: + msg409034
2021-12-11 19:14:50iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
2021-10-22 09:17:33mark.dickinsonsetmessages: + msg404733
2021-10-20 13:50:36iritkatriellinkissue40167 superseder
2021-10-20 13:48:35Beier Liusetkeywords: + patch
nosy: + Beier Liu

pull_requests: + pull_request27362
stage: patch review
2018-10-23 20:18:39scodersetmessages: + msg328341
2018-10-21 12:13:10steve.dowersetmessages: + msg328206
2018-10-21 11:31:55scodersetmessages: + msg328204
2018-10-21 09:50:22serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg328203
2018-10-21 09:49:37mark.dickinsonsetmessages: + msg328202
2018-10-21 09:37:50mark.dickinsonsetnosy: + mark.dickinson
2018-10-21 08:29:06scodersetmessages: + msg328198
2018-10-21 08:22:52scodercreate