@@ -562,22 +562,32 @@ def parse_arguments(self, args):
562562 self .wrapper_products .append (self .model_wrapper )
563563
564564 @classmethod
565- def write_error_wrapper (cls , fname , try_lines , matlab_engine = None ):
565+ def write_error_wrapper (cls , fname , try_lines , env = None ,
566+ matlab_engine = None ):
566567 r"""Write a wrapper for the model that encloses it in a try except so
567568 that the error can be propagated appropriately.
568569
569570 Args:
570571 fname (str): File where the wrapper should be written.
571572 try_lines (list): List of lines to go in the try block.
572573 model_file (str): Path to model that should be wrapped.
574+ env (dict, optional): Dictionary of environment variables
575+ that should be set before calling the model. Defaults
576+ to None and is ignored.
573577 matlab_engine (MatlabEngine, optional): Matlab engine that will be
574- used to call the wrapper. If not provided, it is assumed the
575- error will be called using the Matlab interpreter on the command
576- line. Defautls to None.
578+ used to call the wrapper. If not provided, it is assumed the
579+ error will be called using the Matlab interpreter on the command
580+ line. Defautls to None.
577581
578582 Raises:
579583
580584 """
585+ # Add environment variables explicitly
586+ lines = []
587+ if env is not None :
588+ for k , v in env .items ():
589+ lines .append ('setenv(\' %s\' , \' %s\' )' % (
590+ k , v .encode ("unicode_escape" ).decode ('utf-8' )))
581591 # Create lines based on use of engine or not
582592 if matlab_engine is not None :
583593 catch_block = ["error(e.message);" ]
@@ -587,7 +597,7 @@ def write_error_wrapper(cls, fname, try_lines, matlab_engine=None):
587597 # "disp(e.identifier);",
588598 # "disp(e.stack);",
589599 # "exit(0);"]
590- lines = cls .write_try_except (try_lines , catch_block )
600+ lines + = cls .write_try_except (try_lines , catch_block )
591601 if matlab_engine is None :
592602 lines .append ("exit(0);" )
593603 # Write lines
@@ -615,7 +625,7 @@ def run_code(cls, lines, **kwargs):
615625 kwargs .setdefault ('process_kwargs' , {})
616626 if not kwargs ['process_kwargs' ].get ('dont_wrap_error' , False ):
617627 lines = cls .write_error_wrapper (
618- None , lines ,
628+ None , lines , env = kwargs . get ( 'env' , None ),
619629 matlab_engine = kwargs .get ('matlab_engine' , None ))
620630 kwargs ['process_kwargs' ]['dont_wrap_error' ] = True
621631 return super (MatlabModelDriver , cls ).run_code (lines , ** kwargs )
@@ -677,6 +687,7 @@ def run_executable(cls, args, dont_wrap_error=False, fname_wrapper=None,
677687 if working_dir is not None :
678688 fname_wrapper = os .path .join (working_dir , fname_wrapper )
679689 cls .write_error_wrapper (fname_wrapper , try_block ,
690+ env = kwargs .get ('env' , None ),
680691 matlab_engine = matlab_engine )
681692 assert (os .path .isfile (fname_wrapper ))
682693 args = [os .path .splitext (os .path .basename (fname_wrapper ))[0 ]]
0 commit comments