Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19081

Re: sys.argv as a list of bytes

From Peter Otten <__peter__@web.de>
Subject Re: sys.argv as a list of bytes
Date 2012-01-18 15:01 +0100
Organization None
References <20120118081612.13745187@bigfoot.com> <mailman.4823.1326873960.27778.python-list@python.org> <20120118111627.14d490ac@bigfoot.com>
Newsgroups comp.lang.python
Message-ID <mailman.4827.1326895312.27778.python-list@python.org> (permalink)

Show all headers | View raw


Olive wrote:

> On Wed, 18 Jan 2012 09:05:42 +0100
> Peter Otten <__peter__@web.de> wrote:
> 
>> Olive wrote:
>> 
>> > In Unix the operating system pass argument as a list of C strings.
>> > But C strings does corresponds to the bytes notions of Python3. Is
>> > it possible to have sys.argv as a list of bytes ? What happens if I
>> > pass to a program an argumpent containing funny "character", for
>> > example (with a bash shell)?
>> > 
>> > python -i ./test.py $'\x01'$'\x05'$'\xFF'
>> 
>> Python has a special errorhandler, "surrogateescape" to deal with
>> bytes that are not valid UTF-8. If you try to print such a string you
>> get an error:
>> 
>> $ python3 -c'import sys; print(repr(sys.argv[1]))'
>> $'\x01'$'\x05'$'\xFF' '\x01\x05\udcff'
>> $ python3 -c'import sys; print(sys.argv[1])' $'\x01'$'\x05'$'\xFF'
>> Traceback (most recent call last):
>>   File "<string>", line 1, in <module>
>> UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in
>> position 2: surrogates not allowed
>> 
>> It is still possible to get the original bytes:
>> 
>> $ python3 -c'import sys; print(sys.argv[1].encode("utf-8",
>> "surrogateescape"))' $'\x01'$'\x05'$'\xFF' b'\x01\x05\xff'
>> 
>> 
> 
> But is it safe even if the locale is not UTF-8? I would like to be able
> to pass a file name to a script. I can use bytes for file names in the
> open function. If I keep the filename as bytes everywhere it will work
> reliably whatever the locale or strange character the file name may
> contain.

I believe you need not convert back to bytes explicitly, you can open the 
file with open(sys.argv[i]). I don't know if there are cornercases where 
that won't work; maybe http://www.python.org/dev/peps/pep-0383/ can help you 
figure it out.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

sys.argv as a list of bytes Olive <diolu@bigfoot.com> - 2012-01-18 08:16 +0100
  Re: sys.argv as a list of bytes Peter Otten <__peter__@web.de> - 2012-01-18 09:05 +0100
    Re: sys.argv as a list of bytes Olive <diolu@bigfoot.com> - 2012-01-18 11:16 +0100
      Re: sys.argv as a list of bytes Peter Otten <__peter__@web.de> - 2012-01-18 15:01 +0100
      Re: sys.argv as a list of bytes Nobody <nobody@nowhere.com> - 2012-01-19 05:05 +0000
        Re: sys.argv as a list of bytes jmfauth <wxjmfauth@gmail.com> - 2012-01-19 02:40 -0800

csiph-web