classification
Title: CDecimal disregards subclass passed into __new__ when value argument is an instance of Decimal
Type: behavior Stage: resolved
Components: Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Elvis.Pranskevichus, amaury.forgeotdarc, mark.dickinson, python-dev, skrah, yselivanov
Priority: normal Keywords:

Created on 2012-11-07 19:26 by Elvis.Pranskevichus, last changed 2012-11-08 10:31 by skrah. This issue is now closed.

Messages (6)
msg175121 - (view) Author: Elvis Pranskevichus (Elvis.Pranskevichus) Date: 2012-11-07 19:26
Consider the following:

import decimal

class MyDecimal(decimal.Decimal):
    def __new__(cls, value):
        return super().__new__(cls, value)

a = decimal.Decimal('1.0')
b = MyDecimal(a)
c = MyDecimal('1.0')

print(type(a), type(b), isinstance(b, MyDecimal), type(c), isinstance(c, MyDecimal))

Running the above in 3.3 produces:

<class 'decimal.Decimal'> <class 'decimal.Decimal'> False <class '__main__.MyDecimal'> True

Which shows that Decimal.__new__(cls, Decimal()) will always return its argument regardless of cls.
msg175128 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-07 22:21
New changeset 1b6c972457e6 by Stefan Krah in branch '3.3':
Issue #16431: Use the type information when constructing a Decimal subtype
http://hg.python.org/cpython/rev/1b6c972457e6
msg175130 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-07 22:55
New changeset 7fcc58811e82 by Stefan Krah in branch '3.3':
Issue #16431: Also fix the opposite direction.
http://hg.python.org/cpython/rev/7fcc58811e82
msg175131 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-11-07 23:02
"Py_INCREF(v); return v;" should be used only for immutable types, not for subclasses. in 3.2, the code below prints "3, None":

import decimal
class MyDecimal(decimal.Decimal):
    x = None
    def __new__(cls, value):
        return super().__new__(cls, value)
        return obj
a = MyDecimal("1.1")
a.x = 3
b = MyDecimal(a)
print(a.x, b.x)
msg175148 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-08 10:20
New changeset 9a701e8ec2c9 by Stefan Krah in branch '3.3':
Issue #16431: Finally, consider all permutations.
http://hg.python.org/cpython/rev/9a701e8ec2c9
msg175149 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-11-08 10:31
Thanks, Amaury. Should be fixed now.
History
Date User Action Args
2012-11-08 10:31:17skrahsetstatus: open -> closed
resolution: fixed
messages: + msg175149

stage: resolved
2012-11-08 10:20:41python-devsetmessages: + msg175148
2012-11-07 23:02:41amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg175131
2012-11-07 22:55:16python-devsetmessages: + msg175130
2012-11-07 22:21:17python-devsetnosy: + python-dev
messages: + msg175128
2012-11-07 19:27:41yselivanovsetnosy: + yselivanov
2012-11-07 19:27:21mark.dickinsonsetnosy: + mark.dickinson
2012-11-07 19:26:17Elvis.Pranskevichuscreate