Skip to content

Commit

Permalink
[builtin/printf] Remove usage of getenv() and unsetenv().
Browse files Browse the repository at this point in the history
I couldn't detect this from the outside.  It didn't work in the OVM
build, and simpler code will translate more easily to C++.
  • Loading branch information
Andy Chu committed Mar 19, 2020
1 parent 7132537 commit fddcf67
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static PyMethodDef posix_methods[] = {
{"isatty", posix_isatty, METH_VARARGS},
{"pipe", posix_pipe, METH_NOARGS},
{"putenv", posix_putenv, METH_VARARGS},
/*{"unsetenv", posix_unsetenv, METH_VARARGS},*/
{"strerror", posix_strerror, METH_VARARGS},

/* job control stuff */
Expand Down
2 changes: 1 addition & 1 deletion core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def _InitVarsFromEnv(mem, environ):
# variable. Dash has a loop through environ in init.c
for n, v in iteritems(environ):
mem.SetVar(lvalue.Named(n), value.Str(v), scope_e.GlobalOnly,
flags=SetExport)
flags=SetExport)

# If it's not in the environment, initialize it. This makes it easier to
# update later in MutableOpts.
Expand Down
10 changes: 8 additions & 2 deletions osh/builtin_printf.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,18 @@ def Run(self, cmd_val):
# variable `TZ'. When the exported variable `TZ' is present,
# its value should be reflected in the real environment
# variable `TZ' before call of `tzset'.
#
# Note: unlike LANG, TZ doesn't seem to change behavior if it's
# not exported.
#
# TODO: In Oil, provide an API that doesn't rely on libc's
# global state.

tzcell = self.mem.GetCell('TZ')
if tzcell and tzcell.exported and tzcell.val.tag_() == value_e.Str:
tzval = cast(value__Str, tzcell.val)
posix.putenv('TZ', tzval.s)
elif posix.getenv('TZ'):
posix.unsetenv('TZ')

time.tzset()

# Handle special values:
Expand Down
28 changes: 28 additions & 0 deletions spec/builtin-printf.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,34 @@ status=0
status=1
## END

#### %(strftime format)T doesn't respect TZ if not exported
TZ=Asia/Tokyo # NOT exported
printf '%(%Y-%m-%d)T\n' 1557978599
echo status=$?
## STDOUT:
2019-05-15
status=0
## END
## N-I dash/mksh/zsh/ash STDOUT:
status=1
## END

#### %(strftime format)T TZ in environ but not set in shell

export TZ=Asia/Tokyo
unset TZ # unset in the shell, but still in the environment
#repr TZ

printf '%(%Y-%m-%d)T\n' 1557978599
echo status=$?
## STDOUT:
2019-05-15
status=0
## END
## N-I dash/mksh/zsh/ash STDOUT:
status=1
## END

#### %10.5(strftime format)T
# The result depends on timezone
export TZ=Asia/Tokyo
Expand Down

0 comments on commit fddcf67

Please sign in to comment.