robertwb / issues-import-test

0 stars 0 forks source link

Propagate length hint through generator expressions #147

Open robertwb opened 7 years ago

robertwb commented 7 years ago

Reported by scoder on 19 Nov 2011 15:11 UTC Python generators have a feature that presents a length hint to interested parties that can take advantage of it, such as "list()". Cython generator expressions should propagate the length hint of the underlying iterator if there is only one "for" loop and no "if" selector predicate. The C-API function to ask for a length hint is _PyObject_LengthHint(), which is available from Py2.5 on. Generator expressions would then need to provide a .__length_hint__() special method that returns either a constant result (when known at compile time, e.g. for sliced C arrays), or calls _PyObject_LengthHint() on the underlying iterator.

Migrated-From: http://trac.cython.org/ticket/756

robertwb commented 7 years ago

Modified by scoder on 23 Aug 2013 17:54 UTC

robertwb commented 7 years ago

Modified by scoder on 23 Aug 2013 17:54 UTC Set description to

Python generators have a feature that presents a length hint to interested parties that can take advantage of it, such as "list()". Cython generator expressions should propagate the length hint of the underlying iterator if there is only one "for" loop and no "if" selector predicate. The C-API function to ask for a length hint is _PyObject_LengthHint(), which is available from Py2.5 on. Generator expressions would then need to provide a .__length_hint__() special method that returns either a constant result (when known at compile time, e.g. for sliced C arrays), or calls _PyObject_LengthHint() on the underlying iterator.

This should be easy to do once ticket http://trac.cython.org/ticket/600 is fixed.