Bug#861201: python3: Incoherent "for" behaviour with different iterables

2017-04-25 Thread Florian Bruhin
On Tue, Apr 25, 2017 at 09:55:01PM +0200, Dominik George wrote:
> Hi,
> 
> > Python should raise a RuntimeError exception with a list as with a set.
> 
> It *does*, for a list.
> 
> But in Python 3, with [], you do not get a list, but an iterator with a 
> list-like API ;). It wraps a list which you add to, but __next__ will yield 
> the next item if there is one.

That's not true:

Python 3.6.1 (default, Mar 27 2017, 00:27:06)
[GCC 6.3.1 20170306] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list()
[]
>>> type(list())

>>> type([])


If you want an iterator, you need to explicitly ask for it:

>>> iter([])


Calling next() on a list won't work:

>>> next([])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'list' object is not an iterator

Florian

-- 
https://www.qutebrowser.org  | m...@the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072  | https://the-compiler.org/pubkey.asc
 I love long mails!  | https://email.is-not-s.ms/


signature.asc
Description: PGP signature


Bug#861201: python3: Incoherent "for" behaviour with different iterables

2017-04-25 Thread Dominik George
Hi,

> Python should raise a RuntimeError exception with a list as with a set.

It *does*, for a list.

But in Python 3, with [], you do not get a list, but an iterator with a 
list-like API ;). It wraps a list which you add to, but __next__ will yield the 
next item if there is one.

-nik



Bug#861201: python3: Incoherent "for" behaviour with different iterables

2017-04-25 Thread Nicolas Patrois
Package: python3
Version: 3.5.3-1
Severity: wishlist
Tags: upstream

Dear Maintainer,

In this thread (in French), I saw that the for loop behaves differently with 
sets, lists or frozensets.
http://www.les-mathematiques.net/phorum/read.php?15,1446990,1446990#msg-1446990

With a set:
>>> ens={1, 2}
>>> for i in ens:
...   ens.add(i+1)
...
Traceback (most recent call last):
  File "", line 1, in 
RuntimeError: Set changed size during iteration

With a frozenset:
>>> ens=frozenset({1,2})
>>> for i in ens:
...   ens=ens|{i+1}
... 
>>> ens
frozenset({1, 2, 3})

All is right, if the iterable is a hashable container, the for loops on a fixed 
list.

Now, with a list:
>>> ens=[1, 2]
>>> for i in ens:
...   ens.append(i+1)
...
^CTraceback (most recent call last):
  File "", line 1, in 
KeyboardInterrupt
>>> len(ens)
14453388

And a tuple:
>>> ens=(1,2)
>>> for i in ens:
...   ens=ens+(i,)
... 
>>> 
>>> ens
(1, 2, 1, 2)

Python should raise a RuntimeError exception with a list as with a set.

-- System Information:
Debian Release: 9.0
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: i386
 (i686)

Kernel: Linux 4.3.0-1-686-pae (SMP w/3 CPU cores)
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages python3 depends on:
ii  dh-python  2.20170125
ii  libpython3-stdlib  3.5.3-1
ii  python3-minimal3.5.3-1
ii  python3.5  3.5.3-1

python3 recommends no packages.

Versions of packages python3 suggests:
ii  python3-doc   3.5.3-1
ii  python3-tk3.5.3-1
pn  python3-venv  

-- no debconf information