@@ -129,10 +129,11 @@ int PyRefToInstance::tp_setattr_concrete(PyObject* attrName, PyObject* attrVal)
129129 }
130130
131131 HeldClass* clsType = (HeldClass*)type ()->getEltType ();
132+ instance_ptr heldClassBody = *(instance_ptr*)dataPtr ();
132133
133- int i = clsType->getMemberIndex (PyUnicode_AsUTF8 (attrName));
134+ int memberIndex = clsType->getMemberIndex (PyUnicode_AsUTF8 (attrName));
134135
135- if (i < 0 ) {
136+ if (memberIndex < 0 ) {
136137 auto it = clsType->getClassMembers ().find (PyUnicode_AsUTF8 (attrName));
137138 if (it == clsType->getClassMembers ().end ()) {
138139 PyErr_Format (
@@ -150,23 +151,44 @@ int PyRefToInstance::tp_setattr_concrete(PyObject* attrName, PyObject* attrVal)
150151 return -1 ;
151152 }
152153
153- Type* eltType = clsType->getMemberType (i);
154+ if (!attrVal) {
155+ if (clsType->getMemberIsNonempty (memberIndex)) {
156+ PyErr_Format (
157+ PyExc_AttributeError,
158+ " Attribute '%S' cannot be deleted" ,
159+ attrName
160+ );
161+ return -1 ;
162+ }
154163
155- Type* attrType = extractTypeFrom (attrVal->ob_type );
164+ if (!clsType->checkInitializationFlag (heldClassBody, memberIndex)) {
165+ PyErr_Format (
166+ PyExc_AttributeError,
167+ " Attribute '%S' is not initialized" ,
168+ attrName
169+ );
170+ return -1 ;
171+ }
156172
157- instance_ptr heldClassBody = *(instance_ptr*)dataPtr ();
173+ clsType->delAttribute (heldClassBody, memberIndex);
174+ return 0 ;
175+ }
176+
177+ Type* eltType = clsType->getMemberType (memberIndex);
178+
179+ Type* attrType = extractTypeFrom (attrVal->ob_type );
158180
159181 if (Type::typesEquivalent (eltType, attrType)) {
160182 PyInstance* item_w = (PyInstance*)attrVal;
161183
162- clsType->setAttribute (heldClassBody, i , item_w->dataPtr ());
184+ clsType->setAttribute (heldClassBody, memberIndex , item_w->dataPtr ());
163185
164186 return 0 ;
165187 } else if (attrType && attrType->getTypeCategory () == Type::TypeCategory::catRefTo &&
166188 ((RefTo*)attrType)->getEltType () == eltType) {
167189 PyInstance* item_w = (PyInstance*)attrVal;
168190
169- clsType->setAttribute (heldClassBody, i , *(instance_ptr*)item_w->dataPtr ());
191+ clsType->setAttribute (heldClassBody, memberIndex , *(instance_ptr*)item_w->dataPtr ());
170192
171193 return 0 ;
172194 } else {
@@ -182,7 +204,7 @@ int PyRefToInstance::tp_setattr_concrete(PyObject* attrName, PyObject* attrVal)
182204 return -1 ;
183205 }
184206
185- clsType->setAttribute (heldClassBody, i , tempObj);
207+ clsType->setAttribute (heldClassBody, memberIndex , tempObj);
186208
187209 eltType->destroy (tempObj);
188210 tp_free (tempObj);
0 commit comments