Issue2062

classification
Title: bytearray not accepted for os.write
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jamesls, jeff.allen, santa4nt
Priority: Keywords: patch

Created on 2013-06-18.04:04:14 by jamesls, last changed 2013-12-14.22:40:17 by jeff.allen.

Files
File name Uploaded Description Edit Remove
os_write_bytearray.diff santa4nt, 2013-06-18.21:42:04 Adding an overloaded write() method that takes bytearray.
Messages
msg8048 (view) Author: James (jamesls) Date: 2013-06-18.04:04:13
Using the latest jython2.7, I'm not able to use bytearray with os.write.
In CPython2.7.5 I get:

>>> import os
>>> fd = os.open('/tmp/foo', os.O_RDWR)
>>> os.write(fd, bytearray('foobar'))
6

From jython 2.7b1 I get:

>>> import os
>>> fd = os.open('/tmp/foo', os.O_RDWR)
>>> os.write(fd, bytearray('foobar'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write(): 2nd arg can't be coerced to String
msg8049 (view) Author: Santoso Wijaya (santa4nt) Date: 2013-06-18.21:42:04
A quick-n-dirty fix with adding an overloaded method.
msg8198 (view) Author: Jeff Allen (jeff.allen) Date: 2013-12-14.22:40:17
A complete solution, here and many other places where a String argument really means bytes, is to accept any PyObject supporting the Buffer API. Then this would work as it does on CPython:

>>> fd = os.open('x.tmp', os.O_CREAT | os.O_RDWR)
>>> a = bytearray("Stuff and nonsense. ")
>>> os.write(fd, a)
20
>>> os.write(fd, buffer(a))
20
>>> os.write(fd, memoryview(a))
20

It would be cool to find some automagic implementation, but I think it has to be a ByteUtil class (or some such), offering few well-crafted static methods, and a re-write of these methods one by one. Asking a buffer for its String is least work but goes completely against the idea of the buffer API as efficient in-place access, unless it's really a PyString after all.
History
Date User Action Args
2013-12-14 22:40:17jeff.allensetnosy: + jeff.allen
messages: + msg8198
2013-06-18 21:42:04santa4ntsetfiles: + os_write_bytearray.diff
keywords: + patch
messages: + msg8049
nosy: + santa4nt
2013-06-18 04:04:14jameslscreate