Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.08; 'bash': 0.09; 'corresponds': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'error:': 0.10; 'argument': 0.15; 'codec': 0.16; 'encode': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'surrogates': 0.16; 'wrote:': 0.16; 'bytes': 0.18; '(most': 0.21; 'subject:list': 0.21; 'from:addr:web.de': 0.23; 'string': 0.24; 'traceback': 0.24; 'pass': 0.28; 'example': 0.28; "skip:' 10": 0.29; 'print': 0.29; 'skip:p 30': 0.29; 'python3': 0.30; 'strings.': 0.30; 'does': 0.32; 'list': 0.32; "can't": 0.32; 'to:addr:python-list': 0.33; 'character': 0.34; 'last):': 0.34; 'header:X-Complaints-To:1': 0.34; 'skip:" 20': 0.35; 'operating': 0.35; 'file': 0.35; 'skip:" 10': 0.36; 'but': 0.37; 'received:org': 0.37; 'happens': 0.37; '(with': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'special': 0.67; 'funny': 0.76 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: sys.argv as a list of bytes Date: Wed, 18 Jan 2012 09:05:42 +0100 Organization: None References: <20120118081612.13745187@bigfoot.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849617.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326873960 news.xs4all.nl 6935 [2001:888:2000:d::a6]:42058 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19075 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 "", line 1, in 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'