From: <sv...@ww...> - 2004-10-01 06:26:23
|
Author: mkrose Date: 2004-09-30 23:26:10 -0700 (Thu, 30 Sep 2004) New Revision: 1266 Modified: trunk/CSP/__init__.py trunk/CSP/tools/CSP_bootstrap Log: Fix a failure mode of the CSP bootstrap loader that could result in an infinite recursion. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1266 Modified: trunk/CSP/__init__.py =================================================================== --- trunk/CSP/__init__.py 2004-09-30 07:20:48 UTC (rev 1265) +++ trunk/CSP/__init__.py 2004-10-01 06:26:10 UTC (rev 1266) @@ -16,12 +16,18 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -CSP = 1 import sys import os import os.path +# ugly kludge to allow us to import dl. the bootstrap loader removes the +# original contents of sys.path, and stashes the full path in sys.CSP_PATH. +# if it has been set we restore the path here and clear the sys attribute. +if len(sys.path) == 1 and hasattr(sys, 'CSP_PATH'): + sys.path = sys.CSP_PATH + del sys.CSP_PATH + # make SimData available as CSP.SimData __path__ += [os.path.join(os.path.dirname(__file__), 'SimData')] Modified: trunk/CSP/tools/CSP_bootstrap =================================================================== --- trunk/CSP/tools/CSP_bootstrap 2004-09-30 07:20:48 UTC (rev 1265) +++ trunk/CSP/tools/CSP_bootstrap 2004-10-01 06:26:10 UTC (rev 1266) @@ -29,15 +29,9 @@ import os.path import sys -# remove '.' from sys.path -try: - dotindex = sys.path.index('.') -except ValueError: - dotindex = -1 +# save the path +old_path = sys.path[:] -if dotindex >= 0: - del sys.path[dotindex] - # remove this module from sys.modules self = sys.modules['CSP'] del sys.modules['CSP'] @@ -51,21 +45,30 @@ while parts[-1] != 'CSP': parts = parts[:-1] -ROOT = os.path.sep.join(parts[:-1]) +CSP_PATH = os.path.sep.join(parts[:-1]) +ROOT = os.path.join(CSP_PATH, 'CSP') -if not os.path.exists(os.path.join(ROOT, 'CSP', '.svn')): +if not os.path.exists(os.path.join(ROOT, '.svn')): print 'Warning: %s does not contain .svn and may not be a valid CSP workspace.' % ROOT -# load the real CSP package -sys.path.insert(0, ROOT) +# load the real CSP package. we strip away the original sys.path to +# prevent this module from being loaded recursively on failure. the +# only problem is that the real CSP package __init__ needs to the +# full path to import dl. the expedient (but ugly, ugly) workaround +# is to cache the full path in sys.CSP_PATH, which the CSP module +# uses to restore the full path (after which it deletes sys.CSP_PATH). +sys.path = [CSP_PATH] +sys.CSP_PATH = [CSP_PATH] + old_path try: import CSP except ImportError, e: - print 'Unable to bootstrap a CSP module space from the current working directory:' - print e + # cleanup, notify, and pass the error along. + del sys.CSP_PATH + sys.path = old_path + print 'Unable to bootstrap a CSP module space from the current working directory.' + raise -# restore '.' to the path -if dotindex >= 0: - sys.path.insert(dotindex + 1, '.') +# restore the path +sys.path = [CSP_PATH] + old_path |