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


Groups > comp.lang.python > #19075

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 09:05 +0100
Organization None
References <20120118081612.13745187@bigfoot.com>
Newsgroups comp.lang.python
Message-ID <mailman.4823.1326873960.27778.python-list@python.org> (permalink)

Show all headers | View raw


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'

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