Easy debugging at FreeCAD macros using pdb.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Easy debugging at FreeCAD macros using pdb.

Post by manos »

If you want to debug a .py macro at FreeCAD the pdb debugger is the easiest way. It works at once without any tricks and wows.
You have just to put the "import pdb" command at the beginning of the .py file and next invoke the debugger at the line you wish. There are many ways for that. The simplest is by using the "pdb.set_trace" command at the wished line. When the program execution reaches this line it stops and you have at your disposal a variety of commands: Like "c" to continue running the program, "s" or "n" to execute the next line etc . Also you can type a variable name at python console and see its value at ReportView. There is a very good help about debugger usage and commands -see below.

Three tips.
1) Do not use a variable with name "c". The pdb debugger confuse the variable with the command "c" and gets crazy. Period.
2) Command:"unt [lineno]" .Use it without "[ ]" e.g. unt 35 = execute al lines until line number 35. unt [35] simply gives an error.
3) If you have reach at least one "pdb.set_trace" or similar command: At the end of program running be sure that you are out of pdb BEFORE exit FreeCAD. Aways use the pdb command "q" or "quit" before exit from FreeCAD. If you disobey this rule you will be punished with a black FreeCAD GUI or frozen FreeCAD or even frozen OSystem! .

There is a very useful post about pdb debugger by @ediloren: https://forum.freecadweb.org/viewtopic. ... ng#p298485
The help is well written and you find it at :https://docs.python.org/3/library/pdb.html

Code: Select all

OS: Ubuntu 18.04.5 LTS (ubuntu:GNOME/ubuntu)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: English/United States (en_US)
Installed mods: 
  * workfeature-macro
  * Manipulator 1.4.9
  * Help 1.0.3
  * workfeature
Post Reply