Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Doubley imported module caused devastating bug
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Zac Burns  
View profile  
 More options Sep 24 2009, 1:26 pm
Newsgroups: comp.lang.python
From: Zac Burns <zac...@gmail.com>
Date: Thu, 24 Sep 2009 10:26:21 -0700
Local: Thurs, Sep 24 2009 1:26 pm
Subject: Doubley imported module caused devastating bug
Currently it is possible to import a file of one path to more than one
'instance' of a module. One notable example is "import __init__" from
a package. See http://stackoverflow.com/questions/436497/python-import-the-containin...

This recently caused a devastating bug in some of my code. What I have
is support for the Perforce global options as a context for a perforce
module. http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#...
This way one can call functions that call many perforce command and
have them execute on a different client for example.

So, in module A and module B both imported the Perforce module, but
they turned out not to be the same module. Module A did "with
Perforce.GlobalOptions(client=client): B.function()"

B.function did not receive the new GlobalOptions because of this
problem. As a result important files on the original client were
overwritten (OUCH).

I would like to propose that it be made impossible in the Python
source to import two instances of the same module.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Sep 24 2009, 3:51 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Thu, 24 Sep 2009 12:51:23 -0700
Local: Thurs, Sep 24 2009 3:51 pm
Subject: Re: Doubley imported module caused devastating bug

I believe that modules are imported only once, and my toy example
demonstrates that (python 2.5):

test_import.py:
   """testing multiple imports"""
   CONSTANT = 928
   version = (2, 0, 9)
   plug_ins = []
   random_text = 'some text here'
   def set_text(new_text):
       global random_text
       random_text = new_text
       plug_ins.append(new_text)

A.py:
   import test_import
   print test_import.version
   test_import.set_text('hello!')
   print test_import.plug_ins
   print test_import.CONSTANT
   test_import.CONSTANT = 'changed!'

B.py:
   import test_import
   print test_import.version
   test_import.set_text('world!')
   print test_import.plug_ins
   print test_import.CONSTANT

Running...
   In [1]: import A
   (2, 0, 9)
   ['hello!']
   928

   In [2]: import B
   (2, 0, 9)
   ['hello!', 'world!']
   changed!

As you can see, module A made a change to test_import.CONSTANT, and if
they were different things then B would not have seen it, yet B *did*
see it.

This makes me wonder if A) Perforce.GlobalOptions isn't actually setting
module level variables, or B) B.function is using copies of those
variables that were set when B was originally imported, so is not seeing
the changes.... or C) there is yet another wrinkle here that I don't
know about.  ;-)

~Ethan~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Banks  
View profile  
 More options Sep 24 2009, 4:38 pm
Newsgroups: comp.lang.python
From: Carl Banks <pavlovevide...@gmail.com>
Date: Thu, 24 Sep 2009 13:38:45 -0700 (PDT)
Local: Thurs, Sep 24 2009 4:38 pm
Subject: Re: Doubley imported module caused devastating bug
On Sep 24, 10:26 am, Zac Burns <zac...@gmail.com> wrote:

Impossible's a pretty strong word.

It's a reasonable request, but with Python's importing the way it is
it'd be kind of hard to do.  A Python file can be visible in multiple
ways.

However, anyone who does "import __init__" (or "from . import
__init__" with relative import) is asking for trouble, I can't think
of any valid reason to do it, and I wouldn't mind seeing that
forbidden, but it's simple to avoid.  Someone probably did that
because they didn't know how to import a containing package from one
of its modules, failing to realize that it created a new module.

Carl Banks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zac Burns  
View profile  
 More options Sep 24 2009, 4:52 pm
Newsgroups: comp.lang.python
From: Zac Burns <zac...@gmail.com>
Date: Thu, 24 Sep 2009 13:52:20 -0700
Local: Thurs, Sep 24 2009 4:52 pm
Subject: Re: Doubley imported module caused devastating bug

There are corner cases. The corner case that I ran into was that there
were two ways to find the module on PATH because one value of PATH was
over another. Since then this problem has been removed and it wasn't
too much trouble to work around - but finding the problem was a real
pain.

I am not intimately familiar with the import code and trust your
judgment that it is difficult. If people are in agreement that this
should be changed though it could be put in a list somewhere waiting
for some ambitious person to figure out the implementation, no?

Personally I think it would be worthwhile.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Sep 24 2009, 5:26 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 24 Sep 2009 17:26:03 -0400
Local: Thurs, Sep 24 2009 5:26 pm
Subject: Re: Doubley imported module caused devastating bug

/__init__.py is basically an implementation hack to make a directory
also 'be' a file. Use at one own risk, I say.

> There are corner cases. The corner case that I ran into was that there
> were two ways to find the module on PATH because one value of PATH was
> over another. Since then this problem has been removed and it wasn't
> too much trouble to work around - but finding the problem was a real
> pain.

> I am not intimately familiar with the import code and trust your
> judgment that it is difficult. If people are in agreement that this
> should be changed though it could be put in a list somewhere waiting
> for some ambitious person to figure out the implementation, no?

1. It would slow down all imports, at least a bit.

2. It would kill code that intentionally makes use of duplicate modules
(but this could be considered exploitation of a bug, perhaps). It would
also make forced module reloads harder, it not impossible. Currently,
just delete the entry in sys.modules.

3. The language itself does not specify how and where from an
implementation 'initializes' a module on first import. Indeed, CPython
has at least three options (.py, .zip, and .dll or .pyd (Windows)), with
hooks for more. Lets a take the request as specifically preventing the
creation of duplicate module objects from a particular .py file.

One implementatin *might* be add a set to sys, say sys.mod_files for
x.py or x.pyc files already used to initialize a module. The .py or .pyc
or .pyo would be stripped but the name otherwise should be the absolute
path. (Including drive letter, on Windows).

This would not cover the case when files are symlinked (or copied). For
*nix, a set of inode numbers could be used, but not for Windows.  I
suspect there might be other system-specific problems I have not thought of.

Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Carr  
View profile  
 More options Sep 27 2009, 8:42 am
Newsgroups: comp.lang.python
From: Francis Carr <coldtort...@gmail.com>
Date: Sun, 27 Sep 2009 05:42:28 -0700 (PDT)
Local: Sun, Sep 27 2009 8:42 am
Subject: Re: Doubley imported module caused devastating bug

> I would like to propose that it be made impossible in the Python
> source to import two instances of the same module.

A fully-automatic solution is more difficult than it might seem at
first:
  http://www.python.org/dev/peps/pep-0328/
But there is a simple code-discipline solution: never ever use
relative imports, even between code in the same package.

We got "bit" by double-imports (a mix of relative, absolute, and even
cross-imports A "import B" and B "import A") early on in one of our
projects.  The symptom was that the imported module would be
initialized *twice*, once for a relative import and once for an
absolute.  This is not a happy situation for pseudo-singletons like
the "logging" module --- esp. if one is hacking the internals! :-)  We
no longer use relative imports *EVER*, even within the same package.

Perhaps Perforce is doing something tricky with scoping or importing,
and you've just managed to stumble across this trickiness because of a
double-import.  Good luck, these things are a bugger to debug.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »