[issue2021] Turn NamedTemporaryFile into a context manager

Alexander Belopolsky report at bugs.python.org
Sun Feb 10 23:57:19 CET 2008


Alexander Belopolsky added the comment:

Nick's comment made me think why NamedTemporaryFile can't simply 
subclass file and get properly working context manager's methods for 
free.

It turned out that although file is subclassable, in its present form, 
it does not allow NamedTemporaryFile implementation for the following 
reasons:

1. os.fdopen cannot create a subclass of file.

2. file.__exit__ does not call subclass' close method.

The attached patch fixes both issues and reimplements NamedTemporaryFile.

I understand that adding new functionality to file objects should be 
brought up on python-dev, but I would like to hear comments from the 
"nosy list" first.

The patch is proof-of-concept quality at the moment and may not work 
non-posix platforms.

Added file: http://bugs.python.org/file9401/subclass-file.diff

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2021>
__________________________________


More information about the Python-bugs-list mailing list