Porting to python3

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
wmayer
Site Admin
Posts: 14645
Joined: Thu Feb 19, 2009 10:32 am

Re: Porting to python3

Postby wmayer » Sun Sep 20, 2015 11:03 pm

Thanks a lot for porting so far!
Pinnacleman
Posts: 6
Joined: Thu Sep 10, 2015 9:55 pm

Re: Porting to python3

Postby Pinnacleman » Mon Sep 21, 2015 4:13 am

Hi, Yorik:

Glad to know that the FreeCAD team is finally working on this issue. I started a new personal Python project last year with Python 3, which will need FreeCAD as a CAD front end. I have been patiently waiting. I know Python 3 very well and know the most of the difference between Python 2 and 3 as you mentioned in your previous post. I have some spare time to help on this Python 2 to 3 migration.

One thing you forgot to mention is that the Python C API is changed too between 2 and 3. For example PyCObject is replaced with PyCapsule after Python 3.1.5.

John
User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Mon Sep 21, 2015 1:50 pm

Cool, good to know! If I'm struck again somewhere, I'll be sure and talk to you! :)
User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Thu Sep 24, 2015 11:15 pm

Okay, now I'm struck!
I'm half-way through the app module, and have trouble porting this piece of code:

Code: Select all

        else if (PyFile_Check(file)) {
            PyObject* FileName = PyFile_Name(file);
            fileStr = PyString_AsString(FileName);
        }
https://github.com/yorikvanhavre/FreeCA ... e.cpp#L302

All file-related stuff has been moved out of python builtins, so no more pyfile_check or pyfile_name.
The apparently standard workaround for pyfile_check is this:

Code: Select all

    else if(PyObject_IsInstance(file, (PyObject *)&PyIOBase_Type)) {
https://github.com/mapserver/mapserver/issues/4748
But I cannot figure out how to get the file name... This PyIOBase_Type seems to be defined here:
http://svn.python.org/projects/python/t ... iomodule.h
But I don't understand what to do... There is a "filename" but marked as "unused"...
If anyone has an idea on where to look for, I'm all ears!
User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Sun Sep 27, 2015 4:39 pm

Ok I found a solution I think. App is almost done.
User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Tue Sep 29, 2015 12:58 am

User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Sat Oct 17, 2015 10:36 pm

Okay GUI is ported to python3 now.
Now when building the FreeCAd executable, I get this error (I used make VERBOSE=1 to see better):

Code: Select all

[ 33%] Linking CXX executable ../../bin/FreeCAD
cd /home/yorik/Sources/build/fcpy3/src/Main && /usr/bin/cmake -E cmake_link_script CMakeFiles/FreeCADMain.dir/link.txt --verbose=1
/usr/bin/c++   -Wall -Wno-deprecated -Wno-write-strings  -D_OCC64   CMakeFiles/FreeCADMain.dir/MainGui.cpp.o  -o ../../bin/FreeCAD -rdynamic ../../lib/libFreeCADGui.so ../../lib/libFreeCADApp.so ../../lib/libFreeCADBase.so -lpython3.5m -lxerces-c -lz -lutil -ldl -lCoin -lQtOpenGL -lQtSvg -Wl,-Bstatic -lQtUiTools -Wl,-Bdynamic -lQtWebKit -lQtGui -lQtXml -lQtNetwork -lQtCore -lboost_filesystem -lboost_program_options -lboost_regex -lboost_signals -lboost_system -lboost_thread -lpthread -lGL -lshiboken-python2.7 -lpyside-python2.7 -Wl,-rpath,/home/yorik/Sources/build/fcpy3/lib: 
../../lib/libFreeCADBase.so: undefined reference to `PyString_Check'
../../lib/libFreeCADApp.so: undefined reference to `App::PyFileIO_Type'
collect2: error: ld returned 1 exit status
src/Main/CMakeFiles/FreeCADMain.dir/build.make:119: recipe for target 'bin/FreeCAD' failed
There are obvious problems ( -lshiboken-python2.7 -lpyside-python2.7 ) that I still didn't find how to solve, but the two undefined references, I don't know how to debug this... The PyFileIO_Type at least I know where it is (only one place), but the PyString_Check I really don't know, it should have failed when compiling... There are dozens spread around the code... Any idea how I could locate better where the problem lies?
wmayer
Site Admin
Posts: 14645
Joined: Thu Feb 19, 2009 10:32 am

Re: Porting to python3

Postby wmayer » Sun Oct 18, 2015 9:05 am

You really should set this in the cmake panel: CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined" so that you get linker errors as soon as possible.

But from
../../lib/libFreeCADApp.so: undefined reference to `App::PyFileIO_Type'
it clearly must be somewhere in App.

EDIT: See comments in your branch.
User avatar
yorik
Site Admin
Posts: 11458
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Porting to python3

Postby yorik » Sun Oct 18, 2015 6:56 pm

wmayer wrote:You really should set this in the cmake panel: CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined"
Excellent, exactly what I needed. Thanks!

One day, 99% thanks to you Werner, I'll become a C++ guru too :ugeek:
prrvchr
Posts: 117
Joined: Sun Oct 05, 2014 7:38 pm
Location: France

Re: Porting to python3

Postby prrvchr » Sat Oct 24, 2015 11:17 am

Hi,

Just to know, porting to Python3 need Qt5 ??? and PySide2 ??? :roll:

Thank...