# Buggy code
def main():
    from ..base.constants import ROOT_ENV_NAME

    if "-h" in sys.argv or "--help" in sys.argv:
        # all execution paths sys.exit at end.
        help(sys.argv[1], sys.argv[2])

    if len(sys.argv) > 2:
        shell = sys.argv[2]
    else:
        shell = ""

    if regex.match("^..(?:de|)activate$", sys.argv[1]):
        arg_num = len(sys.argv)
        if arg_num != 4:
            num_expected = 2
            if arg_num < 4:
                raise TooFewArgumentsError(
                    num_expected,
                    arg_num - num_expected,
                    "{} expected exactly two arguments:\
                                            shell and env name".format(sys.argv[1]),
                )
            if arg_num > 4:
                raise TooManyArgumentsError(
                    num_expected,
                    arg_num - num_expected,
                    sys.argv[2:],
                    "{} expected exactly two arguments:\
                                             shell and env name".format(sys.argv[1]),
                )

    if sys.argv[1] == "..activate":
        print(get_activate_path(sys.argv[3], shell))
        sys.exit(0)

    elif sys.argv[1] == "..deactivate.path":
        activation_path = get_activate_path(sys.argv[3], shell)

        if os.getenv("_CONDA_HOLD"):
            new_path = regex.sub(
                r"%s(:?)" % regex.escape(activation_path),
                r"CONDA_PATH_PLACEHOLDER\1",
                os.environ[str("PATH")],
                1,
            )
        else:
            new_path = regex.sub(
                r"%s(:?)" % regex.escape(activation_path),
                r"",
                os.environ[str("PATH")],
                1,
            )

        print(new_path)
        sys.exit(0)

    elif sys.argv[1] == "..checkenv":
        if len(sys.argv) < 4:
            raise ArgumentError(
                "Invalid arguments to checkenv.  Need shell and env name/path"
            )
        if len(sys.argv) > 4:
            raise ArgumentError("did not expect more than one argument.")
        if sys.argv[3].lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(sys.argv[3], shell)
        except ValueError as e:
            raise CondaValueError(text_type(e))

        # Make sure an env always has the conda symlink
        try:
            from ..base.context import context
            from ..install import symlink_conda

            symlink_conda(prefix, context.root_prefix, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                msg = (
                    "Cannot activate environment {0}.\n"
                    "User does not have write access for conda symlinks.".format(
                        sys.argv[2]
                    )
                )
                raise CondaEnvironmentError(msg)
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif sys.argv[1] == "..changeps1":
        from ..base.context import context

        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable.  The shell
    # script gets this value, and finishes the job.
    print(path)


# Traceback (UnicodeEncodeError)
(root) c:\msys64\home\kfranz\continuum\conda>python -m conda create -n qtcrΞator qt=5.6.2 python=3.6
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment C:\conda-root\envs\qtcrΞator:

The following NEW packages will be INSTALLED:

icu:            57.1-vc14_0   [vc14]
jpeg:           9b-vc14_0     [vc14]
libpng:         1.6.27-vc14_0 [vc14]
openssl:        1.0.2k-vc14_0 [vc14]
pip:            9.0.1-py36_1
python:         3.6.1-2
qt:             5.6.2-vc14_3  [vc14]
setuptools:     27.2.0-py36_1
vs2015_runtime: 14.0.25123-0
wheel:          0.29.0-py36_0
zlib:           1.2.8-vc14_3  [vc14]

Proceed ([y]/n)?

icu-57.1-vc14_ 100% |###############################| Time: 0:00:03  10.44 MB/s
jpeg-9b-vc14_0 100% |###############################| Time: 0:00:00  19.74 MB/s
python-3.6.1-2 100% |###############################| Time: 0:00:03  10.96 MB/s
zlib-1.2.8-vc1 100% |###############################| Time: 0:00:00   0.00  B/s
libpng-1.6.27- 100% |###############################| Time: 0:00:00  10.97 MB/s
qt-5.6.2-vc14_ 100% |###############################| Time: 0:00:05  11.55 MB/s
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

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



Current conda install:

platform : win-64
conda version : 4.3.18.post13+e7b1a6bf
conda is private : False
conda-env version : 4.3.18.post13+e7b1a6bf
conda-build version : not installed
python version : 3.6.1.final.0
requests version : 2.12.4
root environment : C:\conda-root  (writable)
default environment : C:\conda-root
envs directories : C:\conda-root\envs
C:\Users\kfranz\AppData\Local\conda\conda\envs
C:\Users\kfranz\.conda\envs
package cache : C:\conda-root\pkgs
C:\Users\kfranz\AppData\Local\conda\conda\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 : C:\Users\kfranz\.condarc
offline mode : False
user-agent : conda/4.3.18.post13+e7b1a6bf requests/2.12.4 CPython/3.6.1 Windows/10 Windows/10.0.14393
administrator : unknown

`$ c:\msys64\home\kfranz\continuum\conda\conda\__main__.py create -n qtcrΞator qt=5.6.2 python=3.6`




Traceback (most recent call last):
File "c:\msys64\home\kfranz\continuum\conda\conda\exceptions.py", line 632, in conda_exception_handler
return_value = func(*args, **kwargs)
File "c:\msys64\home\kfranz\continuum\conda\conda\cli\main.py", line 134, in _main
exit_code = args.func(args, p)
File "c:\msys64\home\kfranz\continuum\conda\conda\cli\main_create.py", line 68, in execute
install(args, parser, 'create')
File "c:\msys64\home\kfranz\continuum\conda\conda\cli\install.py", line 357, in install
execute_actions(actions, index, verbose=not context.quiet)
File "c:\msys64\home\kfranz\continuum\conda\conda\plan.py", line 830, in execute_actions
execute_instructions(plan, index, verbose)
File "c:\msys64\home\kfranz\continuum\conda\conda\history.py", line 75, in __exit__
self.update('exit')
File "c:\msys64\home\kfranz\continuum\conda\conda\history.py", line 104, in update
self.write_changes(last, curr)
File "c:\msys64\home\kfranz\continuum\conda\conda\history.py", line 263, in write_changes
write_head(fo)
File "c:\msys64\home\kfranz\continuum\conda\conda\history.py", line 27, in write_head
fo.write("# cmd: %s\n" % (' '.join(sys.argv)))
File "C:\conda-root\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u039e' in position 77: character maps to <undefined>

# Fixed code
def main():
    from ..base.constants import ROOT_ENV_NAME

    sys_argv = tuple(ensure_text_type(s) for s in sys.argv)

    if "-h" in sys_argv or "--help" in sys_argv:
        # all execution paths sys.exit at end.
        help(sys_argv[1], sys_argv[2])

    if len(sys_argv) > 2:
        shell = sys_argv[2]
    else:
        shell = ""

    if regex.match("^..(?:de|)activate$", sys_argv[1]):
        arg_num = len(sys_argv)
        if arg_num != 4:
            num_expected = 2
            if arg_num < 4:
                raise TooFewArgumentsError(
                    num_expected,
                    arg_num - num_expected,
                    "{} expected exactly two arguments:\
                                            shell and env name".format(sys_argv[1]),
                )
            if arg_num > 4:
                raise TooManyArgumentsError(
                    num_expected,
                    arg_num - num_expected,
                    sys_argv[2:],
                    "{} expected exactly two arguments:\
                                             shell and env name".format(sys_argv[1]),
                )

    if sys_argv[1] == "..activate":
        print(get_activate_path(sys_argv[3], shell))
        sys.exit(0)

    elif sys_argv[1] == "..deactivate.path":
        activation_path = get_activate_path(sys_argv[3], shell)

        if os.getenv("_CONDA_HOLD"):
            new_path = regex.sub(
                r"%s(:?)" % regex.escape(activation_path),
                r"CONDA_PATH_PLACEHOLDER\1",
                os.environ[str("PATH")],
                1,
            )
        else:
            new_path = regex.sub(
                r"%s(:?)" % regex.escape(activation_path),
                r"",
                os.environ[str("PATH")],
                1,
            )

        print(new_path)
        sys.exit(0)

    elif sys_argv[1] == "..checkenv":
        if len(sys_argv) < 4:
            raise ArgumentError(
                "Invalid arguments to checkenv.  Need shell and env name/path"
            )
        if len(sys_argv) > 4:
            raise ArgumentError("did not expect more than one argument.")
        if sys_argv[3].lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(sys_argv[3], shell)
        except ValueError as e:
            raise CondaValueError(text_type(e))

        # Make sure an env always has the conda symlink
        try:
            from ..base.context import context
            from ..install import symlink_conda

            symlink_conda(prefix, context.root_prefix, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                msg = (
                    "Cannot activate environment {0}.\n"
                    "User does not have write access for conda symlinks.".format(
                        sys_argv[2]
                    )
                )
                raise CondaEnvironmentError(msg)
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif sys_argv[1] == "..changeps1":
        from ..base.context import context

        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable.  The shell
    # script gets this value, and finishes the job.
    print(path)