Skip to content

Commit

Permalink
Delegate quoting of build options to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
SemMulder committed Nov 27, 2018
1 parent b53097e commit 887930d
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions pyopencl/__init__.py
Expand Up @@ -242,7 +242,7 @@ 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,10 @@ def _find_pyopencl_include_path():
if not exists(include_path):
raise

# Quote the path if it contains a space and is not quoted already.
if ' ' in include_path and not include_path.startswith('"'):
return '"' + include_path + '"'
else:
return include_path

# }}}
Expand Down Expand Up @@ -384,35 +388,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 +422,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 +436,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 +448,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 887930d

Please sign in to comment.