-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
size() does not work in Python 2 in a KaitaiStream backed by a 'file' object #72
Comments
A non-technical solution to this would be to end support for Python 2 (it was EOLed nearly three years ago). |
Yes, that is a valid solution. Frankly, I don't know how much (if any) interest there is among Python users of Kaitai Struct to keep support for Python 2. From https://pypistats.org/packages/kaitaistruct, the scarce units of downloads on Python 2 look more like accidental downloads, far from any serious use. It's possible that the effort we put into keeping Python 2 from extinction isn't appreciated by anyone and doesn't have any real reason. |
None of my own projects have ever supported python 2, since when started using it, 3 was already enough to cover all my needs (and in rare cases of python 2-only code 2to3 worked mostly fine (soketimes small fixes were required) ). Native modules though an issue. For example I have ported +1 for dropping 2, but also +1 for keeping its support, if there was be a person who would fix the broken compat when it happens. I mean that maintaining compat to python 2 in newly-introduced code (under this I mean it should not be a war of changes, so intentionally dropping compat is not allowed, but introducing new code written the way not taking into account the case of python 2 is allowed) should not be a req for accepting PRs, but also that PRs fixing the compat are welcome too. |
I see that my memory is a bit deceptive (it wasn't "in almost all tests") - I've just checked out the CI logs and the only test that was failing due to this was "ParamsDef": {
"status": "failed",
"elapsed": 0.001,
"failure": {
"file_name": null,
"line_num": null,
"message": "'file' object has no attribute 'seekable'",
"trace": "Traceback (most recent call last):\n File \"/home/travis/build/kaitai-io/ci_targets/tests/spec/python/test_params_def.py\", line 9, in test_params_def\n r = ParamsDef(5, True, io, None, None)\n File \"/home/travis/build/kaitai-io/ci_targets/tests/compiled/python/params_def.py\", line 17, in __init__\n self._read()\n File \"/home/travis/build/kaitai-io/ci_targets/tests/compiled/python/params_def.py\", line 20, in _read\n self.buf = (self._io.read_bytes(self.len)).decode(u\"UTF-8\")\n File \"/home/travis/build/kaitai-io/ci_targets/runtime/python/kaitaistruct.py\", line 303, in read_bytes\n if self._io.seekable():\nAttributeError: 'file' object has no attribute 'seekable'"
}
}, And it was exactly because the class TestParamsDef(unittest.TestCase):
def test_params_def(self):
io = KaitaiStream(open("src/term_strz.bin", "rb"))
r = ParamsDef(5, True, io, None, None)
self.assertEqual(r.buf, "foo|b")
self.assertEqual(r.trailer, 0x61)
io.close() |
As @armijnhemel suggested in #61 (comment), in Python 3 the
io.tell()
is redundant:kaitai_struct_python_runtime/kaitaistruct.py
Lines 102 to 105 in 486e9ab
and can be simplified to this (because
io.IOBase.seek
returns the new absolute position already):kaitai_struct_python_runtime/kaitaistruct.py
Lines 102 to 103 in 255f5b7
I applied this suggestion in 255f5b7. This has been released in 0.10.
However, I was reading through some old discussions and came across this comment by @arekbulski:
I tried this in my Python 2.7 installation and indeed - if you open a file using the built-in
open()
function, you get a 'file' object, and if you use it to initialize theKaitaiStream
, thesize()
returnsNone
:This is documented in Python 2 docs (https://docs.python.org/2/library/stdtypes.html#file.seek):
On the other hand, the typical
from_file
helper works because it usesio.open()
(as recommended in https://docs.python.org/3/howto/pyporting.html#text-versus-binary-data since it's consistent from Python 2 to 3) instead ofopen()
:I'm not sure why none of the tests caught this - yes, they typically use the
from_file
helper, but I remember that theread_bytes
method was raising errors in almost all tests (IIRC) in the CI when I tried calling_io.seekable()
unconditionally:kaitai_struct_python_runtime/kaitaistruct.py
Lines 311 to 314 in 4accba9
and that error clearly mentioned the 'file' object.
The text was updated successfully, but these errors were encountered: