[Python-Dev] If you shadow a module in the standard library that IDLE depends on, bad things happen (original) (raw)
Ryan Gonzalez rymg19 at gmail.com
Thu Oct 29 21:00:43 EDT 2015
- Previous message (by thread): [Python-Dev] If you shadow a module in the standard library that IDLE depends on, bad things happen
- Next message (by thread): [Python-Dev] If you shadow a module in the standard library that IDLE depends on, bad things happen
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Well, this works on Python 2 only (I'm on a phone with only access to 2 right now), but this is a start (apologies for the screwy syntax highlighting):
import sys, imp, logging, os
modules = ''' imp string ... '''.split()
class StdlibTester(object): base = os.path.dirname(os.file) # hack; is there a better way to do this?
def find_module(self, fullname, path=None):
if fullname in modules:
self.path = path
return self
return None
def load_module(self, name):
if name in sys.modules:
return sys.modules[name]
module_info = imp.find_module(name, self.path)
module = imp.load_module(name, *module_info)
sys.modules[name] = module
if hasattr(module, '__file__') and not os.path.dirname(module.__file__).startswith(self.base):
logging.warning('stdlib module %s was overriden', name)
return module
sys.meta_path.append(StdlibTester()) import string
On October 29, 2015 7:06:51 PM CDT, "R. David Murray" <rdmurray at bitdance.com> wrote:
On Thu, 29 Oct 2015 16:56:38 -0700, Nathaniel Smith <njs at pobox.com> wrote:
On Thu, Oct 29, 2015 at 1:50 PM, Ryan Gonzalez <rymg19 at gmail.com> wrote: > Why not just check the path of the imported modules and compare it with the > Python library directory?
It works, but it requires that everyone who could run into this problem carefully add some extra guard code to every stdlib import statement, and in practice nobody will (or at least, not until after they've already gotten bitten by this at least once... at which point they no longer need it). Given that AFAICT there's no reason this couldn't be part of the default import system's functionality and "just work" for everyone, if I were going to spend time on trying to fix this I'd probably target that :-). (I guess the trickiest bit would be to find an efficient and maintainable way to check whether a given package name is present in the stdlib.) For Idle, though, it sounds like a very viable strategy, and that's what Laura is concerned about. --David
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/rymg19%40gmail.com
-- Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20151029/e3231cc2/attachment.html>
- Previous message (by thread): [Python-Dev] If you shadow a module in the standard library that IDLE depends on, bad things happen
- Next message (by thread): [Python-Dev] If you shadow a module in the standard library that IDLE depends on, bad things happen
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]