Python.h conflicts with stdlib.h if included after that
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Issue: Inconsistent _POSIX_C_SOURCE definition causes compilation errors when Python.h is included after system headers
Environment:
- Fedora 44
- CPython 3.14 (distro-provided packages)
- Go 1.26 with CGo
Description:
When Python.h is included after system headers such as stdlib.h, a compilation error occurs due to conflicting definitions of the _POSIX_C_SOURCE macro. The conflict arises because:
Python.hindirectly defines_POSIX_C_SOURCEvia its inclusion ofpyconfig-64.h(located at/usr/include/python3.14/pyconfig-64.h)- System headers like
stdlib.halso define_POSIX_C_SOURCEvia their inclusion of/usr/include/features.h - The two definitions differ, triggering a compiler diagnostic
Context:
While the official CPython documentation recommends including Python.h before any other headers, this is not always feasible. In my specific use case, I'm using the libpython3.so shared library from Go via CGo. The CGo toolchain automatically generates a C helper file that puts #include <stdlib.h> before any user-provided code, making it impossible to adhere to the "include Python.h first" recommendation.
Current workaround:
I'm currently using #undef _POSIX_C_SOURCE immediately before including Python.h:
#undef _POSIX_C_SOURCE
#include <Python.h>
However, this approach is fragile and potentially unsafe, as it may cause system headers to be compiled with different _POSIX_C_SOURCE values across translation units, leading to subtle ABI mismatches or undefined behavior.
Proposed fix:
Modify the generated pyconfig-*.h files to guard the _POSIX_C_SOURCE definition with #ifndef/#endif preprocessor directives, similar to how other feature test macros are often handled:
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L /* or whatever version is currently used */
#endif
This change would make the definition idempotent and prevent compilation errors regardless of include order, while maintaining the intended feature test macro settings.
Impact:
This issue affects any project that:
- Uses CPython's C API
- Cannot control the include order of system headers (e.g., via CGo, SWIG, or other binding generators)
- Builds on platforms where
_POSIX_C_SOURCEis defined by system headers
Cc: @sobolevn
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-155343
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng việc xem xét PR được liên kết trong issue, gh-155343, cùng các tệp pyconfig-*.h được tạo có liên quan đến định nghĩa _POSIX_C_SOURCE. So sánh cách Python.h hoạt động khi được include sau stdlib.h, sau đó sử dụng thiết lập Fedora 44 và CPython 3.14 được báo cáo để tái hiện lỗi biên dịch. Được xem là hoàn tất khi trường hợp include-order không còn tạo ra các chẩn đoán macro xung đột mà không đưa vào các thiết lập feature-test không nhất quán.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, python
- Lĩnh vực
- build-system
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100