[C++-sig] boost/python 1.33.1 breaks aliasing rules

Philipp Thomas pth at suse.de
Wed Dec 7 14:16:25 CET 2005


* Ralf W. Grosse-Kunstleve (rwgk at yahoo.com) [20051205 17:54]:

> It looks to me like you are effectively asking for a re-write of C Python.

I've now looked at the Python sources and the Python folks decided to use
-fno-strict-aliasing instead of fixing the code 
( http://sourceforge.net/tracker/index.php?func=detail&aid=766696&group_id=5470&atid=105470 ).

> I don't think we can do anything about this in boost. 

Two things would be possible, both specific to gcc:

1) use -fno-strict-aliasing (valid for gcc >= 3.0), preferably only for the
   boost python libs

2) use a union for type punning, i.e. :

*** str.cpp     2005/12/07 12:55:17     1.1
--- str.cpp     2005/12/07 13:02:18
***************
*** 8,16 ****

  detail::new_reference str_base::call(object const& arg_)
  {
      return (detail::new_reference)PyObject_CallFunction(
!         (PyObject*)&PyString_Type, "(O)",
!         arg_.ptr());
  }

  str_base::str_base()
--- 8,17 ----

  detail::new_reference str_base::call(object const& arg_)
  {
+     union { PyTypeObject *ptp; PyObject *pop; } pun = { &PyString_Type };
+
      return (detail::new_reference)PyObject_CallFunction(
!         pun.pop, "(O)", arg_.ptr());
  }

using a union to pun a type is a gcc specific solution. I'll do a
patch that changes all affected places that I can send here if there is
interest to add it to boost. Otherwise I'll only use the patch for the
version of boost in SUSE Linux.

> Since there is no inheritance in C you are probably suggesting C++ Python,
> a.k.a. Python 4000.

You only need inheritance to really cleanly solve this problem. The other
solution, like I said,  would be to use generic pointers. But that would be
something to discuss with the python folks.

Philipp



More information about the Cplusplus-sig mailing list