def rebuild(module, doLog=1):
 global lastRebuild
 lastRebuild = time.time()
 if hasattr(module, 'ALLOW_TWISTED_REBUILD'):
  if (not module.ALLOW_TWISTED_REBUILD):
   raise RuntimeError('I am not allowed to be rebuilt.')
 if doLog:
  log.msg(('Rebuilding %s...' % str(module.__name__)))
 from twisted.python import components
 components.ALLOW_DUPLICATES = True
 d = module.__dict__
 _modDictIDMap[id(d)] = module
 newclasses = {}
 classes = {}
 functions = {}
 values = {}
 if doLog:
  log.msg(('  (scanning %s): ' % str(module.__name__)))
 for (k, v) in d.items():
  if (type(v) == types.ClassType):
   if (v.__module__ == module.__name__):
    classes[v] = 1
    if doLog:
     log.logfile.write('c')
     log.logfile.flush()
  elif (type(v) == types.FunctionType):
   if (v.func_globals is module.__dict__):
    functions[v] = 1
    if doLog:
     log.logfile.write('f')
     log.logfile.flush()
  elif isinstance(v, type):
   if (v.__module__ == module.__name__):
    newclasses[v] = 1
    if doLog:
     log.logfile.write('o')
     log.logfile.flush()
 values.update(classes)
 values.update(functions)
 fromOldModule = values.has_key
 newclasses = newclasses.keys()
 classes = classes.keys()
 functions = functions.keys()
 if doLog:
  log.msg('')
  log.msg(('  (reload   %s)' % str(module.__name__)))
 reload(module)
 linecache.clearcache()
 if doLog:
  log.msg(('  (cleaning %s): ' % str(module.__name__)))
 for clazz in classes:
  if (getattr(module, clazz.__name__) is clazz):
   log.msg(('WARNING: class %s not replaced by reload!' % reflect.qual(clazz)))
  else:
   if doLog:
    log.logfile.write('x')
    log.logfile.flush()
   clazz.__bases__ = ()
   clazz.__dict__.clear()
   clazz.__getattr__ = __getattr__
   clazz.__module__ = module.__name__
 if newclasses:
  import gc
 for nclass in newclasses:
  ga = getattr(module, nclass.__name__)
  if (ga is nclass):
   log.msg(('WARNING: new-class %s not replaced by reload!' % reflect.qual(nclass)))
  else:
   for r in gc.get_referrers(nclass):
    if (getattr(r, '__class__', None) is nclass):
     r.__class__ = ga
 if doLog:
  log.msg('')
  log.msg(('  (fixing   %s): ' % str(module.__name__)))
 modcount = 0
 for (mk, mod) in sys.modules.items():
  modcount = (modcount + 1)
  if ((mod == module) or (mod is None)):
   continue
  if (not hasattr(mod, '__file__')):
   continue
  if hasattr(mod, '__bundle__'):
   continue
  changed = 0
  for (k, v) in mod.__dict__.items():
   try:
    hash(v)
   except Exception:
    continue
   if fromOldModule(v):
    if (type(v) == types.ClassType):
     if doLog:
      log.logfile.write('c')
      log.logfile.flush()
     nv = latestClass(v)
    else:
     if doLog:
      log.logfile.write('f')
      log.logfile.flush()
     nv = latestFunction(v)
    changed = 1
    setattr(mod, k, nv)
   elif (type(v) == types.ClassType):
    for base in v.__bases__:
     if fromOldModule(base):
      latestClass(v)
  if (doLog and (not changed) and ((modcount % 10) == 0)):
   log.logfile.write('.')
   log.logfile.flush()
 components.ALLOW_DUPLICATES = False
 if doLog:
  log.msg('')
  log.msg(('   Rebuilt %s.' % str(module.__name__)))
 return module

def rebuild ( module , doLog = 1 ) : global lastRebuild lastRebuild = time . time ( ) if hasattr ( module , 'ALLOW_TWISTED_REBUILD' ) : if ( not module . ALLOW_TWISTED_REBUILD ) : raise RuntimeError ( 'I am not allowed to be rebuilt.' ) if doLog : log . msg ( ( 'Rebuilding %s...' % str ( module . __name__ ) ) ) from twisted . python import components components . ALLOW_DUPLICATES = True d = module . __dict__ _modDictIDMap [ id ( d ) ] = module newclasses = { } classes = { } functions = { } values = { } if doLog : log . msg ( ( '  (scanning %s): ' % str ( module . __name__ ) ) ) for ( k , v ) in d . items ( ) : if ( type ( v ) == types . ClassType ) : if ( v . __module__ == module . __name__ ) : classes [ v ] = 1 if doLog : log . logfile . write ( 'c' ) log . logfile . flush ( ) elif ( type ( v ) == types . FunctionType ) : if ( v . func_globals is module . __dict__ ) : functions [ v ] = 1 if doLog : log . logfile . write ( 'f' ) log . logfile . flush ( ) elif isinstance ( v , type ) : if ( v . __module__ == module . __name__ ) : newclasses [ v ] = 1 if doLog : log . logfile . write ( 'o' ) log . logfile . flush ( ) values . update ( classes ) values . update ( functions ) fromOldModule = values . has_key newclasses = newclasses . keys ( ) classes = classes . keys ( ) functions = functions . keys ( ) if doLog : log . msg ( '' ) log . msg ( ( '  (reload   %s)' % str ( module . __name__ ) ) ) reload ( module ) linecache . clearcache ( ) if doLog : log . msg ( ( '  (cleaning %s): ' % str ( module . __name__ ) ) ) for clazz in classes : if ( getattr ( module , clazz . __name__ ) is clazz ) : log . msg ( ( 'WARNING: class %s not replaced by reload!' % reflect . qual ( clazz ) ) ) else : if doLog : log . logfile . write ( 'x' ) log . logfile . flush ( ) clazz . __bases__ = ( ) clazz . __dict__ . clear ( ) clazz . __getattr__ = __getattr__ clazz . __module__ = module . __name__ if newclasses : import gc for nclass in newclasses : ga = getattr ( module , nclass . __name__ ) if ( ga is nclass ) : log . msg ( ( 'WARNING: new-class %s not replaced by reload!' % reflect . qual ( nclass ) ) ) else : for r in gc . get_referrers ( nclass ) : if ( getattr ( r , '__class__' , None ) is nclass ) : r . __class__ = ga if doLog : log . msg ( '' ) log . msg ( ( '  (fixing   %s): ' % str ( module . __name__ ) ) ) modcount = 0 for ( mk , mod ) in sys . modules . items ( ) : modcount = ( modcount + 1 ) if ( ( mod == module ) or ( mod is None ) ) : continue if ( not hasattr ( mod , '__file__' ) ) : continue if hasattr ( mod , '__bundle__' ) : continue changed = 0 for ( k , v ) in mod . __dict__ . items ( ) : try : hash ( v ) except Exception : continue if fromOldModule ( v ) : if ( type ( v ) == types . ClassType ) : if doLog : log . logfile . write ( 'c' ) log . logfile . flush ( ) nv = latestClass ( v ) else : if doLog : log . logfile . write ( 'f' ) log . logfile . flush ( ) nv = latestFunction ( v ) changed = 1 setattr ( mod , k , nv ) elif ( type ( v ) == types . ClassType ) : for base in v . __bases__ : if fromOldModule ( base ) : latestClass ( v ) if ( doLog and ( not changed ) and ( ( modcount % 10 ) == 0 ) ) : log . logfile . write ( '.' ) log . logfile . flush ( ) components . ALLOW_DUPLICATES = False if doLog : log . msg ( '' ) log . msg ( ( '   Rebuilt %s.' % str ( module . __name__ ) ) ) return module
rebuild a sympy tree this function recursively calls constructors in the expression tree .