Skip to content

Commit

Permalink
new import_module
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha authored and gtrubetskoy committed Mar 23, 2002
1 parent b5c5ac0 commit c478700
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 70 deletions.
9 changes: 6 additions & 3 deletions CREDITS
Expand Up @@ -5,9 +5,9 @@ development of mod_python.
Anyone who has contributed code or otherwise made contributions that
were constructive to the development of the project may have his name
listed here. Note that the decision on whether a name goes on this
list or not is initially taken by me (grisha@modpython.org) and that I can
be wrong. So if you feel that you should be credited as well, or if
you feel that you should not be listed, please e-mail me.
list or not is initially taken by me (grisha@modpython.org) and that I
can be wrong. So if you feel that you should be credited as well, or
if you feel that you should not be listed, please e-mail me.

The names are listed alphabetically by last name.

Expand All @@ -23,6 +23,9 @@ James Gessling <jgessling@etrade.com>
Mads Kiilerich <mads@kiilerich.com>
[RH rpm]

J�rgen Fr�jk Kj�rsgaard <jfk@metation.com>
[rewrite of apache.import_module]

Miguel Marques <miguel@yorku.ca>
[use of req->request_config instead of atol hack]

Expand Down
122 changes: 55 additions & 67 deletions lib/python/mod_python/apache.py
Expand Up @@ -41,7 +41,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================
#
# $Id: apache.py,v 1.39 2001/11/06 05:06:58 gtrubetskoy Exp $
# $Id: apache.py,v 1.40 2002/03/23 17:54:02 gtrubetskoy Exp $

import sys
import string
Expand Down Expand Up @@ -377,93 +377,81 @@ def ReportError(self, req, etype, evalue, etb, phase="N/A", hname="N/A", debug=0
# we do not return anything

def import_module(module_name, req=None, path=None):
"""
"""
Get the module to handle the request. If
autoreload is on, then the module will be reloaded
if it has changed since the last import.
"""

# get the options
autoreload, debug = 1, None
# Get options
debug, autoreload = 0, 1
if req:
config = req.get_config()
debug = config.has_key("PythonDebug")
if config.has_key("PythonAutoReload"):
autoreload = int(config["PythonAutoReload"])
debug = config.has_key("PythonDebug")

# try to import the module

oldmtime = None
mtime = None

if not autoreload:

# import module
module = __import__(module_name)
components = string.split(module_name, '.')
for cmp in components[1:]:
module = getattr(module, cmp)

else:

# keep track of file modification time and
# try to reload it if it is newer
if sys.modules.has_key(module_name):

# the we won't even bother importing
module = sys.modules[module_name]

# does it have __mtime__ ?
if sys.modules[module_name].__dict__.has_key("__mtime__"):
# remember it
oldmtime = sys.modules[ module_name ].__mtime__

# (Re)import
if sys.modules.has_key(module_name):

# The module has been imported already
module = sys.modules[module_name]

# import the module for the first time
if autoreload:
oldmtime = module.__dict__.get("__mtime__", 0)
mtime = module_mtime(module)
else:
mtime, oldmtime = 0, 0

parts = string.split(module_name, '.')
for i in range(len(parts)):
f, p, d = imp.find_module(parts[i], path)
try:
mname = string.join(parts[:i+1], ".")
module = imp.load_module(mname, f, p, d)
finally:
if f: f.close()
if hasattr(module, "__path__"):
path = module.__path__

# find out the last modification time
# but only if there is a __file__ attr
if module.__dict__.has_key("__file__"):
else:
mtime, oldmtime = 0, -1

if mtime > oldmtime:

filepath = module.__file__
# Import the module
if debug:
s = 'mod_python: (Re)importing %s from %s' % (module_name, path)
_apache.log_error(s, APLOG_NOERRNO|APLOG_NOTICE)

parts = string.split(module_name, '.')
for i in range(len(parts)):
f, p, d = imp.find_module(parts[i], path)
try:
# this try/except block is a workaround for a Python bug in
# 2.0, 2.1 and 2.1.1. See
# http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=422004
if os.path.exists(filepath):
mname = string.join(parts[:i+1], ".")
module = imp.load_module(mname, f, p, d)
finally:
if f: f.close()
if hasattr(module, "__path__"):
path = module.__path__

mod = os.stat(filepath)
mtime = mod[stat.ST_MTIME]
if mtime == 0:
mtime = module_mtime(module)

# check also .py and take the newest
if os.path.exists(filepath[:-1]) :
module.__mtime__ = mtime

return module

# get the time of the .py file
mod = os.stat(filepath[:-1])
mtime = max(mtime, mod[stat.ST_MTIME])
def module_mtime(module):
"""Get modification time of module"""
mtime = 0
if module.__dict__.has_key("__file__"):

filepath = module.__file__

try:
# this try/except block is a workaround for a Python bug in
# 2.0, 2.1 and 2.1.1. See
# http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=422004

except OSError: pass
if os.path.exists(filepath):
mtime = os.path.getmtime(filepath)

# if module is newer - reload
if (autoreload and (oldmtime < mtime)):
module = reload(module)
if os.path.exists(filepath[:-1]) :
mtime = max(mtime, os.path.getmtime(filepath[:-1]))

# save mtime
module.__mtime__ = mtime
except OSError: pass

return module
return mtime

def resolve_object(req, module, object_str, silent=0):
"""
Expand Down

0 comments on commit c478700

Please sign in to comment.