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
# HG changeset patch
# Date 1314866294 18000
# Node ID c211892946d807d55de99de5e9598b25e2d212c9
# Parent  a4615971743fd7fc14b79ae2fc3e55143f74a078
Python3: Added slice assignment support to range object.

diff -r a4615971743f -r c211892946d8 src/if_python3.c
--- a/src/if_python3.c	Thu Sep 01 01:04:07 2011 -0500
+++ b/src/if_python3.c	Thu Sep 01 03:38:14 2011 -0500
@@ -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)
 {
@@ -1145,6 +1155,29 @@
     }
 }
 
+    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
  */
 

Raspunde prin e-mail lui