This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author avrahami.ben
Recipients aganders3, aviramha, avrahami.ben, bar.harel, benjamin.peterson, brandtbucher, bukzor, georg.brandl, levkivskyi, methane, miss-islington, petr.viktorin, pitrou, rhettinger, serhiy.storchaka
Date 2022-01-25.09:43:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1643103782.62.0.0290217715979.issue46376@roundup.psfhosted.org>
In-reply-to
Content
IMHO, I don't think any alternative to aviramha's solution addresses the issue, And I don't think the need is niche enough to be ignored.

PyType_HasFeature excludes strings, bytes, and other esoteric types.
PyMapping_Check includes mappings like dict and MappingProxyType.
PySequence_Check includes non-dict mappings like MappingProxyType.

The only possible solutions right now are:

a. Import the "collections.abc.Sequence" class into C and run "PyObject_IsInstance" on it. This would be the correct solution, but it would be slower than aviramha's propsal.
b. Perform's aviramha's proposal manually: first check the "Py_TPFLAGS_SEQUENCE" feature flag, and fallback for instance checking strings, bytecodes, and other esoteric types individually. This would be correct and fast, but is cumbersome to perform, and users are bound to forget some types.

A question as simple as "would isinstance(X, <Sequence/Mapping>) returns true?" should be easier to answer for low-level developer, one only needs to look at the finagling numpy, among others, has to perform to parse a sequence (the relevant function in numpy is called "PyArray_FromAny").

A simple implementation of a new function for non-CPython alternatives will be to do implement solution a: import "collections.abc.Sequence", and check for instance. For CPYTHON, this can be optimized by implementing solution b.

There is already a precedence for ABI functions that only exist because its trivial implementation can be optimized with CPython specific behaviour. I don't understand the reluctance to add this one.
History
Date User Action Args
2022-01-25 09:43:02avrahami.bensetrecipients: + avrahami.ben, georg.brandl, rhettinger, pitrou, benjamin.peterson, petr.viktorin, methane, bukzor, aganders3, serhiy.storchaka, levkivskyi, bar.harel, miss-islington, brandtbucher, aviramha
2022-01-25 09:43:02avrahami.bensetmessageid: <1643103782.62.0.0290217715979.issue46376@roundup.psfhosted.org>
2022-01-25 09:43:02avrahami.benlinkissue46376 messages
2022-01-25 09:43:02avrahami.bencreate