Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can a function know what module it's in?

12 views
Skip to first unread message

Joe Strout

unread,
Nov 11, 2008, 11:10:26 PM11/11/08
to Python List
I've been using docstring to exercise each of my modules, with code
like this:

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()


This works great when I execute each module by itself. However, if I
want to call mymodule._test() from somewhere else, it doesn't work,
because doctest.testmod() tests the __main__ module instead of
mymodule. And of course changing the call to this doesn't work either:

doctest.testmod(mymodule)

This actually works fine if I'm importing the module (with the
standard name) somewhere else, but not when I'm executing it directly,
or (I would guess) if the module is imported under a different name.

What I want to express is "doctest THIS module, right here, the one
this code is in!" But I can't find a clean way to do that.

I noticed that a function object has a __module__ attribute, that is a
reference to the module the function is in. But that just pushes the
problem back a step: how can a function get a reference to itself?

I'm sure there is a magic identifier somewhere that lets a code get a
reference to its own module, but I haven't been able to find it. Can
someone share a clue?

Thanks,
- Joe

Joe Strout

unread,
Nov 11, 2008, 11:17:49 PM11/11/08
to Python List
Some corrections, to highlight the depth of my confusion...

On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:

> doctest.testmod(mymodule)
>
> This actually works fine if I'm importing the module (with the
> standard name) somewhere else

Actually, it does not.

> I noticed that a function object has a __module__ attribute, that is
> a reference to the module the function is in.

And no, it isn't; it's the NAME of the module the function is in. I'm
not sure what good that does me. docstring.testmod does take an
optional "name" parameter, but the documentation (at least in 2.5.2)
does not explain what this parameter is used for. I tried using it
thusly:

doctest.testmod(name=_test.__module__)

but that didn't work; it appears to still be testing the __main__
module. (Perhaps the name parameter is only used to describe the
module in the output, in which case, all I've accomplished here is
getting doctest to lie.)

> I'm sure there is a magic identifier somewhere that lets a code get
> a reference to its own module, but I haven't been able to find it.
> Can someone share a clue?

This question remains open. :)

Thanks,
- Joe

Rafe

unread,
Nov 11, 2008, 11:49:31 PM11/11/08
to

import sys
this_module = sys.modules[__name__]

sys.modules is a dictionary of all modules which have been imported
during the current session. Since the module had to be imported to
access it, it will be in there. '__name__' is available inside
functions because it is in the module scope.

Classes are a little more tricky because doing something like:
this_module = sys.modules[self.__class__.__module__]
will return a different module if the class is inherited in a
different module (since the base class is __class__ now). However,
putting a function at the module level (in the super-class module)
should anchor the results (untested though).

I'm not sure if this is the answer you need with regards to doctest,
but it I think it answers the question in the subject of this thread.

- Rafe

Steve Holden

unread,
Nov 12, 2008, 8:07:42 AM11/12/08
to pytho...@python.org
Joe Strout wrote:
> Some corrections, to highlight the depth of my confusion...
>
> On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
>
>> doctest.testmod(mymodule)
>>
>> This actually works fine if I'm importing the module (with the
>> standard name) somewhere else
>
> Actually, it does not.
>
>> I noticed that a function object has a __module__ attribute, that is a
>> reference to the module the function is in.
>
> And no, it isn't; it's the NAME of the module the function is in.

Are you sure it's not a reference to the name? ;-)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Joe Strout

unread,
Nov 12, 2008, 10:10:27 AM11/12/08
to Python List
On Nov 11, 2008, at 9:49 PM, Rafe wrote:

>>> I'm sure there is a magic identifier somewhere that lets a code get
>>> a reference to its own module, but I haven't been able to find it.
>

> import sys
> this_module = sys.modules[__name__]

Beautiful! Thanks very much. For the archives, here is my standard
module-testing idiom now:

def _test():
import doctest, sys
doctest.testmod(sys.modules[__name__])

if __name__ == "__main__":
_test()

Now, when I execute the module directly, it will test itself; and if I
need to test it from the outside (for example, under pdb) I can import
the module and then run themodule._test().

To whom should I make the suggestion that this go into doctest docs?

Cheers,
- Joe

Arnaud Delobelle

unread,
Nov 12, 2008, 1:20:35 PM11/12/08
to
Joe Strout <j...@strout.net> writes:

There isn't. There was a proposal for one but it was rejected.
Nevertheless I think you can achieve this very easily.

In mymodule.py
--------------

def _test():
import doctest
import mymodule
doctest.testmod(mymodule)


That's it!

--
Arnaud

0 new messages