Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloader: Force the key of environment variable into string type #1320

Closed
wants to merge 2 commits into from

Conversation

greyli
Copy link
Member

@greyli greyli commented Jun 7, 2018

When enabling reloader with Python2 on Windows, it always throw out TypeError: environment can only contain strings:

Traceback (most recent call last):
  File "c:\python27\Lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Users\Administrator\.virtualenvs\bluelog-deploy-96IrcjQo\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\flask\cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\flask\cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\flask\cli.py", line 771, in run_command
    threaded=with_threads, ssl_context=cert)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\werkzeug\serving.py", line 812, in run_simple
    reloader_type)
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\werkzeug\_reloader.py", line 283, in run_with_reloader
    sys.exit(reloader.restart_with_reloader())
  File "c:\users\administrator\.virtualenvs\bluelog-deploy-96ircjqo\lib\site-packages\werkzeug\_reloader.py", line 140, in restart_with_reloader
    close_fds=False)
  File "c:\python27\Lib\subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "c:\python27\Lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "c:\python27\Lib\subprocess.py", line 640, in _execute_child
    startupinfo)
TypeError: environment can only contain strings

Dig into the source, the problem root is at werkzeug/_reloader.py#L123:

   def restart_with_reloader(self):
       # ...
            # a weird bug on windows. sometimes unicode strings end up in the
            # environment and subprocess.call does not like this, encode them
            # to latin1 and continue.
            if os.name == 'nt' and PY2:
                for key, value in iteritems(new_environ):
                    if isinstance(value, text_type):
                        new_environ[key] = value.encode('iso-8859-1')  # <-- only ecode value
            # ...

I found it only encode the value of the enviroment variable. So I try to print the new_environ after the process:

{'SYSTEMROOT': 'C:\\Windows', u'FLASK_APP': 'bluelog', 'PATH': 'C:\\Users\\Administrator\\.virtualenvs\\bluelog-dep...'}

I got something like u'FLASK_APP': 'bluelog', the key still be unicode!

This PR will encode the key of the enviroment variable into string type to prevent this issue.

@greyli greyli changed the title Force the key of env var into string type Reloader: Force the key of environment variable into string type Jun 7, 2018
lepture added a commit that referenced this pull request Jun 7, 2018
@lepture
Copy link
Contributor

lepture commented Jun 7, 2018

close it in favor of #1321

@lepture lepture closed this Jun 7, 2018
@greyli greyli deleted the force-envvar-to-string branch June 7, 2018 05:57
SpotlightKid referenced this pull request in theskumar/python-dotenv Nov 7, 2018
* Force environment variables to str

With Python2 on Windows, force environment variables to str to avoid "TypeError: environment can only contain strings " in Python's subprocess.py.

* PEP8 fix

* Add Python3 compatible

* Fix flake8 fails with F821

* Fix flake8 fails with F821
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants