473,467 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"env" parameter to "popen" won't accept Unicode on Windows - minorUnicode bug

I passed a dict for the "env" variable to Popen with Unicode strings
for the dictionary values.

Got:

File "D:\Python24\lib\subprocess.py", line 706, in _execute_child
TypeError: environment can only contain strings

It turns out that the strings in the "env" parameter have to be ASCII,
not Unicode, even though Windows fully supports Unicode in CreateProcess.

John Nagle
Jan 15 '08 #1
11 4253
John Nagle wrote:
It turns out that the strings in the "env" parameter have to be
ASCII, not Unicode, even though Windows fully supports Unicode in
CreateProcess.
Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.

Regards,
Björn

--
BOFH excuse #31:

cellular telephone interference

Jan 15 '08 #2
On Jan 14, 6:26 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
John Nagle wrote:
It turns out that the strings in the "env" parameter have to be
ASCII, not Unicode, even though Windows fully supports Unicode in
CreateProcess.

Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.
Otherwise: bugs.python.org
>
Regards,

Björn

--
BOFH excuse #31:

cellular telephone interference
Jan 15 '08 #3
On Jan 14, 6:26 pm, John Nagle <na...@animats.comwrote:
I passed a dict for the "env" variable to Popen with Unicode strings
for the dictionary values.

Got:

File "D:\Python24\lib\subprocess.py", line 706, in _execute_child
TypeError: environment can only contain strings

It turns out that the strings in the "env" parameter have to be ASCII,
not Unicode, even though Windows fully supports Unicode in CreateProcess.
>
John Nagle
Jan 15 '08 #4
Benjamin wrote:
On Jan 14, 6:26 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
>John Nagle wrote:
>>It turns out that the strings in the "env" parameter have to be
ASCII, not Unicode, even though Windows fully supports Unicode in
CreateProcess.
Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.
Otherwise: bugs.python.org
Whatever translation is necessary should be done in "popen", which
has cases for Windows and POSIX. "popen" is supposed to be cross-platform
to the extent possible. I think it's just something that didn't get fixed
when Unicode support went in.

John Nagle
Jan 15 '08 #5
John Nagle wrote:
Benjamin wrote:
>On Jan 14, 6:26 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
>>John Nagle wrote:
It turns out that the strings in the "env" parameter have to be
ASCII, not Unicode, even though Windows fully supports Unicode in
CreateProcess.
That's of course nonsense, they don't need to be ascii, they need to be
byte-strings in whatever encoding you like.
>>Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.
Otherwise: bugs.python.org
John's understanding of the differences between unicode and it's encodings
is a bit blurry, to say the least.
Whatever translation is necessary should be done in "popen", which
has cases for Windows and POSIX. "popen" is supposed to be cross-platform
to the extent possible. I think it's just something that didn't get fixed
when Unicode support went in.
Sure thing, python will just magically convert unicode to the encoding the
program YOU invoke will expect. Right after we introduced the

solve_my_problem()

built-in-function. Any other wishes?

If I write this simple program

------ test.py -------
import os
import sys

ENCODDINGS = ['utf-8', 'latin1']

os.env["MY_VARIABLE"].encode(ENCODINGS[int(sys.argv[1])])
------ test.py -------
how's python supposed to know that

suprocess.call("python test.py 0", env=dict(MY_VARIABLE=u'foo'))

needs to be UTF-8?

Diez
Jan 15 '08 #6
Diez B. Roggisch wrote:
Sure thing, python will just magically convert unicode to the
encoding the program YOU invoke will expect. Right after we
introduced the

solve_my_problem()

built-in-function. Any other wishes?
There's no reason to be rude.

Anyway, at least on Windows it makes perfect sense for people to expect
Unicode to be handled automatically. popen() knows that it is running on
Windows, and it knows what encoding Windows needs for its environment
(it's either UCS2 or UTF-16 for most Windows APIs). At least when it
receives a unicode string, it has enough information to apply the
conversion automatically, and doing so saves the caller from having to
figure out what exact encoding is to be used.

- Brian

Jan 15 '08 #7
Brian Smith wrote:
Diez B. Roggisch wrote:
>Sure thing, python will just magically convert unicode to the
encoding the program YOU invoke will expect. Right after we
introduced the

solve_my_problem()

built-in-function. Any other wishes?

There's no reason to be rude.
If you'd know John, you'd know there is.
Anyway, at least on Windows it makes perfect sense for people to expect
Unicode to be handled automatically. popen() knows that it is running on
Windows, and it knows what encoding Windows needs for its environment
(it's either UCS2 or UTF-16 for most Windows APIs). At least when it
receives a unicode string, it has enough information to apply the
conversion automatically, and doing so saves the caller from having to
figure out what exact encoding is to be used.

For once, the distinction between windows and other platforms is debatable.
I admit that subprocess contains already quite a few platform specific
aspects, but it's purpose is to abstract these away as much as possible.

However, I'm not sure that just because there are wide-char windows apis
available automatically means that using UCS2/UTF-16 would succeed. A look
into the python sources (PC/_subprocess.c) reveals that someone already
thought about this, but it seems that just setting a
CREATE_UNICODE_ENVIRONMENT in the CreateProcess-function should have been
easy enough to do it if there weren't any troubles to expect.

Additionally, passing unicode to env would also imply that os.environ should
yield unicode as well. Not sure how much code _that_ breaks.

Diez
Jan 15 '08 #8
Brian Smith wrote:
popen() knows that it is running on Windows, and it knows what
encoding Windows needs for its environment (it's either UCS2 or
UTF-16 for most Windows APIs). At least when it receives a unicode
string, it has enough information to apply the conversion
automatically, and doing so saves the caller from having to figure
out what exact encoding is to be used.
So you propose Python should employ a hidden automatism that
automagically guesses the right encoding? Why not leave it
explicitly/consistently and let the user decide? What will happen
if a future Windows changes its encoding? Will we need another
magic routine to tell it apart?

Regards,
Björn

--
BOFH excuse #353:

Second-system effect.

Jan 15 '08 #9
Diez B. Roggisch wrote:
John Nagle wrote:
>Benjamin wrote:
>>On Jan 14, 6:26 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
John Nagle wrote:
It turns out that the strings in the "env" parameter have to be
ASCII, not Unicode, even though Windows fully supports Unicode in
CreateProcess.

That's of course nonsense, they don't need to be ascii, they need to be
byte-strings in whatever encoding you like.
>>>Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.
Otherwise: bugs.python.org

John's understanding of the differences between unicode and it's encodings
is a bit blurry, to say the least.
Who's this guy?
>
> Whatever translation is necessary should be done in "popen", which
has cases for Windows and POSIX. "popen" is supposed to be cross-platform
to the extent possible. I think it's just something that didn't get fixed
when Unicode support went in.
I've been looking at the source code. There's "_PyPopenCreateProcess"
in "posixmodule.c". That one doesn't support passing an environment at
all; see the call to Windows CreateProcess. Is that the one that Popen uses?

Where is "win32process" in the source? It ought to be in Modules, but
it's not.

John Nagle
Jan 15 '08 #10
Diez B. Roggisch wrote:
Brian Smith wrote:
>Diez B. Roggisch wrote:
>>Sure thing, python will just magically convert unicode to the
encoding the program YOU invoke will expect. Right after we
introduced the

solve_my_problem()

built-in-function. Any other wishes?
There's no reason to be rude.

If you'd know John, you'd know there is.
?
>Anyway, at least on Windows it makes perfect sense for people to expect
Unicode to be handled automatically. popen() knows that it is running on
Windows, and it knows what encoding Windows needs for its environment
(it's either UCS2 or UTF-16 for most Windows APIs). At least when it
receives a unicode string, it has enough information to apply the
conversion automatically, and doing so saves the caller from having to
figure out what exact encoding is to be used.


For once, the distinction between windows and other platforms is debatable.
I admit that subprocess contains already quite a few platform specific
aspects, but it's purpose is to abstract these away as much as possible.

However, I'm not sure that just because there are wide-char windows apis
available automatically means that using UCS2/UTF-16 would succeed. A look
into the python sources (PC/_subprocess.c) reveals that someone already
thought about this, but it seems that just setting a
CREATE_UNICODE_ENVIRONMENT in the CreateProcess-function should have been
easy enough to do it if there weren't any troubles to expect.
The problem is that only the NT-derived Microsoft systems talk Unicode.
The DOS/Win16/Win9x family did not. But they did have CreateProcess.
So the current code will handle Win9x, but not Unicode.

When do we drop support for Win9x? It probably has to happen in
Python 3K, since that's Unicode-everywhere.

John Nagle
Jan 15 '08 #11
John Nagle wrote:
The problem is that only the NT-derived Microsoft systems
talk Unicode. The DOS/Win16/Win9x family did not. But they did
have CreateProcess. So the current code will handle Win9x, but not
Unicode.
Please explain, I don't understand. If you try using Windows system
functions in older Windows versions, u"mystring" will fail, too.
Those functions need byte strings, not Unicode string instances.
The latter have to be encoded to byte strings to pass them.

Regards,
Björn

--
BOFH excuse #70:

nesting roaches shorted out the ether cable

Jan 15 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jo Schambach | last post by:
I am trying to write a GUI with tkinter that displays the stdout from a regular C/C++ program in a text widget. The idea i was trying to use was as follows: 1) use "popen" to execute the C/C++...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.