Python 3.5.1 C API, the global available available is not destroyed when delete the module

Chris Angelico rosuav at gmail.com
Tue Sep 20 04:01:06 EDT 2016


On Tue, Sep 20, 2016 at 4:19 PM, dl l <ldlchina at gmail.com> wrote:
> Yes, it's a workaround to set the global variables to None. But My app just
> provide a framework for my customers to run python scripts. That's means
> the workaround requires my customers to update their python scripts. That
> may make them unhappy :(. Is there a workaround in my code C++ side to call
> Python C APIs to resolve the memory leak issue?
>

Python's import system is fundamentally not designed for what I
believe is your use-case here, of running potentially-edited user
code. Instead, I suggest keeping 'import' for stable modules that
don't get updated without restarting the process, and for the more
dynamic code, build something using exec (or PyRun_StringFlags, which
I believe is the C equivalent). Object lifetimes will then be entirely
under your control.

ChrisA



More information about the Python-list mailing list