PY-40974 Always treat environment variables as raw bytes

It's unnecessary and sometimes wrong to assume any encoding
for an environment variable. They can be encoded differently
depending on the requirements of programs that utilize them.
In our case we just need to pass all the environment
variables "as is" to the IDE process.

I've modified printenv.py so that it's the same version of
the script for both Linux and macOS and for all the Python
versions from 2.3 to at least 3.9.

GitOrigin-RevId: 40d73ef6eb56da8609d75fbce8a4a04c52e317b4
This commit is contained in:
Andrey Vlasovskikh
2020-05-31 15:42:12 +03:00
committed by intellij-monorepo-bot
parent 9df95ee092
commit f07f53d38e
2 changed files with 40 additions and 9 deletions

View File

@@ -2,6 +2,7 @@
# Dumps environment variables into specified file.
# Format: zero-separated "name=value" pairs in platform encoding.
# The script can work with any version of Python from 2.3 to at least 3.9
import os
import sys
@@ -9,10 +10,24 @@ import sys
if len(sys.argv) != 2:
raise Exception('Exactly one argument expected')
f = open(sys.argv[1], 'wb')
PY2 = sys.version_info < (3,)
if PY2:
environ = os.environ
else:
environ = os.environb
def b(s):
if PY2:
return s
else:
return s.encode('utf-8')
fd = open(sys.argv[1], 'wb')
try:
for key, value in os.environ.items():
s = '%s=%s\0' % (key, value)
f.write(s.encode('utf-8'))
for key, value in environ.items():
fd.writelines([key, b('='), value, b('\0')])
finally:
f.close()
fd.close()

View File

@@ -2,6 +2,7 @@
# Dumps environment variables into specified file.
# Format: zero-separated "name=value" pairs in platform encoding.
# The script can work with any version of Python from 2.3 to at least 3.9
import os
import sys
@@ -9,9 +10,24 @@ import sys
if len(sys.argv) != 2:
raise Exception('Exactly one argument expected')
f = open(sys.argv[1], 'w')
PY2 = sys.version_info < (3,)
if PY2:
environ = os.environ
else:
environ = os.environb
def b(s):
if PY2:
return s
else:
return s.encode('utf-8')
fd = open(sys.argv[1], 'wb')
try:
for key, value in os.environ.items():
f.writelines([key, '=', value, '\0'])
for key, value in environ.items():
fd.writelines([key, b('='), value, b('\0')])
finally:
f.close()
fd.close()