classification
Title: xdrlib.Packer().pack_fstring throws a TypeError when called with a str()
Type: behavior Stage:
Components: Documentation Versions: Python 3.1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: arnoldp, benjamin.peterson, docs@python, eric.araujo, gruszczy, mark.dickinson
Priority: normal Keywords: patch

Created on 2010-08-09 05:51 by arnoldp, last changed 2011-04-14 13:22 by gruszczy.

Files
File name Uploaded Description Edit
9544.patch gruszczy, 2011-03-27 10:43 review
Messages (11)
msg113386 - (view) Author: Paul Arnold (arnoldp) Date: 2010-08-09 05:51
In Python 3.1, xdrlib.Packer().pack_fstring() throws a TypeError if called with a str() (an encoded string bytes() works just fine).

>>> xdrlib.Packer().pack_fstring(6, "foobar")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.1/xdrlib.py", line 81, in pack_fstring
    data = data + (n - len(data)) * b'\0'
TypeError: Can't convert 'bytes' object to str implicitly
msg113387 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-09 05:53
Thanks for the report. Can you tell if it applies to 3.2? Also, is the doc unclear on this point?
msg113388 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-09 05:54
Editing type. (FYI, you can follow the link to see some descriptions.)
msg113390 - (view) Author: Paul Arnold (arnoldp) Date: 2010-08-09 06:09
It will apply to 3.2 also, checking in SVN there have been no changes to xdrlib.py for a couple of years.

The documentation makes no mention of the need to encode strings.
msg132306 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-03-27 10:43
Here is a simple patch with a test. Depending o bytes in this library seems strange, maybe it should be changed somehow? Anyway, this simple patch should be a quick fix to the problem.
msg133700 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-04-13 22:05
This doesn't really make sense, since XDR doesn't seem to say anything about unicode strings, just bytes.
msg133719 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-04-14 10:19
Actually documentation doesn't say that it supports only bytes, but:

"The following methods support packing strings, bytes, and opaque data:"

Also under python2 you can easily do this:

In [1]: import xdrlib

In [2]: p = xdrlib.Packer()

In [3]: p.pack_string('some str')

In [4]: p.pack_string(u'some str')

So to conclude I believe either docs for python3 should be changed to say that only bytes are allowed or it should be changed to work as in python2. It's clear that in python2 some unicode string are accepted and I think we should allow such strings to be accepted in python3 too.
msg133735 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-04-14 13:03
2011/4/14 Filip Gruszczyński <report@bugs.python.org>:
>
> Filip Gruszczyński <gruszczy@gmail.com> added the comment:
>
> Actually documentation doesn't say that it supports only bytes, but:
>
> "The following methods support packing strings, bytes, and opaque data:"
>
> Also under python2 you can easily do this:
>
> In [1]: import xdrlib
>
> In [2]: p = xdrlib.Packer()
>
> In [3]: p.pack_string('some str')
>
> In [4]: p.pack_string(u'some str')

I doubt u"美", will, though.
msg133736 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-04-14 13:11
I am well aware of this, Benjamin. I am not trying to force any solution, but rather trying to point out, that current documentation is misleading. There might be no need for patching the code, but rather updating the docs to say, that only bytes are allowed and that will be sufficient.
msg133737 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-04-14 13:15
2011/4/14 Filip Gruszczyński <report@bugs.python.org>:
>
> Filip Gruszczyński <gruszczy@gmail.com> added the comment:
>
> I am well aware of this, Benjamin. I am not trying to force any solution, but rather trying to point out, that current documentation is misleading. There might be no need for patching the code, but rather updating the docs to say, that only bytes are allowed and that will be sufficient.

Yes, I think a doc patch is the correct thing to do.
msg133739 - (view) Author: Filip Gruszczyński (gruszczy) Date: 2011-04-14 13:22
I'll provide docs patch.
History
Date User Action Args
2011-04-14 13:22:05gruszczysetnosy: + docs@python
messages: + msg133739

assignee: docs@python
components: + Documentation, - None
2011-04-14 13:15:39benjamin.petersonsetmessages: + msg133737
2011-04-14 13:11:55gruszczysetmessages: + msg133736
2011-04-14 13:03:46benjamin.petersonsetmessages: + msg133735
2011-04-14 10:19:42gruszczysetmessages: + msg133719
2011-04-13 22:05:24benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg133700
2011-03-28 16:19:48gruszczysetnosy: + mark.dickinson
2011-03-27 10:43:15gruszczysetfiles: + 9544.patch

nosy: + gruszczy
messages: + msg132306

keywords: + patch
2010-08-09 06:09:12arnoldpsetmessages: + msg113390
2010-08-09 05:54:46eric.araujosettype: crash -> behavior
messages: + msg113388
2010-08-09 05:53:34eric.araujosetnosy: + eric.araujo
messages: + msg113387
2010-08-09 05:51:25arnoldpcreate