* classobject.c: in instance_getattr, don't make a method out of a

function found as instance data.
* socketmodule.c: added 'flags' argument sendto/recvfrom, rewrite
  argument parsing in send/recv.
* More changes related to access (terminology change: owner instead of
  class; allow any object as owner; local/global variables are owned
  by their dictionary, only class/instance data is owned by the class;
  "from...import *" now only imports objects with public access; etc.)
This commit is contained in:
Guido van Rossum 1993-05-25 09:38:27 +00:00
parent 23301a9467
commit eb6b33a837
9 changed files with 125 additions and 101 deletions

View File

@ -28,7 +28,7 @@ object *call_object PROTO((object *, object *));
object *getglobals PROTO((void));
object *getlocals PROTO((void));
object *getclass PROTO((void));
object *getowner PROTO((void));
void mergelocals PROTO((void));
void printtraceback PROTO((object *));

View File

@ -36,7 +36,7 @@ typedef struct _frame {
codeobject *f_code; /* code segment */
object *f_globals; /* global symbol table (dictobject) */
object *f_locals; /* local symbol table (dictobject) */
object *f_class; /* class context (classobject or NULL) */
object *f_owner; /* owner (e.g. class or module) or NULL */
object *f_fastlocals; /* fast local variables (listobject) */
object *f_localmap; /* local variable names (dictobject) */
object **f_valuestack; /* malloc'ed array */

View File

@ -32,7 +32,7 @@ Limitations:
- only AF_INET and AF_UNIX address families are supported
- no asynchronous I/O (but read polling: avail)
- no read/write operations (use send/recv or makefile instead)
- no flags on sendto/recvfrom operations
- no flags on recvfrom operations
- setsockopt() and getsockopt() only support integer options
Interface:
@ -61,10 +61,10 @@ Socket methods:
- s.getpeername() --> sockaddr
- s.listen(n) --> None
- s.makefile(mode) --> file object
- s.recv(nbytes) --> string
- s.recv(nbytes [,flags]) --> string
- s.recvfrom(nbytes) --> string, sockaddr
- s.send(string) --> None
- s.sendto(string, sockaddr) --> None
- s.send(string [,flags]) --> None
- s.sendto(string, [flags,] sockaddr) --> None
- s.shutdown(how) --> None
- s.close() --> None
@ -669,7 +669,7 @@ sock_makefile(s, args)
}
/* s.recv(nbytes) method */
/* s.recv(nbytes [,flags]) method */
static object *
sock_recv(s, args)
@ -678,11 +678,11 @@ sock_recv(s, args)
{
int len, n, flags;
object *buf;
if (!getintintarg(args, &len, &flags)) {
flags = 0;
if (!getargs(args, "i", &len)) {
err_clear();
if (!getintarg(args, &len))
if (!getargs(args, "(ii)", &len, &flags))
return NULL;
flags = 0;
}
buf = newsizedstringobject((char *) 0, len);
if (buf == NULL)
@ -698,7 +698,7 @@ sock_recv(s, args)
}
/* s.recvfrom(nbytes) method */
/* s.recvfrom(nbytes [,flags]) method */
static object *
sock_recvfrom(s, args)
@ -707,14 +707,16 @@ sock_recvfrom(s, args)
{
char addrbuf[256];
object *buf, *addr, *ret;
int addrlen, len, n;
if (!getintarg(args, &len))
return NULL;
if (!getsockaddrlen(s, &addrlen))
return NULL;
int addrlen, len, n, flags;
flags = 0;
if (!getargs(args, "i", &len)) {
err_clear();
if (!getargs(args, "(ii)", &len, &flags))
return NULL;
}
buf = newsizedstringobject((char *) 0, len);
BGN_SAVE
n = recvfrom(s->sock_fd, getstringvalue(buf), len, 0,
n = recvfrom(s->sock_fd, getstringvalue(buf), len, flags,
addrbuf, &addrlen);
END_SAVE
if (n < 0)
@ -729,7 +731,7 @@ sock_recvfrom(s, args)
}
/* s.send(data) method */
/* s.send(data [,flags]) method */
static object *
sock_send(s, args)
@ -738,11 +740,11 @@ sock_send(s, args)
{
char *buf;
int len, n, flags;
if (!getargs(args, "(s#i)", &buf, &len, &flags)) {
flags = 0;
if (!getargs(args, "(s#)", &buf, &len)) {
err_clear();
if (!getargs(args, "s#", &buf, &len))
if (!getargs(args, "s#", &buf, &len, &flags))
return NULL;
flags = 0;
}
BGN_SAVE
n = send(s->sock_fd, buf, len, flags);
@ -754,7 +756,7 @@ sock_send(s, args)
}
/* s.sendto(data, sockaddr) method */
/* s.sendto(data, [flags,] sockaddr) method */
static object *
sock_sendto(s, args)
@ -764,16 +766,17 @@ sock_sendto(s, args)
object *addro;
char *buf;
struct sockaddr *addr;
int addrlen, len, n;
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
err_badarg();
return NULL;
int addrlen, len, n, flags;
flags = 0;
if (!getargs(args, "(s#O)", &buf, &len, &addro)) {
err_clear();
if (!getargs(args, "(s#iO)", &buf, &len, &flags, &addro))
return NULL;
}
if (!getargs(args, "(s#O)", &buf, &len, &addro) ||
!getsockaddrarg(s, addro, &addr, &addrlen))
if (!getsockaddrarg(s, addro, &addr, &addrlen))
return NULL;
BGN_SAVE
n = sendto(s->sock_fd, buf, len, 0, addr, addrlen);
n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
END_SAVE
if (n < 0)
return socket_error();

View File

@ -32,27 +32,23 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
typedef struct {
OB_HEAD
object *ac_value;
object *ac_class;
object *ac_owner;
typeobject *ac_type;
int ac_mode;
} accessobject;
/* Forward */
static int typecheck PROTO((object *, typeobject *));
static int classcheck PROTO((object *, object *, int, int));
static int ownercheck PROTO((object *, object *, int, int));
object *
newaccessobject(value, class, type, mode)
newaccessobject(value, owner, type, mode)
object *value;
object *class;
object *owner;
typeobject *type;
int mode;
{
accessobject *ap;
if (class != NULL && !is_classobject(class)) {
err_badcall();
return NULL;
}
if (!typecheck(value, type)) {
err_setstr(AccessError,
"access: initial value has inappropriate type");
@ -63,8 +59,8 @@ newaccessobject(value, class, type, mode)
return NULL;
XINCREF(value);
ap->ac_value = value;
XINCREF(class);
ap->ac_class = class;
XINCREF(owner);
ap->ac_owner = owner;
XINCREF(type);
ap->ac_type = (typeobject *)type;
ap->ac_mode = mode;
@ -81,22 +77,22 @@ cloneaccessobject(op)
return NULL;
}
ap = (accessobject *)op;
return newaccessobject(ap->ac_value, ap->ac_class,
return newaccessobject(ap->ac_value, ap->ac_owner,
ap->ac_type, ap->ac_mode);
}
void
setaccessowner(op, class)
setaccessowner(op, owner)
object *op;
object *class;
object *owner;
{
register accessobject *ap;
if (!is_accessobject(op) || class != NULL && !is_classobject(class))
if (!is_accessobject(op))
return; /* XXX no error */
ap = (accessobject *)op;
XDECREF(ap->ac_class);
XINCREF(class);
ap->ac_class = class;
XDECREF(ap->ac_owner);
XINCREF(owner);
ap->ac_owner = owner;
}
int
@ -109,9 +105,9 @@ hasaccessvalue(op)
}
object *
getaccessvalue(op, class)
getaccessvalue(op, owner)
object *op;
object *class;
object *owner;
{
register accessobject *ap;
if (!is_accessobject(op)) {
@ -120,7 +116,7 @@ getaccessvalue(op, class)
}
ap = (accessobject *)op;
if (!classcheck(class, ap->ac_class, AC_R, ap->ac_mode)) {
if (!ownercheck(owner, ap->ac_owner, AC_R, ap->ac_mode)) {
err_setstr(AccessError, "read access denied");
return NULL;
}
@ -134,9 +130,9 @@ getaccessvalue(op, class)
}
int
setaccessvalue(op, class, value)
setaccessvalue(op, owner, value)
object *op;
object *class;
object *owner;
object *value;
{
register accessobject *ap;
@ -146,7 +142,7 @@ setaccessvalue(op, class, value)
}
ap = (accessobject *)op;
if (!classcheck(class, ap->ac_class, AC_W, ap->ac_mode)) {
if (!ownercheck(owner, ap->ac_owner, AC_W, ap->ac_mode)) {
err_setstr(AccessError, "write access denied");
return -1;
}
@ -227,17 +223,20 @@ typecheck(value, type)
}
static int
classcheck(caller, owner, access, mode)
ownercheck(caller, owner, access, mode)
object *caller;
object *owner;
int access;
int mode;
{
if (caller == owner && owner != NULL)
return access & mode & (AC_PRIVATE|AC_PROTECTED|AC_PUBLIC);
if (caller != NULL && owner != NULL && issubclass(caller, owner))
return access & mode & (AC_PROTECTED|AC_PUBLIC);
return access & mode & AC_PUBLIC;
int mask = AC_PUBLIC;
if (owner != NULL) {
if (caller == owner)
mask |= AC_PRIVATE | AC_PROTECTED;
else if (is_classobject(owner) && issubclass(caller, owner))
mask |= AC_PROTECTED;
}
return access & mode & mask;
}
/* Access methods */
@ -247,7 +246,7 @@ access_dealloc(ap)
accessobject *ap;
{
XDECREF(ap->ac_value);
XDECREF(ap->ac_class);
XDECREF(ap->ac_owner);
XDECREF(ap->ac_type);
DEL(ap);
}
@ -256,7 +255,7 @@ access_dealloc(ap)
static struct memberlist access_memberlist[] = {
{"ac_value", T_OBJECT, OFF(ac_value)},
{"ac_class", T_OBJECT, OFF(ac_class)},
{"ac_owner", T_OBJECT, OFF(ac_owner)},
{"ac_type", T_OBJECT, OFF(ac_type)},
{"ac_mode", T_INT, OFF(ac_mode)},
{NULL} /* Sentinel */
@ -275,12 +274,21 @@ access_repr(ap)
accessobject *ap;
{
char buf[300];
classobject *class = (classobject *)ap->ac_class;
char buf2[20];
char *ownername;
typeobject *type = ap->ac_type;
if (is_classobject(ap->ac_owner)) {
ownername =
getstringvalue(((classobject *)ap->ac_owner)->cl_name);
}
else {
sprintf(buf2, "0x%lx", (long)ap->ac_owner);
ownername = buf2;
}
sprintf(buf,
"<access object, value 0x%lx, class %.100s, type %.100s, mode %04o>",
"<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>",
(long)(ap->ac_value),
class ? getstringvalue(class->cl_name) : "-",
ownername,
type ? type->tp_name : "-",
ap->ac_mode);
return newstringobject(buf);
@ -305,7 +313,8 @@ typeobject Accesstype = {
0, /*tp_hash*/
};
/* Dummy type objects to indicate classes of types */
/* Pseudo type objects to indicate collections of types */
/* XXX This should be replaced by a more general "subclassing"
XXX mechanism for type objects... */

View File

@ -126,7 +126,7 @@ class_getattr(op, name)
return NULL;
}
if (is_accessobject(v)) {
v = getaccessvalue(v, getclass());
v = getaccessvalue(v, getowner());
if (v == NULL)
return NULL;
}
@ -157,7 +157,7 @@ class_setattr(op, name, v)
}
ac = dictlookup(op->cl_dict, name);
if (ac != NULL && is_accessobject(ac))
return setaccessvalue(ac, getclass(), v);
return setaccessvalue(ac, getowner(), v);
if (v == NULL) {
int rv = dictremove(op->cl_dict, name);
if (rv < 0)
@ -207,10 +207,10 @@ issubclass(class, base)
{
int i, n;
classobject *cp;
if (class == NULL || !is_classobject(class))
return 0;
if (class == base)
return 1;
if (class == NULL || !is_classobject(class))
return 0;
cp = (classobject *)class;
n = gettuplesize(cp->cl_bases);
for (i = 0; i < n; i++) {
@ -363,6 +363,7 @@ instance_getattr(inst, name)
INCREF(inst->in_class);
return (object *)inst->in_class;
}
class = NULL;
v = dictlookup(inst->in_dict, name);
if (v == NULL) {
v = class_lookup(inst->in_class, name, &class);
@ -372,13 +373,13 @@ instance_getattr(inst, name)
}
}
if (is_accessobject(v)) {
v = getaccessvalue(v, getclass());
v = getaccessvalue(v, getowner());
if (v == NULL)
return NULL;
}
else
INCREF(v);
if (is_funcobject(v)) {
if (is_funcobject(v) && class != NULL) {
object *w = newinstancemethodobject(v, (object *)inst,
(object *)class);
DECREF(v);
@ -403,7 +404,7 @@ instance_setattr(inst, name, v)
}
ac = dictlookup(inst->in_dict, name);
if (ac != NULL && is_accessobject(ac))
return setaccessvalue(ac, getclass(), v);
return setaccessvalue(ac, getowner(), v);
if (v == NULL) {
int rv = dictremove(inst->in_dict, name);
if (rv < 0)

View File

@ -38,7 +38,7 @@ static struct memberlist frame_memberlist[] = {
{"f_code", T_OBJECT, OFF(f_code)},
{"f_globals", T_OBJECT, OFF(f_globals)},
{"f_locals", T_OBJECT, OFF(f_locals)},
{"f_class", T_OBJECT, OFF(f_class)},
{"f_owner", T_OBJECT, OFF(f_owner)},
/* {"f_fastlocals",T_OBJECT, OFF(f_fastlocals)}, /* XXX Unsafe */
{"f_localmap", T_OBJECT, OFF(f_localmap)},
{"f_lasti", T_INT, OFF(f_lasti)},
@ -85,7 +85,7 @@ frame_dealloc(f)
XDECREF(f->f_code);
XDECREF(f->f_globals);
XDECREF(f->f_locals);
XDECREF(f->f_class);
XDECREF(f->f_owner);
XDECREF(f->f_fastlocals);
XDECREF(f->f_localmap);
f->f_back = free_list;
@ -110,12 +110,12 @@ typeobject Frametype = {
};
frameobject *
newframeobject(back, code, globals, locals, class, nvalues, nblocks)
newframeobject(back, code, globals, locals, owner, nvalues, nblocks)
frameobject *back;
codeobject *code;
object *globals;
object *locals;
object *class;
object *owner;
int nvalues;
int nblocks;
{
@ -124,7 +124,6 @@ newframeobject(back, code, globals, locals, class, nvalues, nblocks)
code == NULL || !is_codeobject(code) ||
globals == NULL || !is_dictobject(globals) ||
locals == NULL || !is_dictobject(locals) ||
(class != NULL && !is_classobject(class)) ||
nvalues < 0 || nblocks < 0) {
err_badcall();
return NULL;
@ -150,8 +149,8 @@ newframeobject(back, code, globals, locals, class, nvalues, nblocks)
f->f_globals = globals;
INCREF(locals);
f->f_locals = locals;
XINCREF(class);
f->f_class = class;
XINCREF(owner);
f->f_owner = owner;
f->f_fastlocals = NULL;
f->f_localmap = NULL;
if (nvalues > f->f_nvalues || f->f_valuestack == NULL) {

View File

@ -112,7 +112,7 @@ module_getattr(m, name)
err_setstr(AttributeError, name);
else {
if (is_accessobject(res))
res = getaccessvalue(res, getclass());
res = getaccessvalue(res, getglobals());
else
INCREF(res);
}
@ -135,7 +135,7 @@ module_setattr(m, name, v)
}
ac = dictlookup(m->md_dict, name);
if (ac != NULL && is_accessobject(ac))
return setaccessvalue(ac, getclass(), v);
return setaccessvalue(ac, getglobals(), v);
if (v == NULL) {
int rv = dictremove(m->md_dict, name);
if (rv < 0)

View File

@ -161,11 +161,11 @@ enum why_code {
/* Interpreter main loop */
object *
eval_code(co, globals, locals, class, arg)
eval_code(co, globals, locals, owner, arg)
codeobject *co;
object *globals;
object *locals;
object *class;
object *owner;
object *arg;
{
register unsigned char *next_instr;
@ -246,7 +246,7 @@ eval_code(co, globals, locals, class, arg)
co, /*code*/
globals, /*globals*/
locals, /*locals*/
class, /*class*/
owner, /*owner*/
50, /*nvalues*/
20); /*nblocks*/
if (f == NULL)
@ -767,7 +767,7 @@ eval_code(co, globals, locals, class, arg)
u = (object *)v->ob_type;
else
u = NULL;
x = newaccessobject(v, class,
x = newaccessobject(v, f->f_locals,
(typeobject *)u,
defmode);
DECREF(v);
@ -777,7 +777,7 @@ eval_code(co, globals, locals, class, arg)
}
}
else if (is_accessobject(u)) {
err = setaccessvalue(u, class, v);
err = setaccessvalue(u, f->f_locals, v);
DECREF(v);
break;
}
@ -789,7 +789,7 @@ eval_code(co, globals, locals, class, arg)
w = GETNAMEV(oparg);
u = dict2lookup(f->f_locals, w);
if (u != NULL && is_accessobject(u)) {
err = setaccessvalue(u, class,
err = setaccessvalue(u, f->f_locals,
(object *)NULL);
break;
}
@ -987,7 +987,7 @@ eval_code(co, globals, locals, class, arg)
v = POP();
u = dict2lookup(f->f_locals, w);
if (u != NULL && is_accessobject(u)) {
err = setaccessvalue(u, class, v);
err = setaccessvalue(u, f->f_globals, v);
DECREF(v);
break;
}
@ -999,7 +999,7 @@ eval_code(co, globals, locals, class, arg)
w = GETNAMEV(oparg);
u = dict2lookup(f->f_locals, w);
if (u != NULL && is_accessobject(u)) {
err = setaccessvalue(u, class,
err = setaccessvalue(u, f->f_globals,
(object *)NULL);
break;
}
@ -1030,7 +1030,7 @@ eval_code(co, globals, locals, class, arg)
}
}
if (is_accessobject(x)) {
x = getaccessvalue(x, class);
x = getaccessvalue(x, f->f_globals /* XXX */);
if (x == NULL)
break;
}
@ -1052,7 +1052,7 @@ eval_code(co, globals, locals, class, arg)
}
}
if (is_accessobject(x)) {
x = getaccessvalue(x, class);
x = getaccessvalue(x, f->f_globals);
if (x == NULL)
break;
}
@ -1069,7 +1069,7 @@ eval_code(co, globals, locals, class, arg)
break;
}
if (is_accessobject(x)) {
x = getaccessvalue(x, class);
x = getaccessvalue(x, f->f_locals);
if (x == NULL)
break;
}
@ -1105,7 +1105,7 @@ eval_code(co, globals, locals, class, arg)
break;
}
if (is_accessobject(x)) {
x = getaccessvalue(x, class);
x = getaccessvalue(x, f->f_locals);
if (x == NULL)
break;
}
@ -1118,7 +1118,7 @@ eval_code(co, globals, locals, class, arg)
v = POP();
w = GETLISTITEM(fastlocals, oparg);
if (w != NULL && is_accessobject(w)) {
err = setaccessvalue(w, class, v);
err = setaccessvalue(w, f->f_locals, v);
DECREF(v);
break;
}
@ -1134,7 +1134,8 @@ eval_code(co, globals, locals, class, arg)
break;
}
if (w != NULL && is_accessobject(w)) {
err = setaccessvalue(w, class, (object *)NULL);
err = setaccessvalue(w, f->f_locals,
(object *)NULL);
break;
}
DECREF(x);
@ -1668,12 +1669,12 @@ getglobals()
}
object *
getclass()
getowner()
{
if (current_frame == NULL)
return NULL;
else
return current_frame->f_class;
return current_frame->f_owner;
}
void
@ -2363,14 +2364,25 @@ import_from(locals, v, name)
object *w, *x;
w = getmoduledict(v);
if (getstringvalue(name)[0] == '*') {
int pos;
int pos, err;
object *name, *value;
pos = 0;
while (mappinggetnext(w, &pos, &name, &value)) {
if (!is_stringobject(name) ||
getstringvalue(name)[0] == '_')
continue;
if (dict2insert(locals, name, value) != 0)
if (is_accessobject(value)) {
value = getaccessvalue(value, (object *)NULL);
if (value == NULL) {
err_clear();
continue;
}
}
else
INCREF(value);
err = dict2insert(locals, name, value);
DECREF(value);
if (err != 0)
return -1;
}
return 0;
@ -2456,7 +2468,7 @@ access_statement(name, vmode, f)
type = value->ob_type;
else
type = NULL;
ac = newaccessobject(value, f->f_class, type, mode);
ac = newaccessobject(value, f->f_locals, type, mode);
if (ac == NULL)
return -1;
if (fastind >= 0)

View File

@ -303,7 +303,7 @@ get_module(m, name, m_ret)
}
}
}
v = eval_code(co, d, d, (object *)NULL, (object *)NULL);
v = eval_code(co, d, d, d, (object *)NULL);
DECREF(co);
return v;
}
@ -422,7 +422,7 @@ init_frozen(name)
return -1;
if ((m = add_module(name)) == NULL ||
(d = getmoduledict(m)) == NULL ||
(v = eval_code(co, d, d, (object*)NULL, (object*)NULL)) == NULL) {
(v = eval_code(co, d, d, d, (object*)NULL)) == NULL) {
DECREF(co);
return -1;
}