updated for version 7.3.671
Problem: More Python code can be shared between Python 2 and 3.
Solution: Move code to if_py_both.h. (ZyX)
diff --git a/src/if_py_both.h b/src/if_py_both.h
index c8a7fca..0909981 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -71,6 +71,31 @@
/* Output buffer management
*/
+ static int
+OutputSetattr(PyObject *self, char *name, PyObject *val)
+{
+ if (val == NULL)
+ {
+ PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
+ return -1;
+ }
+
+ if (strcmp(name, "softspace") == 0)
+ {
+ if (!PyInt_Check(val))
+ {
+ PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
+ return -1;
+ }
+
+ ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
+ return 0;
+ }
+
+ PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
+ return -1;
+}
+
static PyObject *
OutputWrite(PyObject *self, PyObject *args)
{
diff --git a/src/if_python.c b/src/if_python.c
index a20aba0..3c72b26 100644
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -951,31 +951,6 @@
return Py_FindMethod(OutputMethods, self, name);
}
- static int
-OutputSetattr(PyObject *self, char *name, PyObject *val)
-{
- if (val == NULL)
- {
- PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
- return -1;
- }
-
- if (strcmp(name, "softspace") == 0)
- {
- if (!PyInt_Check(val))
- {
- PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
- return -1;
- }
-
- ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
- return 0;
- }
-
- PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
- return -1;
-}
-
/***************/
static int
diff --git a/src/if_python3.c b/src/if_python3.c
index 43ed0e2..6dc7551 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -88,6 +88,9 @@
#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
#define PyString_FromString(repr) PyUnicode_FromString(repr)
#define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
+#define PyInt_Check(obj) PyLong_Check(obj)
+#define PyInt_FromLong(i) PyLong_FromLong(i)
+#define PyInt_AsLong(obj) PyLong_AsLong(obj)
#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
@@ -586,6 +589,11 @@
*/
#include "if_py_both.h"
+#define GET_ATTR_STRING(name, nameobj) \
+ char *name = ""; \
+ if(PyUnicode_Check(nameobj)) \
+ name = _PyUnicode_AsString(nameobj)
+
#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
static void
@@ -923,9 +931,7 @@
static PyObject *
OutputGetattro(PyObject *self, PyObject *nameobj)
{
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
if (strcmp(name, "softspace") == 0)
return PyLong_FromLong(((OutputObject *)(self))->softspace);
@@ -936,30 +942,9 @@
static int
OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
{
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
- if (val == NULL)
- {
- PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
- return -1;
- }
-
- if (strcmp(name, "softspace") == 0)
- {
- if (!PyLong_Check(val))
- {
- PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
- return -1;
- }
-
- ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
- return 0;
- }
-
- PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
- return -1;
+ return OutputSetattr(self, name, val);
}
/***************/
@@ -1091,9 +1076,7 @@
{
BufferObject *this = (BufferObject *)(self);
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
if (CheckBuffer(this))
return NULL;
@@ -1257,9 +1240,7 @@
static PyObject *
RangeGetattro(PyObject *self, PyObject *nameobj)
{
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
if (strcmp(name, "start") == 0)
return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
@@ -1430,10 +1411,7 @@
{
WindowObject *this = (WindowObject *)(self);
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
-
+ GET_ATTR_STRING(name, nameobj);
if (CheckWindow(this))
return NULL;
@@ -1461,10 +1439,7 @@
static int
WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
{
- char *name = "";
-
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
return WindowSetattr(self, name, val);
}
@@ -1508,9 +1483,7 @@
static PyObject *
CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
{
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ GET_ATTR_STRING(name, nameobj);
if (strcmp(name, "buffer") == 0)
return (PyObject *)BufferNew(curbuf);
@@ -1681,9 +1654,8 @@
FunctionGetattro(PyObject *self, PyObject *nameobj)
{
FunctionObject *this = (FunctionObject *)(self);
- char *name = "";
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+
+ GET_ATTR_STRING(name, nameobj);
if (strcmp(name, "name") == 0)
return PyUnicode_FromString((char *)(this->name));
diff --git a/src/version.c b/src/version.c
index 4fb2743..3e054b7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -720,6 +720,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 671,
+/**/
670,
/**/
669,