prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Unreadable error message appears when `nvcc` command is not found
  Only "No suche file or directory" is shown in such case.
  
  related to #698
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `cupy/cuda/compiler.py`
  Content:
  ```
  1 import hashlib
  2 import os
  3 import re
  4 import subprocess
  5 import sys
  6 import tempfile
  7 
  8 import filelock
  9 import six
  10 
  11 from cupy.cuda import device
  12 from cupy.cuda import function
  13 
  14 
  15 def _get_arch():
  16     cc = device.Device().compute_capability
  17     return 'sm_%s' % cc
  18 
  19 
  20 class TemporaryDirectory(object):
  21 
  22     def __enter__(self):
  23         self.path = tempfile.mkdtemp()
  24         return self.path
  25 
  26     def __exit__(self, exc_type, exc_value, traceback):
  27         if exc_value is not None:
  28             return
  29 
  30         for name in os.listdir(self.path):
  31             os.unlink(os.path.join(self.path, name))
  32         os.rmdir(self.path)
  33 
  34 
  35 def nvcc(source, options=(), arch=None):
  36     if not arch:
  37         arch = _get_arch()
  38     cmd = ['nvcc', '--cubin', '-arch', arch] + list(options)
  39 
  40     with TemporaryDirectory() as root_dir:
  41         path = os.path.join(root_dir, 'kern')
  42         cu_path = '%s.cu' % path
  43         cubin_path = '%s.cubin' % path
  44 
  45         with open(cu_path, 'w') as cu_file:
  46             cu_file.write(source)
  47 
  48         cmd.append(cu_path)
  49         subprocess.check_output(cmd, cwd=root_dir)
  50 
  51         with open(cubin_path, 'rb') as bin_file:
  52             return bin_file.read()
  53 
  54 
  55 def preprocess(source, options=()):
  56     cmd = ['nvcc', '--preprocess'] + list(options)
  57     with TemporaryDirectory() as root_dir:
  58         path = os.path.join(root_dir, 'kern')
  59         cu_path = '%s.cu' % path
  60 
  61         with open(cu_path, 'w') as cu_file:
  62             cu_file.write(source)
  63 
  64         cmd.append(cu_path)
  65         pp_src = subprocess.check_output(cmd, cwd=root_dir)
  66 
  67         if isinstance(pp_src, six.binary_type):
  68             pp_src = pp_src.decode('utf-8')
  69         return re.sub('(?m)^#.*$', '', pp_src)
  70 
  71 
  72 _default_cache_dir = os.path.expanduser('~/.cupy/kernel_cache')
  73 
  74 
  75 def get_cache_dir():
  76     return os.environ.get('CUPY_CACHE_DIR', _default_cache_dir)
  77 
  78 
  79 _empty_file_preprocess_cache = {}
  80 
  81 
  82 def compile_with_cache(source, options=(), arch=None, cache_dir=None):
  83     global _empty_file_preprocess_cache
  84     if cache_dir is None:
  85         cache_dir = get_cache_dir()
  86     if arch is None:
  87         arch = _get_arch()
  88 
  89     if 'win32' == sys.platform:
  90         options += ('-Xcompiler', '/wd 4819')
  91         if sys.maxsize == 9223372036854775807:
  92             options += '-m64',
  93         elif sys.maxsize == 2147483647:
  94             options += '-m32',
  95 
  96     env = (arch, options)
  97     if '#include' in source:
  98         pp_src = '%s %s' % (env, preprocess(source, options))
  99     else:
  100         base = _empty_file_preprocess_cache.get(env, None)
  101         if base is None:
  102             base = _empty_file_preprocess_cache[env] = preprocess('', options)
  103         pp_src = '%s %s %s' % (env, base, source)
  104 
  105     if isinstance(pp_src, six.text_type):
  106         pp_src = pp_src.encode('utf-8')
  107     name = '%s.cubin' % hashlib.md5(pp_src).hexdigest()
  108 
  109     mod = function.Module()
  110 
  111     if not os.path.exists(cache_dir):
  112         os.makedirs(cache_dir)
  113 
  114     lock_path = os.path.join(cache_dir, 'lock_file.lock')
  115 
  116     path = os.path.join(cache_dir, name)
  117     with filelock.FileLock(lock_path) as lock:
  118         if os.path.exists(path):
  119             with open(path, 'rb') as file:
  120                 cubin = file.read()
  121             mod.load(cubin)
  122         else:
  123             lock.release()
  124             cubin = nvcc(source, options, arch)
  125             mod.load(cubin)
  126             lock.acquire()
  127             with open(path, 'wb') as cubin_file:
  128                 cubin_file.write(cubin)
  129 
  130     return mod
  131
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/cupy/cuda/compiler.py b/cupy/cuda/compiler.py
  --- a/cupy/cuda/compiler.py
  +++ b/cupy/cuda/compiler.py
  @@ -32,6 +32,15 @@
           os.rmdir(self.path)
   
   
  +def _run_nvcc(cmd, cwd):
  +    try:
  +        return subprocess.check_output(cmd, cwd=cwd)
  +    except OSError as e:
  +        trace = sys.exc_info()[2]
  +        msg = 'Failed to run `nvcc` command: ' + str(e)
  +        six.reraise(OSError, msg, trace)
  +
  +
   def nvcc(source, options=(), arch=None):
       if not arch:
           arch = _get_arch()
  @@ -46,7 +55,7 @@
               cu_file.write(source)
   
           cmd.append(cu_path)
  -        subprocess.check_output(cmd, cwd=root_dir)
  +        _run_nvcc(cmd, root_dir)
   
           with open(cubin_path, 'rb') as bin_file:
               return bin_file.read()
  @@ -62,7 +71,7 @@
               cu_file.write(source)
   
           cmd.append(cu_path)
  -        pp_src = subprocess.check_output(cmd, cwd=root_dir)
  +        pp_src = _run_nvcc(cmd, root_dir)
   
           if isinstance(pp_src, six.binary_type):
               pp_src = pp_src.decode('utf-8')
  
verification_info: {"golden_diff": "diff --git a/cupy/cuda/compiler.py b/cupy/cuda/compiler.py\n--- a/cupy/cuda/compiler.py\n+++ b/cupy/cuda/compiler.py\n@@ -32,6 +32,15 @@\n         os.rmdir(self.path)\n \n \n+def _run_nvcc(cmd, cwd):\n+    try:\n+        return subprocess.check_output(cmd, cwd=cwd)\n+    except OSError as e:\n+        trace = sys.exc_info()[2]\n+        msg = 'Failed to run `nvcc` command: ' + str(e)\n+        six.reraise(OSError, msg, trace)\n+\n+\n def nvcc(source, options=(), arch=None):\n     if not arch:\n         arch = _get_arch()\n@@ -46,7 +55,7 @@\n             cu_file.write(source)\n \n         cmd.append(cu_path)\n-        subprocess.check_output(cmd, cwd=root_dir)\n+        _run_nvcc(cmd, root_dir)\n \n         with open(cubin_path, 'rb') as bin_file:\n             return bin_file.read()\n@@ -62,7 +71,7 @@\n             cu_file.write(source)\n \n         cmd.append(cu_path)\n-        pp_src = subprocess.check_output(cmd, cwd=root_dir)\n+        pp_src = _run_nvcc(cmd, root_dir)\n \n         if isinstance(pp_src, six.binary_type):\n             pp_src = pp_src.decode('utf-8')\n", "issue": "Unreadable error message appears when `nvcc` command is not found\nOnly \"No suche file or directory\" is shown in such case.\n\nrelated to #698\n\n", "before_files": [{"content": "import hashlib\nimport os\nimport re\nimport subprocess\nimport sys\nimport tempfile\n\nimport filelock\nimport six\n\nfrom cupy.cuda import device\nfrom cupy.cuda import function\n\n\ndef _get_arch():\n    cc = device.Device().compute_capability\n    return 'sm_%s' % cc\n\n\nclass TemporaryDirectory(object):\n\n    def __enter__(self):\n        self.path = tempfile.mkdtemp()\n        return self.path\n\n    def __exit__(self, exc_type, exc_value, traceback):\n        if exc_value is not None:\n            return\n\n        for name in os.listdir(self.path):\n            os.unlink(os.path.join(self.path, name))\n        os.rmdir(self.path)\n\n\ndef nvcc(source, options=(), arch=None):\n    if not arch:\n        arch = _get_arch()\n    cmd = ['nvcc', '--cubin', '-arch', arch] + list(options)\n\n    with TemporaryDirectory() as root_dir:\n        path = os.path.join(root_dir, 'kern')\n        cu_path = '%s.cu' % path\n        cubin_path = '%s.cubin' % path\n\n        with open(cu_path, 'w') as cu_file:\n            cu_file.write(source)\n\n        cmd.append(cu_path)\n        subprocess.check_output(cmd, cwd=root_dir)\n\n        with open(cubin_path, 'rb') as bin_file:\n            return bin_file.read()\n\n\ndef preprocess(source, options=()):\n    cmd = ['nvcc', '--preprocess'] + list(options)\n    with TemporaryDirectory() as root_dir:\n        path = os.path.join(root_dir, 'kern')\n        cu_path = '%s.cu' % path\n\n        with open(cu_path, 'w') as cu_file:\n            cu_file.write(source)\n\n        cmd.append(cu_path)\n        pp_src = subprocess.check_output(cmd, cwd=root_dir)\n\n        if isinstance(pp_src, six.binary_type):\n            pp_src = pp_src.decode('utf-8')\n        return re.sub('(?m)^#.*$', '', pp_src)\n\n\n_default_cache_dir = os.path.expanduser('~/.cupy/kernel_cache')\n\n\ndef get_cache_dir():\n    return os.environ.get('CUPY_CACHE_DIR', _default_cache_dir)\n\n\n_empty_file_preprocess_cache = {}\n\n\ndef compile_with_cache(source, options=(), arch=None, cache_dir=None):\n    global _empty_file_preprocess_cache\n    if cache_dir is None:\n        cache_dir = get_cache_dir()\n    if arch is None:\n        arch = _get_arch()\n\n    if 'win32' == sys.platform:\n        options += ('-Xcompiler', '/wd 4819')\n        if sys.maxsize == 9223372036854775807:\n            options += '-m64',\n        elif sys.maxsize == 2147483647:\n            options += '-m32',\n\n    env = (arch, options)\n    if '#include' in source:\n        pp_src = '%s %s' % (env, preprocess(source, options))\n    else:\n        base = _empty_file_preprocess_cache.get(env, None)\n        if base is None:\n            base = _empty_file_preprocess_cache[env] = preprocess('', options)\n        pp_src = '%s %s %s' % (env, base, source)\n\n    if isinstance(pp_src, six.text_type):\n        pp_src = pp_src.encode('utf-8')\n    name = '%s.cubin' % hashlib.md5(pp_src).hexdigest()\n\n    mod = function.Module()\n\n    if not os.path.exists(cache_dir):\n        os.makedirs(cache_dir)\n\n    lock_path = os.path.join(cache_dir, 'lock_file.lock')\n\n    path = os.path.join(cache_dir, name)\n    with filelock.FileLock(lock_path) as lock:\n        if os.path.exists(path):\n            with open(path, 'rb') as file:\n                cubin = file.read()\n            mod.load(cubin)\n        else:\n            lock.release()\n            cubin = nvcc(source, options, arch)\n            mod.load(cubin)\n            lock.acquire()\n            with open(path, 'wb') as cubin_file:\n                cubin_file.write(cubin)\n\n    return mod\n", "path": "cupy/cuda/compiler.py"}], "after_files": [{"content": "import hashlib\nimport os\nimport re\nimport subprocess\nimport sys\nimport tempfile\n\nimport filelock\nimport six\n\nfrom cupy.cuda import device\nfrom cupy.cuda import function\n\n\ndef _get_arch():\n    cc = device.Device().compute_capability\n    return 'sm_%s' % cc\n\n\nclass TemporaryDirectory(object):\n\n    def __enter__(self):\n        self.path = tempfile.mkdtemp()\n        return self.path\n\n    def __exit__(self, exc_type, exc_value, traceback):\n        if exc_value is not None:\n            return\n\n        for name in os.listdir(self.path):\n            os.unlink(os.path.join(self.path, name))\n        os.rmdir(self.path)\n\n\ndef _run_nvcc(cmd, cwd):\n    try:\n        return subprocess.check_output(cmd, cwd=cwd)\n    except OSError as e:\n        trace = sys.exc_info()[2]\n        msg = 'Failed to run `nvcc` command: ' + str(e)\n        six.reraise(OSError, msg, trace)\n\n\ndef nvcc(source, options=(), arch=None):\n    if not arch:\n        arch = _get_arch()\n    cmd = ['nvcc', '--cubin', '-arch', arch] + list(options)\n\n    with TemporaryDirectory() as root_dir:\n        path = os.path.join(root_dir, 'kern')\n        cu_path = '%s.cu' % path\n        cubin_path = '%s.cubin' % path\n\n        with open(cu_path, 'w') as cu_file:\n            cu_file.write(source)\n\n        cmd.append(cu_path)\n        _run_nvcc(cmd, root_dir)\n\n        with open(cubin_path, 'rb') as bin_file:\n            return bin_file.read()\n\n\ndef preprocess(source, options=()):\n    cmd = ['nvcc', '--preprocess'] + list(options)\n    with TemporaryDirectory() as root_dir:\n        path = os.path.join(root_dir, 'kern')\n        cu_path = '%s.cu' % path\n\n        with open(cu_path, 'w') as cu_file:\n            cu_file.write(source)\n\n        cmd.append(cu_path)\n        pp_src = _run_nvcc(cmd, root_dir)\n\n        if isinstance(pp_src, six.binary_type):\n            pp_src = pp_src.decode('utf-8')\n        return re.sub('(?m)^#.*$', '', pp_src)\n\n\n_default_cache_dir = os.path.expanduser('~/.cupy/kernel_cache')\n\n\ndef get_cache_dir():\n    return os.environ.get('CUPY_CACHE_DIR', _default_cache_dir)\n\n\n_empty_file_preprocess_cache = {}\n\n\ndef compile_with_cache(source, options=(), arch=None, cache_dir=None):\n    global _empty_file_preprocess_cache\n    if cache_dir is None:\n        cache_dir = get_cache_dir()\n    if arch is None:\n        arch = _get_arch()\n\n    if 'win32' == sys.platform:\n        options += ('-Xcompiler', '/wd 4819')\n        if sys.maxsize == 9223372036854775807:\n            options += '-m64',\n        elif sys.maxsize == 2147483647:\n            options += '-m32',\n\n    env = (arch, options)\n    if '#include' in source:\n        pp_src = '%s %s' % (env, preprocess(source, options))\n    else:\n        base = _empty_file_preprocess_cache.get(env, None)\n        if base is None:\n            base = _empty_file_preprocess_cache[env] = preprocess('', options)\n        pp_src = '%s %s %s' % (env, base, source)\n\n    if isinstance(pp_src, six.text_type):\n        pp_src = pp_src.encode('utf-8')\n    name = '%s.cubin' % hashlib.md5(pp_src).hexdigest()\n\n    mod = function.Module()\n\n    if not os.path.exists(cache_dir):\n        os.makedirs(cache_dir)\n\n    lock_path = os.path.join(cache_dir, 'lock_file.lock')\n\n    path = os.path.join(cache_dir, name)\n    with filelock.FileLock(lock_path) as lock:\n        if os.path.exists(path):\n            with open(path, 'rb') as file:\n                cubin = file.read()\n            mod.load(cubin)\n        else:\n            lock.release()\n            cubin = nvcc(source, options, arch)\n            mod.load(cubin)\n            lock.acquire()\n            with open(path, 'wb') as cubin_file:\n                cubin_file.write(cubin)\n\n    return mod\n", "path": "cupy/cuda/compiler.py"}]}