Skip to content

Commit

Permalink
Merge branch 'fix-quoting' into 'master'
Browse files Browse the repository at this point in the history
Fix quoting

See merge request inducer/pyopencl!65
  • Loading branch information
inducer committed Nov 27, 2018
2 parents 7c35c3a + 96e6f3d commit fbc1336
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -64,3 +64,4 @@ build-and-test-py-project.sh
cffi_build.py

.cache
.idea
50 changes: 15 additions & 35 deletions pyopencl/__init__.py
Expand Up @@ -24,7 +24,6 @@
THE SOFTWARE.
"""

import re
import six
from six.moves import input, intern

Expand Down Expand Up @@ -242,7 +241,8 @@ def _find_pyopencl_include_path():
try:
# Try to find the resource with pkg_resources (the recommended
# setuptools approach)
return resource_filename(Requirement.parse("pyopencl"), "pyopencl/cl")
include_path = resource_filename(
Requirement.parse("pyopencl"), "pyopencl/cl")
except DistributionNotFound:
# If pkg_resources can't find it (e.g. if the module is part of a
# frozen application), try to find the include path in the same
Expand All @@ -255,6 +255,11 @@ def _find_pyopencl_include_path():
if not exists(include_path):
raise

# Quote the path if it contains a space and is not quoted already.
# See https://github.com/inducer/pyopencl/issues/250 for discussion.
if ' ' in include_path and not include_path.startswith('"'):
return '"' + include_path + '"'
else:
return include_path

# }}}
Expand Down Expand Up @@ -384,35 +389,6 @@ def __getattr__(self, attr):

# {{{ build

if six.PY3:
_find_unsafe_re_opts = re.ASCII
else:
_find_unsafe_re_opts = 0

_find_unsafe = re.compile(br'[^\w@%+=:,./-]', _find_unsafe_re_opts).search

@classmethod
def _shlex_quote(cls, s):
"""Return a shell-escaped version of the string *s*."""

# Stolen from https://hg.python.org/cpython/file/default/Lib/shlex.py#l276

if not s:
return "''"

if cls._find_unsafe(s) is None:
return s

# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
import sys
if sys.platform.startswith("win"):
# not sure how to escape that
assert b'"' not in s
return b'"' + s + b'"'
else:
return b"'" + s.replace(b"'", b"'\"'\"'") + b"'"

@classmethod
def _process_build_options(cls, context, options):
if isinstance(options, six.string_types):
Expand Down Expand Up @@ -447,6 +423,12 @@ def encode_if_necessary(s):

# {{{ find include path

def unquote(path):
if path.startswith('"') and path.endswith('"'):
return path[1:-1]
else:
return path

include_path = ["."]

option_idx = 0
Expand All @@ -455,10 +437,10 @@ def encode_if_necessary(s):
if option.startswith("-I") or option.startswith("/I"):
if len(option) == 2:
if option_idx+1 < len(options):
include_path.append(options[option_idx+1])
include_path.append(unquote(options[option_idx+1]))
option_idx += 2
else:
include_path.append(option[2:].lstrip())
include_path.append(unquote(option[2:].lstrip()))
option_idx += 1
else:
option_idx += 1
Expand All @@ -467,8 +449,6 @@ def encode_if_necessary(s):

options = [encode_if_necessary(s) for s in options]

options = [cls._shlex_quote(s) for s in options]

return b" ".join(options), include_path

def build(self, options=[], devices=None, cache_dir=None):
Expand Down

0 comments on commit fbc1336

Please sign in to comment.