On 21 May, 22:17, Peter Otten <[EMAIL PROTECTED]> wrote: > If these don't work you'll have to give a bit more context. > > Peter
Thanks again Peter. Here's something much closer to what I really want to do. You should be able to cut and paste this post into a file "post.txt". Running the command `python -c "import doctest; doctest.testfile('post.txt')"` gives a test failure even though everything works fine in an interpreted Python session. I'd like to find a way to make the doctest pass. >>> def announce(f): ... " Return a function which announces calls to the input function. " ... def wrapper(*v, **k): ... print "Calling %s" % f.__name__ ... return f(*v, **k) ... return wrapper We can rebind a function to announce calls to it: >>> def g(): pass ... >>> g = announce(g) >>> g() Calling g Or we can use decorator syntax: >>> @announce ... def f(): pass ... >>> f() Calling f Here's a function which rebinds a function at the top level of a module (it won't work for nested functions). >>> def announce_function(f): ... " Rebind f within a module so that calls to f are announced. " ... import inspect ... setattr(inspect.getmodule(f), f.__name__, announce(f)) Let's give it a try. This next works fine in an interactive Python session but fails when doctested. >>> def h(): pass ... >>> announce_function(h) >>> h() Calling h Here's the doctest failure: python -c "import doctest; doctest.testfile('post.txt')" ********************************************************************** File "post.txt", line 37, in post.txt Failed example: announce_function(h) 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 post.txt[8]>", line 1, in <module> announce_function(h) File "<doctest post.txt[6]>", line 4, in announce_function setattr(inspect.getmodule(f), f.__name__, announce(f)) AttributeError: 'NoneType' object has no attribute 'h' ********************************************************************** File "post.txt", line 38, in post.txt Failed example: h() Expected: Calling h Got nothing ********************************************************************** 1 items had failures: 2 of 10 in post.txt ***Test Failed*** 2 failures. -- http://mail.python.org/mailman/listinfo/python-list