# Buggy code
def main(args=None):
    colorful = False
    if sys.stderr.isatty():
        colorful = True
        try:
            import colorama

            colorama.init()
        except ImportError:
            if os.name == "nt":
                colorful = False

    ColorfulStderrHandler._colorful = colorful

    if args is None:
        args = sys.argv[1:]
    quiet = False
    if len(args) > 0 and args[0] == b"build" and b"--strict" in args:
        LOGGER.notice("Running in strict mode")
        STRICT_HANDLER.push_application()
    if len(args) > 0 and args[0] == b"build" and b"-q" in args or b"--quiet" in args:
        nullhandler = NullHandler()
        nullhandler.push_application()
        quiet = True
    global config

    # Those commands do not require a `conf.py`.  (Issue #1132)
    # Moreover, actually having one somewhere in the tree can be bad, putting
    # the output of that command (the new site) in an unknown directory that is
    # not the current working directory.  (does not apply to `version`)
    argname = args[0] if len(args) > 0 else None
    # FIXME there are import plugins in the repo, so how do we handle this?
    if (
        argname
        and argname not in ["init", "version"]
        and not argname.startswith("import_")
    ):
        root = get_root_dir()
        if root:
            os.chdir(root)

    sys.path.append("")
    try:
        import conf

        _reload(conf)
        config = conf.__dict__
    except Exception:
        if os.path.exists("conf.py"):
            msg = traceback.format_exc(0).splitlines()[1]
            LOGGER.error(
                "In conf.py line {0}: {1}".format(sys.exc_info()[2].tb_lineno, msg)
            )
            sys.exit(1)
        config = {}

    invariant = False

    if len(args) > 0 and args[0] == b"build" and b"--invariant" in args:
        try:
            import freezegun

            freeze = freezegun.freeze_time("2014-01-01")
            freeze.start()
            invariant = True
        except ImportError:
            req_missing(["freezegun"], "perform invariant builds")

    if config:
        if os.path.exists("plugins") and not os.path.exists("plugins/__init__.py"):
            with open("plugins/__init__.py", "w") as fh:
                fh.write("# Plugin modules go here.")

    config["__colorful__"] = colorful
    config["__invariant__"] = invariant
    config["__quiet__"] = quiet

    site = Nikola(**config)
    _ = DoitNikola(site, quiet).run(args)

    if site.invariant:
        freeze.stop()
    return _


# Traceback (UnicodeEncodeError)
D:\cocos_pristine>py -2.6 -c "import sys; from nikola.__main__ import main; main
(sys.argv[1:])" init --demo nkdemo
Creating Nikola Site
====================

This is Nikola v7.0.0.  We will now ask you a few easy questions about your new
site.
If you do not want to answer and want to go with the defaults instead, simply re
start with the `-q` parameter.
--- Questions about the site ---
Site title [My Nikola Site]:
Site author [Nikola Tesla]:
Site author's e-mail [n.tesla@example.com]:
Site description [This is a demo site for Nikola.]:
Site URL [http://getnikola.com/]:
--- Questions about languages and locales ---
We will now ask you to provide the list of languages you want to use.
Please list all the desired languages, comma-separated, using ISO 639-1 codes.
The first language will be used as the default.
Type '?' (a question mark, sans quotes) to list available languages.
Language(s) to use [en]: ?

# Available languages:
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\doit\doit_cmd.py", line 103, in run
return self.sub_cmds[command].parse_execute(args)
File "C:\Python26\lib\site-packages\doit\cmd_base.py", line 60, in parse_execu
te
return self.execute(params, args)
File "D:\cocos_pristine\getnikola\nikola\plugin_categories.py", line 98, in ex
ecute
self._execute(options, args)
File "D:\cocos_pristine\getnikola\nikola\plugins\command\init.py", line 398, i
n _execute
st = self.ask_questions(target=target)
File "D:\cocos_pristine\getnikola\nikola\plugins\command\init.py", line 375, i
n ask_questions
query(default, toconf)
File "D:\cocos_pristine\getnikola\nikola\plugins\command\init.py", line 266, i
n lhandler
print(SAMPLE_CONF['_SUPPORTED_LANGUAGES'] + '\n')
File "C:\Python26\lib\site-packages\colorama-0.3.1-py2.6.egg\colorama\ansitowi
n32.py", line 35, in write
self.__convertor.write(text)
File "C:\Python26\lib\site-packages\colorama-0.3.1-py2.6.egg\colorama\ansitowi
n32.py", line 116, in write
self.write_and_convert(text)
File "C:\Python26\lib\site-packages\colorama-0.3.1-py2.6.egg\colorama\ansitowi
n32.py", line 143, in write_and_convert
self.write_plain_text(text, cursor, len(text))
File "C:\Python26\lib\site-packages\colorama-0.3.1-py2.6.egg\colorama\ansitowi
n32.py", line 148, in write_plain_text
self.wrapped.write(text[start:end])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 347
: ordinal not in range(128)

# Fixed code
def main(args=None):
    colorful = False
    if sys.stderr.isatty() and os.name != "nt":
        colorful = True

    ColorfulStderrHandler._colorful = colorful

    if args is None:
        args = sys.argv[1:]
    quiet = False
    if len(args) > 0 and args[0] == b"build" and b"--strict" in args:
        LOGGER.notice("Running in strict mode")
        STRICT_HANDLER.push_application()
    if len(args) > 0 and args[0] == b"build" and b"-q" in args or b"--quiet" in args:
        nullhandler = NullHandler()
        nullhandler.push_application()
        quiet = True
    global config

    # Those commands do not require a `conf.py`.  (Issue #1132)
    # Moreover, actually having one somewhere in the tree can be bad, putting
    # the output of that command (the new site) in an unknown directory that is
    # not the current working directory.  (does not apply to `version`)
    argname = args[0] if len(args) > 0 else None
    # FIXME there are import plugins in the repo, so how do we handle this?
    if (
        argname
        and argname not in ["init", "version"]
        and not argname.startswith("import_")
    ):
        root = get_root_dir()
        if root:
            os.chdir(root)

    sys.path.append("")
    try:
        import conf

        _reload(conf)
        config = conf.__dict__
    except Exception:
        if os.path.exists("conf.py"):
            msg = traceback.format_exc(0).splitlines()[1]
            LOGGER.error(
                "In conf.py line {0}: {1}".format(sys.exc_info()[2].tb_lineno, msg)
            )
            sys.exit(1)
        config = {}

    invariant = False

    if len(args) > 0 and args[0] == b"build" and b"--invariant" in args:
        try:
            import freezegun

            freeze = freezegun.freeze_time("2014-01-01")
            freeze.start()
            invariant = True
        except ImportError:
            req_missing(["freezegun"], "perform invariant builds")

    if config:
        if os.path.exists("plugins") and not os.path.exists("plugins/__init__.py"):
            with open("plugins/__init__.py", "w") as fh:
                fh.write("# Plugin modules go here.")

    config["__colorful__"] = colorful
    config["__invariant__"] = invariant
    config["__quiet__"] = quiet

    site = Nikola(**config)
    _ = DoitNikola(site, quiet).run(args)

    if site.invariant:
        freeze.stop()
    return _