Skip to content

Commit fb02521

Browse files
author
Andy C
committed
[osh] Hide string export check behind shopt --set strict_array
Note that bash does NOT respect the "$array is ${array[0]}" rule for export FOO FOO=(a b c) It allows it, but nothing is exported. For Nix compatibility. Reported by Samuel: https://oilshell.zulipchat.com/#narrow/stream/307442-nix/topic/Replacing.20bash.20with.20osh.20in.20Nixpkgs.20stdenv
1 parent e47d54a commit fb02521

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

core/state.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,8 +1666,9 @@ def SetNamed(self, lval, val, which_scopes, flags=0):
16661666

16671667
if cell.val.tag() not in (value_e.Undef, value_e.Str):
16681668
if cell.exported:
1669-
# TODO: error context
1670-
e_die("Only strings can be exported", lval.blame_loc)
1669+
if self.exec_opts.strict_array():
1670+
e_die("Only strings can be exported (strict_array)",
1671+
lval.blame_loc)
16711672
if cell.nameref:
16721673
e_die("nameref must be a string", lval.blame_loc)
16731674

core/state_test.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,11 @@ def testSetVarClearFlag(self):
178178
self.assertEqual(['1', '2', '3'],
179179
mem.var_stack[0]['COMPREPLY'].val.strs)
180180

181-
# export COMPREPLY
182-
try:
183-
mem.SetValue(location.LName('COMPREPLY'),
184-
None,
185-
scope_e.Dynamic,
186-
flags=state.SetExport)
187-
except error.FatalRuntime as e:
188-
pass
189-
else:
190-
self.fail("Expected failure")
181+
# export COMPREPLY - allowed when strict_array not set
182+
mem.SetValue(location.LName('COMPREPLY'),
183+
None,
184+
scope_e.Dynamic,
185+
flags=state.SetExport)
191186

192187
# readonly r=1
193188
mem.SetValue(location.LName('r'),

spec/array.test.sh

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,41 @@ argv.py "${a[@]}"
263263
# bash parses, but doesn't execute.
264264
# mksh gives syntax error -- parses differently with 'export'
265265
# osh no longer parses this statically.
266-
export PYTHONPATH=(a b c)
267-
export PYTHONPATH=a # NOTE: in bash, this doesn't work afterward!
266+
267+
export PYTHONPATH
268+
269+
PYTHONPATH=mystr # NOTE: in bash, this doesn't work afterward!
268270
printenv.py PYTHONPATH
269-
## stdout-json: ""
271+
272+
PYTHONPATH=(myarray)
273+
printenv.py PYTHONPATH
274+
275+
PYTHONPATH=(a b c)
276+
printenv.py PYTHONPATH
277+
278+
## status: 0
279+
## STDOUT:
280+
mystr
281+
None
282+
None
283+
## END
284+
285+
#### strict_array prevents exporting array
286+
287+
shopt -s strict_array
288+
289+
export PYTHONPATH
290+
PYTHONPATH=(a b c)
291+
printenv.py PYTHONPATH
292+
270293
## status: 1
271-
## OK bash stdout: None
272-
## OK bash status: 0
294+
## STDOUT:
295+
## END
296+
297+
## N-I bash/mksh status: 0
298+
## N-I bash/mksh STDOUT:
299+
None
300+
## END
273301

274302
#### Arrays can't be used as env bindings
275303
# Hm bash it treats it as a string!

0 commit comments

Comments
 (0)