mirror of
https://github.com/python/cpython.git
synced 2025-10-07 15:42:02 +00:00
Renamed class methods to instance methods (which they are)
This commit is contained in:
parent
d4905454cc
commit
e8122f19a0
2 changed files with 64 additions and 64 deletions
|
@ -111,59 +111,59 @@ typeobject Classtype = {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
OB_HEAD
|
OB_HEAD
|
||||||
classobject *cm_class; /* The class object */
|
classobject *in_class; /* The class object */
|
||||||
object *cm_attr; /* A dictionary */
|
object *in_attr; /* A dictionary */
|
||||||
} instanceobject;
|
} instanceobject;
|
||||||
|
|
||||||
object *
|
object *
|
||||||
newinstanceobject(class)
|
newinstanceobject(class)
|
||||||
register object *class;
|
register object *class;
|
||||||
{
|
{
|
||||||
register instanceobject *cm;
|
register instanceobject *inst;
|
||||||
if (!is_classobject(class)) {
|
if (!is_classobject(class)) {
|
||||||
err_badcall();
|
err_badcall();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
cm = NEWOBJ(instanceobject, &Instancetype);
|
inst = NEWOBJ(instanceobject, &Instancetype);
|
||||||
if (cm == NULL)
|
if (inst == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
INCREF(class);
|
INCREF(class);
|
||||||
cm->cm_class = (classobject *)class;
|
inst->in_class = (classobject *)class;
|
||||||
cm->cm_attr = newdictobject();
|
inst->in_attr = newdictobject();
|
||||||
if (cm->cm_attr == NULL) {
|
if (inst->in_attr == NULL) {
|
||||||
DECREF(cm);
|
DECREF(inst);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return (object *)cm;
|
return (object *)inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Instance methods */
|
/* Instance methods */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
instance_dealloc(cm)
|
instance_dealloc(inst)
|
||||||
register instanceobject *cm;
|
register instanceobject *inst;
|
||||||
{
|
{
|
||||||
DECREF(cm->cm_class);
|
DECREF(inst->in_class);
|
||||||
if (cm->cm_attr != NULL)
|
if (inst->in_attr != NULL)
|
||||||
DECREF(cm->cm_attr);
|
DECREF(inst->in_attr);
|
||||||
free((ANY *)cm);
|
free((ANY *)inst);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
instance_getattr(cm, name)
|
instance_getattr(inst, name)
|
||||||
register instanceobject *cm;
|
register instanceobject *inst;
|
||||||
register char *name;
|
register char *name;
|
||||||
{
|
{
|
||||||
register object *v = dictlookup(cm->cm_attr, name);
|
register object *v = dictlookup(inst->in_attr, name);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
INCREF(v);
|
INCREF(v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
v = class_getattr(cm->cm_class, name);
|
v = class_getattr(inst->in_class, name);
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
return v; /* class_getattr() has set the error */
|
return v; /* class_getattr() has set the error */
|
||||||
if (is_funcobject(v)) {
|
if (is_funcobject(v)) {
|
||||||
object *w = newclassmethodobject(v, (object *)cm);
|
object *w = newinstancemethodobject(v, (object *)inst);
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
@ -173,15 +173,15 @@ instance_getattr(cm, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
instance_setattr(cm, name, v)
|
instance_setattr(inst, name, v)
|
||||||
instanceobject *cm;
|
instanceobject *inst;
|
||||||
char *name;
|
char *name;
|
||||||
object *v;
|
object *v;
|
||||||
{
|
{
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
return dictremove(cm->cm_attr, name);
|
return dictremove(inst->in_attr, name);
|
||||||
else
|
else
|
||||||
return dictinsert(cm->cm_attr, name, v);
|
return dictinsert(inst->in_attr, name, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
typeobject Instancetype = {
|
typeobject Instancetype = {
|
||||||
|
@ -207,88 +207,88 @@ typeobject Instancetype = {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
OB_HEAD
|
OB_HEAD
|
||||||
object *cm_func; /* The method function */
|
object *im_func; /* The method function */
|
||||||
object *cm_self; /* The object to which this applies */
|
object *im_self; /* The object to which this applies */
|
||||||
} classmethodobject;
|
} instancemethodobject;
|
||||||
|
|
||||||
object *
|
object *
|
||||||
newclassmethodobject(func, self)
|
newinstancemethodobject(func, self)
|
||||||
object *func;
|
object *func;
|
||||||
object *self;
|
object *self;
|
||||||
{
|
{
|
||||||
register classmethodobject *cm;
|
register instancemethodobject *im;
|
||||||
if (!is_funcobject(func)) {
|
if (!is_funcobject(func)) {
|
||||||
err_badcall();
|
err_badcall();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
cm = NEWOBJ(classmethodobject, &Classmethodtype);
|
im = NEWOBJ(instancemethodobject, &Instancemethodtype);
|
||||||
if (cm == NULL)
|
if (im == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
INCREF(func);
|
INCREF(func);
|
||||||
cm->cm_func = func;
|
im->im_func = func;
|
||||||
INCREF(self);
|
INCREF(self);
|
||||||
cm->cm_self = self;
|
im->im_self = self;
|
||||||
return (object *)cm;
|
return (object *)im;
|
||||||
}
|
}
|
||||||
|
|
||||||
object *
|
object *
|
||||||
classmethodgetfunc(cm)
|
instancemethodgetfunc(im)
|
||||||
register object *cm;
|
register object *im;
|
||||||
{
|
{
|
||||||
if (!is_classmethodobject(cm)) {
|
if (!is_instancemethodobject(im)) {
|
||||||
err_badcall();
|
err_badcall();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return ((classmethodobject *)cm)->cm_func;
|
return ((instancemethodobject *)im)->im_func;
|
||||||
}
|
}
|
||||||
|
|
||||||
object *
|
object *
|
||||||
classmethodgetself(cm)
|
instancemethodgetself(im)
|
||||||
register object *cm;
|
register object *im;
|
||||||
{
|
{
|
||||||
if (!is_classmethodobject(cm)) {
|
if (!is_instancemethodobject(im)) {
|
||||||
err_badcall();
|
err_badcall();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return ((classmethodobject *)cm)->cm_self;
|
return ((instancemethodobject *)im)->im_self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Class method methods */
|
/* Class method methods */
|
||||||
|
|
||||||
#define OFF(x) offsetof(classmethodobject, x)
|
#define OFF(x) offsetof(instancemethodobject, x)
|
||||||
|
|
||||||
static struct memberlist classmethod_memberlist[] = {
|
static struct memberlist instancemethod_memberlist[] = {
|
||||||
{"cm_func", T_OBJECT, OFF(cm_func)},
|
{"im_func", T_OBJECT, OFF(im_func)},
|
||||||
{"cm_self", T_OBJECT, OFF(cm_self)},
|
{"im_self", T_OBJECT, OFF(im_self)},
|
||||||
{NULL} /* Sentinel */
|
{NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
classmethod_getattr(cm, name)
|
instancemethod_getattr(im, name)
|
||||||
register classmethodobject *cm;
|
register instancemethodobject *im;
|
||||||
char *name;
|
char *name;
|
||||||
{
|
{
|
||||||
return getmember((char *)cm, classmethod_memberlist, name);
|
return getmember((char *)im, instancemethod_memberlist, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
classmethod_dealloc(cm)
|
instancemethod_dealloc(im)
|
||||||
register classmethodobject *cm;
|
register instancemethodobject *im;
|
||||||
{
|
{
|
||||||
DECREF(cm->cm_func);
|
DECREF(im->im_func);
|
||||||
DECREF(cm->cm_self);
|
DECREF(im->im_self);
|
||||||
free((ANY *)cm);
|
free((ANY *)im);
|
||||||
}
|
}
|
||||||
|
|
||||||
typeobject Classmethodtype = {
|
typeobject Instancemethodtype = {
|
||||||
OB_HEAD_INIT(&Typetype)
|
OB_HEAD_INIT(&Typetype)
|
||||||
0,
|
0,
|
||||||
"instance method",
|
"instance method",
|
||||||
sizeof(classmethodobject),
|
sizeof(instancemethodobject),
|
||||||
0,
|
0,
|
||||||
classmethod_dealloc, /*tp_dealloc*/
|
instancemethod_dealloc, /*tp_dealloc*/
|
||||||
0, /*tp_print*/
|
0, /*tp_print*/
|
||||||
classmethod_getattr, /*tp_getattr*/
|
instancemethod_getattr, /*tp_getattr*/
|
||||||
0, /*tp_setattr*/
|
0, /*tp_setattr*/
|
||||||
0, /*tp_compare*/
|
0, /*tp_compare*/
|
||||||
0, /*tp_repr*/
|
0, /*tp_repr*/
|
||||||
|
|
|
@ -266,7 +266,7 @@ eval_code(co, globals, locals, arg)
|
||||||
|
|
||||||
case UNARY_CALL:
|
case UNARY_CALL:
|
||||||
v = POP();
|
v = POP();
|
||||||
if (is_classmethodobject(v) || is_funcobject(v))
|
if (is_instancemethodobject(v) || is_funcobject(v))
|
||||||
x = call_function(v, (object *)NULL);
|
x = call_function(v, (object *)NULL);
|
||||||
else
|
else
|
||||||
x = call_builtin(v, (object *)NULL);
|
x = call_builtin(v, (object *)NULL);
|
||||||
|
@ -331,7 +331,7 @@ eval_code(co, globals, locals, arg)
|
||||||
case BINARY_CALL:
|
case BINARY_CALL:
|
||||||
w = POP();
|
w = POP();
|
||||||
v = POP();
|
v = POP();
|
||||||
if (is_classmethodobject(v) || is_funcobject(v))
|
if (is_instancemethodobject(v) || is_funcobject(v))
|
||||||
x = call_function(v, w);
|
x = call_function(v, w);
|
||||||
else
|
else
|
||||||
x = call_builtin(v, w);
|
x = call_builtin(v, w);
|
||||||
|
@ -1134,9 +1134,9 @@ call_function(func, arg)
|
||||||
object *newlocals, *newglobals;
|
object *newlocals, *newglobals;
|
||||||
object *co, *v;
|
object *co, *v;
|
||||||
|
|
||||||
if (is_classmethodobject(func)) {
|
if (is_instancemethodobject(func)) {
|
||||||
object *self = classmethodgetself(func);
|
object *self = instancemethodgetself(func);
|
||||||
func = classmethodgetfunc(func);
|
func = instancemethodgetfunc(func);
|
||||||
if (arg == NULL) {
|
if (arg == NULL) {
|
||||||
arg = self;
|
arg = self;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue