Skip to content

Commit eb6b33a

Browse files
committed
* 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.)
1 parent 23301a9 commit eb6b33a

9 files changed

Lines changed: 125 additions & 101 deletions

File tree

Include/ceval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object *call_object PROTO((object *, object *));
2828

2929
object *getglobals PROTO((void));
3030
object *getlocals PROTO((void));
31-
object *getclass PROTO((void));
31+
object *getowner PROTO((void));
3232
void mergelocals PROTO((void));
3333

3434
void printtraceback PROTO((object *));

Include/frameobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef struct _frame {
3636
codeobject *f_code; /* code segment */
3737
object *f_globals; /* global symbol table (dictobject) */
3838
object *f_locals; /* local symbol table (dictobject) */
39-
object *f_class; /* class context (classobject or NULL) */
39+
object *f_owner; /* owner (e.g. class or module) or NULL */
4040
object *f_fastlocals; /* fast local variables (listobject) */
4141
object *f_localmap; /* local variable names (dictobject) */
4242
object **f_valuestack; /* malloc'ed array */

Modules/socketmodule.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This module provides an interface to Berkeley socket IPC.
3232
- only AF_INET and AF_UNIX address families are supported
3333
- no asynchronous I/O (but read polling: avail)
3434
- no read/write operations (use send/recv or makefile instead)
35-
- no flags on sendto/recvfrom operations
35+
- no flags on recvfrom operations
3636
- setsockopt() and getsockopt() only support integer options
3737
3838
Interface:
@@ -61,10 +61,10 @@ Socket methods:
6161
- s.getpeername() --> sockaddr
6262
- s.listen(n) --> None
6363
- s.makefile(mode) --> file object
64-
- s.recv(nbytes) --> string
64+
- s.recv(nbytes [,flags]) --> string
6565
- s.recvfrom(nbytes) --> string, sockaddr
66-
- s.send(string) --> None
67-
- s.sendto(string, sockaddr) --> None
66+
- s.send(string [,flags]) --> None
67+
- s.sendto(string, [flags,] sockaddr) --> None
6868
- s.shutdown(how) --> None
6969
- s.close() --> None
7070
@@ -669,7 +669,7 @@ sock_makefile(s, args)
669669
}
670670

671671

672-
/* s.recv(nbytes) method */
672+
/* s.recv(nbytes [,flags]) method */
673673

674674
static object *
675675
sock_recv(s, args)
@@ -678,11 +678,11 @@ sock_recv(s, args)
678678
{
679679
int len, n, flags;
680680
object *buf;
681-
if (!getintintarg(args, &len, &flags)) {
681+
flags = 0;
682+
if (!getargs(args, "i", &len)) {
682683
err_clear();
683-
if (!getintarg(args, &len))
684+
if (!getargs(args, "(ii)", &len, &flags))
684685
return NULL;
685-
flags = 0;
686686
}
687687
buf = newsizedstringobject((char *) 0, len);
688688
if (buf == NULL)
@@ -698,7 +698,7 @@ sock_recv(s, args)
698698
}
699699

700700

701-
/* s.recvfrom(nbytes) method */
701+
/* s.recvfrom(nbytes [,flags]) method */
702702

703703
static object *
704704
sock_recvfrom(s, args)
@@ -707,14 +707,16 @@ sock_recvfrom(s, args)
707707
{
708708
char addrbuf[256];
709709
object *buf, *addr, *ret;
710-
int addrlen, len, n;
711-
if (!getintarg(args, &len))
712-
return NULL;
713-
if (!getsockaddrlen(s, &addrlen))
714-
return NULL;
710+
int addrlen, len, n, flags;
711+
flags = 0;
712+
if (!getargs(args, "i", &len)) {
713+
err_clear();
714+
if (!getargs(args, "(ii)", &len, &flags))
715+
return NULL;
716+
}
715717
buf = newsizedstringobject((char *) 0, len);
716718
BGN_SAVE
717-
n = recvfrom(s->sock_fd, getstringvalue(buf), len, 0,
719+
n = recvfrom(s->sock_fd, getstringvalue(buf), len, flags,
718720
addrbuf, &addrlen);
719721
END_SAVE
720722
if (n < 0)
@@ -729,7 +731,7 @@ sock_recvfrom(s, args)
729731
}
730732

731733

732-
/* s.send(data) method */
734+
/* s.send(data [,flags]) method */
733735

734736
static object *
735737
sock_send(s, args)
@@ -738,11 +740,11 @@ sock_send(s, args)
738740
{
739741
char *buf;
740742
int len, n, flags;
741-
if (!getargs(args, "(s#i)", &buf, &len, &flags)) {
743+
flags = 0;
744+
if (!getargs(args, "(s#)", &buf, &len)) {
742745
err_clear();
743-
if (!getargs(args, "s#", &buf, &len))
746+
if (!getargs(args, "s#", &buf, &len, &flags))
744747
return NULL;
745-
flags = 0;
746748
}
747749
BGN_SAVE
748750
n = send(s->sock_fd, buf, len, flags);
@@ -754,7 +756,7 @@ sock_send(s, args)
754756
}
755757

756758

757-
/* s.sendto(data, sockaddr) method */
759+
/* s.sendto(data, [flags,] sockaddr) method */
758760

759761
static object *
760762
sock_sendto(s, args)
@@ -764,16 +766,17 @@ sock_sendto(s, args)
764766
object *addro;
765767
char *buf;
766768
struct sockaddr *addr;
767-
int addrlen, len, n;
768-
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
769-
err_badarg();
770-
return NULL;
769+
int addrlen, len, n, flags;
770+
flags = 0;
771+
if (!getargs(args, "(s#O)", &buf, &len, &addro)) {
772+
err_clear();
773+
if (!getargs(args, "(s#iO)", &buf, &len, &flags, &addro))
774+
return NULL;
771775
}
772-
if (!getargs(args, "(s#O)", &buf, &len, &addro) ||
773-
!getsockaddrarg(s, addro, &addr, &addrlen))
776+
if (!getsockaddrarg(s, addro, &addr, &addrlen))
774777
return NULL;
775778
BGN_SAVE
776-
n = sendto(s->sock_fd, buf, len, 0, addr, addrlen);
779+
n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
777780
END_SAVE
778781
if (n < 0)
779782
return socket_error();

Objects/accessobject.c

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,23 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3232
typedef struct {
3333
OB_HEAD
3434
object *ac_value;
35-
object *ac_class;
35+
object *ac_owner;
3636
typeobject *ac_type;
3737
int ac_mode;
3838
} accessobject;
3939

4040
/* Forward */
4141
static int typecheck PROTO((object *, typeobject *));
42-
static int classcheck PROTO((object *, object *, int, int));
42+
static int ownercheck PROTO((object *, object *, int, int));
4343

4444
object *
45-
newaccessobject(value, class, type, mode)
45+
newaccessobject(value, owner, type, mode)
4646
object *value;
47-
object *class;
47+
object *owner;
4848
typeobject *type;
4949
int mode;
5050
{
5151
accessobject *ap;
52-
if (class != NULL && !is_classobject(class)) {
53-
err_badcall();
54-
return NULL;
55-
}
5652
if (!typecheck(value, type)) {
5753
err_setstr(AccessError,
5854
"access: initial value has inappropriate type");
@@ -63,8 +59,8 @@ newaccessobject(value, class, type, mode)
6359
return NULL;
6460
XINCREF(value);
6561
ap->ac_value = value;
66-
XINCREF(class);
67-
ap->ac_class = class;
62+
XINCREF(owner);
63+
ap->ac_owner = owner;
6864
XINCREF(type);
6965
ap->ac_type = (typeobject *)type;
7066
ap->ac_mode = mode;
@@ -81,22 +77,22 @@ cloneaccessobject(op)
8177
return NULL;
8278
}
8379
ap = (accessobject *)op;
84-
return newaccessobject(ap->ac_value, ap->ac_class,
80+
return newaccessobject(ap->ac_value, ap->ac_owner,
8581
ap->ac_type, ap->ac_mode);
8682
}
8783

8884
void
89-
setaccessowner(op, class)
85+
setaccessowner(op, owner)
9086
object *op;
91-
object *class;
87+
object *owner;
9288
{
9389
register accessobject *ap;
94-
if (!is_accessobject(op) || class != NULL && !is_classobject(class))
90+
if (!is_accessobject(op))
9591
return; /* XXX no error */
9692
ap = (accessobject *)op;
97-
XDECREF(ap->ac_class);
98-
XINCREF(class);
99-
ap->ac_class = class;
93+
XDECREF(ap->ac_owner);
94+
XINCREF(owner);
95+
ap->ac_owner = owner;
10096
}
10197

10298
int
@@ -109,9 +105,9 @@ hasaccessvalue(op)
109105
}
110106

111107
object *
112-
getaccessvalue(op, class)
108+
getaccessvalue(op, owner)
113109
object *op;
114-
object *class;
110+
object *owner;
115111
{
116112
register accessobject *ap;
117113
if (!is_accessobject(op)) {
@@ -120,7 +116,7 @@ getaccessvalue(op, class)
120116
}
121117
ap = (accessobject *)op;
122118

123-
if (!classcheck(class, ap->ac_class, AC_R, ap->ac_mode)) {
119+
if (!ownercheck(owner, ap->ac_owner, AC_R, ap->ac_mode)) {
124120
err_setstr(AccessError, "read access denied");
125121
return NULL;
126122
}
@@ -134,9 +130,9 @@ getaccessvalue(op, class)
134130
}
135131

136132
int
137-
setaccessvalue(op, class, value)
133+
setaccessvalue(op, owner, value)
138134
object *op;
139-
object *class;
135+
object *owner;
140136
object *value;
141137
{
142138
register accessobject *ap;
@@ -146,7 +142,7 @@ setaccessvalue(op, class, value)
146142
}
147143
ap = (accessobject *)op;
148144

149-
if (!classcheck(class, ap->ac_class, AC_W, ap->ac_mode)) {
145+
if (!ownercheck(owner, ap->ac_owner, AC_W, ap->ac_mode)) {
150146
err_setstr(AccessError, "write access denied");
151147
return -1;
152148
}
@@ -227,17 +223,20 @@ typecheck(value, type)
227223
}
228224

229225
static int
230-
classcheck(caller, owner, access, mode)
226+
ownercheck(caller, owner, access, mode)
231227
object *caller;
232228
object *owner;
233229
int access;
234230
int mode;
235231
{
236-
if (caller == owner && owner != NULL)
237-
return access & mode & (AC_PRIVATE|AC_PROTECTED|AC_PUBLIC);
238-
if (caller != NULL && owner != NULL && issubclass(caller, owner))
239-
return access & mode & (AC_PROTECTED|AC_PUBLIC);
240-
return access & mode & AC_PUBLIC;
232+
int mask = AC_PUBLIC;
233+
if (owner != NULL) {
234+
if (caller == owner)
235+
mask |= AC_PRIVATE | AC_PROTECTED;
236+
else if (is_classobject(owner) && issubclass(caller, owner))
237+
mask |= AC_PROTECTED;
238+
}
239+
return access & mode & mask;
241240
}
242241

243242
/* Access methods */
@@ -247,7 +246,7 @@ access_dealloc(ap)
247246
accessobject *ap;
248247
{
249248
XDECREF(ap->ac_value);
250-
XDECREF(ap->ac_class);
249+
XDECREF(ap->ac_owner);
251250
XDECREF(ap->ac_type);
252251
DEL(ap);
253252
}
@@ -256,7 +255,7 @@ access_dealloc(ap)
256255

257256
static struct memberlist access_memberlist[] = {
258257
{"ac_value", T_OBJECT, OFF(ac_value)},
259-
{"ac_class", T_OBJECT, OFF(ac_class)},
258+
{"ac_owner", T_OBJECT, OFF(ac_owner)},
260259
{"ac_type", T_OBJECT, OFF(ac_type)},
261260
{"ac_mode", T_INT, OFF(ac_mode)},
262261
{NULL} /* Sentinel */
@@ -275,12 +274,21 @@ access_repr(ap)
275274
accessobject *ap;
276275
{
277276
char buf[300];
278-
classobject *class = (classobject *)ap->ac_class;
277+
char buf2[20];
278+
char *ownername;
279279
typeobject *type = ap->ac_type;
280+
if (is_classobject(ap->ac_owner)) {
281+
ownername =
282+
getstringvalue(((classobject *)ap->ac_owner)->cl_name);
283+
}
284+
else {
285+
sprintf(buf2, "0x%lx", (long)ap->ac_owner);
286+
ownername = buf2;
287+
}
280288
sprintf(buf,
281-
"<access object, value 0x%lx, class %.100s, type %.100s, mode %04o>",
289+
"<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>",
282290
(long)(ap->ac_value),
283-
class ? getstringvalue(class->cl_name) : "-",
291+
ownername,
284292
type ? type->tp_name : "-",
285293
ap->ac_mode);
286294
return newstringobject(buf);
@@ -305,7 +313,8 @@ typeobject Accesstype = {
305313
0, /*tp_hash*/
306314
};
307315

308-
/* Dummy type objects to indicate classes of types */
316+
317+
/* Pseudo type objects to indicate collections of types */
309318

310319
/* XXX This should be replaced by a more general "subclassing"
311320
XXX mechanism for type objects... */

0 commit comments

Comments
 (0)