Windows XP - Environment variable - Unicode

Martin v. Löwis martin at v.loewis.de
Sun Jul 13 05:09:54 EDT 2003


"John Roth" <newsgroups at jhrothjr.com> writes:

> The OP's question revolved around ***which*** code page was
> being used internally. Windows uses Unicode. That's not the same
> question as what code set Python uses to attempt to translate Unicode
> into a single byte character set.

Yes and no. What Windows uses is largely irrelevant, as Python does
not use Windows here. Instead, it uses the Microsoft C library, in
which environment variables are *not* stored in some Unicode encoding,
when accessed through the _environ pointer.

> As to more difficult, as I said above, I haven't perused the source,
> so I can't comment on that. If I had to do it myself, I'd probably
> start out by always using the Unicode variant of the Windows API
> call, and then check the type of the arguement to environ() to determine
> which to pass back. I'm not sure whether or not I'd throw an exception
> if the actual value couldn't be translated to the current SBCS code.

Notice that os.environ is not a function, but a dictionary. So there
is no system call involved when retrieving an environment
variable. Instead, they are all precomputed.

> On reading this over, it does sound a bit more strident than my
> responses usually do, but I will admit to being irritated at the
> assumption that you need to read the source to find out the
> answer to various questions.

If the question is "how does software Foo do something", the *only*
reliable way is to read the source. You may have a mental model that
may allow you to give an educated guess how Foo *might* do
something. In this case, your educated guess was wrong, that's why I
referred you to the source.

Regards,
Martin





More information about the Python-list mailing list