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


Groups > comp.lang.python > #63467

Re: Bytes indexing returns an int

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'languages.': 0.04; 'assignment': 0.07; 'indexing': 0.07; 'string': 0.09; 'bytes,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subtle': 0.09; 'python': 0.11; 'def': 0.12; '2.7': 0.14; 'changes': 0.15; '.........': 0.16; 'inconvenient': 0.16; 'indicators': 0.16; 'porting': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'unicode.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'issue.': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'bytes': 0.24; 'rid': 0.24; 'text.': 0.24; 'unicode': 0.24; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'words': 0.29; "i'm": 0.30; 'code': 0.31; 'python2.7': 0.31; 'class': 0.32; 'compatible': 0.32; 'quite': 0.32; 'style': 0.33; 'could': 0.34; "can't": 0.35; 'problem.': 0.35; 'etc': 0.35; 'but': 0.35; 'version': 0.36; 'useful': 0.36; 'possible': 0.36; 'two': 0.37; 'expected': 0.38; 'represent': 0.38; 'to:addr:python-list': 0.38; 'pdf': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'simple': 0.61; 'making': 0.63; 'real': 0.63; 'covers': 0.68; 'skip:r 30': 0.69; 'received:109': 0.72; '.....': 0.78; 'glad': 0.83; 'dispatching': 0.84; 'western': 0.86
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robin Becker <robin@reportlab.com>
Subject Re: Bytes indexing returns an int
Date Wed, 08 Jan 2014 11:05:49 +0000
References <52cbe15a$0$29993$c3e8da3$5496439d@news.astraweb.com> <mailman.5135.1389107956.18130.python-list@python.org> <52cc278c$0$29979$c3e8da3$5496439d@news.astraweb.com> <lahll7$f9c$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 109.174.168.73
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <lahll7$f9c$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5162.1389179166.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389179166 news.xs4all.nl 2869 [2001:888:2000:d::a6]:38836
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63467

Show key headers only | View raw


On 07/01/2014 19:48, Serhiy Storchaka wrote:
........
> data[0] == b'\xE1'[0] works as expected in both Python 2.7 and 3.x.
>
>
I have been porting a lot of python 2 only code to a python2.7 + 3.3 version for 
a few months now. Bytes indexing was a particular problem. PDF uses quite a lot 
of single byte indicators so code like

if text[k] == 'R':
    .....

or

dispatch_dict.get(text[k],error)()

is much harder to make compatible because of this issue. I think this change was 
a mistake.

To get round this I have tried the following class to resurrect the old style 
behaviour

if isPy3:
	class RLBytes(bytes):
		'''simply ensures that B[x] returns a bytes type object and not an int'''
		def __getitem__(self,x):
			if isinstance(x,int):
				return RLBytes([bytes.__getitem__(self,x)])
			else:
				return RLBytes(bytes.__getitem__(self,x))

I'm not sure if that covers all possible cases, but it works for my dispatching 
cases. Unfortunately you can't do simple class assignment to change the 
behaviour so you have to copy the text.

I find a lot of the "so glad we got rid of byte strings" fervour a bit silly. 
Bytes, chars,  words etc etc were around long before unicode. Byte strings could 
already represent unicode in efficient ways that happened to be useful for 
western languages. Having two string types is inconvenient and error prone, 
swapping their labels and making subtle changes is a real pain.
-- 
Robin Becker

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


Thread

Bytes indexing returns an int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-07 22:13 +1100
  Re: Bytes indexing returns an int Ervin Hegedüs <airween@gmail.com> - 2014-01-07 12:53 +0100
    Re: Bytes indexing returns an int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-07 23:04 +1100
  Re: Bytes indexing returns an int Terry Reedy <tjreedy@udel.edu> - 2014-01-07 09:29 -0500
  Re: Bytes indexing returns an int David Robinow <drobinow@gmail.com> - 2014-01-07 10:19 -0500
    Re: Bytes indexing returns an int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-08 03:12 +1100
      Re: Bytes indexing returns an int Serhiy Storchaka <storchaka@gmail.com> - 2014-01-07 21:48 +0200
      Re: Bytes indexing returns an int Robin Becker <robin@reportlab.com> - 2014-01-08 11:05 +0000
        Re: Bytes indexing returns an int wxjmfauth@gmail.com - 2014-01-08 08:08 -0800
          Re: Bytes indexing returns an int Ned Batchelder <ned@nedbatchelder.com> - 2014-01-08 12:19 -0500
            Re: Bytes indexing returns an int Piet van Oostrum <piet@vanoostrum.org> - 2014-01-09 18:05 +0100
              Re: Bytes indexing returns an int Ethan Furman <ethan@stoneleaf.us> - 2014-01-09 09:28 -0800
              Re: Bytes indexing returns an int Serhiy Storchaka <storchaka@gmail.com> - 2014-01-09 21:36 +0200
          Re: Bytes indexing returns an int Michael Torrie <torriem@gmail.com> - 2014-01-08 10:25 -0700
  Re: Bytes indexing returns an int David Robinow <drobinow@gmail.com> - 2014-01-07 10:23 -0500
  Re: Bytes indexing returns an int Ethan Furman <ethan@stoneleaf.us> - 2014-01-07 09:02 -0800
    Re: Bytes indexing returns an int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-08 11:15 +1100
      Re: Bytes indexing returns an int Chris Angelico <rosuav@gmail.com> - 2014-01-08 11:30 +1100
        Re: Bytes indexing returns an int Grant Edwards <invalid@invalid.invalid> - 2014-01-08 02:34 +0000
          Re: Bytes indexing returns an int Chris Angelico <rosuav@gmail.com> - 2014-01-08 14:46 +1100
      Re: Bytes indexing returns an int Ethan Furman <ethan@stoneleaf.us> - 2014-01-07 16:37 -0800

csiph-web