[ python-Patches-1708353 ] Make isinstance/issubclass overloadable for PEP 3119

"SourceForge.net" <[email protected]>
Newsgroups gmane.comp.python.patches
Message-ID <[email protected]>
Patches item #1708353, was opened at 2007-04-26 16:16
Message generated for change (Comment added) made by gvanrossum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1708353&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 3000
>Status: Closed
>Resolution: Accepted
Priority: 5
Private: No
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Make isinstance/issubclass overloadable for PEP 3119

Initial Comment:
I came up with a fairly simple way to overload isinstance() and issubclass().  The class that is the second argument can define a (class) method named __instancecheck__ or __subclasscheck__ which, if present, will be called *instead* of the normal approach.

The names are different to remind users that the calling convention is the opposite -- isinstance(x, C) maps to C.__instancecheck__(x).


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

>Comment By: Guido van Rossum (gvanrossum)
Date: 2007-05-25 18:15

Message:
Logged In: YES 
user_id=6380
Originator: YES

Checked in. The recursion bug is warded off by a standard recursion check
-- you'll now get an inexplicable RuntimeError exception instead of an
inexplicable SegFault. :-)

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

Comment By: Georg Brandl (gbrandl)
Date: 2007-05-12 15:27

Message:
Logged In: YES 
user_id=849994
Originator: NO

Probably a warning would be good for the non-classmethod case.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-05-11 17:40

Message:
Logged In: YES 
user_id=6380
Originator: YES

FWIW, this can run into unchecked infinite recursion easily:

class C:
    # There's no @classmethod decorator here as there should have been
    def __instancecheck__(self, arg):
        return False

isinstance(42, C)

The reason is that on line 372 in classobject.c there's a call to
PyObject_IsInstance(self, klass).

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-05-11 10:16

Message:
Logged In: YES 
user_id=6380
Originator: YES

Version 3 compiles with older C89 compilers like gcc 2.96.
File Added: typechecks.diff

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-26 17:27

Message:
Logged In: YES 
user_id=6380
Originator: YES

I'll add a description and motivation for this to PEP 3119.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-26 16:34

Message:
Logged In: YES 
user_id=6380
Originator: YES

Oops, forget the first attempt; isinstance(Integer(), Integer) would be
False!  Now it's True, and more tests are added.
File Added: typechecks.diff

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1708353&group_id=5470
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.