Skip to content

Commit f2ac5a2

Browse files
authored
[refactor builtin/assign_osh] Remove duplicate codes in NewVar (#2267)
1 parent dc5a100 commit f2ac5a2

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

builtin/assign_osh.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from _devbuild.gen.option_asdl import builtin_i
66
from _devbuild.gen.runtime_asdl import (
77
scope_e,
8+
scope_t,
89
cmd_value,
910
AssignArg,
1011
)
@@ -192,17 +193,15 @@ def _PrintVariables(mem, cmd_val, attrs, print_flags, builtin=_OTHER):
192193
return 1
193194

194195

195-
def _ExportReadonly(mem, rval, pair, flags):
196-
# type: (Mem, value_t, AssignArg, int) -> None
197-
"""For 'export' and 'readonly' to respect += and flags.
196+
def _AssignVarForBuiltin(mem, rval, pair, which_scopes, flags):
197+
# type: (Mem, value_t, AssignArg, scope_t, int) -> None
198+
"""For 'export', 'readonly', and NewVar to respect += and flags.
198199
199200
Like 'setvar' (scope_e.LocalOnly), unless dynamic scope is on. That is, it
200201
respects shopt --unset dynamic_scope.
201202
202203
Used for assignment builtins, (( a = b )), {fd}>out, ${x=}, etc.
203204
"""
204-
which_scopes = mem.ScopesForWriting()
205-
206205
lval = LeftName(pair.var_name, pair.blame_word)
207206
if pair.plus_eq:
208207
old_val = sh_expr_eval.OldValue(lval, mem, None) # ignore set -u
@@ -260,8 +259,10 @@ def Run(self, cmd_val):
260259
# NOTE: we don't care if it wasn't found, like bash.
261260
self.mem.ClearFlag(pair.var_name, state.ClearExport)
262261
else:
262+
which_scopes = self.mem.ScopesForWriting()
263263
for pair in cmd_val.pairs:
264-
_ExportReadonly(self.mem, pair.rval, pair, state.SetExport)
264+
_AssignVarForBuiltin(self.mem, pair.rval, pair, which_scopes,
265+
state.SetExport)
265266

266267
return 0
267268

@@ -318,6 +319,7 @@ def Run(self, cmd_val):
318319
True,
319320
builtin=_READONLY)
320321

322+
which_scopes = self.mem.ScopesForWriting()
321323
for pair in cmd_val.pairs:
322324
if pair.rval is None:
323325
if arg.a:
@@ -336,7 +338,8 @@ def Run(self, cmd_val):
336338
# NOTE:
337339
# - when rval is None, only flags are changed
338340
# - dynamic scope because flags on locals can be changed, etc.
339-
_ExportReadonly(self.mem, rval, pair, state.SetReadOnly)
341+
_AssignVarForBuiltin(self.mem, rval, pair, which_scopes,
342+
state.SetReadOnly)
340343

341344
return 0
342345

@@ -463,19 +466,9 @@ def Run(self, cmd_val):
463466
tmp = NewDict() # type: Dict[str, str]
464467
rval = value.BashAssoc(tmp)
465468

466-
lval = LeftName(pair.var_name, pair.blame_word)
467-
468-
if pair.plus_eq:
469-
old_val = sh_expr_eval.OldValue(lval, self.mem,
470-
None) # ignore set -u
471-
# When 'typeset e+=', then rval is value.Str('')
472-
# When 'typeset foo', the pair.plus_eq flag is false.
473-
assert pair.rval is not None
474-
rval = cmd_eval.PlusEquals(old_val, pair.rval)
475-
else:
476-
rval = _ReconcileTypes(rval, arg.a, arg.A, pair.blame_word)
469+
rval = _ReconcileTypes(rval, arg.a, arg.A, pair.blame_word)
477470

478-
self.mem.SetNamed(lval, rval, which_scopes, flags=flags)
471+
_AssignVarForBuiltin(self.mem, rval, pair, which_scopes, flags)
479472

480473
return status
481474

0 commit comments

Comments
 (0)