Hi there,
I noticed that actually the buffer objects in Python interface have
a 'number' attribute, but it's neither documented nor seen via 'dir()'
function in Python 3: Python 3 no longer uses the '__members__' special
attribute. I did not know its existance until I looked into the source
code. The attached patch fixes both the doc and the source code.
--
Best regards,
lilydjwg
Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/runtime/doc/if_pyth.txt b/runtime/doc/if_pyth.txt
index 03d24a5..acc191a 100644
--- a/runtime/doc/if_pyth.txt
+++ b/runtime/doc/if_pyth.txt
@@ -222,8 +222,9 @@ Buffer objects represent vim buffers. You can obtain them
in a number of ways:
- from indexing vim.buffers (|python-buffers|)
- from the "buffer" attribute of a window (|python-window|)
-Buffer objects have one read-only attribute - name - the full file name for
-the buffer. They also have three methods (append, mark, and range; see below).
+Buffer objects have two read-only attributes - name - the full file name for
+the buffer, and number - the buffer number. They also have three methods
+(append, mark, and range; see below).
You can also treat buffer objects as sequence objects. In this context, they
act as if they were lists (yes, they are mutable) of strings, with each
diff --git a/src/if_py_both.h b/src/if_py_both.h
index 53c2167..aa25912 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -1479,6 +1479,9 @@ static struct PyMethodDef BufferMethods[] = {
{"append", BufferAppend, 1, "Append data to Vim buffer"
},
{"mark", BufferMark, 1, "Return (row,col)
representing position of named mark" },
{"range", BufferRange, 1, "Return a range object
which represents the part of the given buffer between line numbers s and e" },
+#if PY_VERSION_HEX >= 0x03000000
+ {"__dir__", BufferDir, 4, "List its
attributes" },
+#endif
{ NULL, NULL, 0, NULL }
};
diff --git a/src/if_python3.c b/src/if_python3.c
index 18d5b77..c5df746 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -468,6 +468,7 @@ get_py3_exceptions()
static PyObject *BufferNew (buf_T *);
static PyObject *WindowNew(win_T *);
static PyObject *LineToString(const char *);
+static PyObject *BufferDir(PyObject *, PyObject *);
static PyTypeObject RangeType;
@@ -961,13 +962,18 @@ BufferGetattro(PyObject *self, PyObject*nameobj)
return Py_BuildValue("s", this->buf->b_ffname);
else if (strcmp(name, "number") == 0)
return Py_BuildValue("n", this->buf->b_fnum);
- else if (strcmp(name,"__members__") == 0)
- return Py_BuildValue("[ss]", "name", "number");
else
return PyObject_GenericGetAttr(self, nameobj);
}
static PyObject *
+BufferDir(PyObject *self, PyObject *args)
+{
+ return Py_BuildValue("[sssss]", "name", "number", \
+ "append", "mark", "range");
+}
+
+ static PyObject *
BufferRepr(PyObject *self)
{
static char repr[100];