Minor bug in tempfile module (possibly __doc__ error)

James T. Dennis jadestar at idiom.com
Thu May 10 02:45:20 EDT 2007


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> In <1178693438.689184 at smirk>, James T. Dennis wrote:

>>  Tonight I discovered something odd in the __doc__ for tempfile
>>  as shipped with Python 2.4.4 and 2.5: it says:
>> 
>>       This module also provides some data items to the user:
>> 
>>         TMP_MAX  - maximum number of names that will be tried before
>>                    giving up.
>>         template - the default prefix for all temporary names.
>>                    You may change this to control the default prefix.
>> 
>>  ... which would lead one to think that the following code would work:
>> 
>>       >>> import tempfile
>>       >>> tempfile.template = 'mytest'
>>       >>> tf = tempfile.NamedTemporaryFile()
>>         >>> tf.name
>>       '/tmp/mytest-XXXXXX'
>> 
>>  It doesn't.

> The source says:

> __all__ = [
>    "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
>    "mkstemp", "mkdtemp",                  # low level safe interfaces
>    "mktemp",                              # deprecated unsafe interface
>    "TMP_MAX", "gettempprefix",            # constants
>    "tempdir", "gettempdir"
>   ]

> Maybe the doc should be clearer in saying "constants" too.

>>  Secondly, the author(s) of the tempfile module apparently didn't
>>  understand this either.  And no one else even noticed that the __doc__
>>  is wrong (or at least misleading -- since the only way I can see to
>>  change tempfile.template is to edit the .py file!

> You can change it by simply assigning to the name:

> In [15]: tempfile.template = 'spam'

> In [16]: tempfile.template
> Out[16]: 'spam'

	I know you can change it.  But changing it in your namespace
	doesn't change the results returned by the functions called
	from the module.

> If you want to change the outcome of the functions and objects then simply
> give the prefix as argument.

	I know how to provide the prefix arguments and that was
	never the issue.

	The issue was twofold:

		The docs are wrong (or at least confusing/misleading)

		I don't quite understand how this name/variable in
		my namespace (__main__) is able to change the value
		while the functions in the module still hold the old
		value.

-- 
Jim Dennis,
Starshine: Signed, Sealed, Delivered




More information about the Python-list mailing list