Message8649

Author jeff.allen
Recipients jamesls, jeff.allen, santa4nt, zyasoft
Date 2014-06-15.23:37:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1402875439.7.0.924863040382.issue2062@psf.upfronthosting.co.za>
In-reply-to
Content
Clean & polished fix, now in my local code base, behaves like this:
>>> import os
>>> s = "This is a test."
>>> m = memoryview(s)
>>> fd = os.open("x.tmp", os.O_WRONLY | os.O_CREAT)
>>> os.write(fd, s)
15
>>> os.write(fd, m)
15
>>> os.write(fd, bytearray(s))
15
>>> os.write(fd, buffer(s))
15
>>> os.write(fd, 1.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write(): 2nd arg can't be coerced to org.python.core.BufferProtocol
>>> os.write(fd, m[1::2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BufferError: underlying buffer is not C-contiguous
>>> os.close(fd)
>>> os.write(fd, s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor 

I was surprised that I could declare this simply as:
public static int write(PyObject fd, BufferProtocol bytes)
and let Jython take care of the type test. (Indra Talip did this in binascii.) Previously I've accepted a PyObject, then tested it with instanceof. It means the error message is not quite the same as CPython when you hand it, say, a float in place of bytes, but the code is simpler.
History
Date User Action Args
2014-06-15 23:37:19jeff.allensetmessageid: <1402875439.7.0.924863040382.issue2062@psf.upfronthosting.co.za>
2014-06-15 23:37:19jeff.allensetrecipients: + jeff.allen, zyasoft, santa4nt, jamesls
2014-06-15 23:37:19jeff.allenlinkissue2062 messages
2014-06-15 23:37:19jeff.allencreate