-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-122239: Add actual count in unbalanced unpacking error message when possible #122244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Misc/NEWS.d/next/Core_and_Builtins/2024-07-25-01-45-21.gh-issue-122239.7zh-sW.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: blhsing <[email protected]>
Python/ceval.c
Outdated
| argcnt); | ||
|
|
||
| ll = PyObject_Size(v); | ||
| if (ll < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (ll < 0) { | |
| if (ll <= argcnt) { |
Please consider, and test, evil classes that lie about their size. For example:
class EvilLiar:
def __len__(self):
return 2
def __iter__(self):
yield from (1, 2, 3)
a, b = EvilLiar()At this level, I don't think we want to print (expected 2, got 2), even if user is at fault here.
In other evil classes, __len__ can fail with an exception. Please test this.
IMO, if it's not a TypeError/AttributeError, and especially if it's a BaseException but not Exception, it needs to be chained (e.g. using PyException_SetContext).
Other evil classes can have side effects, or take a long time. IMO, that's OK -- an extra __len__ call shouldn't hurt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this, I'll test and take care of these cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've handled the weird cases now, but I couldn't figure out what would raise an AttributeError in the normal case, so I'm only ignoring TypeError's context right now.
There's also a bunch of code duplication for _PyErr_Format, should it be deduped in some way?
|
Sorry if it seems like I'm adding more work or making bad suggestions! I don't know the right answer here. Looking at the resulting traceback, it doesn't seem correct: Perhaps they should be chained the other way around? But then the error type is different... So, my suggestion for a the next thing to try would be:
Or, if this rabbit hole is getting too deep:
|
I'm leaning towards raising
Absolutely not! I'm here for learning the process. |
7e609f4 to
a5d926f
Compare
|
I'm not sure if |
|
Please use |
Please correct me if I'm wrong, but doesn't |
|
Yes, |
|
I think this will be good to merge after the simplification I suggested. |
|
@encukou Thanks for the reminder, I think that should be it. |
Co-authored-by: Bénédikt Tran <[email protected]>
encukou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! The scope was reduced a lot, but now it should not be controversial :)
Misc/NEWS.d/next/Core_and_Builtins/2024-07-25-01-45-21.gh-issue-122239.7zh-sW.rst
Outdated
Show resolved
Hide resolved
…e-122239.7zh-sW.rst Co-authored-by: Petr Viktorin <[email protected]>
|
I don't think it needs one, but should I bring back the What's new in Python entry? |
|
If you want it, go ahead. We can always remove it if it looks out of place -- for example if it ends up as the only entry in Improved error messages). |
|
Congratulations on your contribution @tusharsadhwani 🚀 Great work ✨ |
This should allow showing the actual count of received items during an unbalanced unpacking, if the value has a pre-determined length. Such as:
This is my first time trying to modify C code in Python, so let me know if I'm doing something obviously wrong.