Repository: conda/conda

Issue:
LookupError: unknown encoding: 65001 caused by pull/8289
<!--
Hi!  Read this; it's important.

This is an issue tracker for conda -- the package manager.  File feature requests
for conda here, as well as bug reports about something conda has messed up.

If your issue is a bug report for:
  * a specific conda package from Anaconda ('defaults' channel):
      ==> file at https://github.com/ContinuumIO/anaconda-issues
  * a specific conda package from conda-forge:
      ==> file at the corresponding feedstock under https://github.com/conda-forge
  * repo.anaconda.com access and service:
      ==> file at https://github.com/ContinuumIO/anaconda-issues
  * anaconda.org access and service:
      ==> file at https://anaconda.org/contact/report
  * commands under 'conda build':
      ==> file at https://github.com/conda/conda-build
  * commands under 'conda env':
      ==> please file it here!
  * all other conda commands that start with 'conda':
      ==> please file it here!

If you continue on,

   **please include all requested information below.**

If a maintainer determines the information is required to understand 
your issue, and if it is not provided, your issue may be
closed automatically.

-->

## Current Behavior
<!-- What actually happens?
     If you want to include console output, please use "Steps to Reproduce" below. -->
![image](https://user-images.githubusercontent.com/4780199/53404057-d250fa00-39ef-11e9-9b54-57a880fc09e7.png)


### Steps to Reproduce
<!-- If the current behavior is a bug, please provide specific, minimal steps to independently reproduce.
     Include the exact conda commands that reproduce the issue and their output between the ticks below. -->
```
chcp 65001 
conda update --all
```

## Expected Behavior
<!-- What do you think should happen? -->
No Exception should be throw

## Environment Information
<details open><summary><code>`conda info`</code></summary><p>
<!-- between the ticks below, paste the output of 'conda info' -->

```
     active environment : None
       user config file : C:\Users\xxxxx\.condarc
 populated config files : C:\Users\xxxxx\.condarc
          conda version : 4.6.7
    conda-build version : not installed
         python version : 3.7.2.final.0
       base environment : D:\Miniconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : D:\Miniconda3\pkgs
                          C:\Users\xxxxx\.conda\pkgs
                          C:\Users\xxxxx\AppData\Local\conda\conda\pkgs
       envs directories : D:\Miniconda3\envs
                          C:\Users\xxxxx\.conda\envs
                          C:\Users\xxxxx\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.6.7 requests/2.21.0 CPython/3.7.2 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False
```
</p></details>


<details open><summary><code>`conda config --show-sources`</code></summary><p>
<!-- between the ticks below, paste the output of 'conda config --show-sources' -->

```
==> C:\Users\xxxxx\.condarc <==
ssl_verify: True
channels:
  - defaults
report_errors: False
```
</p></details>


<details><summary><code>`conda list --show-channel-urls`</code></summary><p>
<!-- between the ticks below, paste the output of 'conda list --show-channel-urls' -->

```
# packages in environment at D:\Miniconda3:
#
# Name                    Version                   Build  Channel
asn1crypto                0.24.0                   py37_0    defaults
backcall                  0.1.0                    py37_0    defaults
bleach                    3.1.0                    py37_0    defaults
ca-certificates           2019.1.23                     0    defaults
certifi                   2018.11.29               py37_0    defaults
cffi                      1.11.5           py37h74b6da3_1    defaults
chardet                   3.0.4                    py37_1    defaults
colorama                  0.4.1                    py37_0    defaults
conda                     4.6.7                    py37_0    defaults
conda-env                 2.6.0                         1    defaults
console_shortcut          0.1.1                         3    defaults
cryptography              2.5              py37h7a1dbc1_0    defaults
decorator                 4.3.2                    py37_0    defaults
entrypoints               0.3                      py37_0    defaults
idna                      2.8                      py37_0    defaults
ipykernel                 5.1.0            py37h39e3cac_0    defaults
ipython                   7.3.0            py37h39e3cac_0    defaults
ipython_genutils          0.2.0                    py37_0    defaults
jedi                      0.13.2                   py37_0    defaults
jinja2                    2.10                     py37_0    defaults
jsonschema                2.6.0                    py37_0    defaults
jupyter_client            5.2.4                    py37_0    defaults
jupyter_core              4.4.0                    py37_0    defaults
jupyterlab_server         0.2.0                    py37_0    defaults
libsodium                 1.0.16               h9d3ae62_0    defaults
m2w64-gcc-libgfortran     5.3.0                         6    defaults
m2w64-gcc-libs            5.3.0                         7    defaults
m2w64-gcc-libs-core       5.3.0                         7    defaults
m2w64-gmp                 6.1.0                         2    defaults
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    defaults
markupsafe                1.1.0            py37he774522_0    defaults
menuinst                  1.4.14           py37hfa6e2cd_0    defaults
mistune                   0.8.4            py37he774522_0    defaults
msys2-conda-epoch         20160418                      1    defaults
nbconvert                 5.3.1                    py37_0    defaults
nbformat                  4.4.0                    py37_0    defaults
notebook                  5.7.4                    py37_0    defaults
openssl                   1.1.1a               he774522_0    defaults
pandoc                    2.2.3.2                       0    defaults
pandocfilters             1.4.2                    py37_1    defaults
parso                     0.3.2                    py37_0    defaults
pickleshare               0.7.5                    py37_0    defaults
pip                       19.0.1                   py37_0    defaults
prometheus_client         0.5.0                    py37_0    defaults
prompt_toolkit            2.0.8                      py_0    defaults
pycosat                   0.6.3            py37hfa6e2cd_0    defaults
pycparser                 2.19                     py37_0    defaults
pygments                  2.3.1                    py37_0    defaults
pyopenssl                 19.0.0                   py37_0    defaults
pysocks                   1.6.8                    py37_0    defaults
python                    3.7.2               h8c8aaf0_10    defaults
python-dateutil           2.7.5                    py37_0    defaults
pywin32                   223              py37hfa6e2cd_1    defaults
pywinpty                  0.5.5                 py37_1000    defaults
pyzmq                     17.1.2           py37ha925a31_2    defaults
requests                  2.21.0                   py37_0    defaults
ruamel_yaml               0.15.46          py37hfa6e2cd_0    defaults
send2trash                1.5.0                    py37_0    defaults
setuptools                40.8.0                   py37_0    defaults
six                       1.12.0                   py37_0    defaults
sqlite                    3.26.0               he774522_0    defaults
terminado                 0.8.1                    py37_1    defaults
testpath                  0.4.2                    py37_0    defaults
tornado                   5.1.1            py37hfa6e2cd_0    defaults
traitlets                 4.3.2                    py37_0    defaults
urllib3                   1.24.1                   py37_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.15.26706          h3a45250_0    defaults
wcwidth                   0.1.7                    py37_0    defaults
webencodings              0.5.1                    py37_1    defaults
wheel                     0.32.3                   py37_0    defaults
win_inet_pton             1.0.1                    py37_1    defaults
wincertstore              0.2                      py37_0    defaults
winpty                    0.4.3                         4    defaults
yaml                      0.1.7                hc54c509_2    defaults
zeromq                    4.3.1                h33f27b4_3    defaults
```
</p></details>


Discussion:
pr https://github.com/conda/conda/pull/8289 caused this problem by introduce:

```
    if on_win:
        import ctypes
        acp = ctypes.cdll.kernel32.GetACP()
        encoding = str(acp)
    try:
        return value.encode(encoding)
```
https://github.com/conda/conda/blob/master/conda/activate.py#L533-L534

cdll.kernel32.GetACP() returns codepage number, which should not be use as encode format without 'cp' prefix.

instead it should be:
```
    encoding = 'cp' + str(cdll.kernel32.GetACP())
```


![image](https://user-images.githubusercontent.com/4780199/53404770-4f30a380-39f1-11e9-9865-201be93efbdb.png)


I believe it also caused issue https://github.com/conda/conda/issues/8327
```
                "PYTHONIOENCODING": ctypes.cdll.kernel32.GetACP(),
```
https://github.com/conda/conda/blob/master/conda/activate.py#L739

here should be another problem, 'cp' prefix should be included

Resolving patch:
diff --git a/conda/activate.py b/conda/activate.py
--- a/conda/activate.py
+++ b/conda/activate.py
@@ -530,8 +530,7 @@ def ensure_binary(value):
     encoding = 'utf-8'
     if on_win:
         import ctypes
-        acp = ctypes.cdll.kernel32.GetACP()
-        encoding = str(acp)
+        encoding = 'cp' + str(ctypes.cdll.kernel32.GetACP())
     try:
         return value.encode(encoding)
     except AttributeError:  # pragma: no cover
@@ -736,7 +735,7 @@ def _build_activate_shell_custom(self, export_vars):
         if on_win:
             import ctypes
             export_vars.update({
-                "PYTHONIOENCODING": ctypes.cdll.kernel32.GetACP(),
+                "PYTHONIOENCODING": 'cp' + str(ctypes.cdll.kernel32.GetACP()),
             })
 
     def _hook_preamble(self):


Test patch:
diff --git a/tests/test_activate.py b/tests/test_activate.py
--- a/tests/test_activate.py
+++ b/tests/test_activate.py
@@ -38,7 +38,7 @@
 
 if on_win:
     import ctypes
-    PYTHONIOENCODING = ctypes.cdll.kernel32.GetACP()
+    PYTHONIOENCODING = 'cp' + str(ctypes.cdll.kernel32.GetACP())
 else:
     PYTHONIOENCODING = None