mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
type_call(): Change in policy. The keyword args (if any) are now passed
on to the tp_new slot (if non-NULL), as well as to the tp_init slot (if any). A sane type implementing both tp_new and tp_init should probably pay attention to the arguments in only one of them.
This commit is contained in:
parent
bafedecc06
commit
3f996e7266
2 changed files with 14 additions and 3 deletions
|
@ -343,8 +343,10 @@ def complexes():
|
||||||
numops(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge'])
|
numops(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge'])
|
||||||
class Number(complex):
|
class Number(complex):
|
||||||
__slots__ = ['prec']
|
__slots__ = ['prec']
|
||||||
def __init__(self, *args, **kwds):
|
def __new__(cls, *args, **kwds):
|
||||||
self.prec = kwds.get('prec', 12)
|
result = complex.__new__(cls, *args)
|
||||||
|
result.prec = kwds.get('prec', 12)
|
||||||
|
return result
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
prec = self.prec
|
prec = self.prec
|
||||||
if self.imag == 0.0:
|
if self.imag == 0.0:
|
||||||
|
@ -353,10 +355,19 @@ def complexes():
|
||||||
return "%.*gj" % (prec, self.imag)
|
return "%.*gj" % (prec, self.imag)
|
||||||
return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
|
return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
|
||||||
__str__ = __repr__
|
__str__ = __repr__
|
||||||
|
|
||||||
a = Number(3.14, prec=6)
|
a = Number(3.14, prec=6)
|
||||||
verify(`a` == "3.14")
|
verify(`a` == "3.14")
|
||||||
verify(a.prec == 6)
|
verify(a.prec == 6)
|
||||||
|
|
||||||
|
a = Number(a, prec=2)
|
||||||
|
verify(`a` == "3.1")
|
||||||
|
verify(a.prec == 2)
|
||||||
|
|
||||||
|
a = Number(234.5)
|
||||||
|
verify(`a` == "234.5")
|
||||||
|
verify(a.prec == 12)
|
||||||
|
|
||||||
def spamlists():
|
def spamlists():
|
||||||
if verbose: print "Testing spamlist operations..."
|
if verbose: print "Testing spamlist operations..."
|
||||||
import copy, xxsubtype as spam
|
import copy, xxsubtype as spam
|
||||||
|
|
|
@ -151,7 +151,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
obj = type->tp_new(type, args, NULL);
|
obj = type->tp_new(type, args, kwds);
|
||||||
if (obj != NULL) {
|
if (obj != NULL) {
|
||||||
type = obj->ob_type;
|
type = obj->ob_type;
|
||||||
if (type->tp_init != NULL &&
|
if (type->tp_init != NULL &&
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue