Skip to content

Box2i binding rejects tuples that contain NumPy scalar values #2133

@markdjwilliams

Description

@markdjwilliams

Passing a dataWindow as a tuple of tuples that contain NumPy scalar values causes the OpenEXR Python binding to reject the Box2i, even though pure Python ints or NumPy arrays work.

import numpy as np
import OpenEXR


def test_box2i(dataWindow):
    w = dataWindow[1][0] - dataWindow[0][0]
    h = dataWindow[1][1] - dataWindow[0][1]
    parts = []
    part = OpenEXR.Part(
        header={
            "type": OpenEXR.scanlineimage,
            "dataWindow": dataWindow,
        },
        channels={"RGB": np.zeros((h, w, 3), dtype=np.float16)},
    )
    parts.append(part)

    f = OpenEXR.File(parts)
    try:
        f.write("out.exr")
    except Exception as e:
        raise RuntimeError(f"Failed for {dataWindow}") from e

mn = np.asarray((0, 0), dtype=np.int32)
mx = np.asarray((15, 15), dtype=np.int32)

test_box2i(((0, 0), (15, 15)))
test_box2i((mn, mx))
test_box2i(((mn[0], mn[1]), (mx[0], mx[1]))) # Fails

The above script raises an exception on the final line:

ValueError: invalid value for attribute 'dataWindow': expected a box2i tuple, got ((0, 0), (15, 15))

I suspect using py::cast<> over py::isinstance<> would allow for greater flexibility, for example:

if (tup.size() == 2)
{
    try
    {
        v.x = py::cast<T>(tup[0]);
        v.y = py::cast<T>(tup[1]);
        return true;
    }
    catch (const py::cast_error&) {
        return false;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions