Repository: conda/conda-build

Issue:
TypeError: environment can only contain string
I just saw a strange error on windows when building GPy. In this case, it only seemed to happen on Python 2.

```
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

    https://github.com/conda/conda-build/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
  File "C:\Miniconda\Scripts\conda-build-script.py", line 4, in <module>
    sys.exit(main())
  File "C:\Miniconda\lib\site-packages\conda_build\main_build.py", line 194, in main
    args_func(args, p)
  File "C:\Miniconda\lib\site-packages\conda_build\main_build.py", line 474, in args_func
    args.func(args, p)
  File "C:\Miniconda\lib\site-packages\conda_build\main_build.py", line 423, in execute
    include_recipe=args.include_recipe)
  File "C:\Miniconda\lib\site-packages\conda_build\build.py", line 417, in build
    m.parse_again(permit_undefined_jinja=False)
  File "C:\Miniconda\lib\site-packages\conda_build\metadata.py", line 351, in parse_again
    self.meta = parse(self._get_contents(permit_undefined_jinja))
  File "C:\Miniconda\lib\site-packages\conda_build\metadata.py", line 628, in _get_contents
    env.globals.update(context_processor(self, path))
  File "C:\Miniconda\lib\site-packages\conda_build\jinja_context.py", line 65, in context_processor
    ctx = get_environ(m=initial_metadata)
  File "C:\Miniconda\lib\site-packages\conda_build\environ.py", line 165, in get_dict
    m.get_value('source/git_rev')))
  File "C:\Miniconda\lib\site-packages\conda_build\environ.py", line 64, in get_git_build_info
    remote_details = subprocess.check_output(["git", "remote", "-v"], env=env)
  File "C:\Miniconda\lib\subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "C:\Miniconda\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Miniconda\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
TypeError: environment can only contain string
```

Full log: https://ci.appveyor.com/project/rmcgibbo/conda-recipes/build/1.0.709/job/mb723cisbnwdddte



Discussion:
The error message is the same as #188, but this is with the latest (conda-build-1.19.1).

yeh this breaks with conda-build=1.19.1 (but works with 1.19.0), but ONLY ON WINDOWS.
This works fine on macosx/linux

```
package:
    name: pandas
    version: {{ GIT_DESCRIBE_TAG|replace("v","") }}

build:
    number: {{ GIT_DESCRIBE_NUMBER|int }}
    {% if GIT_DESCRIBE_NUMBER|int == 0 %}string: np{{ CONDA_NPY }}py{{ CONDA_PY }}_0
    {% else %}string: np{{ CONDA_NPY }}py{{ CONDA_PY }}_{{ GIT_BUILD_STR }}{% endif %}
```

cc @msarahan 
cc @jakirkham 

The problem is on [these lines](https://github.com/conda/conda-build/blob/3d60a3cc27ca49dd72641c0f62ccb036fd0007f3/conda_build/environ.py#L72-L74):

``` python
        env['GIT_DIR'] = cache_dir
        remote_details = check_output(["git", "remote", "-v"], env=env,
                                      stderr=STDOUT)
```

Apparently on Windows, the `env` argument to `subprocess.check_output` may contain only `str`, not `unicode`.  I tried this little test on Windows:

``` python
﻿>>> subprocess.check_call('echo "hi"', shell=True, env={'MYVAR': 'blabla'})
"hi"
0
>>> subprocess.check_call('echo "hi"', shell=True, env={'MYVAR': u'blabla'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ilastik\Miniconda\lib\subprocess.py", line 535, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Users\ilastik\Miniconda\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Users\ilastik\Miniconda\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Users\ilastik\Miniconda\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
TypeError: environment can only contain strings
```

I'll implement a pull request soonish, unless somebody beats me to it.  (Feel free...)


Resolving patch:
diff --git a/conda_build/environ.py b/conda_build/environ.py
--- a/conda_build/environ.py
+++ b/conda_build/environ.py
@@ -47,6 +47,10 @@ def get_git_build_info(src_dir, git_url, expected_rev):
     env = os.environ.copy()
     d = {}
     git_dir = join(src_dir, '.git')
+    if not isinstance(git_dir, str):
+        # On Windows, subprocess env can't handle unicode.
+        git_dir = git_dir.encode(sys.getfilesystemencoding() or 'utf-8')
+
     if not os.path.exists(git_dir):
         return d
 
@@ -68,6 +72,9 @@ def get_git_build_info(src_dir, git_url, expected_rev):
         cache_details = cache_details.decode('utf-8')
         cache_dir = cache_details.split('\n')[0].split()[1]
         assert "conda-bld/git_cache" in cache_dir
+        if not isinstance(cache_dir, str):
+            # On Windows, subprocess env can't handle unicode.
+            cache_dir = cache_dir.encode(sys.getfilesystemencoding() or 'utf-8')
 
         env['GIT_DIR'] = cache_dir
         remote_details = check_output(["git", "remote", "-v"], env=env,


Test patch: