comparison src/if_py_both.h @ 4377:8ec7323f417d v7.3.937

updated for version 7.3.937 Problem: More can be shared between Python 2 and 3. Solution: Move code to if_py_both.h. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Sun, 12 May 2013 18:44:48 +0200
parents 6d45e6f97415
children a2f03b41dca7
comparison
equal deleted inserted replaced
4376:249da4a43d4c 4377:8ec7323f417d
1780 } 1780 }
1781 1781
1782 return 0; 1782 return 0;
1783 } 1783 }
1784 1784
1785 /* Window object
1786 */
1787
1785 static int WindowSetattr(PyObject *, char *, PyObject *); 1788 static int WindowSetattr(PyObject *, char *, PyObject *);
1786 static PyObject *WindowRepr(PyObject *); 1789 static PyObject *WindowRepr(PyObject *);
1787 static PyTypeObject WindowType; 1790 static PyTypeObject WindowType;
1791
1792 static PyObject *
1793 WindowNew(win_T *win)
1794 {
1795 /* We need to handle deletion of windows underneath us.
1796 * If we add a "w_python*_ref" field to the win_T structure,
1797 * then we can get at it in win_free() in vim. We then
1798 * need to create only ONE Python object per window - if
1799 * we try to create a second, just INCREF the existing one
1800 * and return it. The (single) Python object referring to
1801 * the window is stored in "w_python*_ref".
1802 * On a win_free() we set the Python object's win_T* field
1803 * to an invalid value. We trap all uses of a window
1804 * object, and reject them if the win_T* field is invalid.
1805 *
1806 * Python2 and Python3 get different fields and different objects:
1807 * w_python_ref and w_python3_ref fields respectively.
1808 */
1809
1810 WindowObject *self;
1811
1812 if (WIN_PYTHON_REF(win))
1813 {
1814 self = WIN_PYTHON_REF(win);
1815 Py_INCREF(self);
1816 }
1817 else
1818 {
1819 self = PyObject_NEW(WindowObject, &WindowType);
1820 if (self == NULL)
1821 return NULL;
1822 self->win = win;
1823 WIN_PYTHON_REF(win) = self;
1824 }
1825
1826 return (PyObject *)(self);
1827 }
1788 1828
1789 static PyObject * 1829 static PyObject *
1790 WindowAttr(WindowObject *this, char *name) 1830 WindowAttr(WindowObject *this, char *name)
1791 { 1831 {
1792 if (strcmp(name, "buffer") == 0) 1832 if (strcmp(name, "buffer") == 0)
1807 return DictionaryNew(this->win->w_vars); 1847 return DictionaryNew(this->win->w_vars);
1808 else if (strcmp(name, "options") == 0) 1848 else if (strcmp(name, "options") == 0)
1809 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow, 1849 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1810 (PyObject *) this); 1850 (PyObject *) this);
1811 else if (strcmp(name,"__members__") == 0) 1851 else if (strcmp(name,"__members__") == 0)
1812 return Py_BuildValue("[sssss]", "buffer", "cursor", "height", "vars", 1852 return Py_BuildValue("[ssssss]", "buffer", "cursor", "height", "vars",
1813 "options"); 1853 "options");
1814 else 1854 else
1815 return NULL; 1855 return NULL;
1816 } 1856 }
1817 1857
1819 WindowDestructor(PyObject *self) 1859 WindowDestructor(PyObject *self)
1820 { 1860 {
1821 WindowObject *this = (WindowObject *)(self); 1861 WindowObject *this = (WindowObject *)(self);
1822 1862
1823 if (this->win && this->win != INVALID_WINDOW_VALUE) 1863 if (this->win && this->win != INVALID_WINDOW_VALUE)
1824 #if PY_MAJOR_VERSION >= 3 1864 WIN_PYTHON_REF(this->win) = NULL;
1825 this->win->w_python3_ref = NULL;
1826 #else
1827 this->win->w_python_ref = NULL;
1828 #endif
1829 1865
1830 DESTRUCTOR_FINISH(self); 1866 DESTRUCTOR_FINISH(self);
1831 } 1867 }
1832 1868
1833 static int 1869 static int
2754 BufferDestructor(PyObject *self) 2790 BufferDestructor(PyObject *self)
2755 { 2791 {
2756 BufferObject *this = (BufferObject *)(self); 2792 BufferObject *this = (BufferObject *)(self);
2757 2793
2758 if (this->buf && this->buf != INVALID_BUFFER_VALUE) 2794 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
2759 #if PY_MAJOR_VERSION >= 3 2795 BUF_PYTHON_REF(this->buf) = NULL;
2760 this->buf->b_python3_ref = NULL;
2761 #else
2762 this->buf->b_python_ref = NULL;
2763 #endif
2764 2796
2765 DESTRUCTOR_FINISH(self); 2797 DESTRUCTOR_FINISH(self);
2798 }
2799
2800 static PyObject *
2801 BufferNew(buf_T *buf)
2802 {
2803 /* We need to handle deletion of buffers underneath us.
2804 * If we add a "b_python*_ref" field to the buf_T structure,
2805 * then we can get at it in buf_freeall() in vim. We then
2806 * need to create only ONE Python object per buffer - if
2807 * we try to create a second, just INCREF the existing one
2808 * and return it. The (single) Python object referring to
2809 * the buffer is stored in "b_python*_ref".
2810 * Question: what to do on a buf_freeall(). We'll probably
2811 * have to either delete the Python object (DECREF it to
2812 * zero - a bad idea, as it leaves dangling refs!) or
2813 * set the buf_T * value to an invalid value (-1?), which
2814 * means we need checks in all access functions... Bah.
2815 *
2816 * Python2 and Python3 get different fields and different objects:
2817 * b_python_ref and b_python3_ref fields respectively.
2818 */
2819
2820 BufferObject *self;
2821
2822 if (BUF_PYTHON_REF(buf) != NULL)
2823 {
2824 self = BUF_PYTHON_REF(buf);
2825 Py_INCREF(self);
2826 }
2827 else
2828 {
2829 self = PyObject_NEW(BufferObject, &BufferType);
2830 if (self == NULL)
2831 return NULL;
2832 self->buf = buf;
2833 BUF_PYTHON_REF(buf) = self;
2834 }
2835
2836 return (PyObject *)(self);
2766 } 2837 }
2767 2838
2768 static PyObject * 2839 static PyObject *
2769 BufferAttr(BufferObject *this, char *name) 2840 BufferAttr(BufferObject *this, char *name)
2770 { 2841 {
2779 (PyObject *) this); 2850 (PyObject *) this);
2780 else if (strcmp(name,"__members__") == 0) 2851 else if (strcmp(name,"__members__") == 0)
2781 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); 2852 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
2782 else 2853 else
2783 return NULL; 2854 return NULL;
2855 }
2856
2857 static PyInt
2858 BufferLength(PyObject *self)
2859 {
2860 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2861 if (CheckBuffer((BufferObject *)(self)))
2862 return -1; /* ??? */
2863
2864 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
2865 }
2866
2867 static PyObject *
2868 BufferItem(PyObject *self, PyInt n)
2869 {
2870 return RBItem((BufferObject *)(self), n, 1,
2871 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count);
2872 }
2873
2874 static PyObject *
2875 BufferSlice(PyObject *self, PyInt lo, PyInt hi)
2876 {
2877 return RBSlice((BufferObject *)(self), lo, hi, 1,
2878 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count);
2784 } 2879 }
2785 2880
2786 static PyObject * 2881 static PyObject *
2787 BufferAppend(PyObject *self, PyObject *args) 2882 BufferAppend(PyObject *self, PyObject *args)
2788 { 2883 {