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


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embpy10.gms

$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


* On the major platforms (Windows, Linux, Mac), GMSPYTHONLIB gets automatically set 
* to use the internal Python installation in sysdir/GMSPython.
$if not setEnv GMSPYTHONLIB $abort.noError Embedded code Python not ready to be used
$log --- Using Python library %sysEnv.GMSPYTHONLIB%


$setEnv ABC abc
$onEmbeddedCode Python:
import os
if not os.environ['ABC'] == 'abc':
   raise Exception('Expected ABC to be abc')
$offEmbeddedCode
$setEnv ABC def
$onEmbeddedCode Python:
# We expect ABC inside Python not be updated
if not os.environ['ABC'] == 'abc':
   raise Exception('Expected os.environ["ABC"] to be abc')
# We expect ABC in the process environment to be updated
if not 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 updated
if not os.environ['ABC'] == 'ghi':
   raise Exception('Expected os.environ["ABC"] to be ghi')
# We expect ABC in the process environment to be gone
if not 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 updated
if not os.environ['ABC'] == 'ghi':
   raise Exception('Expected os.environ["ABC"] to be abc')
if not gams.get_env('ABC') == None:
   raise Exception('Expected gams.get_env("ABC") to return None')
endEmbeddedCode
put_utility 'setEnv' / 'ABC' / 'abc';
embeddedCode Python:
os.environ['ABC'] = gams.get_env('ABC')
if not os.environ['ABC'] == 'abc':
   raise Exception('Expected ABC to be abc')
endEmbeddedCode 
put_utility 'setEnv' / 'ABC' / 'def';
embeddedCode Python:
# We expect ABC inside Python not be updated
if not os.environ['ABC'] == 'abc':
   raise Exception('Expected os.environ["ABC"] to be abc')
# We expect ABC in the process environment to be updated
if not gams.get_env('ABC') == 'def':
   raise Exception('Expected gams.get_env("ABC") to be def')
os.environ['ABC'] = 'ghi'
endEmbeddedCode
put_utility 'shell.checkErrorLevel' / 'test ghi = ' sysEnv.ABC;
put_utility 'dropEnv' / 'ABC';
embeddedCode Python:
# We expect ABC inside Python not be updated
if not os.environ['ABC'] == 'ghi':
   raise Exception('Expected os.environ["ABC"] to be abc')
if not gams.get_env('ABC') == None:
   raise Exception('Expected gams.get_env("ABC") to return None')
endEmbeddedCode