View Issue Details

IDProjectCategoryView StatusLast Update
0000528FreeCADFeaturepublic2011-12-12 23:06
Reportersparr0 Assigned Towmayer  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version0.11 
Target Version0.13Fixed in Version0.13 
Summary0000528: Need a way to reset the Python instance
DescriptionFreeCAD's Python instance cannot be reset (to eliminate previous imports and globals) without restarting FreeCAD. This makes many Python debugging problems virtually impossible to solve while working on models.
TagsNo tags attached.
FreeCAD Information

Activities

wmayer

2011-12-09 09:17

administrator   ~0001427

I might be wrong but this is rather a Python issue than a FreeCAD issue. And for the import especially of extension modules (i.e. .pyd or .so files) Python doesn't offer a mechanism for this. Somewhere (I don't remember where) I read that unloading of an extension module would be very complicated because Python had to unset any references and that's practically simply impossible.

To reset the globals there might be a Python function but I don't know it if so.

wmayer

2011-12-09 10:03

administrator   ~0001428

According to http://docs.python.org/c-api/init.html Py_Finalize() and Py_Initialize() could be called but this causes too much collateral damages (I tried it) and also might be too risky to use.

wmayer

2011-12-09 10:12

administrator   ~0001429

Here is a script that do some clean-up: http://pastebin.com/Yg5MSXcB

BTW, when you're editing python modules you can use the reload() function to re-import them without starting the application.

sparr0

2011-12-09 19:48

reporter   ~0001430

When I run python from the command line there is a very easy way to do this. CTRL-D then run python again. FreeCAD doesn't give me any way to do that without also restarting FreeCAD.

wmayer

2011-12-10 09:52

administrator   ~0001433

A CTRL-D also exits the python interpreter. So, it's pretty much the same as exiting FreeCAD.

When working with the command line and you restart python then you probably still have the commands in your history but this is provided by the shell and has nothing to do with python.

sparr0

2011-12-10 10:37

reporter   ~0001436

Exiting the python interpreter is not the same as exiting FreeCAD. I want the effects of exiting python, without losing all of my open drawings and scripts in FreeCAD.

This isn't about the command history, this is about the python objects. If you do "foo = 3" in the python console, then foo will be a global object available to every python command or script that you run from then on out.

To take this a step further, I'd like a way to specify for a macro "this macro should run in its own python instance". I hate having to restart FreeCAD over and over to find out if my changes to a macro are going to work for other people.

wmayer

2011-12-10 11:51

administrator   ~0001438

> To take this a step further, I'd like a way to specify for a macro "this macro should run in its own python instance". I hate having to restart FreeCAD over and over to find out if my changes to a macro are going to work for other people.

If it's only to have cleaned up your interpreter by running a macro there is a much better way. Look at the API http://pyspanishdoc.sourceforge.net/api/veryhigh.html The function PyRun_File() allows to pass a local dictionary so that the global environment is not touched. I think this would be a suitable solution. What do you think?

sparr0

2011-12-10 11:55

reporter   ~0001439

That might work. I can't say for sure. If it keeps one run of a macro from interfering with how the next run of the same macro, or another macro, works, then it would probably be a great solution.

wmayer

2011-12-10 12:06

administrator   ~0001440

Yes, actually it works the other way round. Instead of trying to clean-up your environment (which isn't very feasible anyway) we make a copy of the global dictionary and pass this copy to the function call. This means that only this copy is changed during the execution of the macro but not the global dictionary.

wmayer

2011-12-10 12:43

administrator   ~0001441

Implemented in rev 5250. I'll wait for some feedback...

Issue History

Date Modified Username Field Change
2011-12-09 03:38 sparr0 New Issue
2011-12-09 09:17 wmayer Note Added: 0001427
2011-12-09 10:03 wmayer Note Added: 0001428
2011-12-09 10:12 wmayer Note Added: 0001429
2011-12-09 19:48 sparr0 Note Added: 0001430
2011-12-10 09:52 wmayer Note Added: 0001433
2011-12-10 10:37 sparr0 Note Added: 0001436
2011-12-10 11:51 wmayer Note Added: 0001438
2011-12-10 11:55 sparr0 Note Added: 0001439
2011-12-10 12:06 wmayer Note Added: 0001440
2011-12-10 12:43 wmayer Note Added: 0001441
2011-12-10 12:43 wmayer Assigned To => wmayer
2011-12-10 12:43 wmayer Status new => resolved
2011-12-10 12:43 wmayer Fixed in Version => 0.13
2011-12-10 12:43 wmayer Target Version => 0.13
2011-12-12 23:06 wmayer Status resolved => closed
2011-12-12 23:06 wmayer Resolution open => fixed