This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: IndexError inside list comprehension + workaround
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: NetAlien, Stefan Pochmann, steven.daprano
Priority: normal Keywords:

Created on 2022-01-07 23:22 by NetAlien, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mytest NetAlien, 2022-01-07 23:22 test script illustrating issue
Messages (5)
msg410054 - (view) Author: Pierre Fortin (NetAlien) Date: 2022-01-07 23:22
var = "u2"
var.strip()[0] 
Works as expected, except that it returns IndexError (instead of "u") if used in a list comprehension -- at least, that's where I found it.  Attached example script illustrates the issue.
Call it with "mytest N" where N is 1 (fails), 2 (works), 3 (fails).
mytest 1 -- KeyError expected; this was due to infile design change
            adding a digit to previously single char code
mytest 2 -- workaround to actual issue in next test
mytest 3 -- adding [0] fails when used in list comprehension
msg410062 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-01-08 00:41
> it returns IndexError (instead of "u") if used in a list comprehension

Works as expected inside a list comprehension:

>>> var = "u2"
>>> [var.strip()[0] for i in range(2)]
['u', 'u']

>>> ["ok" for i in range(2) if var.strip()[0] == "u"]
['ok', 'ok']


I am 99.99% certain that you have a bug in your code, but your code is so complicated that it is not obvious at a glance where the bug is. I am strongly tempted to just close this as "Works for me" and tell you to come back and re-open the bug report when you have isolated the issue to a simpler case, but I will resist the temptation for now.
msg410066 - (view) Author: Stefan Pochmann (Stefan Pochmann) * Date: 2022-01-08 01:10
The error occurs when you do code.strip()[0] when code is " ", not "u2".
msg410067 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-01-08 01:11
Your functions test1 and test2 are irrelevant to the bug report. The driver code using eval() to pick which function to call is unneeded. The business of simulating a file is complexity for no purpose.

Those ignore, count, unique functions are also irrelevant.

Removing all the irrelevant and extraneous complexity that obfuscates the problem, we get to two lines of simplified code sufficient to demonstrate the issue:

    each = "name = "
    [code.strip()[0] for func, code in [each.split('=')]]

which sure enough raises IndexError.

The hint was looking at your fix() function, which made it clear that you are getting an IndexError because the string is an empty string. Well of course you do, that is expected behaviour.

Why do you get an empty string? Because you split the line "name = " on the equals sign, giving

    func = "name "
    code = " "

then you strip the whitespace from code giving:

    code = ""

then you try to extract the first character of code using code[0], which raises IndexError, because that's what it is supposed to do.

So there is no bug here, Python is working correctly.
msg410096 - (view) Author: Pierre Fortin (NetAlien) Date: 2022-01-08 12:14
[Thanks for the replies! I was trying to post this before seeing them.]

Major egg on face...
The more complex the code becomes, the more likely you will be burned by a rookie mistake...
var = ''
var[0]  WILL give IndexError  -- Duh!
It was buried in the each.split('=') returning an empty string -- that's what you get for making things easier for the user. 
The easier code is to use, the more complex it must be...

Sorry for the noise.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90460
2022-01-08 12:14:26NetAliensetmessages: + msg410096
2022-01-08 01:11:35steven.dapranosetstatus: open -> closed
resolution: not a bug
messages: + msg410067

stage: resolved
2022-01-08 01:10:03Stefan Pochmannsetnosy: + Stefan Pochmann
messages: + msg410066
2022-01-08 00:41:55steven.dapranosetnosy: + steven.daprano
messages: + msg410062
2022-01-07 23:22:39NetAliencreate