forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1473498 - Fix Python 3 environment variables with subprocess r=gl…
…andium On Windows in Python 2, the subprocess module requires the use of bytes with the 'env' argument. For that reason, we would sometimes use byte strings with 'os.environ' like so: os.environ[b"FOO"] = b"bar" However, this is a failure with Python 3 as 'os.environ' must only be used with the text type. This patch creates a new 'setenv' helper that ensures we create new environment with 'bytes' on Python 2, and 'text' on Python 3. Differential Revision: https://phabricator.services.mozilla.com/D38237
- Loading branch information
Showing
6 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
import os | ||
|
||
from mach.test.common import TestBase | ||
from mozunit import main | ||
|
||
|
||
def test_set_isatty_environ(monkeypatch): | ||
# Make sure the 'MACH_STDOUT_ISATTY' variable gets set. | ||
monkeypatch.delenv('MACH_STDOUT_ISATTY', raising=False) | ||
monkeypatch.setattr(os, 'isatty', lambda fd: True) | ||
|
||
m = TestBase.get_mach() | ||
orig_run = m._run | ||
env_is_set = [] | ||
|
||
def wrap_run(*args, **kwargs): | ||
env_is_set.append('MACH_STDOUT_ISATTY' in os.environ) | ||
return orig_run(*args, **kwargs) | ||
|
||
monkeypatch.setattr(m, '_run', wrap_run) | ||
|
||
ret = m.run([]) | ||
assert ret == 0 | ||
assert env_is_set[0] | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
import os | ||
import sys | ||
|
||
|
||
def setenv(key, value): | ||
"""Compatibility shim to ensure the proper string type is used with | ||
os.environ for the version of Python being used. | ||
""" | ||
encoding = "mbcs" if sys.platform == "win32" else "utf-8" | ||
|
||
if sys.version_info[0] == 2: | ||
if isinstance(key, unicode): | ||
key = key.encode(encoding) | ||
if isinstance(value, unicode): | ||
value = value.encode(encoding) | ||
else: | ||
if isinstance(key, bytes): | ||
key = key.decode(encoding) | ||
if isinstance(value, bytes): | ||
value = value.decode(encoding) | ||
|
||
os.environ[key] = value |