Skip to content

Commit 8e2b48b

Browse files
authored
[gyb] Support -o with relative path (#67995)
Fixes a surprising bug where a relative output file path would be interpreted relative to the directory of the `.gyb` file being processed rather than the current working directory upon invocation.
1 parent 131f325 commit 8e2b48b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

utils/gyb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,16 +1242,20 @@ def succ(a):
12421242
ast = parse_template(args.file, f.read())
12431243
if args.dump:
12441244
print(ast)
1245+
12451246
# Allow the template to open files and import .py files relative to its own
12461247
# directory
1248+
saved_cwd = os.getcwd()
12471249
os.chdir(os.path.dirname(os.path.abspath(args.file)))
12481250
sys.path = ['.'] + sys.path
1251+
result_text = execute_template(ast, args.line_directive, **bindings)
12491252

12501253
if args.target == '-':
1251-
sys.stdout.write(execute_template(ast, args.line_directive, **bindings))
1254+
sys.stdout.write(result_text)
12521255
else:
1256+
os.chdir(saved_cwd)
12531257
with io.open(args.target, 'w', encoding='utf-8', newline='\n') as f:
1254-
f.write(execute_template(ast, args.line_directive, **bindings))
1258+
f.write(result_text)
12551259

12561260

12571261
if __name__ == '__main__':

0 commit comments

Comments
 (0)