Package NotificationFramework :: Module mems_lib
[show private | hide private]
[frames | no frames]

Module NotificationFramework.mems_lib

The code in this module is borrowed from the ZODB Programmer's Guide.

It redefines the python2.1's builtin functions issubclass() and isinstance() so that they work properly with extension classes. Versions 2.2 and up of python do not have the problem so the module's functions issubclass() and isinstance() are the default ones, unchanged.


This is not indicated in the ZODB Programming Guide anymore, probably because it's of no use for python2.2 and up. Last time I checked I was able to find the page I'm referring to at: but this URL might be unavailable at the time you're reading this.

So in case it is not available, here is a copy of the comments A.M. Kuchling wrote around the code (speaking of python 2.1):

Python's built-in functions isinstance() and issubclass don't work on ExtensionClass instances, for much the same reason that __cmp__ is never called; in some bits of the Python core code, branches are taken only if an object is of the InstanceType type, and this can never be true for an ExtensionClass instance. Python 2.1 tried to fix this, and changed these functions slightly in an effort to make them work for ExtensionClasses; unfortunately, the changes didn't work.

The solution is to use customized versions of these functions that handle ExtensionClasses specially and fall back to the built-in version otherwise. Here are the versions we've written at the MEMS Exchange:

[code here for issubclass() and isinstance(), see below]

I'd recommend putting these functions in a module that always gets imported. The convention on my work project is to put them in mems/lib/, which contains various fundamental classes and functions for our system, and access them like this:

from mems.lib import base ... if base.isinstance(object, Class): ...

Don't insert the modified functions into Python's __builtin__ module, or import just the isinstance() and issubclass functions. If you consistently use base.isinstance(), then forgetting to import the base module will result in a NameError exception. In the case of a forgotten import, calling the functions directly would use Python's built-in versions, leading to subtle bugs that might not be noticed for some time.

The quoted text above, and most of the code included in this module, is extracted from the ZODB Programmer's Guide release 0.03, February 8, 2002 by A.M. Kuchling, (c) 2002 and distributed under the GNU Free Documentation License, Version 1.1 or any later.

Generated by Epydoc 2.1 on Sat Mar 4 13:36:32 2006