Repository: conda/conda-build

Issue:
env vars must be bytestrings on Windows
Some recipes are failing when `conda-build` tries to apply the patch:

``` shell
Source cache directory is: C:\Miniconda\conda-bld\src_cache
Downloading source to cache: freexl-1.0.2.tar.gz
Downloading http://www.gaia-gis.it/gaia-sins/freexl-1.0.2.tar.gz
Success
Applying patch: u'C:\\projects\\conda-recipes\\recipes\\freexl\\makefile.vc.patch'
Traceback (most recent call last):
  File "C:\Miniconda\Scripts\conda-build-all-script.py", line 11, in <module>
    load_entry_point('conda-build-all==1.0.0', 'console_scripts', 'conda-build-all')()
  File "C:\Miniconda\lib\site-packages\conda_build_all\cli.py", line 90, in main
    b.main()
  File "C:\Miniconda\lib\site-packages\conda_build_all\builder.py", line 263, in main
    built_dist_location = self.build(meta, build_config)
  File "C:\Miniconda\lib\site-packages\conda_build_all\builder.py", line 200, in build
    conda_build.api.build(meta.meta, config=config)
  File "C:\Miniconda\lib\site-packages\conda_build\api.py", line 83, in build
    need_source_download=need_source_download, config=config)
  File "C:\Miniconda\lib\site-packages\conda_build\build.py", line 1004, in build_tree
    config=recipe_config)
  File "C:\Miniconda\lib\site-packages\conda_build\build.py", line 593, in build
    config=config)
  File "C:\Miniconda\lib\site-packages\conda_build\render.py", line 87, in parse_or_try_download
    source.provide(metadata.path, metadata.get_section('source'), config=config)
  File "C:\Miniconda\lib\site-packages\conda_build\source.py", line 509, in provide
    apply_patch(src_dir, join(recipe_dir, patch), config, git)
  File "C:\Miniconda\lib\site-packages\conda_build\source.py", line 468, in apply_patch
    check_call_env([patch] + patch_args, cwd=src_dir)
  File "C:\Miniconda\lib\site-packages\conda_build\utils.py", line 462, in check_call_env
    return _func_defaulting_env_to_os_environ(subprocess.check_call, *popenargs, **kwargs)
  File "C:\Miniconda\lib\site-packages\conda_build\utils.py", line 458, in _func_defaulting_env_to_os_environ
    return func(*popenargs, **kwargs)
  File "C:\Miniconda\lib\subprocess.py", line 536, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Miniconda\lib\subprocess.py",
```

Full log https://ci.appveyor.com/project/comtbot/conda-recipes/build/job/kxq68fcnidpdj2kr and recipe https://github.com/ioos/conda-recipes/tree/master/recipes/freexl

(Tried to fix this in https://github.com/conda/conda-build/pull/1377 and https://github.com/conda/conda-build/pull/1383 but gave up because I do not have a Windows machine to finish it :unamused:)



Discussion:

Resolving patch:
diff --git a/conda_build/source.py b/conda_build/source.py
--- a/conda_build/source.py
+++ b/conda_build/source.py
@@ -13,7 +13,7 @@
 from .conda_interface import hashsum_file
 
 from conda_build.os_utils import external
-from conda_build.utils import (tar_xf, unzip, safe_print_unicode, copy_into, on_win,
+from conda_build.utils import (tar_xf, unzip, safe_print_unicode, copy_into, on_win, codec,
                                check_output_env, check_call_env, convert_path_for_cygwin_or_msys2)
 
 # legacy exports for conda
diff --git a/conda_build/utils.py b/conda_build/utils.py
--- a/conda_build/utils.py
+++ b/conda_build/utils.py
@@ -455,7 +455,13 @@ def _func_defaulting_env_to_os_environ(func, *popenargs, **kwargs):
         kwargs = kwargs.copy()
         env_copy = os.environ.copy()
         kwargs.update({'env': env_copy})
-    return func(*popenargs, **kwargs)
+    args = []
+    for arg in popenargs:
+        # arguments to subprocess need to be bytestrings
+        if hasattr(arg, 'encode'):
+            arg = arg.encode(codec)
+        args.append(arg)
+    return func(*args, **kwargs)
 
 
 def check_call_env(*popenargs, **kwargs):


Test patch: