Skip to content

Commit

Permalink
Python bindings: raise proper exceptions for IO operations (#1313)
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso authored Jun 7, 2022
1 parent 9e904dc commit d9f3e4d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ ErrorStatusHandler::~ErrorStatusHandler() noexcept(false) {
case ErrorStatus::JSON_PARSE_ERROR:
throw py::value_error("JSON parse error while reading: " + details());
case ErrorStatus::FILE_OPEN_FAILED:
throw py::value_error("failed to open file for reading: " + details());
PyErr_SetFromErrnoWithFilename(PyExc_OSError, details().c_str());
throw py::error_already_set();
case ErrorStatus::FILE_WRITE_FAILED:
throw py::value_error("failed to open file for writing: " + details());
PyErr_SetFromErrnoWithFilename(PyExc_OSError, details().c_str());
throw py::error_already_set();
case ErrorStatus::SCHEMA_VERSION_UNSUPPORTED:
throw _UnsupportedSchemaException(full_details());
case ErrorStatus::NOT_A_CHILD_OF:
Expand Down
52 changes: 52 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project

import sys
import shutil
import tempfile
import unittest

import opentimelineio as otio


class TestCoreFunctions(unittest.TestCase):
def setUp(self):
self.tmpDir = tempfile.mkdtemp()

def tearDown(self):
shutil.rmtree(self.tmpDir)

def test_deserialize_json_from_file_errors(self):
"""Verify that the bindings return the correct errors based on the errno"""
if sys.version_info[0] < 3:
excType = OSError
else:
excType = FileNotFoundError # noqa: F821

with self.assertRaises(excType) as exc:
otio.core.deserialize_json_from_file('non-existent-file-here')
self.assertIsInstance(exc.exception, excType)

@unittest.skipUnless(not sys.platform.startswith("win"), "requires non Windows sytem") # noqa
def test_serialize_json_to_file_errors_non_windows(self):
"""Verify that the bindings return the correct errors based on the errno"""
if sys.version_info[0] < 3:
excType = OSError
else:
excType = IsADirectoryError # noqa: F821

with self.assertRaises(excType) as exc:
otio.core.serialize_json_to_file({}, self.tmpDir)
self.assertIsInstance(exc.exception, excType)

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_serialize_json_to_file_errors_windows(self):
"""Verify that the bindings return the correct errors based on the errno"""
if sys.version_info[0] < 3:
excType = OSError
else:
excType = PermissionError # noqa: F821

with self.assertRaises(excType) as exc:
otio.core.serialize_json_to_file({}, self.tmpDir)
self.assertIsInstance(exc.exception, excType)

0 comments on commit d9f3e4d

Please sign in to comment.