Skip to content

Commit

Permalink
[translation] Avoid stdin / stdout / stderr name conflicts
Browse files Browse the repository at this point in the history
One instance of this was reported on #1615, but this isn't the first.

Solve it for good by adding a rule to mycpp.
  • Loading branch information
Andy C committed May 18, 2023
1 parent 56591a4 commit e4a0194
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,15 @@ def Main(lang, arg_r, environ, login_shell, loader, readline):
line_reader = None # unused!
# Not setting '-i' flag for now. Some people's bashrc may want it?
else:
stdin = mylib.Stdin()
if stdin.isatty():
stdin_ = mylib.Stdin()
if stdin_.isatty():
src = source.Interactive
line_reader = reader.InteractiveLineReader(
arena, prompt_ev, hist_ev, readline, prompt_state)
mutable_opts.set_interactive()
else:
src = source.Stdin('')
line_reader = reader.FileLineReader(stdin, arena)
line_reader = reader.FileLineReader(stdin_, arena)
else:
src = source.MainFile(script_name)
try:
Expand Down
20 changes: 17 additions & 3 deletions mycpp/cppgen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

T = None

NAME_CONFLICTS = ('stdin', 'stdout', 'stderr')


class UnsupportedException(Exception):
pass
Expand Down Expand Up @@ -598,6 +600,13 @@ def visit_name_expr(self, o: 'mypy.nodes.NameExpr') -> T:
self.write('this')
return

if o.name in NAME_CONFLICTS:
self.report_error(
o,
"The name %r conflicts with C macros on some platforms; choose a different name"
% o.name)
return

self.write(o.name)

def visit_member_expr(self, o: 'mypy.nodes.MemberExpr') -> T:
Expand All @@ -620,6 +629,11 @@ def visit_member_expr(self, o: 'mypy.nodes.MemberExpr') -> T:
if o.name == 'errno':
# e->errno -> e->errno_ to avoid conflict with C macro
self.write('errno_')
elif o.name in NAME_CONFLICTS:
self.report_error(
o,
"The name %r conflicts with C macros on some platforms; choose a different name"
% o.name)
else:
self.write('%s', o.name)

Expand Down Expand Up @@ -2517,9 +2531,9 @@ def visit_import_from(self, o: 'mypy.nodes.ImportFrom') -> T:
# 'using' of names that aren't declared yet.
# suffix_op is needed for string_ops.py, for some reason
if self.decl and name in (
'Id', 'scope_e', 'lex_mode_e',
'suffix_op', 'lvalue', 'part_value', 'loc', 'word',
'word_part', 'cmd_value', 'hnode'):
'Id', 'scope_e', 'lex_mode_e', 'suffix_op',
'lvalue', 'part_value', 'loc', 'word', 'word_part',
'cmd_value', 'hnode'):
self.f.write(using_str)

else:
Expand Down
14 changes: 7 additions & 7 deletions oil_lang/builtin_oil.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, mem, errfmt, procs, arena):
_Builtin.__init__(self, mem, errfmt)
self.procs = procs
self.arena = arena
self.stdout = Stdout()
self.stdout_ = Stdout()

def Run(self, cmd_val):
# type: (cmd_value.Argv) -> int
Expand Down Expand Up @@ -78,11 +78,11 @@ def Run(self, cmd_val):
blame_loc=locs[i])
status = 1
else:
self.stdout.write('%s = ' % name)
self.stdout_.write('%s = ' % name)
if mylib.PYTHON:
cell.PrettyPrint() # may be color

self.stdout.write('\n')
self.stdout_.write('\n')

elif action == 'proc':
names, locs = arg_r.Rest2()
Expand Down Expand Up @@ -223,7 +223,7 @@ class Write(_Builtin):
def __init__(self, mem, errfmt):
# type: (state.Mem, ErrorFormatter) -> None
_Builtin.__init__(self, mem, errfmt)
self.stdout = Stdout()
self.stdout_ = Stdout()

def Run(self, cmd_val):
# type: (cmd_value.Argv) -> int
Expand All @@ -243,21 +243,21 @@ def Run(self, cmd_val):
i = 0
while not arg_r.AtEnd():
if i != 0:
self.stdout.write(arg.sep)
self.stdout_.write(arg.sep)
s = arg_r.Peek()

if arg.qsn:
s = qsn.maybe_encode(s, bit8_display)

self.stdout.write(s)
self.stdout_.write(s)

arg_r.Next()
i += 1

if arg.n:
pass
elif len(arg.end):
self.stdout.write(arg.end)
self.stdout_.write(arg.end)

return 0

Expand Down
6 changes: 3 additions & 3 deletions osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,12 +2185,12 @@ def CheckCircularDeps(self):

def _EvalCommandSub(self, cs_part, quoted):
# type: (CommandSub, bool) -> part_value_t
stdout = self.shell_ex.RunCommandSub(cs_part)
stdout_str = self.shell_ex.RunCommandSub(cs_part)
if cs_part.left_token.id == Id.Left_AtParen:
strs = self.splitter.SplitForWordEval(stdout)
strs = self.splitter.SplitForWordEval(stdout_str)
return part_value.Array(strs)
else:
return part_value.String(stdout, quoted, not quoted)
return part_value.String(stdout_str, quoted, not quoted)

def _EvalProcessSub(self, cs_part):
# type: (CommandSub) -> part_value.String
Expand Down

0 comments on commit e4a0194

Please sign in to comment.