[Python-bugs-list] [ python-Bugs-633152 ] list slice ass ignores subtypes of list

noreply@sourceforge.net noreply@sourceforge.net
Mon, 11 Nov 2002 03:50:43 -0800


Bugs item #633152, was opened at 2002-11-04 08:19
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=633152&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: Fixed
Priority: 5
Submitted By: Ronald Oussoren (ronaldoussoren)
>Assigned to: Michael Hudson (mwh)
Summary: list slice ass ignores subtypes of list

Initial Comment:
When assigning a subtype of list to a list slice the implementation 
of slice-assignment directly accesses the list representation and 
ignores any modified accessor functions:

class MyList  (list):
    def __getitem__(self, idx):
        if idx % 2 == 0:
            return 'E'
        return 'O'

mylst = MyList()
mylst.append(1)
mylst.append(1)

lst = [ 1, 2, 3, 4]
lst [2:3] = mylst
print lst
# prints [1, 2, 1, 1, 4] I'd expect it to print [1, 2, 'E', 'O', 4 ]

----------------------------------------------------------------------

>Comment By: Michael Hudson (mwh)
Date: 2002-11-11 11:50

Message:
Logged In: YES 
user_id=6656

I've thought about this a bit.

You're writing a NSArray proxy, right?

Here's some advice: do NOT inherit from list.

What does inheriting from list get you?  The ability to pass
`PyList_Check'.  But almost inevitably code that calls that
promptly pokes into the internal structure of the list.  For
example, you'll find iterating over your proxy list doesn't
respect the subtype (unless you supply an __iter__ method, I
guess/hope).

If you find things that fail because of this, let's fix
those to take more general sequences.

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-06 11:21

Message:
Logged In: YES 
user_id=6656

Sigh.  More s/PyList_Check/PyList_CheckExact/ I guess.

----------------------------------------------------------------------

Comment By: Ronald Oussoren (ronaldoussoren)
Date: 2002-11-06 08:45

Message:
Logged In: YES 
user_id=580910

Sorry, but it is not fixed completely. The problem is in PyList_AsTuple 
(called by PySequence_Tuple if the arguments is a PyList). 
PyList_AsTuple directly accesses the list representation, and therefore 
the code above still fails. I'll try to post a patch for this later this week.

I'm not sure why the patch got corrupted, it (and still is) fine on my 
system (but not when I download it again...). 

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-05 18:15

Message:
Logged In: YES 
user_id=6656

I think this is now fixed, via a somewhat different approach.

You might want to check, though.

While we're at it, there are a bunch of problems with your
patch (for future reference):

1) it contains a bunch of NUL bytes!
2) it's a straight diff.  we like context (-c) or unified
(-u) diffs.  most people prefer context diffs, I think. 
Bass players (e.g. Barry) prefer unified.
3) it has no error checking at all!

----------------------------------------------------------------------

Comment By: Ronald Oussoren (ronaldoussoren)
Date: 2002-11-04 11:04

Message:
Logged In: YES 
user_id=580910

The attached patch (list_ass_slice.patch) updates the implementation of 
slice assigment for lists: If the RHS is a list (exact match) use the 
current implementation and if the RHS is a sequence use 
PySequence_GetItem.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=633152&group_id=5470