[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH 5/9] iotests: Different iterator behavior in Pyt
From: |
Cleber Rosa |
Subject: |
Re: [Qemu-devel] [PATCH 5/9] iotests: Different iterator behavior in Python 3 |
Date: |
Mon, 15 Oct 2018 18:39:14 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 |
On 10/15/18 10:14 AM, Max Reitz wrote:
> In Python 3, several functions now return iterators instead of lists.
> This includes range(), items(), map(), and filter(). This means that if
> we really want a list, we have to wrap those instances with list(). On
> the other hand, sometimes we do just want an iterator, in which case we
> have sometimes used xrange() and iteritems() which no longer exist in
> Python 3. Just change these calls to be range() and items(), which
> costs a bit of performance in Python 2, but will do the right thing in
> Python 3 (which is what is important).
>
> In one instance, we only wanted the first instance of the result of a
> filter() call. Instead of using next(filter()) which would work only in
> Python 3, or list(filter())[0] which would work everywhere but is a bit
> weird, this instance is changed to a single-line for with next() wrapped
> around, which works both in 2.7 and 3.
>
> Signed-off-by: Max Reitz <address@hidden>
> ---
> tests/qemu-iotests/044 | 12 ++++++------
> tests/qemu-iotests/056 | 2 +-
> tests/qemu-iotests/065 | 4 ++--
> tests/qemu-iotests/124 | 4 ++--
> tests/qemu-iotests/139 | 2 +-
> tests/qemu-iotests/163 | 6 +++---
> 6 files changed, 15 insertions(+), 15 deletions(-)
>
You have 2 files here which use xrange (which is a manageable size, and
whose occurrences involve a moderate size of items) to also consider:
if sys.version_info.major == 2:
range = xrange
Defaulting to the Python 3 names, but behaving the same across Python 2
and 3.
To do the same for dict.iteritems() => dict.items() requires a lot more
code, so I'd stay away from it for now. Also, it looks like most of
those dicts are small in size (**kwargs and the like).
Other than that suggestion,
Reviewed-by: Cleber Rosa <address@hidden>
- [Qemu-devel] [PATCH 1/9] iotests: Make nbd-fault-injector flush, (continued)
- [Qemu-devel] [PATCH 1/9] iotests: Make nbd-fault-injector flush, Max Reitz, 2018/10/15
- [Qemu-devel] [PATCH 2/9] iotests: Flush in iotests.py's QemuIoInteractive, Max Reitz, 2018/10/15
- [Qemu-devel] [PATCH 5/9] iotests: Different iterator behavior in Python 3, Max Reitz, 2018/10/15
- [Qemu-devel] [PATCH 3/9] iotests: Use Python byte strings where appropriate, Max Reitz, 2018/10/15
- [Qemu-devel] [PATCH 4/9] iotests: Use // for Python integer division, Max Reitz, 2018/10/15
- [Qemu-devel] [PATCH 6/9] iotests: Explicitly inherit FDs in Python, Max Reitz, 2018/10/15