Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43686

Module import hook

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <thomas.calmant@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'inspired': 0.05; '(using': 0.07; 'method.': 0.07; 'deprecated': 0.09; 'framework.': 0.09; 'imported': 0.09; 'method,': 0.09; 'url:github': 0.09; 'runs': 0.10; 'python': 0.11; '(),': 0.16; 'considers': 0.16; 'deprecation': 0.16; 'dictionary,': 0.16; 'dictview': 0.16; 'distinct': 0.16; 'imports': 0.16; 'loops': 0.16; 'mechanism.': 0.16; 'modules,': 0.16; 'overriding': 0.16; 'path.': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'url:gz': 0.16; 'url:tar': 0.16; 'version).': 0.16; 'folder': 0.16; '(not': 0.18; 'module': 0.19; 'mechanism': 0.19; 'work,': 0.20; 'seems': 0.21; 'import': 0.22; '(in': 0.22; 'comfortable': 0.22; 'tests': 0.22; 'to:name:python-list@python.org': 0.22; 'install': 0.23; 'installation': 0.23; 'case.': 0.24; 'instance,': 0.24; 'module,': 0.24; 'java': 0.24; 'versions': 0.24; "i've": 0.25; 'equivalent': 0.26; 'extension': 0.26; 'nearly': 0.26; 'updating': 0.26; 'installed': 0.27; 'tried': 0.27; 'point': 0.28; 'rest': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'too.': 0.31; 'context,': 0.31; 'loading': 0.31; 'allows': 0.31; 'file': 0.32; 'class': 0.32; 'bugs': 0.33; 'cases': 0.33; 'framework': 0.33; "i'd": 0.34; 'problem': 0.35; "can't": 0.35; 'created': 0.35; 'late': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '2.6': 0.36; 'described': 0.36; 'i.e.': 0.36; 'module.': 0.36; "didn't": 0.36; 'method': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'searching': 0.37; 'too': 0.37; 'being': 0.38; 'architecture': 0.38; 'implement': 0.38; 'problems': 0.38; 'e.g.': 0.38; 'process,': 0.38; 'stopped': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'track': 0.38; 'skip:& 20': 0.39; '(from': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'according': 0.40; 'even': 0.60; 'read': 0.60; 'solve': 0.60; 'full': 0.61; 'url:u': 0.61; 'first': 0.61; 'here:': 0.62; 'notified': 0.63; 'such': 0.63; 'different': 0.65; 'thomas': 0.65; 'internet': 0.71; 'acts': 0.74; 'behavior': 0.77; 'imp': 0.84; 'started,': 0.84; 'updated,': 0.84; 'visitor': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=s6+qKxk3FGquw25PJZg/l9kysynnCLG6Cl9zIWtJWI8=; b=yk/RxH+qALIoPMAuxJv2S9euFF242fj3Ba7UxO9PNdiqgYmel8TxfgMhQyjE4Xc+KP 8nOCASO1uxIT26zCwxPw5T5165z+8lwb0PD7IvqH4anikFq4xmp6DCUuj44HNEPcrQHz vLu3Tu6CryUnyUpgs+WuW6JOk0jLNlBWOf+5sDSnnNfl2SjV57bBXlzA5e+2yGuoACTM 0+HiSP44JfYKKfvb2zrZLXH0NrduBoKvlhDCtZ8Ndw/z8x5LettnPA7085tvCsMnIQ2O Q2NCUpXFziu/OCzdXfiGu8pnY81IqxmuDH61KF3QjcfApyFMRb6HP98FniLW8bAdcz0W N7Aw==
MIME-Version 1.0
X-Received by 10.180.93.134 with SMTP id cu6mr5094492wib.8.1366131827510; Tue, 16 Apr 2013 10:03:47 -0700 (PDT)
Date Tue, 16 Apr 2013 19:03:47 +0200
Subject Module import hook
From Thomas Calmant <thomas.calmant@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type multipart/alternative; boundary=f46d043894c54c164d04da7d5ad3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.677.1366131834.3114.python-list@python.org> (permalink)
Lines 158
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366131834 news.xs4all.nl 2641 [2001:888:2000:d::a6]:38276
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43686

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,

For the context, I'm working on Pelix (https://github.com/tcalmant/ipopo),
a service-oriented architecture framework (in GPLv3), inspired by OSGi
(from the Java world).
It runs on Python >= 2.6 (with the backport of importlib) and Python 3.1
(not tested upon this version).

It considers Python modules as "bundles", i.e. artifacts having a
life-cycle, that can be installed, started, updated, stopped and
uninstalled.

The current implementation allows to install each module individually
according to their full name, using the importlib standard module.
I'm not yet updating the sys.modules dictionary, so any import made in
module will use a different instance than the one created by an
installation using the framework (if any).

I've made some tests the code is available here:
https://dl.dropboxusercontent.com/u/59622687/ModuleLoaderTest.tar.gz

1/ I'd like have the equivalent of a distinct sys.modules dictionary per
framework instance, in order to solve the problem of the different versions
of an imported and installed module, in the same framework.

In my tries, I used a DictView class that replaces sys.modules during the
import process, which allows to write in a given dictionary, but allows to
read from other ones too.
This way, the import process can write in a framework dictionary and can
see the rest of the original sys.modules too (which is left untouched).

To hook the imports:
- I've tried overriding __import__, but I'm not comfortable with it. The
replacement of sys.modules doesn't seem to be enough in Python 2 in this
case.
- I've tried with the Finder/Loader mechanism, but I didn't succeed in
having the exact same behavior than without hooks: I get recursive loops in
some cases in Python 2, or can't find some sub-modules in Python 3.
Also, my finder/loader are based on the imp.find/load_module method, which
will be deprecated in Python 3.4.

a) Which mechanism would you use to implement such a behavior ?
b) Is the deprecation of imp methods a big problem ?
c) Is there any clean Finder/Loader implementation that acts like the
standard import process ? All those I've found on the Internet had the same
problems than me.

Also, I'd like to keep track of the imports from a module installed in the
framework, to store them inside the framework modules dictionary (which is
an extension of point 1, I suppose), even if they are late imports, e.g.
from inside a method.
I'm nearly OK with that using the Finder/Loader mechanism.

As all I want is being called before and after every import (to set up
sys.modules and the framework, then clean it up), without re-implementing
the searching and loading of modules,
is there a way to be only notified of those events ?


2/ I'd like to have a method to install modules by file path, and to
install the modules present in a package folder (using the visitor pattern).
Once again, I think using the imp.find/load_module method to handle a
module file path.
For the package loading, the implementation in loader.py is based on
pkgutil.iter_modules.

This part seems to work, but I'm facing the bugs described in the first
point.
Once again, which mechanisms would you use to do that ?


Thanks for your answers :)
Regards,
Thomas

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Module import hook Thomas Calmant <thomas.calmant@gmail.com> - 2013-04-16 19:03 +0200

csiph-web