classification
Title: complex() gives wrong error when the second argument has an invalid type
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: berker.peksag, manishearth, mark.dickinson, python-dev, serhiy.storchaka, soummyaah
Priority: normal Keywords: patch

Created on 2016-09-19 07:41 by manishearth, last changed 2017-03-31 16:36 by dstufft. 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 "<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'
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 "<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'
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 "<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.
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 "<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.
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
2017-03-31 16:36:39dstufftsetpull_requests: + pull_request1112
2016-09-25 09:49:42soummyaahsetmessages: + msg277353
2016-09-24 14:32:59mark.dickinsonsetstatus: open -> closed
resolution: fixed
messages: + msg277319

stage: patch review -> resolved
2016-09-24 14:29:23python-devsetnosy: + python-dev
messages: + msg277318
2016-09-21 07:55:56mark.dickinsonsetassignee: mark.dickinson
messages: + msg277108
2016-09-20 12:37:13berker.peksagsetmessages: + msg277026
versions: - Python 3.4
2016-09-20 12:32:13soummyaahsetfiles: + Issue28203#4.patch

messages: + msg277025
versions: + Python 3.4
2016-09-20 12:05:01soummyaahsetfiles: + Issue28203#3.patch

messages: + msg277023
2016-09-20 12:04:17soummyaahsetfiles: - Issue28203#2.patch
2016-09-20 12:03:09soummyaahsetfiles: + Issue28203#2.patch

messages: + msg277022
2016-09-20 06:22:53mark.dickinsonsetmessages: + msg277013
2016-09-20 04:00:49soummyaahsetmessages: + msg277003
2016-09-19 11:48:27mark.dickinsonsetmessages: + msg276972
versions: + Python 3.5
2016-09-19 11:04:51mark.dickinsonsetmessages: + msg276970
2016-09-19 08:44:40berker.peksagsetnosy: + berker.peksag
messages: + msg276958
2016-09-19 08:23:39SilentGhostsetstage: patch review
type: enhancement -> behavior
versions: + Python 3.6
2016-09-19 08:20:57soummyaahsetfiles: + Issue28203.patch

nosy: + soummyaah
messages: + msg276954

hgrepos: + hgrepo356
keywords: + patch
2016-09-19 08:09:29serhiy.storchakasetnosy: + mark.dickinson, serhiy.storchaka
2016-09-19 07:41:45manishearthcreate