You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following the discussion from #1140 with @effigies , the Opener does not guarantee the file mode (or err) when a file handler is provided instead of a filename. E.g.:
For read/write, it seems to me that it is no-brainer- just check for the correct chars in the .mode and err when it is different. This change in the contract may require a DeprecationWarning.
Now for text/binary mode, let me know if that's too hacky: When opening in text mode on Py3, the file object has an attribute 'buffer' which is the same as opening the file in binary mode. So something like this would guarantee to always be binary (when requested + for the default mode):
classOpener(object):
def__init__(self, fileish, *args, **kwargs):
mode=kwargs['mode'] # all that kwargs processingifself._is_fileobj(fileish):
# binary mode is requested, but fileish mode is textif'b'inmodeand'b'notinfileish.mode:
self.fobj=fileish.bufferelse:
self.fobj=fileish
...
fd=open('file', 'r')
withOpener(fd, 'rb') asbigO:
assertbigO.read(5) =='hello'# b'hello' == b'hello'
however, if fd is binary mode and Opener requests text mode, probably a TextIOWrapper needs to be used to convert between modes.
The text was updated successfully, but these errors were encountered:
Hi @anibalsolon, just coming round to a bunch of nibabel issues. I would be up for making the deprecationwarning/futurewarning changes needed now to speed up the transition.
My concern with trying to change the mode is that we might get things other than regular filehandles that provide an IO interface. TextIOBase.buffer is considered an implementation detail and not an API, so I would be hesitant to count on it. Instinctively, I would be more inclined to just error if the mode doesn't match.
@anibalsolon I wonder if we should consider accepting fsspecs rather than working more on the Opener. I'm okay with fixing bugs when they arise, but if we're looking to engineer something that Does It Right, it might be wiser to transition to a tool that many other projects are depending on to Do It Right.
Following the discussion from #1140 with @effigies , the Opener does not guarantee the file mode (or err) when a file handler is provided instead of a filename. E.g.:
For read/write, it seems to me that it is no-brainer- just check for the correct chars in the
.mode
and err when it is different. This change in the contract may require a DeprecationWarning.Now for text/binary mode, let me know if that's too hacky: When opening in text mode on Py3, the file object has an attribute 'buffer' which is the same as opening the file in binary mode. So something like this would guarantee to always be binary (when requested + for the default mode):
however, if fd is binary mode and Opener requests text mode, probably a TextIOWrapper needs to be used to convert between modes.
The text was updated successfully, but these errors were encountered: