[egenix-users] Exception exceptions.AttributeError - Depending upon the naming of the module ( connector.py / error_connector.py)

M.-A. Lemburg mal at egenix.com
Mon Feb 9 23:08:44 CET 2009

On 2009-02-09 16:47, Senthil Kumaran wrote:
> Hello,
> This is a strange issue which is occurring with how I name a particular module.
> Is this anyway related to namespace pollution?If yes, can some more
> details/ information be shared?

This doesn't have anything to do with the naming.. well at least
not directly.

The problem with your approach is that you're using a module
global to store the session object.

At interpreter exit time, Python will clear all modules, but the
order in which this happens is undefined (in CPython it depends on
the order of the items in the module dictionary at the time of exit,
but that's an implementation detail of CPython).

As a result, depending on how you name the module holding the
session object reference, it may get cleared before or after
the other modules using it. Once the

It is better to keep globals such as the session object in
a context object that you pass around in your application
code as parameter to the various methods/functions.

Note that it's also better to explicitly close the session
object once you're done with the server communication.

Not doing this can lead to a situation where Python has already
cleared out part of the mxODBC Connect package modules while
still keeping a reference to the session object.

Once this object is cleared as well, it will try to close
to session and this can then very likely fail due to only
half of the functions, classes and objects used by mxODBC
Connect still being in existence at that time.

Please note that this is true for any larger Python
application or package, not just mxODBC Connect.

> I am going to present the snippets in-line (as they are very short)
> and show you the test conditions.
> Case 1: Working Case
> # test_mxconnect.py
> from global_connector import initialize_connector
> CONFIG_PATH = '/usr/local/etc/client-config.ini'
> CONNECTION_STRING = 'DSN=jiva_trunk;UID=jiva_user;PWD=jiva_user'
> if __name__ == '__main__':
>     initialize_connector(connector_name='mxOdbcConnect',
> client_config_path = CONFIG_PATH)
>     from global_connector import MsSQLdbapi
>     if not hasattr(MsSQLdbapi, 'connect'):
>         db_api = MsSQLdbapi.open()
>     print MsSQLdbapi, db_api
> -------------------------------------------------------------------------------------------------------------------------------------
> #global_connector.py
> def initialize_connector (connector_name='mxODBC', **kwargs):
>     global MsSQLdbapi
>     try:
>         if connector_name == 'mxODBC':
>             pass
>         elif connector_name == 'mxOdbcConnect':
>             from mx.ODBCConnect.Client import ServerSession
>             config_path = kwargs.get('client_config_path')
>             if not config_path:
>                 raise AssertionError, "Expected keyword arg: client_config_path"
>             MsSQLdbapi = ServerSession(config_path)
>     except ImportError,e:
>         print e
>         raise
> ----------------------------------------------------------------------------------------------------------------
> Case 2:
> Do a $mv global_connector.py  connector.py
> And in test_mxconnect.py, replace global_connector with connector.py
> # test_mxconnect.py
> from connector import initialize_connector    # Offender in the module name
> CONFIG_PATH = '/usr/local/etc/client-config.ini'
> CONNECTION_STRING = 'DSN=jiva_trunk;UID=jiva_user;PWD=jiva_user'
> if __name__ == '__main__':
>     initialize_connector(connector_name='mxOdbcConnect',
> client_config_path = CONFIG_PATH)
>     from connector import MsSQLdbapi       # Offender in the module name
>     if not hasattr(MsSQLdbapi, 'connect'):
>         db_api = MsSQLdbapi.open()
>     print MsSQLdbapi, db_api
> The Error message, I get after script execution is:
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute 'EndOfCommunicationRequestObject'" in <bound method
> ServerSession.close of <mxODBC Connect ServerSession with
> <TCPConnection to (plain-text)>>> ignored
> Case 3:  Failure Scenario.
> If I name the module global_connector.py as error_connector.py, and
> use that in the test_mxconnect.py, I still get the error Exception at
> the end of completion of the script.
> Any explanations?

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Feb 09 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the egenix-users mailing list