Skip to content

Commit 8dd79cf

Browse files
committed
Don't allow assignment to attributes named __*__
1 parent 5113f5f commit 8dd79cf

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

Objects/classobject.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***********************************************************
2-
Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
2+
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
33
Netherlands.
44
55
All Rights Reserved
@@ -106,7 +106,7 @@ class_getattr(op, name)
106106
int i;
107107
for (i = 0; i < n; i++) {
108108
v = class_getattr((classobject *)
109-
gettupleitem(op->cl_bases, i), name);
109+
gettupleitem(op->cl_bases, i), name);
110110
if (v != NULL)
111111
return v;
112112
err_clear();
@@ -122,6 +122,13 @@ class_setattr(op, name, v)
122122
char *name;
123123
object *v;
124124
{
125+
if (name[0] == '_' && name[1] == '_') {
126+
int n = strlen(name);
127+
if (name[n-1] == '_' && name[n-2] == '_') {
128+
err_setstr(TypeError, "read-only special attribute");
129+
return -1;
130+
}
131+
}
125132
if (v == NULL)
126133
return dictremove(op->cl_methods, name);
127134
else
@@ -226,6 +233,13 @@ instance_setattr(inst, name, v)
226233
char *name;
227234
object *v;
228235
{
236+
if (name[0] == '_' && name[1] == '_') {
237+
int n = strlen(name);
238+
if (name[n-1] == '_' && name[n-2] == '_') {
239+
err_setstr(TypeError, "read-only special attribute");
240+
return -1;
241+
}
242+
}
229243
if (v == NULL)
230244
return dictremove(inst->in_attr, name);
231245
else

0 commit comments

Comments
 (0)