Skip to content

Commit

Permalink
Fix running bkl in non-ASCII path
Browse files Browse the repository at this point in the history
os.path.* functions don’t work with non-ASCII paths if a mix of str and
unicode arguments is used, causing bkl to fail with UnicodeDecodeError.

Fix by explicitly converting path components from .bkl files to ANSI
strings. As a consequence, .bkl files are assumed to be in UTF-8.

Fixes #96.
  • Loading branch information
vslavik committed Feb 18, 2018
1 parent 00bfa97 commit 0b1d600
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/bkl/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Bakefile (http://bakefile.org)
#
# Copyright (C) 2008-2013 Vaclav Slavik
# Copyright (C) 2008-2018 Vaclav Slavik
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -520,7 +520,7 @@ def as_native_path(self, paths_info):
base = paths_info.builddir_abs
else:
assert False, "unsupported anchor in PathExpr.as_native_path()"
comp = (e.as_py() for e in self.components)
comp = (e.as_py().encode('utf-8') for e in self.components)
return os.path.abspath(os.path.join(base, os.path.sep.join(comp)))

def as_native_path_for_output(self, model):
Expand Down Expand Up @@ -860,7 +860,7 @@ def __init__(self, dirsep, outfile, builddir, model):
"""

self.dirsep = dirsep
outdir = os.path.dirname(os.path.abspath(outfile))
outdir = os.path.dirname(os.path.abspath(outfile.encode('utf-8')))
self.outdir_abs = outdir

top_srcdir = os.path.abspath(model.project.top_module.srcdir)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_full.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# This file is part of Bakefile (http://bakefile.org)
#
Expand Down Expand Up @@ -96,3 +97,28 @@ def _do_test_on_file(input, model_file):
""" % expected

assert as_text == expected

def test_unicode_filename():
"""
This test checks that filenames relative to a directory containing
non-ASCII characters work correctly, see
https://github.com/vslavik/bakefile/issues/96
"""
t = bkl.parser.parse("""
toolsets = gnu;
program progname {
sources { ../relpath.c }
}
""", "test.bkl")
i = InterpreterForTestSuite()

import shutil
import tempfile
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp(prefix=u"Üñîçöḍè")
os.chdir(tmpdir)
try:
i.process(t)
finally:
os.chdir(cwd)
shutil.rmtree(tmpdir)

0 comments on commit 0b1d600

Please sign in to comment.