Trivial C-program which includes Python.h does not compile cleanly

Bug #502316 reported by anders musikka
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Python
Invalid
Unknown
python-defaults (Debian)
Fix Released
Unknown
python-defaults (Ubuntu)
Won't Fix
Wishlist
Unassigned

Bug Description

Binary package hint: python-dev

Hello!

The following C-program, test.c:

#include <stdlib.h>
#include <Python.h>

int main()
{
  return 0;
}

Does not compile cleanly on Ubuntu 9.10.
Compiling the above program using

gcc -I/usr/include/python2.6 test.c

Is expected to work with no warnings or error messages.
However, instead it yields the following error-message:

/usr/include/python2.6/pyconfig.h:1028:1: warning: "_POSIX_C_SOURCE" redefined
In file included from /usr/include/stdlib.h:25,
                 from t.c:1:
/usr/include/features.h:210:1: warning: this is the location of the previous definition
This is of course 100% repeatable.

Some information about my system:

lsb_release -rd
Description: Ubuntu 9.10
Release: 9.10

libc6: 2.10.1-0ubuntu15
python-dev: 2.6.4-0ubuntu1
gcc: 4.4.4.1-1ubuntu2

From what I understand, the immediate cause of the error is that the Python header files define a few macros to enable certain system features. However, the standard headers in glibc also enable these macros, leading to a macro redefinition warning.

Arguably, the correct fix is that the Python.h shouldn't be unconditionally defining these macros, and that upstream should fix this. However, it appears that this isn't going to happen, see:

http://docs.python.org/c-api/intro.html#includes
http://bugs.python.org/issue793764
(there are many, many more reports)

The following standard is quoted as rationale: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap02.html .
It says that a *strictly conforming* POSIX application must define the _POSIX_C_SOURCE macro before any system header is included. The Python-headers have taken it upon themselves to do this for the user. If the user does it, Python.h won't work. If the user includes a system header before Python.h, the program is no longer "strictly conforming", the system headers then decide to define _POSIX_C_SOURCE, and Python.h won't work.

In summary:
 * The Python-devs say their header file must be included first, and that this will work on a system supporting strictly conforming POSIX applications.
 * The glibc defines _POSIX_C_SOURCE, if it isn't already defined. (The program is no longer a strictly conforming POSIX application anyway.) Python.h breaks.
 * The Python developers tell us that we must include Python.h first, before *any other header*.
 * Including Python.h first works, but is not an ideal solution. Computer programs often use many different libraries. Only one header file can be included first in any c-file. One of the strengths of Ubuntu is that many different libraries can be used in the same program. As long as only one library requires that its include files be included first, things work. But what if some other package also likes to be included first? The situation isn't sustainable. Python isn't special enough to be able to stipulate that its include files should always be included first.
 * Both the Python-devs and the glibc devs are right. However, we can do better.

My very humble suggestion is that the following patch be applied by Ubuntu for the python-dev package:

--- pyconfig.h.old 2010-01-02 13:40:46.157002791 +0100
+++ pyconfig.h 2010-01-02 14:11:42.155750057 +0100
@@ -1025,7 +1025,12 @@
 /* #undef _OSF_SOURCE */

 /* Define to activate features from IEEE Stds 1003.1-2001 */
+/* NOTE! This has been removed by the Ubuntu maintainer.
+The Ubuntu OS does not require this macro in order to enable
+the latest POSIX features. Redefining it here gives gcc warnings
+about redefined symbols. See bug
 #define _POSIX_C_SOURCE 200112L
+*/

 /* Define if you have POSIX threads, and your system does not define that. */
 /* #undef _POSIX_THREADS */
@@ -1034,7 +1039,12 @@
 /* #undef _REENTRANT */

 /* Define to the level of X/Open that your system supports */
+/* NOTE! This has been removed by the Ubuntu maintainer.
+The Ubuntu OS does not require this macro in order to enable
+the system features. Redefining it here gives gcc warnings
+about redefined symbols.
 #define _XOPEN_SOURCE 600
+*/

 /* Define to activate Unix95-and-earlier features */
 #define _XOPEN_SOURCE_EXTENDED 1

The patch above stops pyconfig.h from defining _POSIX_C_SOURCE. (And also _XOPEN_SOURCE, which is a macro with exactly the same problem).

This patch will never be accepted into upstream Python, since it stops Python from being guaranteed to work on an OS which only supports strictly conforming POSIX Applications (I think). However, we know that in the context of an Ubuntu system, defining this macro isn't necessary! We have more information than the Python-devs, and we can use it.

Now, I understand that this suggestion will be met with skepticism. Altering upstream design decisions shouldn't be done lightly. But perhaps this particular modification is exactly the kind of thing which a Linux distribution should be doing: Making two otherwise incompatible packages (libc6 and python-dev) more compatible?

The effect of the proposed change, as far as I can tell, is:

Advantages:
* Programs can include Python.h and system headers in any order. The two macro definitions removed from pyconfig.h will be defined by the system headers anyway, and system headers are included by Python.h.
* The value of _POSIX_C_SOURCE will be the same for programs regardless of whether they include Python.h. Before this change, you got a lower value of _POSIX_C_SOURCE if you included Python.h. Higher values enable more features, which means that previously you could theoretically lose some POSIX-features if you included Python.h.

Disadvantages:
 * Any existing programs dependent upon the lower value of _POSIX_C_SOURCE defined by Python.h, could conceivably break. These programs can probably almost always be easily fixed by changing them to allow values of _POSIX_C_SOURCE above a certain number, rather than exactly this number.
* A program including pyconfig.h directly, without including Python.h, and without including any system header, and then being dependent on _POSIX_C_SOURCE being set, would no longer work. This seems like a contrived case. Such programs can nonetheless be fixed easily by including for instance <stdlib.h>.

In closing I just want to give a great "Thank You!" to all of the Ubuntu team. You guys are doing great work (regardless of how you respond to this report :-) ).

ProblemType: Bug
Architecture: amd64
Date: Sat Jan 2 13:11:08 2010
DistroRelease: Ubuntu 9.10
NonfreeKernelModules: nvidia
Package: python-dev 2.6.4-0ubuntu1
PackageArchitecture: all
ProcEnviron:
 LANG=en_US.UTF-8
 SHELL=/bin/bash
ProcVersionSignature: Ubuntu 2.6.31-16.53-generic
SourcePackage: python-defaults
Uname: Linux 2.6.31-16-generic x86_64
XsessionErrors:
 (gnome-settings-daemon:2251): GLib-CRITICAL **: g_propagate_error: assertion `src != NULL' failed
 (gnome-settings-daemon:2251): GLib-CRITICAL **: g_propagate_error: assertion `src != NULL' failed
 (polkit-gnome-authentication-agent-1:2288): GLib-CRITICAL **: g_once_init_leave: assertion `initialization_value != 0' failed
 (nautilus:2274): Eel-CRITICAL **: eel_preferences_get_boolean: assertion `preferences_is_initialized ()' failed

Revision history for this message
In , Matthias Klose (doko-cs) wrote : forwarded oython report

forwarded 206805 http://python.org/sf/793764
tags 206805 + upstream
thanks

Revision history for this message
In , Jesus M. Gonzalez-Barahona (jgb-gsyc) wrote : Is it a libc6-dev bug?

I'm also experiencing this bug. It is tagged "upstream", but it seems
that upstream is not considering it a bug, see:

https://sourceforge.net/tracker/?func=detail&atid=105470&aid=793764&group_id=5470

Since /usr/include/features.h is in glibc6-dev, this could be a glibc
bug, in case that we consider that it should check whether
_POSIX_C_SOURCE is defined before redefining it. That's why I'm CCing
glibc maintainers.

In any case, I guess that either this check is done there, or in
/usr/include/python2.3/pyconfig.h

I'd also suggest highering the severity of this bug: I have an ITP that
cannot compile because of it.

Saludos,

 Jesus.

--
Contribute to keep Europe free of software patents!
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es
-----
Jesus M. Gonzalez Barahona | Grupo de Sistemas y Comunicaciones
<email address hidden> / <email address hidden> | ESCET, Universidad Rey Juan Carlos
tel: +34 91 664 74 67 | c/ Tulipan s/n
fax: +34 91 488 70 49 | 28933 Mostoles, Spain

Revision history for this message
In , GOTO Masanori (gotom-debian) wrote :

At Mon, 16 Aug 2004 01:12:17 +0200,
Jesus M. Gonzalez-Barahona <email address hidden> wrote:
> I'm also experiencing this bug. It is tagged "upstream", but it seems
> that upstream is not considering it a bug, see:
>
> https://sourceforge.net/tracker/?func=detail&atid=105470&aid=793764&group_id=5470
>
> Since /usr/include/features.h is in glibc6-dev, this could be a glibc
> bug, in case that we consider that it should check whether
> _POSIX_C_SOURCE is defined before redefining it. That's why I'm CCing
> glibc maintainers.
>
> In any case, I guess that either this check is done there, or in
> /usr/include/python2.3/pyconfig.h
>
> I'd also suggest highering the severity of this bug: I have an ITP that
> cannot compile because of it.

 8. For the C programming language, shall define _POSIX_C_SOURCE to be
    200112L before any header is included

_POSIX_C_SOURCE should be defined "before any header is included".

Regards,
-- gotom

Revision history for this message
In , Jesus M. Gonzalez-Barahona (jgb-gsyc) wrote :

On Mon, 2004-08-16 at 02:22, GOTO Masanori wrote:
> 8. For the C programming language, shall define _POSIX_C_SOURCE to be
> 200112L before any header is included
>
> _POSIX_C_SOURCE should be defined "before any header is included".
>

Thanks!

Could you please give me the reference? (Sorry, I'm not familiar to
POSIX standards). It seems that this is incompatible with Python
mandate, but in any case I'd like to inform upstream about it.

Saludos,

 Jesus.

--
Contribute to keep Europe free of software patents!
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es
-----
Jesus M. Gonzalez Barahona | Grupo de Sistemas y Comunicaciones
<email address hidden> / <email address hidden> | ESCET, Universidad Rey Juan Carlos
tel: +34 91 664 74 67 | c/ Tulipan s/n
fax: +34 91 488 70 49 | 28933 Mostoles, Spain

Revision history for this message
In , GOTO Masanori (gotom-debian) wrote :

At Tue, 17 Aug 2004 03:06:42 +0200,
Jesus M. Gonzalez-Barahona <email address hidden> wrote:
> On Mon, 2004-08-16 at 02:22, GOTO Masanori wrote:
> > 8. For the C programming language, shall define _POSIX_C_SOURCE to be
> > 200112L before any header is included
> >
> > _POSIX_C_SOURCE should be defined "before any header is included".
> >
>
> Thanks!
>
> Could you please give me the reference? (Sorry, I'm not familiar to
> POSIX standards).

That phrase was taken from the following comments:

 https://sourceforge.net/tracker/?func=detail&atid=105470&aid=793764&group_id=5470

It's described at:

 Single Unix Specification 3:
 "2.2.1 Strictly Conforming POSIX Application" 8.

> It seems that this is incompatible with Python mandate, but in any
> case I'd like to inform upstream about it.

I don't know Python mandate well, but I think it's C language issue.

Regards,
-- gotom

Revision history for this message
In , Jason Dorje Short (jdorje) wrote : python2.3-dev: _POSIX_C_SOURCE conficts

Package: python2.3-dev
Version: 2.3.4-18
Followup-For: Bug #206805

I get the same problem:

In file included from /usr/include/python2.3/Python.h:8,
                 from embed.c:28:
/usr/include/python2.3/pyconfig.h:856:1: "_POSIX_C_SOURCE" redefined
In file included from /usr/include/stdio.h:28,
                 from embed.c:9:
/usr/include/features.h:171:1: this is the location of the previous
definition

-- System Information:
Debian Release: 3.1
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: i386 (i686)
Kernel: Linux 2.6.8-1-686
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)

Versions of packages python2.3-dev depends on:
ii python2.3 2.3.4-18 An interactive high-level object-o

-- no debconf information

Revision history for this message
In , Jason Dorje Short (jdorje) wrote : python2.3-dev: python defines _POSIX_C_SOURCE

Package: python2.3-dev
Version: 2.3.4-19
Followup-For: Bug #206805

I also see this problem.

In file included from /usr/include/python2.3/Python.h:8,
                 from embed.c:28:
/usr/include/python2.3/pyconfig.h:856:1: "_POSIX_C_SOURCE" redefined
In file included from /usr/include/stdio.h:28,
                 from embed.c:9:
/usr/include/features.h:171:1: this is the location of the previous
definition

rather problematic.

-- System Information:
Debian Release: 3.1
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: i386 (i686)
Kernel: Linux 2.6.8-2-686
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)

Versions of packages python2.3-dev depends on:
ii python2.3 2.3.4-19 An interactive high-level object-o

-- no debconf information

Revision history for this message
In , Matthias Klose (doko-cs) wrote : reassign python reports

reassign 270127 python2.4
reassign 257472 python2.4
reassign 233305 python2.4
retitle 233305 [fixed in 2.5] urllib2: proxy support broken with some valid proxy URIs
tags 233305 + fixed-upstream
reassign 206805 python2.4
reassign 204988 python2.4
tags 204988 + fixed-upstream
retitle 204988 [fixed in 2.5] Python does not handle Linux-specific "kernel namespace" addressing for AF_UNIX sockets
thanks

Revision history for this message
In , Sandro Tosi (morph-debian) wrote : python forwarded bug uniformation for bts-link
Revision history for this message
anders musikka (anders-musikka) wrote :
Download full text (7.0 KiB)

Binary package hint: python-dev

Hello!

The following C-program, test.c:

#include <stdlib.h>
#include <Python.h>

int main()
{
  return 0;
}

Does not compile cleanly on Ubuntu 9.10.
Compiling the above program using

gcc -I/usr/include/python2.6 test.c

Is expected to work with no warnings or error messages.
However, instead it yields the following error-message:

/usr/include/python2.6/pyconfig.h:1028:1: warning: "_POSIX_C_SOURCE" redefined
In file included from /usr/include/stdlib.h:25,
                 from t.c:1:
/usr/include/features.h:210:1: warning: this is the location of the previous definition
This is of course 100% repeatable.

Some information about my system:

lsb_release -rd
Description: Ubuntu 9.10
Release: 9.10

libc6: 2.10.1-0ubuntu15
python-dev: 2.6.4-0ubuntu1
gcc: 4.4.4.1-1ubuntu2

From what I understand, the immediate cause of the error is that the Python header files define a few macros to enable certain system features. However, the standard headers in glibc also enable these macros, leading to a macro redefinition warning.

Arguably, the correct fix is that the Python.h shouldn't be unconditionally defining these macros, and that upstream should fix this. However, it appears that this isn't going to happen, see:

http://docs.python.org/c-api/intro.html#includes
http://bugs.python.org/issue793764
(there are many, many more reports)

The following standard is quoted as rationale: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap02.html .
It says that a *strictly conforming* POSIX application must define the _POSIX_C_SOURCE macro before any system header is included. The Python-headers have taken it upon themselves to do this for the user. If the user does it, Python.h won't work. If the user includes a system header before Python.h, the program is no longer "strictly conforming", the system headers then decide to define _POSIX_C_SOURCE, and Python.h won't work.

In summary:
 * The Python-devs say their header file must be included first, and that this will work on a system supporting strictly conforming POSIX applications.
 * The glibc defines _POSIX_C_SOURCE, if it isn't already defined. (The program is no longer a strictly conforming POSIX application anyway.) Python.h breaks.
 * The Python developers tell us that we must include Python.h first, before *any other header*.
 * Including Python.h first works, but is not an ideal solution. Computer programs often use many different libraries. Only one header file can be included first in any c-file. One of the strengths of Ubuntu is that many different libraries can be used in the same program. As long as only one library requires that its include files be included first, things work. But what if some other package also likes to be included first? The situation isn't sustainable. Python isn't special enough to be able to stipulate that its include files should always be included first.
 * Both the Python-devs and the glibc devs are right. However, we can do better.

My very humble suggestion is that the following patch be applied by Ubuntu for the python-dev package:

--- pyconfig.h.old 2010-01-02 13:40:46.157002791 +0100
+++ pyconfig.h 2010-01-02 14:11:...

Read more...

Revision history for this message
anders musikka (anders-musikka) wrote :
Revision history for this message
Matthias Klose (doko) wrote :

won't fix as long as upstream doesn't change this

Changed in python-defaults (Ubuntu):
importance: Undecided → Wishlist
status: New → Triaged
Changed in python:
status: Unknown → Invalid
Changed in python-defaults (Debian):
status: Unknown → Confirmed
Changed in python-defaults (Debian):
status: Confirmed → Fix Released
Matthias Klose (doko)
Changed in python-defaults (Ubuntu):
status: Triaged → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers