changeset 3826:530f5a903031 v7.3.671

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)
author Bram Moolenaar <bram@vim.org>
date Fri, 21 Sep 2012 13:46:06 +0200
parents f0a177febcf3
children 72c860159c0f
files src/if_py_both.h src/if_python.c src/if_python3.c src/version.c
diffstat 4 files changed, 45 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -71,6 +71,31 @@ static struct PyMethodDef OutputMethods[
 /* 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)
 {
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -951,31 +951,6 @@ OutputGetattr(PyObject *self, char *name
     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
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -88,6 +88,9 @@ static void init_structs(void);
 #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 @@ static int py3initialised = 0;
  */
 #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 @@ ex_py3file(exarg_T *eap)
     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 @@ OutputGetattro(PyObject *self, PyObject 
     static int
 OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
 {
-    char *name = "";
-    if (PyUnicode_Check(nameobj))
-	name = _PyUnicode_AsString(nameobj);
-
-    if (val == NULL)
-    {
-	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
-	return -1;
-    }
+    GET_ATTR_STRING(name, nameobj);
 
-    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 @@ BufferGetattro(PyObject *self, PyObject*
 {
     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 @@ RangeDestructor(PyObject *self)
     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 @@ WindowGetattro(PyObject *self, PyObject 
 {
     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 @@ WindowGetattro(PyObject *self, PyObject 
     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 PyTypeObject CurrentType;
     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 @@ FunctionDestructor(PyObject *self)
 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));
--- a/src/version.c
+++ b/src/version.c
@@ -720,6 +720,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    671,
+/**/
     670,
 /**/
     669,