Issue Details (XML | Word | Printable)

Key: MODPYTHON-54
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Graham Dumpleton
Reporter: Nicolas Lehuen
Votes: 1
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
mod_python

Add a way to import a published page into another published page

Created: 13/May/05 03:21 PM   Updated: 02/Apr/07 11:21 AM
Return to search
Component/s: importer
Affects Version/s: 3.2.7
Fix Version/s: 3.3.1

Time Tracking:
Not Specified

Issue Links:
dependent
 

Resolution Date: 12/Aug/06 08:13 AM


 Description  « Hide
Before mod_python 3.2, standard Python modules and published modules could be imported the same way, using apache.import_module. This had a number of disadvantages, leading to MODPYTHON-8, MODPYTHON-9, MODPYTHON-10, MODPYTHON-11 and MODPYTHON-12.

All these bugs were fixed by separating the published modules from the standard Python module. apache.import_module can still be used to import standard modules, but published modules are now fully managed by mod_python.publisher, and are not inserted into sys.modules.

The problem is that there is a use case of importing a published module from another published module :

/index.py----------------
def index(req):
    return "Hello, world !"

def utility_function(foobar):
    return foobar+1

/other.py----------------
import os
directory = os.path.split(__file__)[0]
other_index = apache.import_module("index",path=[directory])

def index(req):
    return "%s %i"%(other_index.index(req),other_index.utility_function(2004))

This was alread a bit of a hack in 3.1.4, but in 3.2 it does not really work the expected way since the imported module (other_index in the example) is not the same module as the one the publisher would use to publish /index.py. This could be troublesome if the developer wanted to share some data between the modules, e.g. a cache or a connection pool, but not if he only wanted to share some code.

Therefore, we need to provide a clean API in mod_python.publisher to allow developers to reference another published module.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #169959 Fri May 13 06:57:17 UTC 2005 nlehuen Working on MODPYTHON-54 - this does not work yet, but it does not break anything.
Files Changed
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/cache.py
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/publisher.py

Nicolas Lehuen made changes - 13/May/05 03:32 PM
Field Original Value New Value
Status Open [ 1 ] In Progress [ 3 ]
Repository Revision Date User Message
ASF #170815 Wed May 18 20:24:36 UTC 2005 nlehuen Implemented MODPYTHON-54 - we need a bit of documentation, now...
Files Changed
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/cache.py
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/publisher.py
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h

Nicolas Lehuen added a comment - 19/May/05 05:26 AM
I have added two function to mod_python.publisher :

def import_page(relative_path, auto_reload=True):
    """
        This imports a published page. The relative_path argument is a path
        relative to the directory of the page where import_page() is called.
        Hence, if we have index.py and foobar.py in the same directory, index.py
        can simply do something like :
        
        import mod_python.publisher
        foobar = mod_python.publisher.import_page('foobar.py')
        
        If auto_reload is True (the default), the returned object is not really
        the module itself, but a placeholder object which allows the real module
        to be automatically reloaded whenever its source file changes.
    """

and

def get_page(req, relative_path):
    """
        This imports a published page. The relative_path argument is a path
        relative to the published page where the request is really handled (not
        relative to the path given in the URL).
        
        Warning : in order to maintain consistency in case of module reloading,
        do not store the resulting module in a place that outlives the request
        duration.
    """

Now we need a bit of documentation.

Nicolas Lehuen added a comment - 21/May/05 01:49 AM
OK, I've finally settled to only implement get_page(req,path), which accepts an absolute or relative path. import_module was either too flaky in its previous implementation (with its DummyModule wrapper which I wasn't too proud of), or too ambitious in the envisioned implementation (which managed dependencies between pages) given that Vampire already does this very well.

get_page requires a request object, so it can only be used in the context of a request. So this is possible :

# index.py
from mod_python.publisher import get_page
def index(req):
    return 'foobar.py page says "%s"'%getpage(req,'foobar.py').index(req)

# foobar.py
def index(req):
    return "Hello, world !"

But this is no longer possible :

# index.py
from mod_python.publisher import import_page
foobar = import_page('foobar.py')

I keep this for a future release, with a possible convergence between Vampire and mod_python on this subject ?

Nicolas Lehuen made changes - 21/May/05 01:49 AM
Status In Progress [ 3 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Nicolas Lehuen added a comment - 21/May/05 01:50 AM
Duh, we're not resolved yet, we need a bit of documentation before !

Nicolas Lehuen made changes - 21/May/05 01:50 AM
Resolution Fixed [ 1 ]
Status Resolved [ 5 ] Reopened [ 4 ]
Nicolas Lehuen made changes - 29/Jul/05 12:30 AM
Description Before mod_python 3.2, standard Python modules and published modules could be imported the same way, using apache.import_module. This had a number of disadvantages, leading to MODPYTHON-8, MODPYTHON-9, MODPYTHON-10, MODPYTHON-11 and MODPYTHON-12.

All these bugs were fixed by separating the published modules from the standard Python module. apache.import_module can still be used to import standard modules, but published modules are now fully managed by mod_python.publisher, and are not inserted into sys.modules.

The problem is that there is a use case of importing a published module from another published module :

/index.py----------------
def index(req):
    return "Hello, world !"

def utility_function(foobar):
    return foobar+1

/other.py----------------
import os
directory = os.path.split(__file__)[0]
other_index = apache.import_module("index",path=[directory])

def index(req):
    return "%s %i"%(other_index.index(req),other_index.utility_function(2004))

This was alread a bit of a hack in 3.1.4, but in 3.2 it does not really work the expected way since the imported module (other_index in the example) is not the same module as the one the publisher would use to publish /index.py. This could be troublesome if the developer wanted to share some data between the modules, e.g. a cache or a connection pool, but not if he only wanted to share some code.

Therefore, we need to provide a clean API in mod_python.publisher to allow developers to reference another published module.
Before mod_python 3.2, standard Python modules and published modules could be imported the same way, using apache.import_module. This had a number of disadvantages, leading to MODPYTHON-8, MODPYTHON-9, MODPYTHON-10, MODPYTHON-11 and MODPYTHON-12.

All these bugs were fixed by separating the published modules from the standard Python module. apache.import_module can still be used to import standard modules, but published modules are now fully managed by mod_python.publisher, and are not inserted into sys.modules.

The problem is that there is a use case of importing a published module from another published module :

/index.py----------------
def index(req):
    return "Hello, world !"

def utility_function(foobar):
    return foobar+1

/other.py----------------
import os
directory = os.path.split(__file__)[0]
other_index = apache.import_module("index",path=[directory])

def index(req):
    return "%s %i"%(other_index.index(req),other_index.utility_function(2004))

This was alread a bit of a hack in 3.1.4, but in 3.2 it does not really work the expected way since the imported module (other_index in the example) is not the same module as the one the publisher would use to publish /index.py. This could be troublesome if the developer wanted to share some data between the modules, e.g. a cache or a connection pool, but not if he only wanted to share some code.

Therefore, we need to provide a clean API in mod_python.publisher to allow developers to reference another published module.
Fix Version/s 3.2.0 [ 11060 ]
Environment
Fix Version/s 3.3.0 [ 12310101 ]
Nicolas Lehuen made changes - 22/Oct/05 04:02 PM
Component/s importer [ 12310385 ]
Graham Dumpleton added a comment - 10/Mar/06 01:40 PM
Linked issues which will be addressed by rewritten module importer and top level handler dispatcher.

Graham Dumpleton made changes - 10/Mar/06 01:40 PM
Link This issue depends upon MODPYTHON-143 [ MODPYTHON-143 ]
Graham Dumpleton made changes - 01/Apr/06 02:18 PM
Assignee Nicolas Lehuen [ nlehuen ] Graham Dumpleton [ grahamd ]
Graham Dumpleton made changes - 01/Apr/06 02:19 PM
Status Reopened [ 4 ] In Progress [ 3 ]
Graham Dumpleton added a comment - 12/Aug/06 08:13 AM
Resolved by new module importer for 3.3, but for 3.3 release looks like the new importer will have to be enabled explicitly as will not be the default.

With the new importer, the existing apache.import_module() function should be used as mod_python.publisher will also use that itself and thus all modules are loaded via the same module caching system.

Graham Dumpleton made changes - 12/Aug/06 08:13 AM
Status In Progress [ 3 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Graham Dumpleton made changes - 02/Apr/07 11:21 AM
Status Resolved [ 5 ] Closed [ 6 ]