Skip to content

Commit 5cca3e5

Browse files
mmaterarocky
andauthored
adjust SetEnvironment (#1429)
This is a first example of implementation that works by chance due to a general misimplementation. Also, now the result is compliant with WMA. --------- Co-authored-by: R. Bernstein <rocky@users.noreply.github.com>
1 parent 61a595e commit 5cca3e5

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

mathics/builtin/system.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ class SetEnvironment(Builtin):
603603
604604
Set two environment variables:
605605
S> SetEnvironment[{"FOO" -> "baz", "A" -> "B"}]
606-
= SetEnvironment[{FOO -> baz, A -> B}]
607606
608607
See that the environment variable has changed:
609608
S> GetEnvironment["FOO"]
@@ -621,6 +620,7 @@ class SetEnvironment(Builtin):
621620
If the environment name is not a string, the evaluation fails without a message.
622621
623622
S> SetEnvironment[1 -> "bar"]
623+
= SetEnvironment[1 -> bar]
624624
625625
See also <url>
626626
:'Environment':
@@ -633,24 +633,35 @@ class SetEnvironment(Builtin):
633633
summary_text = "set system environment variable(s)"
634634

635635
def eval(self, rule, evaluation):
636-
"SetEnvironment[rule_]"
636+
"SetEnvironment[rule_Rule]"
637637
env_var_name, env_var_value = rule.elements
638+
# WMA does not give an error message if env_var_name is not a String - weird.
639+
if not isinstance(env_var_name, String):
640+
return None
641+
638642
if not (env_var_value is SymbolNone or isinstance(env_var_value, String)):
639643
evaluation.message("SetEnvironment", "value", env_var_value)
640644
return SymbolFailed
641645

642-
if isinstance(env_var_name, String):
643-
# WMA does not give an error message if env_var_name is not a String - weird.
644-
os.environ[env_var_name.value] = (
645-
None if None is SymbolNone else env_var_value.value
646-
)
646+
os.environ[env_var_name.value] = (
647+
None if None is SymbolNone else env_var_value.value
648+
)
647649
return SymbolNull
648650

649651
def eval_list(self, rules: Expression, evaluation: Evaluation):
650652
"SetEnvironment[{rules__}]"
653+
654+
# All the rules must be of the form
655+
for rule in rules.elements:
656+
if not rule.has_form("System`Rule", 2):
657+
return None
658+
if not isinstance(rule.elements[0], String):
659+
return None
660+
651661
for rule in rules.elements:
652662
self.eval(rule, evaluation)
653-
return None
663+
664+
return SymbolNull
654665

655666

656667
class Share(Builtin):

0 commit comments

Comments
 (0)