On Sun, Mar 21, 2021 at 11:10 PM Chris Angelico <rosuav@gmail.com> wrote:
At what point does the process_objects list cease to be referenced?
After the last visible use of it, or at the end of the function?

In Python as it stands, at the end of the function, as you say.

Skip Montanaro's PEP suggested that in his register machine, locals would be dereferenced after their last visible use. I don't think that's intrinsically a bad idea, but it's not backward compatible. The thing with the process objects was just an example of currently working code that would break.

The example has nothing to do with PyQt5 really. I just happen to know that QProcess objects kill the controlled process when they're collected. I think it's a bad design, but that's the way it is.

Another example would be something like

    td =  tempfile.TemporaryDirectory()
    p = subprocess.Popen([..., td.name, ...], ...)
    p.wait()

where the temporary directory will hang around until the process exits with current semantics, but not if td is deleted after the second line. Of course you should use a with statement in this kind of situation, but there's probably a lot of code that doesn't.