python 3

271 views
Skip to first unread message

Bruno Deremble

unread,
Feb 19, 2018, 11:07:29 AM2/19/18
to basilisk-fr
Hi,

Has anyone tried basilisk with python 3?
I was trying to run
http://basilisk.fr/src/examples/turbulence.py

but I get a segmentation fault at
bas.omega.f = bas.noise

(similar with poisson.py: segmentation fault at a.f = lambda x,y: 0.)

thanks
bruno

Alexis Berny

unread,
Feb 19, 2018, 11:52:59 AM2/19/18
to basilisk-fr
Hi Bruno,

I don't tkink that the code can work with python 3. Indeed, as I can see from the example, the code is written in python 2. If you don't do anything in the example, then you should have get some syntax error message (coming from the print function for examples).

Have you tried to run it with python 2?

Best

-- 
Alexis Berny


Christoph Buchner

unread,
Feb 21, 2018, 4:55:51 AM2/21/18
to basilisk-fr
As far as I can see, swig at least should generate code that is compatible with both python 2 and 3 when using the  -python flag. I don't know about qcc, though, and there could be some parts in the basilisk code (e.g. in src/python.h) where implicitly python 2 is assumed.

I tried to repro this, but to add insult to injury I am using the Anaconda python distribution instead of my Ubuntu's system python, so for some issues I encountered (e.g. complaints about undefined PyInt_AsLong) I could not be sure if they arose because of that.

The print problem in the example can easily be fixed for both versions by adding a "from __future__ import print_function" at the top and using print("t=",t)

Best,
Christoph

Bruno Deremble

unread,
Mar 8, 2018, 9:22:42 AM3/8/18
to basilisk-fr
Thank you for your answers.

Here are the modified files to make it work both in python 2 and 3
I also downloaded the latest numpy.i in src
wget https://raw.githubusercontent.com/numpy/numpy/master/tools/swig/numpy.i

I included the line
#define PY_MAJOR_VERSION 2
in python.h which in principle should be in Python.h
I am not sure how you want to handle this in qcc so I leave it this way
but there is probably a better way to do it

Also, I couldn't find what is the preferred format to submit a patch so
I prefer to send the raw files so you can do a diff but don't hesitate
to let me know if there is another way to submit a patch

hope I got this right
bruno

common.i
python.h

Stephane Popinet

unread,
Mar 12, 2018, 8:53:06 AM3/12/18
to basil...@googlegroups.com
Hi Bruno,

Thanks for the patches.

I don't understand this:

#if PY_MAJOR_VERSION >= 3
int n = PyLong_AsLong (nargs);
#else
int n = PyInt_AsLong (nargs);
#endif

Why do you need to convert to long if you then store in an int? This
seems incorrect to me.

> I included the line
> #define PY_MAJOR_VERSION 2 > in python.h which in principle should be in Python.h

Hmm, if it is defined already in Python.h, then why do you define it
again? Python.h is included by qcc when you use the option -python.

Also, if this is the case, then you should get a warning about a
redefined macro when you compile.

> Also, I couldn't find what is the preferred format to submit a patch so
> I prefer to send the raw files so you can do a diff but don't hesitate
> to let me know if there is another way to submit a patch

I have added some documentation here:

http://basilisk.fr/src/Contributing

cheers,

Stephane

Bruno Deremble

unread,
Mar 12, 2018, 1:11:30 PM3/12/18
to Stephane Popinet, basil...@googlegroups.com
Hi Stephane,

> Why do you need to convert to long if you then store in an int? This
> seems incorrect to me.

I think this is the way int are handled in python 3
https://docs.python.org/3/howto/cporting.html#long-int-unification


>> I included the line
>> #define PY_MAJOR_VERSION 2 > in python.h which in principle should be in Python.h
>
> Hmm, if it is defined already in Python.h, then why do you define it
> again? Python.h is included by qcc when you use the option -python.
>
> Also, if this is the case, then you should get a warning about a
> redefined macro when you compile.

I think this is because the preprocessor doesn't have access to the
definitions in Python.h. a way around (implemented in the attached
patch) is to select the python version in the config file (done in
config.gcc here). I think it is not the worst place to do it since you
already have to choose your python include directory there.

bruno
python3_compat

Christoph Buchner

unread,
Mar 12, 2018, 3:41:34 PM3/12/18
to basilisk-fr
Also, what I don't get from an unitiated perspective why, if you make a version that works on both python 2 and 3, you add a " #define PY_MAJOR_VERSION 2"??

Christoph

Bruno Deremble

unread,
Mar 12, 2018, 4:22:26 PM3/12/18
to Christoph Buchner, basilisk-fr
indeed.. as also suggested by Stephane, I now use a simplified
terminology with @if statements instead of #if to avoid this issue

I also changed all pyint in pylong (it works on my python 2.7 but I hope
it also works for everyone).

I kept the PYTHON3 variable in config for swig

see simplified attached patch

bruno
python3_compatibility.patch

Stephane Popinet

unread,
Mar 13, 2018, 5:46:07 AM3/13/18
to basil...@googlegroups.com
Hi Bruno,

Thanks for the new patch. Unfortunately it doesn't work with python2.7
on my system when I do

cd $BASILISK/examples
make stream.py
python2.7 turbulence.py

I get:

expecting a float

from $BASILISK:python.h:get_double()

I believe I have fixed this in the attached patches. To apply them just do:

cd $BASILISK
darcs apply python_modules.patch
make clean
make

Can you check that this works for you, both for < 3 and >= 3?

A number of things would be nice to fix too:

- examples/turbulence.py is not python3-compatible (print syntax)

- matplotlib does not refresh (for examples/turbulence.py) (I had to do
some weird stuff to make this work before and it does not work
anymore)

- examples/poisson.py segfaults

If you could have a look that would be great.

cheers,

Stephane
python_modules.patch

Bruno Deremble

unread,
Mar 13, 2018, 6:39:14 AM3/13/18
to Stephane Popinet, basil...@googlegroups.com
thanks. your modifications work as well in python 2 and 3

here is a patch for turbulence.py.
I am not sure why poisson.py doesn't work (but I think it is unrelated
to my modifs). I'll look at it.

bruno
pyturbulence.patch

Stephane Popinet

unread,
Mar 13, 2018, 6:52:41 AM3/13/18
to Bruno Deremble, basil...@googlegroups.com
> I am not sure why poisson.py doesn't work (but I think it is unrelated
> to my modifs). I'll look at it.

I agree, but since you are now the official basilisk/python maintainer
;-) it would be nice if you fixed it.

cheers

Stephane
Reply all
Reply to author
Forward
0 new messages