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

How can a module know the module that imported it?

0 views
Skip to first unread message

kj

unread,
Nov 11, 2009, 3:16:50 PM11/11/09
to

The subject line says it all.

Thanks!

kynn

Aahz

unread,
Nov 11, 2009, 4:12:26 PM11/11/09
to
In article <hdf63i$cmp$1...@reader1.panix.com>, kj <no.e...@please.post> wrote:
>
>The subject line says it all.

You are probably trying to remove a screw with a hammer -- why don't you
tell us what you really want to do and we'll come up with a Pythonic
solution?
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

[on old computer technologies and programmers] "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As." --Andrew Dalke

kj

unread,
Nov 11, 2009, 4:55:58 PM11/11/09
to
In <hdf9bq$59s$1...@panix3.panix.com> aa...@pythoncraft.com (Aahz) writes:

>In article <hdf63i$cmp$1...@reader1.panix.com>, kj <no.e...@please.post> wrote:
>>
>>The subject line says it all.

>You are probably trying to remove a screw with a hammer

Worse: I'm trying to write Perl using Python!

>-- why don't you
>tell us what you really want to do and we'll come up with a Pythonic
>solution?

Because the problem that gave rise to this question is insignificant.
I would want to know the answer in any case. *Can* it be done in
Python at all?

OK, if you must know:

With Perl one can set a module-global variable before the module
is loaded. This provides a very handy backdoor during testing.
E.g.

# in t/some_test.t script
...
BEGIN { $My::Module::TESTING = 1; }
use My::Module;
...

and in My/Module.pm:

package My::Module;
our $TESTING ||= 0; # set to 0 unless already initialized to !0
...
if ($TESTING) {
# throw testing switches
}


This does not work in Python, because setting my.module.TESTING
variable can happen only after my.module has been imported, but by
this point, the module's top-level code has already been executed,
so setting my.module.TESTING would have no effect. But one way to
get a similar effect would be to have my.module set its TESTING
(or whatever) variable equal to the value of this variable in the
*importing* module.

kynn

Ethan Furman

unread,
Nov 11, 2009, 4:44:06 PM11/11/09
to pytho...@python.org
Aahz wrote:
> In article <hdf63i$cmp$1...@reader1.panix.com>, kj <no.e...@please.post> wrote:
>
>>The subject line says it all.
>
>
> You are probably trying to remove a screw with a hammer -- why don't you
> tell us what you really want to do and we'll come up with a Pythonic
> solution?


Well, I don't know what kj is trying to do, but my project is another
(!) configuration program. (Don't worry, I won't release it... unless
somebody is interested, of course !)

So here's the idea so far:
The configuration data is stored in a python module (call it
settings.py). In order to be able to do things like add settings to it,
save the file after changes are made, etc., settings.py will import the
configuration module, called configure.py.

A sample might look like this:

<settings.py>
import configure

paths = configure.Item()
paths.tables = 'c:\\app\\data'
paths.temp = 'c:\\temp'
</settings.py>

And in the main program I would have:

<some_app.py>
import settings

main_table = dbf.Table('%s\\main' % paths.tables)

# user can modify path locations, and does, so update
# we'll say it changes to \work\temp

settings.paths.temp = user_setting()
settings.configure.save()
</some_app.py>

And of course, at this point settings.py now looks like

<settings.py>
import configure

paths = configure.Item()
paths.tables = 'c:\\app\\data'
paths.temp = 'c:\\work\\temp'
</settings.py>

Now, the tricky part is the line

settings.configure.save()

How will save know which module it's supposed to be re-writing? The
solution that I have for now is

def _get_module():
"get the calling module -- should be the config'ed module"
target = os.path.splitext(inspect.stack()[2][1])[0]
target = __import__(target)
return target

If there's a better way, I'd love to know about it!

Oh, and I'm using 2.5.4, but I suspect kj is using 2.6.

~Ethan~

Diez B. Roggisch

unread,
Nov 11, 2009, 5:47:31 PM11/11/09
to
> Because the problem that gave rise to this question is insignificant.
> I would want to know the answer in any case. *Can* it be done in
> Python at all?

No.

>
> OK, if you must know:
>
> With Perl one can set a module-global variable before the module
> is loaded. This provides a very handy backdoor during testing.
> E.g.
>
> # in t/some_test.t script
> ...
> BEGIN { $My::Module::TESTING = 1; }
> use My::Module;
> ...
>
> and in My/Module.pm:
>
> package My::Module;
> our $TESTING ||= 0; # set to 0 unless already initialized to !0
> ...
> if ($TESTING) {
> # throw testing switches
> }
>
>
> This does not work in Python, because setting my.module.TESTING
> variable can happen only after my.module has been imported, but by
> this point, the module's top-level code has already been executed,
> so setting my.module.TESTING would have no effect. But one way to
> get a similar effect would be to have my.module set its TESTING
> (or whatever) variable equal to the value of this variable in the
> *importing* module.

I don't understand enough (actually nothing) from perl, but I *am* a
heavily test-driven developer in Python. And never felt that need.

Sure, sometimes one wants to change behavior of a module under test,
e.g. replacing something with a stub or some such.

But where is the problem doing


--- mytest.py ---

import moduletobetested as m

m.SOME_GLOBAL = "whatever"

m.do_something()

---


Diez

Christian Heimes

unread,
Nov 11, 2009, 5:50:20 PM11/11/09
to pytho...@python.org
kj wrote:
> Because the problem that gave rise to this question is insignificant.
> I would want to know the answer in any case. *Can* it be done in
> Python at all?
>
> OK, if you must know:
>
> With Perl one can set a module-global variable before the module
> is loaded. This provides a very handy backdoor during testing.
> E.g.
>
> # in t/some_test.t script
> ....

> BEGIN { $My::Module::TESTING = 1; }
> use My::Module;
> ....

>
> and in My/Module.pm:
>
> package My::Module;
> our $TESTING ||= 0; # set to 0 unless already initialized to !0
> ....

> if ($TESTING) {
> # throw testing switches
> }
>

That's terrible style! Code should not react differently if you run it
inside an unit test harness. If you need to change the environment for
tests because you can test all aspects of your runtime environment, use
a mock up system. Please read
http://www.voidspace.org.uk/python/articles/mocking.shtml if you don't
know the term.

Christian

Steven D'Aprano

unread,
Nov 11, 2009, 6:21:26 PM11/11/09
to
On Wed, 11 Nov 2009 21:55:58 +0000, kj wrote:

> With Perl one can set a module-global variable before the module is
> loaded. This provides a very handy backdoor during testing. E.g.

Any time somebody justifies a features as "a very handy backdoor", a
billion voices cry out and then are suddenly silenced...

[...]


> This does not work in Python, because setting my.module.TESTING variable
> can happen only after my.module has been imported, but by this point,
> the module's top-level code has already been executed, so setting
> my.module.TESTING would have no effect.

(1) Then change your design so you're not relying on changing the
behaviour of top-level code. Instead of:

chant_incantation(arg) # effect module.magic, somehow...
import module
do_something_with( module.magic )

(where magic is created by the top-level code)


do this instead:

import module
do_something_with( module.make_magic(arg) )


(2) If you still want a back-door, encapsulate it in another module:


# Contents of module.py

import _secret_backdoor
if _secret_backdoor.manna > 17:
magic = 42
else:
magic = 23


Then in your calling code:

# chant an incantation to change the behaviour of module
import _secret_backdoor
_secret_backdoor.manna = 1003

import module
do_something_with(module.magic)


(3) Abuse __builtins__ by polluting it with your own junk:


# Contents of module.py

try:
my_secret_name12761
except NameError:
magic = 23
else:
magic = 42

Then in your calling code:

# chant an incantation to change the behaviour of module
import __builtins__
__builtins__.my_secret_name12761 = None

import module
do_something_with(module.magic)


But if you do this one, hundreds of Python developers carrying flaming
torches and pitchforks will track you down and do terrible things to your
corpse...


*wink*


--
Steven

Steven D'Aprano

unread,
Nov 12, 2009, 8:33:21 AM11/12/09
to


Self-modifying code?

UNCLEAN!!! UNCLEAN!!!


> Now, the tricky part is the line
>
> settings.configure.save()
>
> How will save know which module it's supposed to be re-writing?

In my opinion, the cleanest way is to tell it which module to re-write.
Or better still, tell it which *file* to write to:

settings.configure.save(settings.__file__)

which is still self-modifying, but at least it doesn't need magic to
modify itself.


--
Steven

AK Eric

unread,
Nov 12, 2009, 12:49:53 PM11/12/09
to
so:

# moduleA.py
import moduleB

# moduleB.py
import sys
stuff = sys._getframe(1).f_locals
print stuff

Prints:

{'__builtins__': <module '__builtin__' (built-in)>,
'__file__': 'C:\\Documents and Settings\\<userName>\\My Documents\
\python\\moduleA.py',
'__name__': '__main__',
'__doc__': None}

Looks like you could query stuff['__file__'] to pull what you're
after.
?

Ethan Furman

unread,
Nov 12, 2009, 1:10:53 PM11/12/09
to pytho...@python.org

The leading _ in _getframe indicates a private function to sys (aka
implementation detail); in other words, something that could easily
change between one Python version and the next.

I'm using the inspect module (for the moment, at least), and my question
boils down to: Will it work correctly on all versions of Python in the
2.x range? 3.x range?

~Ethan~

AK Eric

unread,
Nov 12, 2009, 1:35:55 PM11/12/09
to

Good point, I totally missed that. Someone had passed that solution
to me at one point and I was so excited I kind of looked that over :P

0 new messages