Skip to content

Commit

Permalink
Fix key encoding in windows reloader.
Browse files Browse the repository at this point in the history
ref: #1320
  • Loading branch information
lepture committed Jun 7, 2018
1 parent d129d17 commit 7ddc8b8
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions werkzeug/_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,22 @@ def restart_with_reloader(self):
while 1:
_log('info', ' * Restarting with %s' % self.name)
args = _get_args_for_reloading()
new_environ = os.environ.copy()
new_environ['WERKZEUG_RUN_MAIN'] = 'true'

# 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):
new_environ = {}
for key, value in iteritems(os.environ):
if isinstance(key, text_type):
key = key.encode('iso-8859-1')
if isinstance(value, text_type):
new_environ[key] = value.encode('iso-8859-1')
value = value.encode('iso-8859-1')
new_environ[key] = value
else:
new_environ = os.environ.copy()

new_environ['WERKZEUG_RUN_MAIN'] = 'true'
exit_code = subprocess.call(args, env=new_environ,
close_fds=False)
if exit_code != 3:
Expand Down

1 comment on commit 7ddc8b8

@bxshcn
Copy link

@bxshcn bxshcn commented on 7ddc8b8 Oct 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, I have just found this problem when i use python-dotenv to get environment variables in a chinese windows platform, and it always tells me that "TypeError: environment can only contain strings", which is caused by the unicode key/value, great job! thanks!

Please sign in to comment.