Title: struct.unpack_from return 2 values instead of one
Type: behavior Stage: resolved
Components: Versions:
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: sebaleme, steven.daprano
Priority: normal Keywords:

Created on 2020-11-28 21:19 by sebaleme, last changed 2020-11-28 22:29 by steven.daprano. This issue is now closed.

File name Uploaded Description Edit
getIntensity.png sebaleme, 2020-11-28 21:19
Messages (3)
msg382017 - (view) Author: Sébastien Lemetter (sebaleme) Date: 2020-11-28 21:19

I am using an old version, but maybe you can still help me. I am using the ROS operating system, and there, the unpack_from function is used for reading values from bags.
When I try to read uint16 with the function pointcloud2.readpoint(), it returns 2 values instead of 1, and I couldn t understand why. Since this readpoint() is from the official pointcloud2 package from ros, and unpack_from is from official struct package, I don t think I could do many error here. In the screenshot, you can see that the inputs parameters seems ok. I am reading a field from my bag which is 2 bytes, and the offset (12) make also sense (4th value after 3 floats).

def read_points(cloud, field_names=None):


    Read points from a L{sensor_msgs.PointCloud2} message.


    @param cloud: The point cloud to read from.

    @type  cloud: L{sensor_msgs.PointCloud2}

    @param field_names: The names of fields to read. If None, read all fields. [default: None]

    @type  field_names: iterable

    @param skip_nans: If True, then don't return any point with a NaN value.

    @type  skip_nans: bool [default: False]

    @param uvs: If specified, then only return the points at the given coordinates. [default: empty list]

    @type  uvs: iterable

    @return: Generator which yields a list of values for each point.

    @rtype:  generator


    assert isinstance(cloud, roslib.message.Message) and cloud._type == 'sensor_msgs/PointCloud2', 'cloud is not a sensor_msgs.msg.PointCloud2'

    fmt = _get_struct_fmt(cloud.is_bigendian, cloud.fields, field_names)

    print('fmt size ' , str(struct.calcsize(fmt)))

    print('cloud.height ' , str(cloud.height))

    print('cloud.row_step ' , str(cloud.row_step))

    width, height, point_step, row_step, data, isnan = cloud.width, cloud.height, cloud.point_step, cloud.row_step,, math.isnan

    unpack_from = struct.Struct(fmt).unpack_from


    for v in range(height):

        offset = row_step * v

        for u in range(width):      

            yield unpack_from(data, offset)

            offset += point_step

def callback(data):

    print("pointcloud size is %d bytes" % len(

    pointCloudArray_a = [1,2,3,4,5]


    for indoux in range(4):



        pointCloudArray_a[indoux] = np.array(list((read_points2(data, field_names=(fields_PI6[indoux])))))

Here my mail again if you need more infos:

msg382018 - (view) Author: Sébastien Lemetter (sebaleme) Date: 2020-11-28 21:22
I forgot, here is my configuration:

Ubuntu 18.04
Ros Melodic
Python 2.7
msg382025 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-11-28 22:29
Hi Sébastien,

This is a bug tracker for reporting bugs, not a help desk to help with your code. There are many community resources to help you debug your code, but this is not one of them. You can try:

* Reddit's /r/learnpython
* the Python-list mailing list or news group or comp.lang.python
* The Python Discuss forums
* various IRC channels
* other forums

There may be others. However I guess you probably need ROS specific forums.

It may help to read these first:

If there is a problem with pointcloud2.readpoint returning two values when you expected one, you should check the pointcloud2 documentation, or the ROS project, and report the bug to them.

I'm going to close this as "Third Party", as any bug is most likely in the pointcloud2 library, or your own code.

Unfortunately, even if it turns out to be a bug in struct.unpack_from, although I don't understand why you think it might be, we aren't accepting any bug reports for Python 2, it is no longer maintained. Please upgrade to Python 3.9 and see if that fixes the problem.

Please note that your sample code gives a function "read_points" but the callback calls a different function "read_points2".
Date User Action Args
2020-11-28 22:29:42steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg382025

resolution: third party
stage: resolved
2020-11-28 21:22:13sebalemesetmessages: + msg382018
2020-11-28 21:19:50sebalemecreate