forking + stdout = confusion

Tim Peters tim_one at email.msn.com
Thu Apr 15 23:02:20 EDT 1999


[Clarence Gardner]
>> Mea culpa.  The os.close() *is* still necessary.  Is there yet another
>> copy of these file objects?  I tried to find that function that returns
>> the reference count, but don't see it in the manual.

[Michael Hudson]
> It's sys.getrefcount:
>
> Python 1.5.2 (#2, Apr 14 1999, 13:02:03)  [GCC egcs-2.91.66
> 19990314 (egcs-1.1.2  on linux2
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import sys
> >>> sys.getrefcount (sys.stdout )
> 5
>
> Five! I don't know where all of those are...

Two are in sys (sys.stdout & sys.__stdout__), two more are in a hidden copy
of the initial contents of sys used to keep extension modules sane, and the
fifth is-- in effect --a temporary side-effect of passing the object to
sys.getrefcount itself (you may have noticed that sys.getrefcount never
returns a count less than 2!).

It doesn't matter, though, because unlike file objects you create with
"open", Python won't close the file descriptor underlying the default
binding for sys.stdout (or .stdin or .stderr) even if all references do go
away.  Those three file objects are initialized specially (in _PySys_Init).

2-plus-2-equals-5-and-0-equals-3-ly y'rs  - tim






More information about the Python-list mailing list