embpy10.gms : Test Embedded Code for Environment Variables
Description
This test ensures that environment variables can be properly communicated
even if GAMS sets environment variables after Python's os has been
initialized
Contributor: Michael Bussieck, June 2021
$title Test Embedded Code for Environment Variables (EMBPY10,SEQ=871)$onText
This test ensures that environment variables can be properly communicated
even if GAMS sets environment variables after Python's os has been
initialized
Contributor: Michael Bussieck, June 2021
$offText$log --- Using Python library %sysEnv.GMSPYTHONLIB%
$setEnv ABC abc$onEmbeddedCode Python:import os
ifnot os.environ['ABC']=='abc':raise Exception('Expected ABC to be abc')$offEmbeddedCode
$setEnv ABC def$onEmbeddedCode Python:# We expect ABC inside Python not be updatedifnot os.environ['ABC']=='abc':raise Exception('Expected os.environ["ABC"] to be abc')# We expect ABC in the process environment to be updatedifnot gams.get_env('ABC')=='def':raise Exception('Expected gams.get_env("ABC") to be def')
os.environ['ABC']='ghi'$offEmbeddedCode
$if not %sysEnv.ABC% == ghi $abort expect sysEnv.ABC to be ghi$dropEnv ABC$onEmbeddedCode Python:# We expect ABC inside Python not be updatedifnot os.environ['ABC']=='ghi':raise Exception('Expected os.environ["ABC"] to be ghi')# We expect ABC in the process environment to be goneifnot gams.get_env('ABC')== None:raise Exception('Expected gams.get_env("ABC") to return None')$offEmbeddedCode
* We assume here GAMS compilation and execution happens in one (action=ce)embeddedCode Python:# We expect ABC inside Python not be updatedifnot os.environ['ABC']=='ghi':raise Exception('Expected os.environ["ABC"] to be abc')ifnot gams.get_env('ABC')== None:raise Exception('Expected gams.get_env("ABC") to return None')endEmbeddedCodeput_utility'setEnv'/'ABC'/'abc';embeddedCode Python:
os.environ['ABC']= gams.get_env('ABC')ifnot os.environ['ABC']=='abc':raise Exception('Expected ABC to be abc')endEmbeddedCodeput_utility'setEnv'/'ABC'/'def';embeddedCode Python:# We expect ABC inside Python not be updatedifnot os.environ['ABC']=='abc':raise Exception('Expected os.environ["ABC"] to be abc')# We expect ABC in the process environment to be updatedifnot gams.get_env('ABC')=='def':raise Exception('Expected gams.get_env("ABC") to be def')
os.environ['ABC']='ghi'endEmbeddedCodeput_utility'shell.checkErrorLevel'/'test ghi = ' sysEnv.ABC;put_utility'dropEnv'/'ABC';embeddedCode Python:# We expect ABC inside Python not be updatedifnot os.environ['ABC']=='ghi':raise Exception('Expected os.environ["ABC"] to be abc')ifnot gams.get_env('ABC')== None:raise Exception('Expected gams.get_env("ABC") to return None')endEmbeddedCode