doctest environment question

Peter Otten __peter__ at web.de
Mon May 21 17:17:14 EDT 2007


tag wrote:

> On 21 May, 18:53, Peter Otten <__pete... at web.de> wrote:
>> The doctest code is executed in a module without a __name__, it seems.
>> Unfortunately (in this case) the builtin module serves as a fallback
>> helping out with its own name:
>>
>> >>> __name__
>> '__main__'
>> >>> del __name__
>> >>> __name__
>>
>> '__builtin__'
>>
>> What to do about it? doctest could be changed to execute code snippets in
>> a module with a name and a sys.modules entry though I don't see much
>> benefit here.
> 
> Peter, thanks for the quick response, but I don't quite understand
> what you're saying. I don't want to change doctest -- I want to find a
> way to make my example pass using doctest.

The environment that doctest provides is similar to the interactive
interpreter but not identical. In particular there is no meaningful
__name__. 
 
> doctest.testfile comes with lots of parameters, and I suspect if I
> knew how to do it I could pass in the correct parameters to make this
> example work. It's not what I actually want to document/test, it's a
> minimal example which demonstrates the problem.

Here are two alternatives:

(1)
>>> def f(): pass
...
>>> del f
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined

(2)
>>> def f(): pass
...
>>> del globals()["f"]
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined

If these don't work you'll have to give a bit more context.

Peter



More information about the Python-list mailing list