4.5.1 Functions

log_error( message[, level, server])
An interface to the Apache ap_log_error() function. message is a string with the error message, level is one of the following flags constants:

    APLOG_EMERG
    APLOG_ALERT
    APLOG_CRIT
    APLOG_ERR
    APLOG_WARNING
    APLOG_NOTICE
    APLOG_INFO
    APLOG_DEBUG
    APLOG_NOERRNO

server is a reference to a req.server object. If server is not specified, then the error will be logged to the default error log, otherwise it will be written to the error log for the appropriate virtual server. When server is not specified, the setting of LogLevel does not apply, the LogLevel is dictated by an httpd compile-time default, usually warn.

If you have a reference to a request object available, consider using req.log_error instead, it will prepend request-specific information such as the source IP of the request to the log entry.

import_module( module_name[, autoreload=None, log=None, path=None])
This function can be used to import modules.

Note: This function and the module importer were completely reimplemented in mod_python 3.3. If you are using an older version of mod_python do not rely on this documentation and instead refer to the documentation for the specific version you are using as the new importer does not behave exactly the same and has additional features.

If you are trying to port code from an older version of mod_python to mod_python 3.3 and can't work out why the new importer is not working for you, you can enable the old module importer for specific Python interpreter instances by using:

    PythonOption mod_python.legacy.importer name

where 'name' is the name of the interpreter instance or '*' for it to be applied to all interpreter instances. This option should be placed at global context within the main Apache configuration files.

When using the apache.import_module() function, the module_name should be a string containing either the module name, or a path to the actual code file for the module; where a module is a candidate for automatic module reloading, autoreload indicates whether the module should be reloaded if it has changed since the last import; when log is true, a message will be written to the logs when a module is reloaded; path can be a list specifying additional directories to be searched for modules.

With the introduction of mod_python 3.3, the default arguments for the autoreload and log arguments have been changed to None, with the arguments effectively now being unnecessary except in special circumstances. When the arguments are left as the default of None, the Apache configuration in scope at the time of the call will always be consulted automatically for any settings for the PythonAutoReload and PythonDebug directives respectively.

Example:

    from mod_python import apache
    module = apache.import_module('module_name')

The apache.import_module() function is not just a wrapper for the standard Python module import mechanism. The purpose of the function and the mod_python module importer in general, is to provide a means of being able to import modules based on their exact location, with modules being distinguished based on their location rather than just the name of the module. Distinguishing modules in this way, rather than by name alone, means that the same module name can be used for handlers and other code in multiple directories and they will not interfere with each other.

A secondary feature of the module importer is to implement a means of having modules automatically reloaded when the corresponding code file has been changed on disk. Having modules be able to be reloaded in this way means that it is possible to change the code for a web application without having to restart the whole Apache web server. Although this was always the intent of the module importer, prior to mod_python 3.3, its effectiveness was limited. With mod_python 3.3 however, the module reloading feature is much more robust and will correctly reload parent modules even when it was only a child module what was changed.

When the apache.import_module() function is called with just the name of the module, as opposed to a path to the actual code file for the module, a search has to be made for the module. The first set of directories that will be checked are those specified by the path argument if supplied.

Where the function is called from another module which had previously been imported by the mod_python importer, the next directory which will be checked will be the same directory as the parent module is located. Where that same parent module contains a global data variable called __mp_path__ containing a list of directories, those directories will also be searched.

Finally, the mod_python module importer will search directories specified by the PythonOption called mod_python.importer.path.

For example:

    PythonOption mod_python.importer.path "['/some/path']"

The argument to the option must be in the form of a Python list. The enclosing quotes are to ensure that Apache interprets the argument as a single value. The list must be self contained and cannot reference any prior value of the option. The list MUST NOT reference sys.path nor should any directory which also appears in sys.path be listed in the mod_python module importer search path.

When searching for the module, a check is made for any code file with the name specified and having a '.py' extension. Because only modules implemented as a single file will be found, packages will not be found nor modules contained within a package.

In any case where a module cannot be found, control is handed off to the standard Python module importer which will attempt to find the module or package by searching sys.path.

Note that only modules found by the mod_python module importer are candidates for automatic module reloading. That is, where the mod_python module importer could not find a module and handed the search off to the standard Python module importer, those modules or packages will not be able to be reloaded.

Although true Python packages are not candidates for reloading and must be located in a directory listed in sys.path, another form of packaging up modules such that they can be maintained within their own namespace is supported. When this mechanism is used, these modules will be candidates for reloading when found by the mod_python module importer.

In this scheme for maintaining a pseudo package, individual modules are still placed into a directory, but the __init__.py file in the directory has no special meaning and will not be automatically imported as is the case with true Python packages. Instead, any module within the directory must always be explicitly identified when performing an import.

To import a named module contained within these pseudo packages, rather than using a '.' to distinguish a sub module from the parent, a '/' is used instead. For example:

    from mod_python import apache
    module = apache.import_module('dirname/module_name')

If an __init__.py file is present and it was necessary to import it to achieve the same result as importing the root of a true Python package, then __init__ can be used as the module name. For example:

    from mod_python import apache
    module = apache.import_module('dirname/__init__')

As a true Python package is not being used, if a module in the directory needs to refer to another module in the same directory, it should use just its name, it should not use any form of dotted path name via the root of the package as would be the case for true Python packages. Modules in subdirectories can be imported by using a '/' separated path where the first part of the path is the name of the subdirectory.

As a new feature in mod_python 3.3, when using the standard Python 'import' statement to import a module, if the import is being done from a module which was previously imported by the mod_python module importer, it is equivalent to having called apache.import_module() directly.

For example:

    import name

is equivalent to:

    from mod_python import apache
    name = apache.import_module('name')

It is also possible to use constructs such as:

    import name as module

and:

    from name import value

Although the 'import' statement is used, that it maps through to the apache.import_module() function ensures that parent/child relationships are maintained correctly and reloading of a parent will still work when only the child has been changed. It also ensures that one will not end up with modules which were separately imported by the mod_python module importer and the standard Python module importer.

With the reimplementation of the module importer in mod_python 3.3, the module_name argument may also now be an absolute path name of an actual Python module contained in a single file. On Windows, a drive letter can be supplied if necessary. For example:

    from mod_python import apache
    name = apache.import_module('/some/path/name.py')

or:

    from mod_python import apache
    import os
    here = os.path.dirname(__file__)
    path = os.path.join(here, 'module.py')
    module = apache.import_module(path)

Where the file has an extension, that extension must be supplied. Although it is recommended that code files still make use of the '.py' extension, it is not actually a requirement and an alternate extension can be used. For example:

    from mod_python import apache
    import os
    here = os.path.dirname(__file__)
    path = os.path.join(here, 'servlet.mps')
    servlet = apache.import_module(path)

To avoid the need to use hard coded absolute path names to modules, a few shortcuts are provided. The first of these allow for the use of relative path names with respect to the directory the module performing the import is located within.

For example:

    from mod_python import apache

    parent = apache.import_module('../module.py')
    subdir = apache.import_module('./subdir/module.py')

Forward slashes must always be used for the prefixes './' and '../', even on Windows hosts where native pathname use a backslash. This convention of using forward slashes is used as that is what Apache normalizes all paths to internally. If you are using Windows and have been using backward slashes with Directory directives etc, you are using Apache contrary to what is the accepted norm.

A further shortcut allows paths to be declared relative to what is regarded as the handler root directory. The handler root directory is the directory context in which the active Python*Handler directive was specified. If the directive was specified within a Location or VirtualHost directive, or at global server scope, the handler root will be the relevant document root for the server.

To express paths relative to the handler root, the '~/' prefix should be used. A forward slash must again always be used, even on Windows.

For example:

    from mod_python import apache

    parent = apache.import_module('~/../module.py')
    subdir = apache.import_module('~/subdir/module.py')

In all cases where a path to the actual code file for a module is given, the path argument is redundant as there is no need to search through a list of directories to find the module. In these situations, the path is instead taken to be a list of directories to use as the initial value of the __mp_path__ variable contained in the imported modules instead of an empty path.

This feature can be used to attach a more restrictive search path to a set of modules rather than using the PythonOption to set a global search path. To do this, the modules should always be imported through a specific parent module. That module should then always import submodules using paths and supply __mp_path__ as the path argument to subsequent calls to apache.import_module() within that module. For example:

    from mod_python import apache

    module1 = apache.import_module('./module1.py', path=__mp_path__)
    module2 = apache.import_module('./module2.py', path=__mp_path__)

with the module being imported as:

    from mod_python import apache

    parent = apache.import_module('~/modules/parent.py', path=['/some/path'])

The parent module may if required extend the value of __mp_path__ prior to using it. Any such directories will be added to those inherited via the path argument. For example:

    from mod_python import apache
    import os

    here = os.path.dirname(__file__)
    subdir = os.path.join(here, 'subdir')
    __mp_path__.append(subdir)

    module1 = apache.import_module('./module1.py', path=__mp_path__)
    module2 = apache.import_module('./module2.py', path=__mp_path__)

In all cases where a search path is being specified which is specific to the mod_python module importer, whether it be specified using the PythonOption called mod_python.importer.path, using the path argument to the apache.import_module() function or in the __mp_path__ attribute, the prefix '~/' can be used in a path and that path will be taken as being relative to handler root. For example:

    PythonOption mod_python.importer.path "['~/modules']"

If wishing to refer to the handler root directory itself, then '~' can be used and the trailing slash left off. For example:

    PythonOption mod_python.importer.path "['~']"

Note that with the new module importer, as directories associated with Python*Handler directives are no longer being added automatically to sys.path and they are instead used directly by the module importer only when required, some existing code which expected to be able to import modules in the handler root directory from a module in a subdirectory may no longer work. In these situations it will be necessary to set the mod_python module importer path to include '~' or list '~' in the __mp_path__ attribute of the module performing the import.

This trick of listing '~' in the module importer path will not however help in the case where Python packages were previously being placed into the handler root directory. In this case, the Python package should either be moved out of the document tree and the directory where it is located listed against the PythonPath directive, or the package converted into the pseudo packages that mod_python supports and change the module imports used to access the package.

Only modules which could be imported by the mod_python module importer will be candidates for automatic reloading when changes are made to the code file on disk. Any modules or packages which were located in a directory listed in sys.path and which were imported using the standard Python module importer will not be candidates for reloading.

Even where modules are candidates for module reloading, unless a true value was explicitly supplied as the autoreload option to the apache.import_module() function they will only be reloaded if the PythonAutoReload directive is On. The default value when the directive is not specified will be On, so the directive need only be used when wishing to set it to Off to disable automatic reloading, such as in a production system.

Where possible, the PythonAutoReload directive should only be specified in one place and in the root context for a specific Python interpreter instance. If the PythonAutoReload directive is used in multiple places with different values, or doesn't cover all directories pertaining to a specific Python interpreter instance, then problems can result. This is because requests against some URLs may result in modules being reloaded whereas others may not, even when through each URL the same module may be imported from a common location.

If absolute certainty is required that module reloading is disabled and that it isn't being enabled through some subset of URLs, the PythonImport directive should be used to import a special module whenever an Apache child process is being created. This module should include a call to the apache.freeze_modules() function. This will have the effect of permanently disabling module reloading for the complete life of that Apache child process, irrespective of what value the PythonAutoReload directive is set to.

Using the new ability within mod_python 3.3 to have PythonImport call a specific function within a module after it has been imported, one could actually dispense with creating a module and instead call the function directory out of the mod_python.apache module. For example:

    PythonImport mod_python.apache::freeze_modules interpreter_name

Where module reloading is being undertaken, unlike the core module importer in versions of mod_python prior to 3.3, they are not reloaded on top of existing modules, but into a completely new module instance. This means that any code that previously relied on state information or data caches to be preserved across reloads will no longer work.

If it is necessary to transfer such information from an old module to the new module, it is necessary to provide a hook function within modules to transfer across the data that must be preserved. The name of this hook function is __mp_clone__(). The argument given to the hook function will be an empty module into which the new module will subsequently be loaded.

When called, the hook function should copy any data from the old module to the new module. In doing this, the code performing the copying should be cognizant of the fact that within a multithreaded Apache MPM that other request handlers could still be trying to access and update the data to be copied. As such, the hook function should ensure that it uses any thread locking mechanisms within the module as appropriate when copying the data. Further, it should copy the actual data locks themselves across to the new module to ensure a clean transition.

Because copying integral values will result in the data then being separate, it may be necessary to always store data within a dictionary so as to provide a level of indirection which will allow the data to be usable from both module instances while they still exist.

For example:

  import threading, time

  if not globals().has_key('_lock'):
    # Initial import of this module.
    _lock = threading.Lock()
    _data1 = { 'value1' : 0, 'value2': 0 }
    _data2 = {}

  def __mp_clone__(module):
    _lock.acquire()
    module._lock = _lock
    module._data1 = _data1
    module._data2 = _data2
    _lock.release()

Because the old module is about to be discarded, the data which is transferred should not consist of data objects which are dependent on code within the old module. Data being copied across to the new module should consist of standard Python data types, or be instances of classes contained within modules which themselves are not candidates for reloading. Otherwise, data should be migrated by transforming it into some neutral intermediate state, with the new module transforming it back when its code executes at the time of being imported.

If these guidelines aren't heeded and data is dependent on code objects within the old module, it will prevent those code objects from being unloaded and if this continues across multiple reloads, then process size may increase over time due to old code objects being retained.

In any case, if for some reason the hook function fails and an exception is raised then both the old and new modules will be discarded. As a last opportunity to release any resources when this occurs, an extra hook function called __mp_purge__() can be supplied. This function will be called with no arguments.

allow_methods( [*args])
A convenience function to set values in req.allowed. req.allowed is a bitmask that is used to construct the "Allow:" header. It should be set before returning a HTTP_NOT_IMPLEMENTED error.

Arguments can be one or more of the following:

    M_GET
    M_PUT
    M_POST
    M_DELETE
    M_CONNECT
    M_OPTIONS
    M_TRACE
    M_PATCH
    M_PROPFIND
    M_PROPPATCH
    M_MKCOL
    M_COPY
    M_MOVE
    M_LOCK
    M_UNLOCK
    M_VERSION_CONTROL
    M_CHECKOUT
    M_UNCHECKOUT
    M_CHECKIN
    M_UPDATE
    M_LABEL
    M_REPORT
    M_MKWORKSPACE
    M_MKACTIVITY
    M_BASELINE_CONTROL
    M_MERGE
    M_INVALID

exists_config_define( name)
This function returns True if the Apache server was launched with the definition with the given name. This means that you can test whether Apache was launched with the -DFOOBAR parameter by calling apache.exists_config_define('FOOBAR').

stat( fname, wanted)
This function returns an instance of an mp_finfo object describing information related to the file with name fname. The wanted argument describes the minimum attributes which should be filled out. The resultant object can be assigned to the req.finfo attribute.

register_cleanup( callable[, data])
Registers a cleanup that will be performed at child shutdown time. Equivalent to server.register_cleanup(), except that a request object is not required. Warning: do not pass directly or indirectly a request object in the data parameter. Since the callable will be called at server shutdown time, the request object won't exist anymore and any manipulation of it in the handler will give undefined behaviour.

config_tree( )
Returns the server-level configuration tree. This tree does not include directives from .htaccess files. This is a copy of the tree, modifying it has no effect on the actual configuration.

server_root( )
Returns the value of ServerRoot.

make_table( )
This function is obsolete and is an alias to table (see below).

mpm_query( code)
Allows querying of the MPM for various parameters such as numbers of processes and threads. The return value is one of three constants:
AP_MPMQ_NOT_SUPPORTED      = 0  # This value specifies whether 
                                # an MPM is capable of         
                                # threading or forking.        
AP_MPMQ_STATIC             = 1  # This value specifies whether 
                                # an MPM is using a static # of
                                # threads or daemons.          
AP_MPMQ_DYNAMIC            = 2  # This value specifies whether 
                                # an MPM is using a dynamic # of
                                # threads or daemons.

The code argument must be one of the following:

AP_MPMQ_MAX_DAEMON_USED    = 1  # Max # of daemons used so far 
AP_MPMQ_IS_THREADED        = 2  # MPM can do threading         
AP_MPMQ_IS_FORKED          = 3  # MPM can do forking           
AP_MPMQ_HARD_LIMIT_DAEMONS = 4  # The compiled max # daemons   
AP_MPMQ_HARD_LIMIT_THREADS = 5  # The compiled max # threads   
AP_MPMQ_MAX_THREADS        = 6  # # of threads/child by config 
AP_MPMQ_MIN_SPARE_DAEMONS  = 7  # Min # of spare daemons       
AP_MPMQ_MIN_SPARE_THREADS  = 8  # Min # of spare threads       
AP_MPMQ_MAX_SPARE_DAEMONS  = 9  # Max # of spare daemons       
AP_MPMQ_MAX_SPARE_THREADS  = 10 # Max # of spare threads       
AP_MPMQ_MAX_REQUESTS_DAEMON= 11 # Max # of requests per daemon 
AP_MPMQ_MAX_DAEMONS        = 12 # Max # of daemons by config

Example:

if apache.mpm_query(apache.AP_MPMQ_IS_THREADED):
    # do something
else:
    # do something else