Ben Avrahami <avrahami....@gmail.com> added the comment:

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.

----------
nosy: +avrahami.ben

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46376>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to