python debugging

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
cline
Posts: 56
Joined: Tue Dec 29, 2009 11:43 pm

python debugging

Post by cline »

Are there python debugging tools that can be used from within FreeCAD? Breakpoints, object examination, stepping - the usual stuff?

Thanks.
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: python debugging

Post by wmayer »

The very best would be that FreeCAD itself has an integrated debugger which is possible but a lot of work. Sometime in the future we'll start to implement it. In the meantime you can try the following solution:

As I don't know of a (Python) debugger that does an interprocess communication (via pipes or something similar) you can't start the usual FreeCAD executable but load its FreeCADGui module. The Python IDE you want to use must be based on GLIB or Win32 and run the following Python code (enter it into its console window if there is one)

Code: Select all

import FreeCADGui
FreeCADGui.showMainWindow()
Inside the Qt library there is some magic which automatically installs some kind of hook into the event loop of its host (i.e. the IDE) which makes FreeCAD running as a normal application. Then you can run and debug your script using the FreeCAD Python modules in your IDE.
cline
Posts: 56
Joined: Tue Dec 29, 2009 11:43 pm

Re: python debugging

Post by cline »

The easiest approach might be redirecting stdin and stdout+stderr to a pair of pipes (or Windows equivalent, I don't do Windows), then launching a terminal emulator running a bidirectional echo program. Instead of starting FreeCAD normally, load pdb or equivalent and execute pdb.run('FreeCADGui.showMainWindow()') instead of FreeCADGui.showMainWindow(), with the necessary imports.

I don't see FreeCADGui.showMainWindow in the python interpreter shown inside FreeCAD, nor do I know how FreeCAD starts up. So I'm not sure how to proceed with your suggestion. I'm too invested in learning the python side of FreeCAD (and learning python, for that matter) to look at this at the moment. I have a lot of experience developing programming languages (and weird one's for that matter - ever seen continuation passing style executable code?), so I'm used to working sans debugger. I would offer my perspective that a debugging interface will be very helpful for many people interested in learning FreeCAD python internals, as well as a time saver for the rest of us.

In the meantime, I'll keep chugging away at the Draft Module, where I'm working with Yorik and getting acquainted with the program.

Ken
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: python debugging

Post by wmayer »

The solution I'm talking about is described in a PDF at http://chaos.troll.no/~ahanssen/devdays ... tyAPIs.pdf from page 42 on.
I don't see FreeCADGui.showMainWindow in the python interpreter shown inside FreeCAD, nor do I know how FreeCAD starts up. So I'm not sure how to proceed with your suggestion.
Nothing further needs to be done here as I stated in my first post. Some months ago I tried this with PythonWin a rather old Python IDE which is based on MFC (and thus is based on Win32) and it worked pretty well.
The easiest approach might be redirecting stdin and stdout+stderr to a pair of pipes (or Windows equivalent, I don't do Windows), then launching a terminal emulator running a bidirectional echo program. Instead of starting FreeCAD normally, load pdb or equivalent and execute pdb.run('FreeCADGui.showMainWindow()') instead of FreeCADGui.showMainWindow(), with the necessary imports.
Is this something you already have done (maybe with another application) and does this work with a Python IDE?
cline
Posts: 56
Joined: Tue Dec 29, 2009 11:43 pm

Re: python debugging

Post by cline »

I found that I can "import pdb" then "pdb.set_trace()" to break into the debugger. When running FreeCAD in an Emacs shell window, the source file of the trace call is automagically displayed with the current line highlighted. stdin comes from a python input dialog box, which is annoying. The debugging is limited - for example, breakpoints don't seem to exist across event dispatches. Still it looks like stepping through the code and examining data should be there.
wmayer wrote:The solution I'm talking about is described in a PDF at http://chaos.troll.no/~ahanssen/devdays ... tyAPIs.pdf from page 42 on.
I'm not sure what you mean. That paper is about Qt. I'm thinking about embedded python debugging. I assume python I/O can come from stdin/out/err, while Qt uses window events.
Is this something you already have done (maybe with another application) and does this work with a Python IDE?
Never mind my earlier suggestion. Obviously it is easier to run FreeCAD in a terminal (or IDE) window directly - as I just did with Emacs. I'd love to get rid of the python input dialog box and let python simply read from stdin. At least while debugging. I'm not sure what the trouble with breakpoints is, but as I said I want to focus on getting up to speed on using FreeCAD from a python perspective first.
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: python debugging

Post by wmayer »

Here is an example how to do debugging with eric4 (tested on Ubuntu Hardy):
eric4 offers a so called passive debugging mode, for more information read the file /usr/share/doc/eric/README-passive-debugging.txt
In order to enable passive mode debugging in the IDE choose the
debugger tab of the preferences dialog and enable the passive mode
debugging checkbox. You may change the default port as well. Please
be aware that you have to tell the debugger the port, if it is different to the
default value of 42424.
Now restart the application and load your script you want to debug (e.g. /tmp/myScript.py). Under /usr/share/eric/modules/ you'll find the directory DebugClients and copy it to some place where you can import its modules or just extend sys.path by this path.

Start FreeCAD is normal executable. Into its Python console enter the following code:

Code: Select all

from DebugClients.Python import DebugClient as dc
c=dc.DebugClient()
import sys
sys.argv.append("--")
sys.argv.append("/tmp/myScript.py")
c.main()
This starts the remote debugger eric4 where you can debug step by step your script.

NOTE: There seems to be some issues where some Python shortcuts in FreeCAD don't exist anymore. E.g. App (=FreeCAD) or Gui (=FreeCADGui) are lost for any reason.
Post Reply