django.utils.six.bytes_types?

128 views
Skip to first unread message

Shai Berger

unread,
May 28, 2014, 11:38:35 AM5/28/14
to django-d...@googlegroups.com
Hi all,

While solving #22715[1], I found myself writing these lines:

elif (isinstance(value, six.memoryview) or
six.PY3 and isinstance(value, bytes)):
....

six.memoryview -- django.utils.six.memoryview, to be accurate, there's no
memoryview in the original six library -- is defined to be `memoryview` on
python3, and `buffer` on python2. But having written this code, I thought the
distinction might have more general value, and so we should maybe add a
"bytes_types" -- similar to six.string_types -- which would be defined as
`buffer` on python2, but `(memoryview, bytes)` on python3. The above would
change to

elif isinstance(value, six.bytes_types):
....

which is obviously nicer.

The question to you is, is it just me? Do you think "bytes_types" can be
useful?

Thanks,
Shai.

[1] https://code.djangoproject.com/ticket/22715

Aymeric Augustin

unread,
May 28, 2014, 4:38:28 PM5/28/14
to django-d...@googlegroups.com
The name seems confusing to me: it's called bytes_type but its main purpose is to exclude bytes on Python 2. Otherwise you'd just write isinstance(value, (bytes, six.memoryview)).

If indeed you want "bytes or bytes-like on Py 3, bytes-like but not bytes on Py 2" then your if clause is perfectly clear and reasonable as is. (Remember that bytes is an alias for str on Py 2.)

FYI django.utils.six.memoryview is a lazy hack to get the test suite to pass on Python 2 and 3, I wouldn't be surprised if it's used inappropriately :-/

--
Aymeric.
> --
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
> To post to this group, send email to django-d...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/201405281838.15889.shai%40platonix.com.
> For more options, visit https://groups.google.com/d/optout.

Shai Berger

unread,
May 28, 2014, 5:57:55 PM5/28/14
to django-d...@googlegroups.com
On Wednesday 28 May 2014 22:38:20 Aymeric Augustin wrote:
> The name seems confusing to me: it's called bytes_type but its main purpose
> is to exclude bytes on Python 2. Otherwise you'd just write
> isinstance(value, (bytes, six.memoryview)).
>

I renamed it to "buffer_types" -- because English doesn't like plurals in the
first part of such noun-combinations, but also because this way it makes more
sense as a concept...

> If indeed you want "bytes or bytes-like on Py 3, bytes-like but not bytes on
> Py 2" then your if clause is perfectly clear and reasonable as is.
> (Remember that bytes is an alias for str on Py 2.)
>

...and I also threw bytearray in. https://github.com/django/django/pull/2732.

> FYI django.utils.six.memoryview is a lazy hack to get the test suite to pass
> on Python 2 and 3, I wouldn't be surprised if it's used inappropriately :-/

So, the PR can be merged without documentation ?

[No, it can't. But perhaps we can leave memoryview out of it]

Shai.

Aymeric Augustin

unread,
May 29, 2014, 5:05:22 AM5/29/14
to django-d...@googlegroups.com
> Le 28 mai 2014 à 23:57, Shai Berger <sh...@platonix.com> a écrit :
>
> So, the PR can be merged without documentation ?

Since it's a private helper, that's OK.

--
Aymeric.

Shai Berger

unread,
May 29, 2014, 8:07:49 AM5/29/14
to django-d...@googlegroups.com

Aymeric Augustin

unread,
May 29, 2014, 9:01:22 AM5/29/14
to django-d...@googlegroups.com
Well the public "django.utils.six" can contain a private "buffer_types" attribute.

We can document it once we're convinced it's a good idea. I'm not sufficiently familiar with buffer, memoryview and friends to make this decision.

-- 
Aymeric.
Reply all
Reply to author
Forward
0 new messages