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
complex() gives wrong error when the second argument has an invalid type #72390
Comments
When the second argument of complex() is not a number/string, the type error reports the error but prints the type of the first argument: > complex({1:2},1j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() argument must be a string or a number, not 'dict'
>complex(1j,{1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() argument must be a string or a number, not 'complex'
>>> complex(1, {1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() argument must be a string or a number, not 'int' |
Contains changes made to Objects/complexobject.c
Changed the error message returned when second argument of complex() is not number/string
Originally:
>complex(1j,{1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() argument must be a string or a number, not 'complex'
After patch:
>complex(1j,{1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() argument must be a string or a number, not 'dict' |
Please upload your patch from a Mercurial clone: Currently, if you pass a string as a second argument, you get: >>> complex(1, "1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() second arg can't be a string So the exception message should probably be changed to include "second arg" or "second argument": >>> complex(1j, {1: 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() second arg must be a number, not 'dict' Also, there is already a check for second argument in line 952 so the "must be a string" part is probably not needed. We probably need to check whether these two cases can be combined. You also need to add some tests to Lib/test/test_complex.py. |
@Manishearth: Nice catch! Thanks for the report. @soummyaah: Thanks for the patch. Please could you sign a contributor agreement[1], so that we can commit the patch? As Berker says, tests would be good to have, too. |
This should be fixed in Python 3.5, too. |
I've signed the contributor agreement form. I think it said that it'll take a few days to process? |
Thank you! I look forward to the new patch. |
Changed error message to: >>> complex({1:2},1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() first arg must be a string or a number, not 'dict'
>>> complex(1j, {1: 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() second arg must be a number, not 'dict' Added tests to check the error raised. |
Apologies. This is the correct file. |
Squashed the commits for better readability. |
Thanks for the patch. We can't fix this in 3.4 because it's in security-fix-only mode: https://docs.python.org/devguide/index.html#status-of-python-branches |
Thanks. I'll apply this shortly (but probably not before the weekend). |
New changeset 92f4ce2d5ebb by Mark Dickinson in branch '3.5': New changeset a2d93e6bcbcf by Mark Dickinson in branch '3.6': New changeset 9790bc211107 by Mark Dickinson in branch 'default': |
Fixed; thanks. I made a couple of changes:
|
Thanks for the merge! |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: