Hello Bram,
will you include this patch?
Regards, Roland
-------- Original Message --------
Subject: Re: [patch] Python 3: Add support for assigning to slices on
RangeObject
Date: Wed, 07 Sep 2011 01:08:22 +0200
From: Roland Puntaier <[email protected]>
Reply-To: [email protected]
To: [email protected]
CC: Brett Overesch <[email protected]>
On 09/03/2011 07:26 AM, Brett Overesch wrote:
Hello,
I've been working with Python 3 scripts a lot lately and noticed that
the range objects in the vim modules (e.g. vim.current.range) do not
support assignment via slices. The Python 2.x interface does support
this, and it works on the buffer object in the Python 3 interface. For
example:
vim.current.range[2:10] = ["some new text"]
or
vim.current.range[5:5] = ["adding a new line at position 5"]
This should work, but instead you get an error stating: "Index must be
int or slice", which of course it is. I created a patch to add this,
trying my best to conform to the existing code structure. Not sure if
this has been covered already; I looked back a few days and searched,
and didn't see anything related. Well, I found a post from someone
stating the problem, but no patches. Anyway, just thought some fellow
vim users might find it useful when working with the Python 3
interface. Have a nice day!
-Brett Overesch
--
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
I've applied your patch and tested it on a line like:
:py3 vim.current.buffer.range(1,3)[0:2]=['line1','line2']
Before your patch this did work for :py but not for :py3.
I've found that
:py3 print(vim.current.buffer.range(1,3)[0:2])
had one entry too much compared to
:py print(vim.current.buffer.range(1,3)[0:2])
I made a little correction for that. The following patch is your patch
plus this one line correction.
-Roland
--
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
--
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 -r 2665b456ee59 src/if_python3.c
--- a/src/if_python3.c Wed Sep 07 19:30:21 2011 +0200
+++ b/src/if_python3.c Wed Sep 07 00:59:25 2011 +0200
@@ -867,6 +867,7 @@
static PyObject* RangeSubscript(PyObject *self, PyObject* idx);
static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
+static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject* idx, PyObject*
val);
/* Current objects type - Implementation functions
* -----------------------------------------------
@@ -1084,7 +1085,7 @@
PyMappingMethods RangeAsMapping = {
/* mp_length */ (lenfunc)RangeLength,
/* mp_subscript */ (binaryfunc)RangeSubscript,
- /* mp_ass_subscript */ (objobjargproc)0,
+ /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
};
/* Line range object - Implementation
@@ -1123,6 +1124,15 @@
&((RangeObject *)(self))->end);
}
+ static Py_ssize_t
+RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
+{
+ return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
+ ((RangeObject *)(self))->start,
+ ((RangeObject *)(self))->end,
+ &((RangeObject *)(self))->end);
+}
+
static PyObject *
RangeSubscript(PyObject *self, PyObject* idx)
{
@@ -1138,13 +1148,36 @@
&step, &slicelen) < 0) {
return NULL;
}
- return RangeSlice(self,start,stop+1);
+ return RangeSlice(self,start,stop);
} else {
PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
return NULL;
}
}
+ static Py_ssize_t
+RangeAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
+{
+ if (PyLong_Check(idx)) {
+ long n = PyLong_AsLong(idx);
+ return RangeAsItem(self, n, val);
+ } else if (PySlice_Check(idx)) {
+ Py_ssize_t start, stop, step, slicelen;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)idx,
+ ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
+ &start, &stop,
+ &step, &slicelen) < 0) {
+ return -1;
+ }
+ return RangeAsSlice(self, start, stop, val);
+ } else {
+ PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
+ return -1;
+ }
+}
+
+
/* Buffer list object - Definitions
*/