}
/* Offset is normalized, so it is negative if days < 0 */
if (GET_TD_DAYS(offset) < 0) {
- PyObject *temp = offset;
sign = '-';
- Py_XSETREF(offset, delta_negative((PyDateTime_Delta *)offset));
- offset = delta_negative((PyDateTime_Delta *)offset);
- Py_DECREF(temp);
++ Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset));
if (offset == NULL)
return -1;
}
if (dst == Py_None)
goto Inconsistent;
if (delta_bool((PyDateTime_Delta *)dst) != 0) {
- Py_XSETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
- PyObject *temp = result;
- result = add_datetime_timedelta((PyDateTime_DateTime *)result,
- (PyDateTime_Delta *)dst, 1);
- Py_DECREF(temp);
++ Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
+ (PyDateTime_Delta *)dst, 1));
if (result == NULL)
goto Fail;
}
return NULL;
if (offdiff != NULL) {
- Py_XSETREF(result, delta_subtract(result, offdiff));
- PyObject *temp = result;
- result = delta_subtract(result, offdiff);
- Py_DECREF(temp);
++ Py_SETREF(result, delta_subtract(result, offdiff));
Py_DECREF(offdiff);
}
}
}
static PyObject*
-element_getattro(ElementObject* self, PyObject* nameobj)
+element_tag_getter(ElementObject *self, void *closure)
{
- PyObject* res;
- char *name = "";
+ PyObject *res = self->tag;
+ Py_INCREF(res);
+ return res;
+}
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+static PyObject*
+element_text_getter(ElementObject *self, void *closure)
+{
+ PyObject *res = element_get_text(self);
+ Py_XINCREF(res);
+ return res;
+}
- if (name == NULL)
- return NULL;
+static PyObject*
+element_tail_getter(ElementObject *self, void *closure)
+{
+ PyObject *res = element_get_tail(self);
+ Py_XINCREF(res);
+ return res;
+}
- /* handle common attributes first */
- if (strcmp(name, "tag") == 0) {
- res = self->tag;
- Py_INCREF(res);
- return res;
- } else if (strcmp(name, "text") == 0) {
- res = element_get_text(self);
- Py_XINCREF(res);
- return res;
+static PyObject*
+element_attrib_getter(ElementObject *self, void *closure)
+{
+ PyObject *res;
+ if (!self->extra) {
+ if (create_extra(self, NULL) < 0)
+ return NULL;
}
+ res = element_get_attrib(self);
+ Py_XINCREF(res);
+ return res;
+}
- /* methods */
- res = PyObject_GenericGetAttr((PyObject*) self, nameobj);
- if (res)
- return res;
-
- /* less common attributes */
- if (strcmp(name, "tail") == 0) {
- PyErr_Clear();
- res = element_get_tail(self);
- } else if (strcmp(name, "attrib") == 0) {
- PyErr_Clear();
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return NULL;
- }
- res = element_get_attrib(self);
+/* macro for setter validation */
+#define _VALIDATE_ATTR_VALUE(V) \
+ if ((V) == NULL) { \
+ PyErr_SetString( \
+ PyExc_AttributeError, \
+ "can't delete element attribute"); \
+ return -1; \
}
- if (!res)
- return NULL;
-
- Py_INCREF(res);
- return res;
+static int
+element_tag_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
- Py_XSETREF(self->tag, value);
++ Py_SETREF(self->tag, value);
+ return 0;
}
static int
-element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
+element_text_setter(ElementObject *self, PyObject *value, void *closure)
{
- char *name = "";
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
+ Py_DECREF(JOIN_OBJ(self->text));
+ self->text = value;
+ return 0;
+}
- if (value == NULL) {
- PyErr_SetString(PyExc_AttributeError,
- "can't delete attribute");
- return -1;
- }
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
- if (name == NULL)
- return -1;
+static int
+element_tail_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
+ Py_DECREF(JOIN_OBJ(self->tail));
+ self->tail = value;
+ return 0;
+}
- if (strcmp(name, "tag") == 0) {
- Py_INCREF(value);
- Py_SETREF(self->tag, value);
- } else if (strcmp(name, "text") == 0) {
- Py_DECREF(JOIN_OBJ(self->text));
- self->text = value;
- Py_INCREF(self->text);
- } else if (strcmp(name, "tail") == 0) {
- Py_DECREF(JOIN_OBJ(self->tail));
- self->tail = value;
- Py_INCREF(self->tail);
- } else if (strcmp(name, "attrib") == 0) {
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return -1;
- }
- Py_INCREF(value);
- Py_SETREF(self->extra->attrib, value);
- } else {
- PyErr_SetString(PyExc_AttributeError,
- "Can't set arbitrary attributes on Element");
- return -1;
+static int
+element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ if (!self->extra) {
+ if (create_extra(self, NULL) < 0)
+ return -1;
}
-
+ Py_INCREF(value);
- Py_XSETREF(self->extra->attrib, value);
++ Py_SETREF(self->extra->attrib, value);
return 0;
}