Issue 28203: complex() gives wrong error when the second argument has an invalid type (original) (raw)

Created on 2016-09-19 07:41 by manishearth, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Issue28203.patch soummyaah,2016-09-19 08:20 diff complexobject.c
Issue28203#3.patch soummyaah,2016-09-20 12:05
Issue28203#4.patch soummyaah,2016-09-20 12:32 review
Pull Requests
URL Status Linked Edit
PR 552 closed dstufft,2017-03-31 16:36

| Repositories containing patches | | | | | ---------------------------------------------------------------------------------------------------- | | | | | https://github.com/soummyaah/cpython | | | |

Messages (15)
msg276952 - (view) Author: Manish Goregaokar (manishearth) Date: 2016-09-19 07:41
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 "", line 1, in TypeError: complex() argument must be a string or a number, not 'dict' >complex(1j,{1:2}) Traceback (most recent call last): File "", line 1, in TypeError: complex() argument must be a string or a number, not 'complex' >>> complex(1, {1:2}) Traceback (most recent call last): File "", line 1, in TypeError: complex() argument must be a string or a number, not 'int'
msg276954 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-19 08:20
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 "", line 1, in TypeError: complex() argument must be a string or a number, not 'complex' After patch: >complex(1j,{1:2}) Traceback (most recent call last): File "", line 1, in TypeError: complex() argument must be a string or a number, not 'dict'
msg276958 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-09-19 08:44
Please upload your patch from a Mercurial clone: * https://docs.python.org/devguide/setup.html#checkout * https://docs.python.org/devguide/patch.html Currently, if you pass a string as a second argument, you get: >>> complex(1, "1") Traceback (most recent call last): File "", line 1, in 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 "", line 1, in 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.
msg276970 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-19 11:04
@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. [1] https://www.python.org/psf/contrib/contrib-form/
msg276972 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-19 11:48
This should be fixed in Python 3.5, too.
msg277003 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-20 04:00
I've signed the contributor agreement form. I think it said that it'll take a few days to process? I've made the changes requested and am currently working on the tests. Will submit a new patch file containing all required changes soon.
msg277013 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-20 06:22
Thank you! I look forward to the new patch.
msg277022 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-20 12:03
Changed error message to: >>> complex({1:2},1) Traceback (most recent call last): File "", line 1, in TypeError: complex() first arg must be a string or a number, not 'dict' >>> complex(1j, {1: 2}) Traceback (most recent call last): File "", line 1, in TypeError: complex() second arg must be a number, not 'dict' Added tests to check the error raised.
msg277023 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-20 12:05
Apologies. This is the correct file.
msg277025 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-20 12:32
Squashed the commits for better readability. Also, change required in Python 3.4 as well
msg277026 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-09-20 12:37
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
msg277108 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-21 07:55
Thanks. I'll apply this shortly (but probably not before the weekend).
msg277318 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-09-24 14:29
New changeset 92f4ce2d5ebb by Mark Dickinson in branch '3.5': Issue #28203: Fix incorrect type in error message from complex(1.0, {2:3}). Patch by Soumya Sharma. https://hg.python.org/cpython/rev/92f4ce2d5ebb New changeset a2d93e6bcbcf by Mark Dickinson in branch '3.6': Issue #28203: Merge from 3.5 https://hg.python.org/cpython/rev/a2d93e6bcbcf New changeset 9790bc211107 by Mark Dickinson in branch 'default': Issue #28203: Merge from 3.6 https://hg.python.org/cpython/rev/9790bc211107
msg277319 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-24 14:32
Fixed; thanks. I made a couple of changes: - Use "argument" rather than "arg", to be consistent with the original code (but admittedly not consistent with the rest of the module, where there doesn't seem to be any consistent choice between "arg" and "argument"). - Reformat C and test code to avoid long lines. - Slight rearrangement of the C code so that all of the "i" handling is in one place.
msg277353 - (view) Author: Soumya Sharma (soummyaah) * Date: 2016-09-25 09:49
Thanks for the merge!
History
Date User Action Args
2022-04-11 14:58:37 admin set github: 72390
2017-03-31 16:36:39 dstufft set pull_requests: + <pull%5Frequest1112>
2016-09-25 09:49:42 soummyaah set messages: +
2016-09-24 14:32:59 mark.dickinson set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2016-09-24 14:29:23 python-dev set nosy: + python-devmessages: +
2016-09-21 07:55:56 mark.dickinson set assignee: mark.dickinsonmessages: +
2016-09-20 12:37:13 berker.peksag set messages: + versions: - Python 3.4
2016-09-20 12:32:13 soummyaah set files: + Issue28203#4.patchmessages: + versions: + Python 3.4
2016-09-20 12:05:01 soummyaah set files: + Issue28203#3.patchmessages: +
2016-09-20 12:04:17 soummyaah set files: - Issue28203#2.patch
2016-09-20 12:03:09 soummyaah set files: + Issue28203#2.patchmessages: +
2016-09-20 06:22:53 mark.dickinson set messages: +
2016-09-20 04:00:49 soummyaah set messages: +
2016-09-19 11:48:27 mark.dickinson set messages: + versions: + Python 3.5
2016-09-19 11:04:51 mark.dickinson set messages: +
2016-09-19 08:44:40 berker.peksag set nosy: + berker.peksagmessages: +
2016-09-19 08:23:39 SilentGhost set stage: patch reviewtype: enhancement -> behaviorversions: + Python 3.6
2016-09-19 08:20:57 soummyaah set files: + Issue28203.patchnosy: + soummyaahmessages: + hgrepos: + hgrepo356keywords: + patch
2016-09-19 08:09:29 serhiy.storchaka set nosy: + mark.dickinson, serhiy.storchaka
2016-09-19 07:41:45 manishearth create