Repository: conda/conda

Issue:
ERROR conda.core.link:_execute_actions(319): An error occurred while installing package 'defaults::qt-5.6.2-vc9_3'
```
Current conda install:

               platform : win-64
          conda version : 4.3.9
       conda is private : False
      conda-env version : 4.3.9
    conda-build version : 2.1.3
         python version : 2.7.13.final.0
       requests version : 2.12.4
       root environment : C:\Users\joelkim\Anaconda2  (writable)
    default environment : C:\Users\joelkim\Anaconda2
       envs directories : C:\Users\joelkim\Anaconda2\envs
          package cache : C:\Users\joelkim\Anaconda2\pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/win-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/win-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/win-64
                          https://repo.continuum.io/pkgs/pro/noarch
                          https://repo.continuum.io/pkgs/msys2/win-64
                          https://repo.continuum.io/pkgs/msys2/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.9 requests/2.12.4 CPython/2.7.13 Windows/10 Windows/10.0.14393
```

I got this error when I tried to install qt:
```
> conda create -n test qt
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment C:\Users\joelkim\Anaconda2\envs\test:

The following NEW packages will be INSTALLED:

    icu:            57.1-vc9_0        [vc9]
    jpeg:           9b-vc9_0          [vc9]
    libpng:         1.6.27-vc9_0      [vc9]
    openssl:        1.0.2k-vc9_0      [vc9]
    pip:            9.0.1-py27_1
    python:         2.7.13-0
    qt:             5.6.2-vc9_3       [vc9]
    setuptools:     27.2.0-py27_1
    vs2008_runtime: 9.00.30729.5054-0
    wheel:          0.29.0-py27_0
    zlib:           1.2.8-vc9_3       [vc9]

Proceed ([y]/n)?

ERROR conda.core.link:_execute_actions(319): An error occurred while installing package 'defaults::qt-5.6.2-vc9_3'.
```
 


Discussion:
If this problem is reproducible for you, can you give me the output of 

    conda create -n test qt -vvv

It'll be a lot of output.  Feel free to email it to me if you want.  kale@franz.io
I have the similar error too.

I found the reason. It is because OS message is not encoded in utf-8 (in my case, CP949)

On installation, ``subprocess_call`` function is called:
https://github.com/conda/conda/blob/33303158f070c0eda1ce0bbcb7807ddc7a4972d6/conda/gateways/subprocess.py#L62

Inside, this function calls ``ensure_text_type`` to validate standard output:
https://github.com/conda/conda/blob/7005e366a2a7dcd19430114c4a3d30f2eb735995/conda/common/compat.py#L154

``ensure_text_type`` raises a Unicode error because subprocess' standard output includes not-encoded-in-utf-8 string.

But, I have no idea why there was no problem when I installed qt in root environment.

 
Temporarily, I fixed this problem by modifying ``ensure_text_type`` like this:
```
import chardet

def ensure_text_type(value):
    if isinstance(value, text_type):
        encoding = 'utf-8'
    else:
        encoding = chardet.detect(value).get('encoding') or 'utf-8'
    return value.decode(encoding) if hasattr(value, 'decode') else value
```

Resolving patch:
diff --git a/conda/common/compat.py b/conda/common/compat.py
--- a/conda/common/compat.py
+++ b/conda/common/compat.py
@@ -152,7 +152,15 @@ def ensure_binary(value):
 
 
 def ensure_text_type(value):
-    return value.decode('utf-8') if hasattr(value, 'decode') else value
+    if hasattr(value, 'decode'):
+        try:
+            return value.decode('utf-8')
+        except UnicodeDecodeError:
+            from requests.packages.chardet import detect
+            encoding = detect(value).get('encoding') or 'utf-8'
+            return value.decode(encoding)
+    else:
+        return value
 
 
 def ensure_unicode(value):


Test patch: