5
5
from _devbuild .gen .option_asdl import builtin_i
6
6
from _devbuild .gen .runtime_asdl import (
7
7
scope_e ,
8
+ scope_t ,
8
9
cmd_value ,
9
10
AssignArg ,
10
11
)
@@ -192,17 +193,15 @@ def _PrintVariables(mem, cmd_val, attrs, print_flags, builtin=_OTHER):
192
193
return 1
193
194
194
195
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.
198
199
199
200
Like 'setvar' (scope_e.LocalOnly), unless dynamic scope is on. That is, it
200
201
respects shopt --unset dynamic_scope.
201
202
202
203
Used for assignment builtins, (( a = b )), {fd}>out, ${x=}, etc.
203
204
"""
204
- which_scopes = mem .ScopesForWriting ()
205
-
206
205
lval = LeftName (pair .var_name , pair .blame_word )
207
206
if pair .plus_eq :
208
207
old_val = sh_expr_eval .OldValue (lval , mem , None ) # ignore set -u
@@ -260,8 +259,10 @@ def Run(self, cmd_val):
260
259
# NOTE: we don't care if it wasn't found, like bash.
261
260
self .mem .ClearFlag (pair .var_name , state .ClearExport )
262
261
else :
262
+ which_scopes = self .mem .ScopesForWriting ()
263
263
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 )
265
266
266
267
return 0
267
268
@@ -318,6 +319,7 @@ def Run(self, cmd_val):
318
319
True ,
319
320
builtin = _READONLY )
320
321
322
+ which_scopes = self .mem .ScopesForWriting ()
321
323
for pair in cmd_val .pairs :
322
324
if pair .rval is None :
323
325
if arg .a :
@@ -336,7 +338,8 @@ def Run(self, cmd_val):
336
338
# NOTE:
337
339
# - when rval is None, only flags are changed
338
340
# - 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 )
340
343
341
344
return 0
342
345
@@ -463,19 +466,9 @@ def Run(self, cmd_val):
463
466
tmp = NewDict () # type: Dict[str, str]
464
467
rval = value .BashAssoc (tmp )
465
468
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 )
477
470
478
- self .mem . SetNamed ( lval , rval , which_scopes , flags = flags )
471
+ _AssignVarForBuiltin ( self .mem , rval , pair , which_scopes , flags )
479
472
480
473
return status
481
474
0 commit comments