473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

import hooks

I recently had a problem caused by path extension (sys.path.extend) so
that a module/package got imported twice both as foo.module and
module, which caused all sorts of problems.

Naturally best is to use absolute paths and avoid path extension, but
this cannot be done in all places.

Anyway, I'd like to prevent such things from happening again by
inserting an __import__ hook to catch these kinds of problems.

I got it installed alright, and it works in the sense that importing
works, but it does not notice double imports. So how exactly am I to
test for this case?

I thought that if __file__ for two modules are the same but not
id(module) would work, but apparently not?

a somewhat related question: why does not built in modules have a
__file__ attribute?

my attempt:
__moduleCache = {}

def __importhook__(name, _globals=None, _locals=None, fromlist=None):
"""Sanity check module imports so that we do not get the same
problem with
doubly imported modules again"""

mod = _orig__import__(name, globals, locals, fromlist)

tempmod = mod
components = name.split(".")
for part in components[1:]:
try:
tempmod = getattr(tempmod, part)
__moduleCache[".".join(components[components.index(part):])]
= tempmod
except AttributeError:
pass

for modname in __moduleCache:
if name in sys.builtin_module_names:
break
if modname in sys.builtin_module_names:
continue

if (getattr(mod, "__file__") ==
getattr(__moduleCache[modname], "__file__") and
id(mod) != id(__moduleCache[modname])):
assert False
return mod
/Simon

PS. pleace CC me directly
Jul 18 '05 #1
0 1022

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Petri Savolainen | last post by:
I was trying to roll my own python code importer, but in the end, it seems that no matter what you try, it is always necessary to supply a REAL file object for python lower-level import machinery...
0
by: Gonçalo Rodrigues | last post by:
Hi, I need to extend the importing mechanism. More precisely, I'm working on a plugin architecture. Plugins are just normal python files but with extension ".plg". I want them to be loaded via...
4
by: Matt Whiteley | last post by:
I have some python code in a directory called customModules and this sits under the main directory which contains a piece of server.py code. Under 2.1.x and 2.2.x, I did the following in server.py...
8
by: Grant D. Watson | last post by:
If this has been answered before, or if my terminology is off, please bear with me; my Python experience is limited to use in one class and to personal projects. I'd like to do something rather...
0
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works...
43
by: Dan Perl | last post by:
Here is a python feature that I would like: to be able to import modules from an archive like the jar files in Java. Maybe a regular tar file? Maybe a python specific file type, let's call it a...
0
by: Carlos Ribeiro | last post by:
I am looking for information on packages & import hooks, including simple examples on how to implement a simple import hook. Quick googling turns out some documents, such as: -- PEP 302:...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
0
by: | last post by:
--- Patrick Stinson <patrickkidd.lists@gmail.com> escribió: Yes, it's different, meta_path is a generic mechanism that doesn't depend on sys.path and is tried before sys.path is traversed;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.