Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19081
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python': 0.08; 'bash': 0.09; 'corresponds': 0.09; 'filename': 0.09; 'locale': 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; 'url:peps': 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; 'wed,': 0.17; 'bytes': 0.18; 'convert': 0.19; 'jan': 0.19; '(most': 0.21; 'maybe': 0.21; 'subject:list': 0.21; 'figure': 0.23; 'from:addr:web.de': 0.23; 'url:dev': 0.23; 'string': 0.24; 'traceback': 0.24; 'pass': 0.28; 'example': 0.28; 'script.': 0.28; "skip:' 10": 0.29; 'print': 0.29; 'skip:p 30': 0.29; 'python3': 0.30; 'strings.': 0.30; '+0100': 0.32; 'does': 0.32; 'list': 0.32; "can't": 0.32; "won't": 0.33; 'to:addr:python-list': 0.33; 'there': 0.33; 'character': 0.34; 'function.': 0.34; 'last):': 0.34; 'header:X-Complaints- To:1': 0.34; 'skip:" 20': 0.35; 'operating': 0.35; 'file': 0.35; 'url:python': 0.36; 'skip:" 10': 0.36; 'but': 0.37; 'received:org': 0.37; 'happens': 0.37; 'open': 0.38; 'url:org': 0.39; '(with': 0.39; 'help': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'back': 0.62; 'believe': 0.65; 'special': 0.67; 'strange': 0.68; 'safe': 0.70; 'funny': 0.76; 'otten': 0.84 |
| 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 15:01:38 +0100 |
| Organization | None |
| References | <20120118081612.13745187@bigfoot.com> <mailman.4823.1326873960.27778.python-list@python.org> <20120118111627.14d490ac@bigfoot.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p50849e60.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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4827.1326895312.27778.python-list@python.org> (permalink) |
| Lines | 45 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1326895313 news.xs4all.nl 6927 [2001:888:2000:d::a6]:38882 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:19081 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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