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: _Py_stat and _Py_wstat using incorrect type for status argument
Type: compile error Stage: resolved
Components: Build, Windows Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, gvanrossum, pacampbell, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: Keywords: patch

Created on 2022-01-08 03:48 by pacampbell, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30478 closed pacampbell, 2022-01-08 04:56
PR 30484 merged vstinner, 2022-01-08 12:02
PR 30528 closed vstinner, 2022-01-11 11:13
PR 30539 closed vstinner, 2022-01-11 16:21
PR 30550 merged vstinner, 2022-01-11 22:58
Messages (21)
msg410073 - (view) Author: Paul Campbell (pacampbell) * Date: 2022-01-08 03:48
While attempting to embed the full cpython source in my application, I found that during compilation on Windows, there was a compilation issue due to struct stat not being defined. Taking a look at the file cpython/fileutils.h, it seems that the type of the status argument should be _Py_stat_struct instead of just stat to satisfy compilation for all supported platforms.
msg410095 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-08 12:06
It's not the first time that private functions included by the public Python.h are causing build errors event if these functions are not used. The previous issue were functions for atomic operations. I solved this build error by moving the whole private C API into the internal C API: Include/internal/pycore_atomic.h. Since that time, we no longer got build error related to this header file.

I propose a similar fix: move all private fileutils.h functions from Include/cpython/fileutils.h to the internal Include/internal/pycore_fileutils.h.

I wrote PR 30484 to implement this change.

To keep the implementation simple, I kept _Py_fopen_obj() in Include/cpython/fileutils.h, for _testcapi which must not use the internal C API.

If this PR is merged, for Python 3.9 and 3.10, I will write a simpler change: modify Include/cpython/fileutils.h to only define functions using "struct stat" in the internal C API (if Py_BUILD_CORE is defined).
msg410288 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 10:56
New changeset ea1a54506b4ac38b712ba63ec884292025f16111 by Victor Stinner in branch 'main':
bpo-46303: Move fileutils.h private functions to internal C API (GH-30484)
https://github.com/python/cpython/commit/ea1a54506b4ac38b712ba63ec884292025f16111
msg410292 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 11:14
> While attempting to embed the full cpython source in my application, I found that during compilation on Windows, there was a compilation issue due to struct stat not being defined.

Do you get the error when building Python? Or on #include <Python.h> when using the Python C API?

How do you build Python? What is your C compiler?

Can you test if my proposed PR 30528 fix your issue?
msg410306 - (view) Author: Paul Campbell (pacampbell) * Date: 2022-01-11 15:08
I was trying to build python core (-DMS_WINDOWS -DPy_BUILD_CORE). I was using clang, which I think is unsupported looking at Windows doc. After looking at the issue though, it seemed that it was just some slight mistake which is why I filed the bug.

clang version 13.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix

I can test to see if it fixes the immediate build problem, but I still find your fix not quite addressing the issue which I initially tried to create a patch for. Someone has already developed a shim here for Windows and it just was not used properly. `_Py_stat_struct` is a define which either evaluates to `stat` on non-Windows systems or a compatibility structure on Windows. Simply replacing the use of `struct stat` with `struct _Py_stat_struct` should solve the issue.
msg410309 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 15:53
Python.h indirectly (via fileutils.h) defines _Py_wstat() and _Py_stat() functions which use the "struct stat*" type for 12 years:

commit 4e31443c4d2c1fb211a6ea90fc6a8fbd9ff81c97
Author: Victor Stinner <victor.stinner@haypocalc.com>
Date:   Thu Oct 7 21:45:39 2010 +0000

    Create fileutils.c/.h
    
     * _Py_fopen() and _Py_stat() come from Python/import.c
     * (_Py)_wrealpath() comes from Python/sysmodule.c
     * _Py_char2wchar(), _Py_wchar2char() and _Py_wfopen() come from Modules/main.c
     * (_Py)_wstat(), (_Py)_wgetcwd(), _Py_wreadlink() come from Modules/getpath.c
msg410311 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 16:22
It seems like _Py_stat() and _Py_wstat() are only needed on non-Windows platforms: only the _get_tcl_lib_path() function of Modules/_tkinter.c uses _Py_stat(). I wrote PR 30539 to not define _Py_stat() and _Py_wstat() on Windows.
msg410351 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 23:13
I marked bpo-46346 as a duplicate of this issue. Copy of the first message:

"""
I am getting these warnings:

C:\Users\gvanrossum\cpython\PC\_testconsole.c(70,38): warning C4013: '_Py_get_osfhandle' undefined; assuming extern returnin
g int [C:\Users\gvanrossum\cpython\PCbuild\_testconsole.vcxproj]
C:\Users\gvanrossum\cpython\PC\_testconsole.c(70,1): warning C4047: 'initializing': 'HANDLE' differs in levels of indirectio
n from 'int' [C:\Users\gvanrossum\cpython\PCbuild\_testconsole.vcxproj]
C:\Users\gvanrossum\cpython\Modules\_tkinter.c(144,37): warning C4013: '_Py_stat' undefined; assuming extern returning int [
C:\Users\gvanrossum\cpython\PCbuild\_tkinter.vcxproj]

I noticed that GitHub also was flagging these in unrelated PRs.
"""

I proposed GH-30550 to fix these warnings.
msg410355 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2022-01-11 23:20
I set the release blocker flag for the ticket.
msg410357 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 23:35
New changeset 08bc1bad11cad39f508bd662c9b28fcd9c995512 by Victor Stinner in branch 'main':
bpo-46303: Fix fileutils.h compiler warnings (GH-30550)
https://github.com/python/cpython/commit/08bc1bad11cad39f508bd662c9b28fcd9c995512
msg410359 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 23:38
Christian Heimes: "I set the release blocker flag for the ticket."

It's just a compiler warning, why marking it as a release blocker?

Anyway, it's now fixed.
msg410360 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 23:40
Paul: Can you please try to build the main branch of Python with clang and tell me if you still have the compiler warnings? If yes, can you please copy/paste the compiler warnings?

Do you build Python on Windows or from another OS (cross-compilation)?
msg410361 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-11 23:42
If possible, I would prefer to not change 3.9 and 3.10 to avoid any risk of introducing a *new* build error, while trying to support a new platform. I don't think that we currently supporting build Python with clang on Windows yet.
msg410365 - (view) Author: Paul Campbell (pacampbell) * Date: 2022-01-11 23:49
Hi Victor, I was trying to compile with clang on Windows 10. I will try to pull your 3.11 changes and test. Sorry to cause so much churn. It looked to me like a simple issue that was missed, probably because whatever was trying to compile was not normally compiled on Windows. I was not trying to make a lot of work to support a new platform :)
msg410366 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2022-01-12 00:03
It's unlikely that you can reproduce the issue with clang. We use MSVC and a manually maintained pyconfig.h on Windows.
msg410367 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-12 00:15
> Hi Victor, I was trying to compile with clang on Windows 10. I will try to pull your 3.11 changes and test. Sorry to cause so much churn. It looked to me like a simple issue that was missed, probably because whatever was trying to compile was not normally compiled on Windows. I was not trying to make a lot of work to support a new platform :)

I tried to write a change which doesn't increase the maintenance on other platforms.

I dislike declaring private functions in the *public* Python.h header file, especially if they cause build error. Over the last years, I moved many private functions to the internal C API.

In Python, we are trying to provide a same C API on all platforms. If "struct stat" is no longer considered as portable, IMO we should attempt to avoid it, at least in the public C API. For these reasons, I dislike PR 30478. The problem is that if the work is written on Linux using "strut stat", the build can fail on Windows if "struct _Py_stat_struct" is now required on Windows. I expected "struct stat" to be portable, but it seems like I was wrong.
msg410369 - (view) Author: Paul Campbell (pacampbell) * Date: 2022-01-12 00:22
> In Python, we are trying to provide a same C API on all platforms. If "struct stat" is no longer considered as portable, IMO we should attempt to avoid it, at least in the public C API.

Microsoft provides stat and struct stat, but they prepend the names with an underscore. So functions like stat are named _stat and struct stat is named struct _stat, etc. Not sure if pros/cons of using such functions are though. Seems it would go back to depending on some type nonstandard python macro to translate between the two during build.
msg410374 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-12 01:07
> Seems it would go back to depending on some type nonstandard python macro to translate between the two during build.

In the internal C API, there are less concerns about writing portable code. We expect users of this API to pay more attention to what they do and there is no backward compatibility warranty.

In the internal C API, it's acceptable to change the parameter type depending on the platform.
msg410377 - (view) Author: Paul Campbell (pacampbell) * Date: 2022-01-12 01:20
Victor: The changes in the main branch gets me past this issue without having to make additional changes.
msg410379 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-12 01:45
Paul Campbell: "The changes in the main branch gets me past this issue without having to make additional changes."

Ok, nice. I close the issue.

I consider that building Python with clang is a new feature. I prefer to not backport these changes to 3.9 and 3.10 to avoid any risk of regression.

I wrote GH-30528: simple fix for Python 3.10, but I don't know if it works. I don't know how to test my change. It seems like clang is not commonly used on Windows, so I close the issue.

If someone wants me to fix build issues with clang on Windows, please comment this issue or open a new issue with an error message and a detailed use case.
msg410429 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2022-01-12 20:44
> Microsoft provides stat and struct stat, but they prepend the names with an underscore.
They are also influenced by various compiler options to choose between 
32-bit and 64-bit fields. This makes it impossible to use the standard 
names as part of an ABI, because we can't/don't enforce that the 
preprocessor definitions match.

We should isolate all structures from libc/equivalent in our public API 
so that we can ensure compatibility. (FILE* was historically also an 
issue, but that was bad enough that Windows fixed it on their side. The 
rest of the C runtime library still bleeds everywhere, so we definitely 
don't want it or its semantics in our public API if avoidable.)
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90461
2022-01-12 20:44:01steve.dowersetmessages: + msg410429
title: Building Python with clang on Windows fails on _Py_stat(): struct stat is not defined -> _Py_stat and _Py_wstat using incorrect type for status argument
2022-01-12 01:45:42vstinnersetstatus: open -> closed
title: _Py_stat and _Py_wstat using incorrect type for status argument -> Building Python with clang on Windows fails on _Py_stat(): struct stat is not defined
messages: + msg410379

resolution: fixed
stage: patch review -> resolved
2022-01-12 01:20:15pacampbellsetmessages: + msg410377
2022-01-12 01:07:58vstinnersetmessages: + msg410374
2022-01-12 00:22:19pacampbellsetmessages: + msg410369
2022-01-12 00:15:09vstinnersetmessages: + msg410367
2022-01-12 00:03:25christian.heimessetmessages: + msg410366
2022-01-11 23:49:53pacampbellsetmessages: + msg410365
2022-01-11 23:42:12vstinnersetmessages: + msg410361
2022-01-11 23:40:02vstinnersetmessages: + msg410360
2022-01-11 23:38:21vstinnersetpriority: release blocker ->

messages: + msg410359
2022-01-11 23:35:34vstinnersetmessages: + msg410357
2022-01-11 23:20:13christian.heimessetnosy: + christian.heimes
messages: + msg410355
2022-01-11 23:19:49christian.heimessetpriority: normal -> release blocker
2022-01-11 23:14:56gvanrossumsetnosy: + gvanrossum
2022-01-11 23:13:15vstinnersetmessages: + msg410351
2022-01-11 23:11:59vstinnerunlinkissue46346 dependencies
2022-01-11 23:11:59vstinnerlinkissue46346 superseder
2022-01-11 22:58:31vstinnersetpull_requests: + pull_request28751
2022-01-11 20:43:28christian.heimeslinkissue46346 dependencies
2022-01-11 16:22:36vstinnersetmessages: + msg410311
2022-01-11 16:21:41vstinnersetpull_requests: + pull_request28739
2022-01-11 15:53:50vstinnersetmessages: + msg410309
2022-01-11 15:08:26pacampbellsetmessages: + msg410306
2022-01-11 11:14:40vstinnersetmessages: + msg410292
2022-01-11 11:13:17vstinnersetpull_requests: + pull_request28729
2022-01-11 10:56:28vstinnersetmessages: + msg410288
2022-01-08 12:06:31vstinnersetmessages: + msg410095
2022-01-08 12:02:31vstinnersetpull_requests: + pull_request28688
2022-01-08 10:15:37serhiy.storchakasetnosy: + vstinner
2022-01-08 05:02:36pacampbellsettitle: _Py_stat using incorrect type for status argument -> _Py_stat and _Py_wstat using incorrect type for status argument
2022-01-08 04:56:17pacampbellsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28681
2022-01-08 04:12:44pacampbellsetnosy: + tim.golden, steve.dower, zach.ware, paul.moore
type: compile error
components: + Build, Windows
2022-01-08 03:48:43pacampbellcreate