# Buggy code
def portable_popen(cmd, *args, **kwargs):
    """A portable version of subprocess.Popen that automatically locates
    executables before invoking them.  This also looks for executables
    in the bundle bin.
    """
    if cmd[0] is None:
        raise RuntimeError("No executable specified")
    exe = locate_executable(cmd[0], kwargs.get("cwd"))
    if exe is None:
        raise RuntimeError('Could not locate executable "%s"' % cmd[0])

    if isinstance(exe, text_type):
        exe = exe.encode(sys.getfilesystemencoding())
    cmd[0] = exe
    return subprocess.Popen(cmd, *args, **kwargs)


# Traceback (TypeError)
λ lektor build
Updating packages in C:\Users\Aperez\AppData\Local\Lektor\Cache\packages\4f6825e8ddb9e94fa8041414ed8a88e0 for project
Traceback (most recent call last):
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\Scripts\lektor-script.py", line 11, in <module>
load_entry_point('Lektor==3.0', 'console_scripts', 'lektor')()
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 697, in main
rv = self.invoke(ctx)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\cli.py", line 198, in build_cmd
ctx.load_plugins()
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\cli.py", line 114, in load_plugins
load_packages(self.get_env(), reinstall=reinstall)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\packages.py", line 304, in load_packages
refresh=reinstall)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\packages.py", line 274, in update_cache
download_and_install_package(package_root, package, version)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\packages.py", line 101, in download_and_install_package
rv = portable_popen(args, env=env).wait()
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\site-packages\lektor-3.0-py3.6.egg\lektor\utils.py", line 490, in portable_popen
return subprocess.Popen(cmd, *args, **kwargs)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 966, in _execute_child
args = list2cmdline(args)
File "C:\Users\Aperez\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 461, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def portable_popen(cmd, *args, **kwargs):
    """A portable version of subprocess.Popen that automatically locates
    executables before invoking them.  This also looks for executables
    in the bundle bin.
    """
    if cmd[0] is None:
        raise RuntimeError("No executable specified")
    exe = locate_executable(cmd[0], kwargs.get("cwd"))
    if exe is None:
        raise RuntimeError('Could not locate executable "%s"' % cmd[0])

    if isinstance(exe, text_type) and sys.platform != "win32":
        exe = exe.encode(sys.getfilesystemencoding())
    cmd[0] = exe
    return subprocess.Popen(cmd, *args, **kwargs)