doctest环境问题 [英] doctest environment question

查看:64
本文介绍了doctest环境问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有在以下方面取得进展,并感谢任何

帮助。


这是一个解释性的Python会话。 />


>> import sys
def f():传递



....


>> this_module = sys.modules [__ name__]
delattr(this_module,''f'')
f()



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

NameError:名称''f''未定义


假设我想对此行为进行doctest测试。我将上面的

剪切并粘贴到文件test.txt中。然后运行:


python -c" import doctest; doctest.testfile(''test.txt'')"


这给了我意想不到的测试失败:


python -c" import doctest; doctest.testfile(''test.txt'')"

**************************** ********************** ********************

文件test.txt,第5行,在test.txt中

失败的例子:

delattr(this_module,''f'')

异常提出:

Traceback(最近一次调用最后一次):

文件" /Library/Frameworks/Python.framework/Versions/2.5/lib/

python2.5 / doctest.py",第1212行,在__run

compileflags,1)in test.globs

文件"< doctest test .txt [3]>",第1行,在< module>

delattr(this_module,''f'')

AttributeError:f

********************************************* ***** ********************

文件" test.txt",第6行,在test.txt中/>
失败的例子:

f()

预计:

回溯(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

NameError:未定义名称''f''

一无所获

****************** ******************************** ****************** **

1项失败:

2 of 5 in test.txt

***测试失败*** 2失败。

解决方案

th * *********@gmail.com 写道:


我没有在以下方面取得进展,并希望任何

帮助。


这是一个解释性的Python会话。


>>>导入系统
def f():传递



...
< blockquote class =post_quotes>


>>> this_module = sys.modules [__ name __]
delattr(this_module,''f'')
f()



Trace返回(最近一次调用最后一次):

文件"< stdin>",第1行,在< module>中

NameError:name''f''未定义


假设我想对此行为进行doctest测试。我将上面的

剪切并粘贴到文件test.txt中。然后运行:


python -c" import doctest; doctest.testfile(''test.txt'')"


这给了我意想不到的测试失败:


python -c" import doctest; doctest.testfile(''test.txt'')"

**************************** ********************** ********************

文件test.txt,第5行,在test.txt中

失败的例子:

delattr(this_module,''f'')

异常提出:

Traceback(最近一次调用最后一次):

文件" /Library/Frameworks/Python.framework/Versions/2.5/lib/

python2.5 / doctest.py",第1212行,在__run

compileflags,1)in test.globs

文件"< doctest test .txt [3]>",第1行,在< module>

delattr(this_module,''f'')

AttributeError:f

********************************************* ***** ********************

文件" test.txt",第6行,在test.txt中/>
失败的例子:

f()

预计:

回溯(最近一次调用最后一次):

档案 < stdin>",第1行,在< module>中

NameError:名称''f''未定义

一无所获

************************************************ ** ********************

1项失败:

of 2 in test.txt

***测试失败*** 2失败。



doctest代码在没有__name__的模块中执行,似乎。

不幸的是(在这种情况下)内置模块用作a backback

帮助自己的名字:


> ;> __ name__



''__ main__''


>> del __name__
__name__



''__builtin__''

该怎么办? doctest可以更改为在带有名称和sys.modules条目的

模块中执行代码片段,虽然我没有看到很多好处

这里。


Peter


5月21日18:53,Peter Otten< __ pete ... @ web.dewrote:


doctest代码在没有__name__的模块中执行,似乎。

不幸的是(在这种情况下)内置模块用作后备

帮助自己的名字:


> __ name__



''__ main__''


> del __name__
__name__



''__ builtin__''


该怎么办? doctest可以更改为在带有名称和sys.modules条目的

模块中执行代码片段,虽然我没有看到很多好处

这里。



彼得,谢谢快速反应,但我不太明白

你在说什么。我不想改变doctest - 我想找到一个

的方法让我的例子使用doctest传递。


doctest.testfile自带很多参数,我怀疑如果我知道如何做到这一点我可以通过正确的参数来使这个

示例工作。这不是我真正想要记录/测试的,它是一个

最小的例子来证明这个问题。


再次感谢。


>

Peter



tag写道:


5月21日,18:53,Peter Otten< __ pete ... @ web.dewrote:


> doctest代码在一个没有__name__的模块中执行,似乎。
不幸的是(在这种情况下)内置模块作为后备帮助
帮助自己的名字:


>> __ name__


''__ main__''


>> del __name__
__name__


'__ builtin__''

该怎么办? doctest可以更改为在具有名称和sys.modules条目的模块中执行代码片段,虽然我在这里看不到多少好处。



彼得,谢谢快速反应,但我不太明白

你在说什么。我不想改变doctest - 我想找到一个

的方式让我的例子使用doctest传递。



doctest提供的环境类似于交互式

解释器但不完全相同。特别是没有意义

__name__。


doctest.testfile附带了很多参数,我怀疑是否我

知道怎么做我可以传递正确的参数来使这个

示例工作。这不是我真正想要记录/测试的,它是一个演示问题的最小例子。



这里有两种选择:


(1)


>> def f():传递



....


>> del f
f( )



Traceback(最近一次调用最后一次):

文件"< stdin>" ,第1行,在< module>

NameError:名称''f''未定义


(2)


>> def f():传递



....


>> del全局()[" f"]
f()



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

NameError:名称''f''未定义


如果这些都不起作用你必须提供更多的背景。


彼得


I''m not making progress with the following and would appreciate any
help.

Here''s an interpreted Python session.

>>import sys
def f(): pass

....

>>this_module = sys.modules[__name__]
delattr(this_module, ''f'')
f()

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

Suppose I want to doctest this behaviour. I cut and paste the above
into a file "test.txt" then run:

python -c "import doctest; doctest.testfile(''test.txt'')"

This gives me unexpected test failures:

python -c "import doctest; doctest.testfile(''test.txt'')"
************************************************** ********************
File "test.txt", line 5, in test.txt
Failed example:
delattr(this_module, ''f'')
Exception raised:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest test.txt[3]>", line 1, in <module>
delattr(this_module, ''f'')
AttributeError: f
************************************************** ********************
File "test.txt", line 6, in test.txt
Failed example:
f()
Expected:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ''f'' is not defined
Got nothing
************************************************** ********************
1 items had failures:
2 of 5 in test.txt
***Test Failed*** 2 failures.

解决方案

th**********@gmail.com wrote:

I''m not making progress with the following and would appreciate any
help.

Here''s an interpreted Python session.

>>>import sys
def f(): pass

...

>>>this_module = sys.modules[__name__]
delattr(this_module, ''f'')
f()

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

Suppose I want to doctest this behaviour. I cut and paste the above
into a file "test.txt" then run:

python -c "import doctest; doctest.testfile(''test.txt'')"

This gives me unexpected test failures:

python -c "import doctest; doctest.testfile(''test.txt'')"
************************************************** ********************
File "test.txt", line 5, in test.txt
Failed example:
delattr(this_module, ''f'')
Exception raised:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest test.txt[3]>", line 1, in <module>
delattr(this_module, ''f'')
AttributeError: f
************************************************** ********************
File "test.txt", line 6, in test.txt
Failed example:
f()
Expected:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ''f'' is not defined
Got nothing
************************************************** ********************
1 items had failures:
2 of 5 in test.txt
***Test Failed*** 2 failures.

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


On 21 May, 18:53, Peter Otten <__pete...@web.dewrote:

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.

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.

Thanks again.

>
Peter



tag wrote:

On 21 May, 18:53, Peter Otten <__pete...@web.dewrote:

>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


这篇关于doctest环境问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆