Module executed twice when imported!

2006-06-28 Thread Michael Abbott
It seems to be an invariant of Python (insofar as Python has invariants) 
that a module is executed at most once in a Python session.  I have a 
rather bizzare example that breaks this invariant: can anyone enlighten 
me as to what is going on?

--- test.py ---
import imptest
execfile('subtest.py', dict(__name__ = 'subtest.py'))
--- imptest.py ---
print 'Imptest imported'
--- subtest.py ---
import imptest
---

   $ python test.py
   Imptest imported
   Imptest imported
   $

Hmm.  If the value __name__ is omitted from the dictionary, or if its 
value doesn't match at least 'subtest.' then the message is printed only 
once (for example, passing dict(__name__='subtest') produces one 
"imported" message).

This happens with both python 2.4 and 2.3.  I don't get it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Import bug: Module executed twice when imported!

2006-06-30 Thread Michael Abbott
Bump

In article <[EMAIL PROTECTED]>,
 Michael Abbott <[EMAIL PROTECTED]> wrote:

> --- test.py ---
> import imptest
> execfile('subtest.py', dict(__name__ = 'subtest.py'))
> --- imptest.py ---
> print 'Imptest imported'
> --- subtest.py ---
> import imptest
> ---
> 
>$ python test.py
>Imptest imported
>Imptest imported
>$

I claim this as an unreported (and highly obscure) Python bug.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Module executed twice when imported!

2006-06-30 Thread Michael Abbott
In article <[EMAIL PROTECTED]>,
 John Salerno <[EMAIL PROTECTED]> wrote:

> > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
> > "It is different from the import statement in that it does not use the
> > module administration --"
> 
> Just after the above statement, it also says:
> 
> "it reads the file unconditionally and does not create a new module."
> 
> so perhaps that means that it isn't really being imported the second 
> time, just that the contents are being executed?

Interesting thought.

I'm not convinced, though: firstly, I read that statement as describing 
what happens to the file named in the execfile() statement;  and 
secondly, the problem *only* happens if the global dictionary passed to 
execfile() has a '__name__' and if the value of that key is sufficiently 
close to the name of the file being passed to execfile().

I found that passing __name__='whatever' resulted in normal import 
behaviour, and so does __name__='subtest', but curiously enough passing 
__name__='subtest.' results in the double import.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Module executed twice when imported!

2006-06-30 Thread Michael Abbott
In article <[EMAIL PROTECTED]>,
 Georg Brandl <[EMAIL PROTECTED]> wrote:
> That's because __name__ is normally set to the module's name in the package
> hierarchy. When you set it to "some1.some2", Python thinks it's
> in a subpackage

A.

So what I *should* have set it to is the module name *without* 
extension, i.e. __name__='imptest'

Thank you.  Now I think I understand it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Import bug: Module executed twice when imported!

2006-06-30 Thread Michael Abbott
In article <[EMAIL PROTECTED]>,
 Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:

> Set __name__ to 'subtest' as it would be if you had really imported
> subtest and the import system will correctly name the modules, causing
> imptest to be imported only once.

Ach.  I get it now.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I have 100 servers which need a new backup server added to a text file, and then the backup agent restarted.

2006-07-02 Thread Michael Abbott
Ove Pettersen <[EMAIL PROTECTED]> wrote:
> for server in "server1 server2 server3  server100"; do

Two comments:

1.  Leave out the quotes(!)

2.  Either iterate as
   for server in $(seq -fserver%g 100); do
or, probably better
   for server in $(cat server-list); do
-- 
http://mail.python.org/mailman/listinfo/python-list