This is a 'for the record' email describing how to use boost.python  
with sage on Mac OS X. Thanks to the gentlefolk who walked me through  
it (especially Carl Witty).

I am assuming a working boost.python extension called 'my_ext.so'.   
"Working" means that you should be able to start up python at the  
command line, run "import my_ext" and call your functions /  
instantiate classes etc.

Attempting to do the same with sage fails.  Start sage, run "import  
my_ext" and it will fail with a message like
ImportError: dlopen(./my_ext.so, 2): Symbol not found:  
_PyUnicodeUCS2_AsWideChar

The problem is that the version of python distributed inside sage uses  
UCS4 (for consistency with linux) rather than UCS2 (which is used by  
Apple's system python).  Because libboost_python is compiled against  
Apple's Python, it assumes UCS2.  There are two possible fixes: (1)  
recompile boost against sage's python, and it will use UCS4.  (2)  
Recompile sage to use UCS2.

The second option is better for me, because I want my python  
extensions binary compatible with both system python and sage python.  
(The alternative is to have system & sage versions of boost, and  
system & sage versions of every extension.)  Changing sage's default  
unicode setting might have consequences that I don't understand, but I  
haven't been bitten yet. You will need to do this for each successive  
version of sage, which could be tedious.

===== Rebuilding sage with UCS2 =====

Sage needs to be rebuilt from source, with python using the option -- 
enable-unicode=ucs2 instead of --enable-unicode=ucs4

To do this:
1. download the sage source,
2. extract it:
tar xf sage-3.0.3.tar

3. find the sage packages
cd sage-3.0.3/spkg/standard

4. open up the python spkg:
tar xjf python-2.5.2.p2.spkg

5. open the file python-2.5.2.p2/spkg-install
open -e python-2.5.2.p2/spkg-install

6. find two instances of "--enable-unicode=ucs4" and replace them with  
"--enable-unicode=ucs2"

7. recreate the spkg:
tar cjf python-2.5.2.p2.spkg python-2.5.2.p2/

8. Then return to the main sage directory, and build sage
cd ../../
make


Once it's built, starting sage and importing my_ext should work.


==== other issues with boost.python ====

boost.python does not automagically convert sage values to ints or  
floats---for example, assume you have made the following accessible to  
python:
struct s_type {
  int n;
  void set_x(double x);
}

Running
s_inst = my_ext.s_type()
will work, but

s_inst.n = 1
will fail with a message like
ArgumentError: Python argument types in
    None.None(s_type, sage.rings.integer.Integer)
did not match C++ signature:
    None(s_type {lvalue}, int)

s_inst.set_x(3)
will similarly fail.

There are various fixes, including:
s_inst.n = int(1)
s_inst.set_x(float(3))

Another option is to switch off the sage preparser:
preparser(False)
s_inst.n = 1
preparser(True)



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to