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

Skip to content

Commit ed18fdc

Browse files
committed
* accessobject.c (ownercheck): allow a base class access to protected
objects of its derived classes; allow anything that has an attribute named "__privileged__" access to anything. * object.[ch]: added hasattr() -- test whether getattr() will succeed.
1 parent 697e7ab commit ed18fdc

3 files changed

Lines changed: 42 additions & 15 deletions

File tree

Include/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ extern int printobject PROTO((object *, FILE *, int));
203203
extern object * reprobject PROTO((object *));
204204
extern int cmpobject PROTO((object *, object *));
205205
extern object *getattr PROTO((object *, char *));
206+
extern int hasattr PROTO((object *, char *));
206207
extern object *getattro PROTO((object *, object *));
207208
extern int setattro PROTO((object *, object *, object *));
208209
extern long hashobject PROTO((object *));

Objects/accessobject.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2525
/* Access object implementation */
2626

2727
/* XXX TO DO LIST
28-
- need a "super user" mechanism for debugger etc.
2928
- __init__ and __del__ (and all other similar methods)
30-
should be usable even when private, not ignored
31-
- "from foo import bar" should check access of bar
29+
should be usable even when private, not ignored (???)
3230
*/
3331

3432
#include "allobjects.h"
35-
33+
#include "ceval.h"
3634
#include "structmember.h"
3735
#include "modsupport.h" /* For getargs() etc. */
3836

@@ -112,9 +110,9 @@ hasaccessvalue(op)
112110
}
113111

114112
object *
115-
getaccessvalue(op, owner)
113+
getaccessvalue(op, caller)
116114
object *op;
117-
object *owner;
115+
object *caller;
118116
{
119117
register accessobject *ap;
120118
if (!is_accessobject(op)) {
@@ -123,7 +121,7 @@ getaccessvalue(op, owner)
123121
}
124122
ap = (accessobject *)op;
125123

126-
if (!ownercheck(owner, ap->ac_owner, AC_R, ap->ac_mode)) {
124+
if (!ownercheck(caller, ap->ac_owner, AC_R, ap->ac_mode)) {
127125
err_setstr(AccessError, "read access denied");
128126
return NULL;
129127
}
@@ -137,9 +135,9 @@ getaccessvalue(op, owner)
137135
}
138136

139137
int
140-
setaccessvalue(op, owner, value)
138+
setaccessvalue(op, caller, value)
141139
object *op;
142-
object *owner;
140+
object *caller;
143141
object *value;
144142
{
145143
register accessobject *ap;
@@ -149,7 +147,7 @@ setaccessvalue(op, owner, value)
149147
}
150148
ap = (accessobject *)op;
151149

152-
if (!ownercheck(owner, ap->ac_owner, AC_W, ap->ac_mode)) {
150+
if (!ownercheck(caller, ap->ac_owner, AC_W, ap->ac_mode)) {
153151
err_setstr(AccessError, "write access denied");
154152
return -1;
155153
}
@@ -229,6 +227,19 @@ typecheck(value, type)
229227
return 0;
230228
}
231229

230+
static int
231+
isprivileged(caller)
232+
object *caller;
233+
{
234+
object *g;
235+
if (caller != NULL && hasattr(caller, "__privileged__"))
236+
return 1;
237+
g = getglobals();
238+
if (g != NULL && dictlookup(g, "__privileged__"))
239+
return 1;
240+
return 0;
241+
}
242+
232243
static int
233244
ownercheck(caller, owner, access, mode)
234245
object *caller;
@@ -237,12 +248,13 @@ ownercheck(caller, owner, access, mode)
237248
int mode;
238249
{
239250
int mask = AC_PUBLIC;
240-
if (owner != NULL) {
241-
if (caller == owner)
242-
mask |= AC_PRIVATE | AC_PROTECTED;
243-
else if (is_classobject(owner) && issubclass(caller, owner))
251+
if (caller == owner || isprivileged(caller))
252+
mask |= AC_PRIVATE | AC_PROTECTED;
253+
else if (caller != NULL && owner != NULL &&
254+
is_classobject(owner) && is_classobject(caller) &&
255+
(issubclass(caller, owner) ||
256+
issubclass(owner, caller)))
244257
mask |= AC_PROTECTED;
245-
}
246258
return access & mode & mask;
247259
}
248260

Objects/object.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ getattr(v, name)
193193
}
194194
}
195195

196+
int
197+
hasattr(v, name)
198+
object *v;
199+
char *name;
200+
{
201+
object *res = getattr(v, name);
202+
if (res != NULL) {
203+
DECREF(res);
204+
return 1;
205+
}
206+
err_clear();
207+
return 0;
208+
}
209+
196210
int
197211
setattr(v, name, w)
198212
object *v;

0 commit comments

Comments
 (0)