Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3f7258

Browse files
committed
* Lots of small changes related to access.
* Added "access *: ...", made access work for class methods. * Introduced subclass check: make sure that when calling ClassName.methodname(instance, ...), the instance is an instance of ClassName or of a subclass thereof (this might break some old code!)
1 parent 81daa32 commit b3f7258

7 files changed

Lines changed: 111 additions & 48 deletions

File tree

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ return_stmt: 'return' [testlist]
100100
raise_stmt: 'raise' test [',' test]
101101
import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
102102
global_stmt: 'global' NAME (',' NAME)*
103-
access_stmt: 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
103+
access_stmt: 'access' ('*' | NAME (',' NAME)*) ':' accesstype (',' accesstype)*
104104
accesstype: NAME+
105105
# accesstype should be ('public' | 'protected' | 'private') ['read'] ['write']
106106
# but can't be because that would create undesirable reserved words!

Include/accessobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ int setaccessvalue PROTO((object *, object *, object *));
5050

5151
void setaccessowner PROTO((object *, object *));
5252
object *cloneaccessobject PROTO((object *));
53+
int hasaccessvalue PROTO((object *));
5354

5455
extern typeobject Anynumbertype, Anysequencetype, Anymappingtype;

Include/classobject.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2424

2525
/* Class object interface */
2626

27-
/*
28-
Classes are really hacked in at the last moment.
29-
It should be possible to use other object types as base classes,
30-
but currently it isn't. We'll see if we can fix that later, sigh...
31-
*/
27+
/* Revealing some structures (not for general use) */
3228

3329
typedef struct {
3430
OB_HEAD
@@ -37,6 +33,12 @@ typedef struct {
3733
object *cl_name; /* A string */
3834
} classobject;
3935

36+
typedef struct {
37+
OB_HEAD
38+
classobject *in_class; /* The class object */
39+
object *in_dict; /* A dictionary */
40+
} instanceobject;
41+
4042
extern typeobject Classtype, Instancetype, Instancemethodtype;
4143

4244
#define is_classobject(op) ((op)->ob_type == &Classtype)

Objects/accessobject.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ setaccessowner(op, class)
9999
ap->ac_class = class;
100100
}
101101

102+
int
103+
hasaccessvalue(op)
104+
object *op;
105+
{
106+
if (!is_accessobject(op))
107+
return 0;
108+
return ((accessobject *)op)->ac_value != NULL;
109+
}
110+
102111
object *
103112
getaccessvalue(op, class)
104113
object *op;
@@ -268,7 +277,9 @@ access_repr(ap)
268277
char buf[300];
269278
classobject *class = (classobject *)ap->ac_class;
270279
typeobject *type = ap->ac_type;
271-
sprintf(buf, "<access object, class %.100s, type %.100s, mode 0%o>",
280+
sprintf(buf,
281+
"<access object, value 0x%lx, class %.100s, type %.100s, mode %04o>",
282+
(long)(ap->ac_value),
272283
class ? getstringvalue(class->cl_name) : "-",
273284
type ? type->tp_name : "-",
274285
ap->ac_mode);

Objects/classobject.c

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,24 @@ class_getattr(op, name)
121121
return v;
122122
}
123123
v = class_lookup(op, name, &class);
124-
if (v != NULL) {
125-
if (is_accessobject(v))
126-
v = getaccessvalue(v, getclass());
127-
else if (is_funcobject(v))
128-
v = newinstancemethodobject(v, (object *)NULL,
124+
if (v == NULL) {
125+
err_setstr(AttributeError, name);
126+
return NULL;
127+
}
128+
if (is_accessobject(v)) {
129+
v = getaccessvalue(v, getclass());
130+
if (v == NULL)
131+
return NULL;
132+
}
133+
else
134+
INCREF(v);
135+
if (is_funcobject(v)) {
136+
object *w = newinstancemethodobject(v, (object *)NULL,
129137
(object *)class);
130-
else
131-
INCREF(v);
132-
return v;
138+
DECREF(v);
139+
v = w;
133140
}
134-
err_setstr(AttributeError, name);
135-
return NULL;
141+
return v;
136142
}
137143

138144
static int
@@ -217,12 +223,6 @@ issubclass(class, base)
217223

218224
/* Instance objects */
219225

220-
typedef struct {
221-
OB_HEAD
222-
classobject *in_class; /* The class object */
223-
object *in_dict; /* A dictionary */
224-
} instanceobject;
225-
226226
static object *instance_getattr PROTO((instanceobject *, char *));
227227

228228
static int
@@ -241,8 +241,11 @@ addaccess(class, inst)
241241

242242
pos = 0;
243243
while (mappinggetnext(class->cl_dict, &pos, &key, &value)) {
244+
object *v;
244245
if (!is_accessobject(value))
245246
continue;
247+
if (hasaccessvalue(value))
248+
continue;
246249
ac = dict2lookup(inst->in_dict, key);
247250
if (ac != NULL && is_accessobject(ac)) {
248251
err_setval(ConflictError, key);
@@ -361,22 +364,27 @@ instance_getattr(inst, name)
361364
return (object *)inst->in_class;
362365
}
363366
v = dictlookup(inst->in_dict, name);
364-
if (v != NULL) {
365-
if (is_accessobject(v))
366-
v = getaccessvalue(v, getclass());
367-
else
368-
INCREF(v);
369-
return v;
367+
if (v == NULL) {
368+
v = class_lookup(inst->in_class, name, &class);
369+
if (v == NULL) {
370+
err_setstr(AttributeError, name);
371+
return NULL;
372+
}
370373
}
371-
v = class_lookup(inst->in_class, name, &class);
372-
if (v == NULL)
373-
goto error;
374-
if (is_funcobject(v))
375-
return newinstancemethodobject(v, (object *)inst,
376-
(object *)class);
377-
error:
378-
err_setstr(AttributeError, name);
379-
return NULL;
374+
if (is_accessobject(v)) {
375+
v = getaccessvalue(v, getclass());
376+
if (v == NULL)
377+
return NULL;
378+
}
379+
else
380+
INCREF(v);
381+
if (is_funcobject(v)) {
382+
object *w = newinstancemethodobject(v, (object *)inst,
383+
(object *)class);
384+
DECREF(v);
385+
v = w;
386+
}
387+
return v;
380388
}
381389

382390
static int

Objects/methodobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ meth_repr(m)
105105
{
106106
char buf[200];
107107
if (m->m_self == NULL)
108-
sprintf(buf, "<built-in function '%.80s'>", m->m_name);
108+
sprintf(buf, "<built-in function %.80s>", m->m_name);
109109
else
110110
sprintf(buf,
111-
"<built-in method '%.80s' of %.80s object at %lx>",
111+
"<built-in method %.80s of %.80s object at %lx>",
112112
m->m_name, m->m_self->ob_type->tp_name,
113113
(long)m->m_self);
114114
return newstringobject(buf);

Python/ceval.c

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int import_from PROTO((object *, object *, object *));
8787
static object *build_class PROTO((object *, object *, object *));
8888
static void locals_2_fast PROTO((frameobject *, int));
8989
static void fast_2_locals PROTO((frameobject *));
90-
static int access_statement PROTO((object *, int, frameobject *));
90+
static int access_statement PROTO((object *, object *, frameobject *));
9191

9292

9393
/* Pointer to current frame, used to link new frames to */
@@ -184,7 +184,8 @@ eval_code(co, globals, locals, class, arg)
184184
object *trace = NULL; /* Trace function or NULL */
185185
object *retval; /* Return value iff why == WHY_RETURN */
186186
char *name; /* Name used by some instructions */
187-
int needmerge = 0;
187+
int needmerge = 0; /* Set if need to merge locals back at end */
188+
int defmode = 0; /* Default access mode for new variables */
188189
#ifdef LLTRACE
189190
int lltrace;
190191
#endif
@@ -760,7 +761,22 @@ eval_code(co, globals, locals, class, arg)
760761
w = GETNAMEV(oparg);
761762
v = POP();
762763
u = dict2lookup(f->f_locals, w);
763-
if (u != NULL && is_accessobject(u)) {
764+
if (u == NULL) {
765+
if (defmode != 0) {
766+
if (v != None)
767+
u = (object *)v->ob_type;
768+
else
769+
u = NULL;
770+
x = newaccessobject(v, class,
771+
(typeobject *)u,
772+
defmode);
773+
DECREF(v);
774+
if (x == NULL)
775+
break;
776+
v = x;
777+
}
778+
}
779+
else if (is_accessobject(u)) {
764780
err = setaccessvalue(u, class, v);
765781
DECREF(v);
766782
break;
@@ -1190,7 +1206,10 @@ eval_code(co, globals, locals, class, arg)
11901206
case ACCESS_MODE:
11911207
v = POP();
11921208
w = GETNAMEV(oparg);
1193-
err = access_statement(w, (int)getintvalue(v), f);
1209+
if (getstringvalue(w)[0] == '*')
1210+
defmode = getintvalue(v);
1211+
else
1212+
err = access_statement(w, v, f);
11941213
DECREF(v);
11951214
break;
11961215

@@ -1995,7 +2014,28 @@ call_function(func, arg)
19952014
object *self = instancemethodgetself(func);
19962015
class = instancemethodgetclass(func);
19972016
func = instancemethodgetfunc(func);
1998-
if (self != NULL) {
2017+
if (self == NULL) {
2018+
/* Unbound methods must be called with an instance of
2019+
the class (or a derived class) as first argument */
2020+
if (arg != NULL && is_tupleobject(arg) &&
2021+
gettuplesize(arg) >= 1) {
2022+
self = gettupleitem(arg, 0);
2023+
if (self != NULL &&
2024+
is_instanceobject(self) &&
2025+
issubclass((object *)
2026+
(((instanceobject *)self)->in_class),
2027+
class))
2028+
/* self = self */ ;
2029+
else
2030+
self = NULL;
2031+
}
2032+
if (self == NULL) {
2033+
err_setstr(TypeError,
2034+
"unbound method must be called with class instance argument");
2035+
return NULL;
2036+
}
2037+
}
2038+
else {
19992039
int argcount;
20002040
if (arg == NULL)
20012041
argcount = 0;
@@ -2380,11 +2420,12 @@ build_class(methods, bases, name)
23802420
}
23812421

23822422
static int
2383-
access_statement(name, mode, f)
2423+
access_statement(name, vmode, f)
23842424
object *name;
2385-
int mode;
2425+
object *vmode;
23862426
frameobject *f;
23872427
{
2428+
int mode = getintvalue(vmode);
23882429
object *value, *ac;
23892430
typeobject *type;
23902431
int fastind, ret;
@@ -2415,7 +2456,7 @@ access_statement(name, mode, f)
24152456
type = value->ob_type;
24162457
else
24172458
type = NULL;
2418-
ac = newaccessobject(value, (object*)NULL, type, mode);
2459+
ac = newaccessobject(value, f->f_class, type, mode);
24192460
if (ac == NULL)
24202461
return -1;
24212462
if (fastind >= 0)

0 commit comments

Comments
 (0)