Skip to content

Commit

Permalink
[osh] Hide string export check behind shopt --set strict_array
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Andy C committed Apr 4, 2024
1 parent e47d54a commit fb02521
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
5 changes: 3 additions & 2 deletions core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,8 +1666,9 @@ def SetNamed(self, lval, val, which_scopes, flags=0):

if cell.val.tag() not in (value_e.Undef, value_e.Str):
if cell.exported:
# TODO: error context
e_die("Only strings can be exported", lval.blame_loc)
if self.exec_opts.strict_array():
e_die("Only strings can be exported (strict_array)",
lval.blame_loc)
if cell.nameref:
e_die("nameref must be a string", lval.blame_loc)

Expand Down
15 changes: 5 additions & 10 deletions core/state_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,11 @@ def testSetVarClearFlag(self):
self.assertEqual(['1', '2', '3'],
mem.var_stack[0]['COMPREPLY'].val.strs)

# export COMPREPLY
try:
mem.SetValue(location.LName('COMPREPLY'),
None,
scope_e.Dynamic,
flags=state.SetExport)
except error.FatalRuntime as e:
pass
else:
self.fail("Expected failure")
# export COMPREPLY - allowed when strict_array not set
mem.SetValue(location.LName('COMPREPLY'),
None,
scope_e.Dynamic,
flags=state.SetExport)

# readonly r=1
mem.SetValue(location.LName('r'),
Expand Down
38 changes: 33 additions & 5 deletions spec/array.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,41 @@ argv.py "${a[@]}"
# bash parses, but doesn't execute.
# mksh gives syntax error -- parses differently with 'export'
# osh no longer parses this statically.
export PYTHONPATH=(a b c)
export PYTHONPATH=a # NOTE: in bash, this doesn't work afterward!

export PYTHONPATH

PYTHONPATH=mystr # NOTE: in bash, this doesn't work afterward!
printenv.py PYTHONPATH
## stdout-json: ""

PYTHONPATH=(myarray)
printenv.py PYTHONPATH

PYTHONPATH=(a b c)
printenv.py PYTHONPATH

## status: 0
## STDOUT:
mystr
None
None
## END

#### strict_array prevents exporting array

shopt -s strict_array

export PYTHONPATH
PYTHONPATH=(a b c)
printenv.py PYTHONPATH

## status: 1
## OK bash stdout: None
## OK bash status: 0
## STDOUT:
## END

## N-I bash/mksh status: 0
## N-I bash/mksh STDOUT:
None
## END

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

0 comments on commit fb02521

Please sign in to comment.