Changeset 3339

Show
Ignore:
Timestamp:
01/10/10 19:34:13 (10 years ago)
Author:
armon
Message:

Updated dylink to provide better error message. Dylink deprecates 'exit' callfunc, and does not provide it. dy_import_module allows for optional 'additional_globals' which are provided from the current context to the import context. getresources is part of the stock api provided by dylink.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/trunk/seattlelib/dylink.repy

    r3256 r3339  
    4242             "stopcomm","open","listdir","removefile","exitall","getlock","getruntime", 
    4343             "randomfloat","settimer","canceltimer","sleep","VirtualNamespace","get_thread_name", 
    44                   "dy_import_module","dy_import_module_symbols","dy_dispatch_module"]) 
     44                   "getresources","dy_import_module","dy_import_module_symbols","dy_dispatch_module"]) 
    4545 
    4646  # Controls if we run the code preprocessor 
     
    8989 
    9090# Constructs a "default" context from the given context 
    91 def _default_context(import_context): 
     91# Additional_globals can be an array of "extra" globals, which are 
     92# copied in addition to the STOCK_API 
     93def _default_context(import_context, additional_globals=[]): 
    9294  # Construct a new context based on the importer's context 
    9395  new_context = SafeDict() 
    94   for func in STOCK_API: 
     96  all_globals = set(additional_globals) 
     97  all_globals.update(STOCK_API) 
     98  for func in all_globals: 
    9599    if func in import_context: 
    96100      new_context[func] = import_context[func] 
     
    177181      virt = DylinkNamespace(code,module) 
    178182    except Exception, e: 
    179       raise Exception, "Failed to initialize the module! Got the following exception: '" + str(e) + "'" 
     183      raise Exception, "Failed to initialize the module ("+module+")! Got the following exception: '" + str(e) + "'" 
    180184     
    181185    # Cache the module 
     
    273277 
    274278# Packages the module being imported into a module like object 
    275 def dylink_import_module(module,import_context, new_callfunc="import"): 
     279def dylink_import_module(module,import_context, new_callfunc="import", additional_globals=[]): 
    276280  """ 
    277281  <Purpose> 
     
    289293          Defaults to "import". 
    290294 
     295    additional_globals: 
     296          An array of string globals which will be provided to the imported module. 
     297          E.g. if the current context defines "foo" and this should be made available 
     298          in addition to the Stock repy API, this can be provided as ["foo"] 
     299 
    291300  <Exceptions> 
    292301    Raises an exception if the module cannot be found, or if there is a problem 
     
    300309 
    301310  # Construct a new context 
    302   new_context = _default_context(import_context) 
     311  new_context = _default_context(import_context, additional_globals) 
    303312 
    304313  # Populate the context 
     
    385394 
    386395  # Define closures around dy_* functions 
    387   def _dy_import_module(module,new_callfunc="import"): 
    388     return dylink_import_module(module,import_context,new_callfunc) 
     396  def _dy_import_module(module,new_callfunc="import",additional_globals=[]): 
     397    return dylink_import_module(module,import_context,new_callfunc,additional_globals) 
    389398 
    390399  def _dy_import_global(module,new_callfunc="import"): 
     
    469478  CHILD_CONTEXT["VirtualNamespace"] = get_DylinkNamespace 
    470479 
    471 # Evaluate the next module 
    472 dylink_dispatch(CHILD_CONTEXT, _context) 
    473  
     480# DEPRECATING "exit" callfunc 
     481if callfunc != "exit": 
     482  # Evaluate the next module 
     483  dylink_dispatch(CHILD_CONTEXT, _context) 
     484