For CPython 3.1: make type Check and CheckExact macros consistent? universal?

There are a bunch of type-checking macros in Include/*.h. Most classes only provide one, Check(), implemented as follows: #define PyWhatever_Check(ob) (Py_TYPE(ob) == &PyWhatever_Type) Examples of this style include PyRange, PyMemoryView, PyFunction, and PySlice. Then there are types with a more sophisticated view of type identity. For example, an object can qualify as a list even if its type is not PyList_Type. For such types, there's a Check() macro that does a more sophisticated check. They'll also have a CheckExact() macro doing an exact type check; that'll look like PyWhatever_Check above. Then there's PyMethod. PyMethod has a CheckExact macro but *doesn't* have a Check macro. Hmm! When I stumbled across that it got me to thinking. Might it be a good idea for all Python types to have both Check and CheckExact macros? The CheckExact form would be consistent, and for types that only need an "exact" check they could define Check in terms of CheckExact: #define PyWhatever_CheckExact(ob) (Py_TYPE(ob) == &PyWhatever_Type) #define PyWhatever_Check PyWhatever_CheckExact This would let you express your intent. If you were expecting a Whatever-like object, you always use Check. If you require that exact object, you always use CheckExact. On the other hand, maybe every type-checking macro that does (Py_TYPE(ob) == &PyWhatever_Type) should be called CheckExact, and for types that don't have an inexact check they don't have a Check. Or maybe the existing subclass-style Check macros should be renamed CheckSubclass (or Implemented or something) and Check should always do a CheckExact-style check. I admit this isn't a stupendous idea, but I thought it was worth typing up. If there's interest I'd be happy to supply a patch for whichever form found favor. Or we could forget the whole thing, you and I, /larry/

Hi Larry. It was fun meeting you at PyCon and seeing your minuteman demo. I like it the way it is -- "Check" means "any satisfactory type" and "CheckExact" means "that type". For types where only that type is satisfactory, then their Check should be the same as CheckExact. I guess PyMethod not having a "Check" is perhaps an oversight. Regards, Zooko

Zooko O'Whielacronx wrote:
But not having a CheckExact for all types means if you want an exact check you have to look up the API docs for the type concerned, instead of just writing PyFoo_CheckExact. More importantly, if the type is ever changed to be subclassable, and its Check function updated accordingly, existing calls to Check that were relying on an exact check will become broken. -- Greg

Hi Larry. It was fun meeting you at PyCon and seeing your minuteman demo. I like it the way it is -- "Check" means "any satisfactory type" and "CheckExact" means "that type". For types where only that type is satisfactory, then their Check should be the same as CheckExact. I guess PyMethod not having a "Check" is perhaps an oversight. Regards, Zooko

Zooko O'Whielacronx wrote:
But not having a CheckExact for all types means if you want an exact check you have to look up the API docs for the type concerned, instead of just writing PyFoo_CheckExact. More importantly, if the type is ever changed to be subclassable, and its Check function updated accordingly, existing calls to Check that were relying on an exact check will become broken. -- Greg
participants (3)
-
Greg Ewing
-
Larry Hastings
-
Zooko O'Whielacronx