Scanning a file

Paul Watson pwatson at
Tue Nov 1 14:26:42 CET 2005

Alex Martelli wrote:
> Steve Holden <steve at> wrote:
>    ...
>>>The runtime knows it is doing it.  Please allow the runtime to tell me
>>>what it knows it is doing.  Thanks.
>>In point oif fact I don't believe the runtime does any such thing 
>>(though I must admit I haven't checked the source, so you may prove me
>>As far as I know, Python simply relies on the opreating system to close
>>files left open at the end of the program.
> Nope, see
> <
> leobject.c?rev=> :
> """
> static void
> file_dealloc(PyFileObject *f)
> {
>         int sts = 0;
>         if (f->weakreflist != NULL)
>                 PyObject_ClearWeakRefs((PyObject *) f);
>         if (f->f_fp != NULL && f->f_close != NULL) {
>                 Py_BEGIN_ALLOW_THREADS
>                 sts = (*f->f_close)(f->f_fp);
> """
> etc.
> Exactly how the OP wants to "allow the runtime to tell [him] what it
> knows it is doing", that is not equivalent to reading the freely
> available sources of that runtime, is totally opaque to me, though.
> "The runtime" (implementation of built-in object type `file`) could be
> doing or not doing a bazillion things (in its ..._dealloc function as
> well as many other functions), up to and including emailing the OP's
> cousin if it detects the OP is up later than his or her bedtime -- the
> language specs neither mandate nor forbid such behavior.  How, exactly,
> does the OP believe the language specs should "allow" (presumably,
> REQUIRE) ``the runtime'' to communicate the sum total of all that it's
> doing or not doing (beyond whatever the language specs themselves may
> require or forbid it to do) on any particular occasion...?!
> Alex

The OP wants to know which files the runtime is closing automatically. 
This may or may not occur on other or future Python implementations. 
Identifying this condition will accelerate remediation efforts to avoid 
the deleterious impact of failure to close().

The mechanism to implement such a capability might be similar to the -v 
switch which traces imports, reporting to stdout.  It might be a 
callback function.

More information about the Python-list mailing list