Porting to python3

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Porting to python3

Post by wmayer »

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

Re: Porting to python3

Post by Pinnacleman »

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
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

Cool, good to know! If I'm struck again somewhere, I'll be sure and talk to you! :)
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

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
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

Ok I found a solution I think. App is almost done.
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

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
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Porting to python3

Post by wmayer »

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
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Porting to python3

Post by yorik »

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: 144
Joined: Sun Oct 05, 2014 7:38 pm
Location: France

Re: Porting to python3

Post by prrvchr »

Hi,

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

Thank...
Post Reply