Debugging of Macros

Need help, or want to share a macro? Post here!
usbhub
Posts: 193
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Debugging of Macros

Postby usbhub » Fri Sep 20, 2019 8:46 pm

Hello,
is it possible to debug a macro (not a workbench) with FreeCAD? I use Linux, so I can't use Visual Studio.
It would be OK, if it is only possible about the command line (GUI would be nicer, but I will stay alive ;) ).

Thanks in advance!
openBrain
Posts: 2716
Joined: Fri Nov 09, 2018 5:38 pm

Re: Debugging of Macros

Postby openBrain » Fri Sep 20, 2019 8:48 pm

What kind of debugging do you expect?
Often printing manual debug traces in the console is enough. ;)
vocx
Posts: 2328
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Postby vocx » Fri Sep 20, 2019 9:17 pm

usbhub wrote:
Fri Sep 20, 2019 8:46 pm
is it possible to debug a macro (not a workbench) with FreeCAD?
...
What do you mean by "debug"? There is no standard interpretation for this word. It basically means "test that everything is okay, and that there are no errors (bugs)".

So, yes, you can debug any macro, just by looking at the code, and testing each line in sequence until you find a problem.
usbhub
Posts: 193
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Debugging of Macros

Postby usbhub » Fri Sep 20, 2019 9:45 pm

With "debugging" I mean things like stepping, stop points, etc.
Of course, you can do a lot with console logging, but for bigger projects, it is a bit uncomfortable, so I asked.

usbhub
Buddy Hy
Posts: 32
Joined: Sat Aug 31, 2019 11:30 am

Re: Debugging of Macros

Postby Buddy Hy » Fri Sep 20, 2019 11:07 pm

Sorry to hijack this thread, but I was about to ask the same question.
I get an error, but can't seem to set a break point so I and step through the code to inspect variables and objects.
Is there a separate workbench for this? Or is it all guess work?
wmayer
Site Admin
Posts: 15122
Joined: Thu Feb 19, 2009 10:32 am

Re: Debugging of Macros

Postby wmayer » Fri Sep 20, 2019 11:12 pm

An option is the program Winpdb. Just search for it in the forum to get some details how to use it.
vocx
Posts: 2328
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Postby vocx » Sat Sep 21, 2019 12:22 am

Buddy Hy wrote:
Fri Sep 20, 2019 11:07 pm
...
I get an error, but can't seem to set a break point so I and step through the code to inspect variables and objects.
Is there a separate workbench for this? Or is it all guess work?
See this thread: Python workbenches debugging.

There is no difference between a workbench and a macro, it's the same thing essentially, it's Python code.

You should develop your code in such a way that is easy to test, using small functions and classes, so that it is obvious where the errors are, and you don't have to inspect every single variable and so on.

For example, I dislike the way many macros are programmed because they use a lot of global variables. Creating classes and modifying the attributes of a class instance would be much better than using a hundred global variables.
Buddy Hy
Posts: 32
Joined: Sat Aug 31, 2019 11:30 am

Re: Debugging of Macros

Postby Buddy Hy » Sat Sep 21, 2019 8:17 am

Thanks Vox, great post you sent us to - I'll be checking out 'pdbpp'.

The program I have trouble with only has 35 lines and I know exactly where the problem is - I just don't know why.
User avatar
Kunda1
Posts: 6209
Joined: Thu Jan 05, 2017 9:03 pm

Re: Debugging of Macros

Postby Kunda1 » Sat Sep 21, 2019 8:10 pm

vocx wrote:
Sat Sep 21, 2019 12:22 am
For example, I dislike the way many macros are programmed because they use a lot of global variables. Creating classes and modifying the attributes of a class instance would be much better than using a hundred global variables.
@vocx would you mind finding examples illustrating your point of good coding standards and vice-versa ?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
vocx
Posts: 2328
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Postby vocx » Sat Sep 21, 2019 8:48 pm

Kunda1 wrote:
Sat Sep 21, 2019 8:10 pm
@vocx would you mind finding examples illustrating your point of good coding standards and vice-versa ?
Well, that's just general good Python knowledge. Make things small and readable. For example, no more than 3 levels of indentation is good practice. If you need more levels, then encapsulate the inner levels into a function, and call that function.

In the Doxygen page I placed relevant documents for the reader. This write up by Moult summarizes many good programming practices; good for both C++ and Python that we use in FreeCAD.
carlopav wrote:
Sat Aug 10, 2019 9:05 pm
Any advice on codeformatting Yorik?
I did find very useful this article by @Moult https://sevenstrokes.net/learn-how-write-good-code .
Maybe it is off topic... But it was really helpful to me.