[Python-Dev] cpython: Issue #16148: implemented PEP 424

Antoine Pitrou solipsis at pitrou.net
Sat Oct 6 14:37:02 CEST 2012


On Sat,  6 Oct 2012 14:12:36 +0200 (CEST)
armin.ronacher <python-checkins at python.org> wrote:
> http://hg.python.org/cpython/rev/a7ec0a1b0f7c
> changeset:   79511:a7ec0a1b0f7c
> parent:      79507:3c1df1ede882
> user:        Armin Ronacher <armin.ronacher at active-4.com>
> date:        Sat Oct 06 14:03:24 2012 +0200
> summary:
>   Issue #16148: implemented PEP 424
> 
[...]
>  
> +.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)
> +
> +   Return an estimated length for the object *o*. First trying to return its
> +   actual length, then an estimate using ``__length_hint__``, and finally
> +   returning the default value. On error ``-1`` is returned. This is the
> +   equivalent to the Python expression ``operator.length_hint(o, default)``.
> +

You need a "versionadded" marker.

> +.. function:: length_hint(obj, default=0)
> +
> +   Return an estimated length for the object *o*. First trying to return its
> +   actual length, then an estimate using ``__length_hint__``, and finally
> +   returning the default value.
> +

Here as well.

> -#ifndef Py_LIMITED_API
> -     PyAPI_FUNC(Py_ssize_t) _PyObject_LengthHint(PyObject *o, Py_ssize_t);
> -#endif
> +PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
> +PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);

If _PyObject_HasLen is private, it shouldn't be in the Py_LIMITED_API.

> diff --git a/Lib/test/test_enumerate.py b/Lib/test/test_enumerate.py
> --- a/Lib/test/test_enumerate.py
> +++ b/Lib/test/test_enumerate.py
> @@ -1,4 +1,5 @@
>  import unittest
> +import operator
>  import sys
>  import pickle
>  
> @@ -168,15 +169,13 @@
>          x = range(1)
>          self.assertEqual(type(reversed(x)), type(iter(x)))
>  
> -    @support.cpython_only
>      def test_len(self):
>          # This is an implementation detail, not an interface requirement
> -        from test.test_iterlen import len
>          for s in ('hello', tuple('hello'), list('hello'), range(5)):
> -            self.assertEqual(len(reversed(s)), len(s))
> +            self.assertEqual(operator.length_hint(reversed(s)), len(s))

You should still test len() as well, no?

> +    else {
> +        return res;
> +    }
> +    PyObject *hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);

Putting variable declarations in the middle of code blocks is not
C89-compliant. You probably broke a couple of buildbots :-)

> +        if (_PyObject_HasLen(it->it_seq)) {
> +            seqsize = PySequence_Size(it->it_seq);
> +            if (seqsize == -1)
> +                return NULL;
> +        }
> +        else {
> +            return Py_NotImplemented;
> +        }

Lacks a Py_INCREF?

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net




More information about the Python-Dev mailing list