Commit 328967bd authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #909 from kmod/change_numdefaults

__new__ is supposed to become a staticmethod
parents d5a4dd72 2f8ba019
......@@ -673,6 +673,10 @@ void BoxedWrapperObject::gcHandler(GCVisitor* v, Box* _o) {
v->visit(&o->obj);
}
extern "C" PyObject* PyStaticMethod_New(PyObject* callable) noexcept {
return new BoxedStaticmethod(callable);
}
void setupDescr() {
member_descriptor_cls->giveAttr("__get__", new BoxedFunction(boxRTFunction((void*)memberGet, UNKNOWN, 3)));
member_descriptor_cls->freeze();
......
......@@ -5426,6 +5426,18 @@ Box* _typeNew(BoxedClass* metatype, BoxedString* name, BoxedTuple* bases, BoxedD
}
basic_size = cur_offset;
// from cpython:
/* Special-case __new__: if it's a plain function,
make it a static function */
Box* tmp = PyDict_GetItemString(attr_dict, "__new__");
if (tmp != NULL && PyFunction_Check(tmp)) {
tmp = PyStaticMethod_New(tmp);
if (tmp == NULL)
throwCAPIException();
PyDict_SetItemString(attr_dict, "__new__", tmp);
Py_DECREF(tmp);
}
size_t total_slots = final_slot_names.size()
+ (base->tp_flags & Py_TPFLAGS_HEAPTYPE ? static_cast<BoxedHeapClass*>(base)->nslots() : 0);
BoxedHeapClass* made = BoxedHeapClass::create(metatype, base, NULL, attrs_offset, weaklist_offset, basic_size, true,
......
......@@ -57,3 +57,17 @@ class MyTuple(tuple):
f.func_defaults = MyTuple((1, 2))
print type(f.__defaults__)
f()
class C(object):
def __new__(cls, arg):
print arg
return object.__new__(cls)
def foo(self, arg):
print arg
print type(C.__new__), type(C.__dict__['__new__'])
C.__new__.__defaults__ = (1,)
print type(C())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment