Kaydet (Commit) 3a3cca5b authored tarafından Guido van Rossum's avatar Guido van Rossum

- list.insert(i, x) now interprets negative i as it would be

  interpreted by slicing, so negative values count from the end of the
  list.  This was the only place where such an interpretation was not
  placed on a list index.
üst b43f15e1
...@@ -941,8 +941,7 @@ The following operations are defined on mutable sequence types (where ...@@ -941,8 +941,7 @@ The following operations are defined on mutable sequence types (where
\lineiii{\var{s}.index(\var{x})} \lineiii{\var{s}.index(\var{x})}
{return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(4)} {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(4)}
\lineiii{\var{s}.insert(\var{i}, \var{x})} \lineiii{\var{s}.insert(\var{i}, \var{x})}
{same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]} {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}}{(5)}
if \code{\var{i} >= 0}}{(5)}
\lineiii{\var{s}.pop(\optional{\var{i}})} \lineiii{\var{s}.pop(\optional{\var{i}})}
{same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(6)} {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(6)}
\lineiii{\var{s}.remove(\var{x})} \lineiii{\var{s}.remove(\var{x})}
...@@ -982,8 +981,10 @@ Notes: ...@@ -982,8 +981,10 @@ Notes:
\var{s}. \var{s}.
\item[(5)] When a negative index is passed as the first parameter to \item[(5)] When a negative index is passed as the first parameter to
the \method{insert()} method, the new element is prepended to the the \method{insert()} method, the list length is added, as for slice
sequence. indices. If it is still negative, it is truncated to zero, as for
slice indices. \versionchanged[Previously, all negative indices
were truncated to zero]{2.3}
\item[(6)] The \method{pop()} method is only supported by the list and \item[(6)] The \method{pop()} method is only supported by the list and
array types. The optional argument \var{i} defaults to \code{-1}, array types. The optional argument \var{i} defaults to \code{-1},
......
...@@ -345,6 +345,11 @@ a.insert(0, -2) ...@@ -345,6 +345,11 @@ a.insert(0, -2)
a.insert(1, -1) a.insert(1, -1)
a.insert(2,0) a.insert(2,0)
if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert' if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
b = a[:]
b.insert(-2, "foo")
b.insert(-200, "left")
b.insert(200, "right")
if b != ["left",-2,-1,0,0,"foo",1,2,"right"]: raise TestFailed, 'list insert2'
if a.count(0) != 2: raise TestFailed, ' list count' if a.count(0) != 2: raise TestFailed, ' list count'
if a.index(0) != 2: raise TestFailed, 'list index' if a.index(0) != 2: raise TestFailed, 'list index'
a.remove(0) a.remove(0)
......
...@@ -12,6 +12,11 @@ What's New in Python 2.3 beta 1? ...@@ -12,6 +12,11 @@ What's New in Python 2.3 beta 1?
Core and builtins Core and builtins
----------------- -----------------
- list.insert(i, x) now interprets negative i as it would be
interpreted by slicing, so negative values count from the end of the
list. This was the only place where such an interpretation was not
placed on a list index.
- range() now works even if the arguments are longs with magnitude - range() now works even if the arguments are longs with magnitude
larger than sys.maxint, as long as the total length of the sequence larger than sys.maxint, as long as the total length of the sequence
fits. E.g., range(2**100, 2**101, 2**100) is the following list: fits. E.g., range(2**100, 2**101, 2**100) is the following list:
......
...@@ -159,8 +159,11 @@ ins1(PyListObject *self, int where, PyObject *v) ...@@ -159,8 +159,11 @@ ins1(PyListObject *self, int where, PyObject *v)
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
if (where < 0) if (where < 0) {
where = 0; where += self->ob_size;
if (where < 0)
where = 0;
}
if (where > self->ob_size) if (where > self->ob_size)
where = self->ob_size; where = self->ob_size;
for (i = self->ob_size; --i >= where; ) for (i = self->ob_size; --i >= where; )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment