python / python/cpython

Python.h conflicts with stdlib.h if included after that

Aperta
#155,321 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

build type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.h indirectly defines _POSIX_C_SOURCE via its inclusion of pyconfig-64.h (located at /usr/include/python3.14/pyconfig-64.h)
  • System headers like stdlib.h also define _POSIX_C_SOURCE via 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_SOURCE is defined by system headers

Cc: @sobolevn

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-155343

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando la PR collegata nell’issue, gh-155343, e i file pyconfig-*.h generati coinvolti nella definizione di _POSIX_C_SOURCE. Confronta il comportamento di Python.h quando viene incluso dopo stdlib.h, quindi usa la configurazione segnalata con Fedora 44 e CPython 3.14 per riprodurre l’errore di compilazione. Il lavoro è completato quando il caso di include-order non produce più diagnostica relativa a macro in conflitto senza introdurre impostazioni feature-test incoerenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
build-system
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.