[issue34494] simple "sequence" class ignoring __len__

2018-08-26 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

I concur with Josh.  This matches the documented behavior and isn't a bug. 
Marking as closed.

There are potentially two ways to stop sequence iteration, either by using len 
or by waiting for IndexError.  Python uses the latter to allow lists to be 
dynamically resized during iteration and because checking len on every 
iteration would be expensive.  

If someone doesn't read the docs, and assumes Python uses len, and writes an 
inconsistent class (one where len doesn't match the point where IndexError is 
raised), then there is little we can do to prevent that person from being 
"surprised".

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-26 Thread Josh Rosenberg


Josh Rosenberg  added the comment:

That's the documented behavior. Per 
https://docs.python.org/3/reference/datamodel.html#object.__getitem__ :

>Note: for loops expect that an IndexError will be raised for illegal indexes 
>to allow proper detection of the end of the sequence. 

The need for *only* __getitem__ is also mentioned in the documentation of the 
iter builtin ( https://docs.python.org/3/library/functions.html#iter ):

>Without a second argument, object must be a collection object which supports 
>the iteration protocol (the __iter__() method), or it must support the 
>sequence protocol (the __getitem__() method with integer arguments starting at 
>0).

At no point is a dependency on __len__ mentioned.

--
nosy: +josh.r

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-25 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee:  -> rhettinger
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-25 Thread Eric Wieser


Eric Wieser  added the comment:

What I think I find surprising is that I'd expect the sequence protocol to be 
defined by `__getitem__` and `__len__`, and for `__iter__` to be inferred as:

def __iter__(self):
for i in range(len(self)):
yield self[i]

But in reality it seems it is inferred only from `__getitem__`, as:

def __iter__(self):
i = 0
while True:
try:
yield self[i]
except IndexError:
return
i += 1

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-25 Thread Eric Wieser


Change by Eric Wieser :


--
nosy: +Eric.Wieser

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-25 Thread Prudvi RajKumar Maddala


Prudvi RajKumar Maddala  added the comment:

I think it should hang, since we are not throwing any StopIteration exception 
to break the infinite loop

--
nosy: +prudvinit

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34494] simple "sequence" class ignoring __len__

2018-08-24 Thread Tyler Reddy


New submission from Tyler Reddy :

Downstream in NumPy we've noticed that a "sequence" object defined as below 
will hang (infinite __getitem__ calls) if we try to turn it into an array. The 
same holds in CPython for converting it to a list:

class OneList:

def __len__(self):
# this won't be checked by
# PySequence_Fast and several
# over C API calls
return 1

def __getitem__(self, x):
# called indefinitely by
# i.e., PySequence_Fast
return 1

Just to confirm -- this is intentional / desired behavior:

list(OneList()) should hang in CPython?

related: 
https://github.com/numpy/numpy/issues/8912
https://github.com/numpy/numpy/pull/11815
https://stackoverflow.com/a/43566241/2942522

--
messages: 324037
nosy: treddy
priority: normal
severity: normal
status: open
title: simple "sequence" class ignoring __len__
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com