OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PyOV] Raise TypeError for unsupported Tensor input without Pillow (#38955)
Oke Iyanuoluwa Enoch19d2f1082c85
diff --git a/src/bindings/python/src/pyopenvino/core/tensor.cpp b/src/bindings/python/src/pyopenvino/core/tensor.cppindex 925c326714f..ada8710fca7 100644--- a/src/bindings/python/src/pyopenvino/core/tensor.cpp+++ b/src/bindings/python/src/pyopenvino/core/tensor.cpp@@ -216,7 +216,16 @@ void regclass_Tensor(py::module m) {py::arg("end"));cls.def(py::init([](py::object& image) {- if (!py::isinstance(image, py::module::import("PIL.Image").attr("Image"))) {+ // Pillow is optional, without it no object can be a Pillow Image.+ py::object pil_image_class;+ try {+ pil_image_class = py::module::import("PIL.Image").attr("Image");+ } catch (py::error_already_set& e) {+ if (!e.matches(PyExc_ImportError)) {+ throw;+ }+ }+ if (!pil_image_class || !py::isinstance(image, pil_image_class)) {throw py::type_error("Input argument must be a PIL.Image.Image/numpy.array/list[int, float, str] object");}diff --git a/src/bindings/python/tests/test_runtime/test_tensor.py b/src/bindings/python/tests/test_runtime/test_tensor.pyindex 00de7a942c2..adccf994a04 100644--- a/src/bindings/python/tests/test_runtime/test_tensor.py+++ b/src/bindings/python/tests/test_runtime/test_tensor.py@@ -684,6 +684,18 @@ def test_tensor_from_pillow(numpy_dtype, shape):assert tensor.data.shape == shape[email protected]("pillow_available", [True, False])[email protected]("value", [object(), np.float32(1.0), {"a": 1}])+def test_tensor_from_unsupported_object(monkeypatch, pillow_available, value):+ if not pillow_available:+ # Simulate an environment without Pillow, which is not a runtime dependency+ monkeypatch.setitem(sys.modules, "PIL", None)+ monkeypatch.setitem(sys.modules, "PIL.Image", None)++ with pytest.raises(TypeError, match="Input argument must be a PIL.Image.Image"):+ ov.Tensor(value)++def test_pack_unpack_u3_numerical():"""Test u3 packing/unpacking with known numerical values."""# 8 values packed into 3 bytes, linear LSB-first bit-stream (value i at bits [3i, 3i+3))