v4l2 throws a TypeError when imported in Python 3.5

Bug #1664158 reported by ronbarak
32
This bug affects 7 people
Affects Status Importance Assigned to Milestone
python-v4l2
New
Undecided
Unassigned

Bug Description

When importing v4l2, I get:

openstack@prclnx04:~/python/v4l2$ python3 -c "import v4l2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/v4l2.py", line 197, in <module>
    ) = range(1, 9) + [0x80]
TypeError: unsupported operand type(s) for +: 'range' and 'list'

And the relevant lines in /usr/local/lib/python3.5/dist-packages/v4l2.py are:

openstack@prclnx04:~/python/v4l2$ cat /usr/local/lib/python3.5/dist-packages/v4l2.py | head -197 | tail -12
v4l2_buf_type = enum
(
    V4L2_BUF_TYPE_VIDEO_CAPTURE,
    V4L2_BUF_TYPE_VIDEO_OUTPUT,
    V4L2_BUF_TYPE_VIDEO_OVERLAY,
    V4L2_BUF_TYPE_VBI_CAPTURE,
    V4L2_BUF_TYPE_VBI_OUTPUT,
    V4L2_BUF_TYPE_SLICED_VBI_CAPTURE,
    V4L2_BUF_TYPE_SLICED_VBI_OUTPUT,
    V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY,
    V4L2_BUF_TYPE_PRIVATE,
) = range(1, 9) + [0x80]

---------------------------------
Notes:
1. vl42 was installed using:

openstack@prclnx04:~/python/v4l2$ sudo -H python3 -m pip install v4l2 --upgrade
Requirement already up-to-date: v4l2 in /usr/local/lib/python3.5/dist-packages

2. I use Python3.5 on Ubuntu 16.04

Related branches

Revision history for this message
ronbarak (p-launchpad-comverse) wrote :

If I change the following two lines:

openstack@prclnx04:~/python/v4l2$ grep -n "i in range" "/usr/local/lib/python3.5/dist-packages/v4l2.py" | grep range
197:) = [i+0x80 for i in range(1, 10)]
249:) = [i+2 for i in range(0, 5)]

The import is successful.

Revision history for this message
ash (sersorrel) wrote :

That's probably not the correct fix: `range(...)` in Python 2 is equivalent to `list(range(...))` in Python 3, so those lines should probably be

    ) = list(range(1, 10)) + [0x80]

and

    ) = list(range(0, 5)) + [2]

instead.

Revision history for this message
Justin Gottula (jgottula) wrote :

Both of the fixes proposed in the comments above are not *quite* right.

------------------------------------------------------------

ronbarak's proposal in #1 results in the wrong values for the enumerations, because the `+ [0x80]` and `+ [2]` are not modifiers that affect all of the enum values; but rather, a way of separately setting the last member of the enum to a different (non-sequential) value.

- For `v4l2_buf_type`, the enum values go sequentially from 1 to 8, and then the last enum member, `V4L2_BUF_TYPE_PRIVATE`, is separately set to 0x80.

- For `v4l2_priority`, the enum values go sequentially from 0 to 3, and then the last enum member, `V4L2_PRIORITY_DEFAULT`, is separately set to 2 (the same value as `V4L2_PRIORITY_INTERACTIVE`).

------------------------------------------------------------

The following links to the V4L2 C headers should help clarify the situation.

- `v4l2_buf_type`: https://git.linuxtv.org/v4l-utils.git/tree/include/linux/videodev2.h?id=v4l-utils-1.14.2#n132

- `v4l2_priority`: https://git.linuxtv.org/v4l-utils.git/tree/include/linux/videodev2.h?id=v4l-utils-1.14.2#n381

------------------------------------------------------------

Josh Holland's comment (#2) is on the mark WRT the correct way to fix the core Python 2+3 compatibility issue; but the code lines provided appear to have re-used the wrong `start` and `stop` values for the `range()` statements (taken from comment #1).

So rather than

  ) = list(range(1, 10)) + [0x80]

the `v4l2_buf_type` line ought to be

  ) = list(range(1, 9)) + [0x80]

instead; and rather than

  ) = list(range(0, 5)) + [2]

the `v4l2_priority` line ought to be

  ) = list(range(0, 4)) + [2]

instead.

------------------------------------------------------------

I've attached a patch with a proper working fix. I've tested it and ensured that it works without error on both Python 2 and Python 3, and I've verified that the enumeration values are correct.

Here's a branch that I created with the fix integrated into it, over a month ago:
https://code.launchpad.net/~jgottula/python-v4l2/fix-for-bug-1664158

I've submitted requests for that branch to be reviewed and merged by the python-v4l2 developers *twice* in the past couple of months; there has been no response or action whatsoever. This, combined with the fact that this trivial bug report has been open (in "new" status, with tumbleweeds blowing by) for over a year now, and the fact that this repository has apparently had no commits in the past ~8 years (the last was on 2010-07-21), leads me to wonder whether the developers in charge of this project/repository are deceased or otherwise incapacitated in some manner...

Revision history for this message
Maxime Kawawa-Beaudan (maximejkb) wrote :

Hi Justin,

Thank you for that patch. I'm using it for a project with an Omega Onion 2+, a single-board computer similar to a Raspberry Pi, which has an attached USB camera. After downloading your patch, I'm able to build the module and import v4l2 at the top of my Python 3.6.0 file. Now I have another problem, and I'm not sure it's related to this patch, but this is the first and only version of the Python V4L2 API I've used, so I figured I'd ask here. I'm trying to run the basic sample code from the V4L2 project description:

>>> import v4l2
>>> import fcntl
>>> vd = open('/dev/video0', 'rw')
>>> cp = v4l2.v4l2_capability()
>>> fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)

I've tried running this both from the REPL and as a file, and whatever I do, I get the error:

Traceback (most recent call last):
  File "Camera.py", line 7, in <module>
    fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)
OSError: [Errno 25] Not a tty

I'm able to open /dev/video0, and I'm able to query the camera for an image with fswebcam, for example. I've also been using V4L2 up until now, just not in Python -- I was using V4L2 to change the camera's exposure and other settings, and using mjpg-streamer to capture still images. I want to be able to capture still images without the latency from mjpg-streamer and more reliably. The camera responds to V4L2, as v4l2-ctl --list-devices yields:

HD USB Camera (usb-101c0000.ehci-1):
 /dev/video0

Any help would be much appreciated.

Revision history for this message
John McMaster (johndmcmaster) wrote :

It sounds like there were a few proposals but they were not merged in? IMHO it would be better to have one of these sub-optimal patches rather than no action. Please let me know if I can do something to get this fixed. Thanks!

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.