From 3cd626101919ec944eedcd72a07d062d1c87ab0e Mon Sep 17 00:00:00 2001 From: Adrian pfleiderer Date: Wed, 10 Jan 2024 13:00:51 +0100 Subject: [PATCH 1/2] BUG: check if input and output path contains unsupported special characters --- .../Generators/Python/itk/support/extras.py | 18 ++++++++++++++++++ .../Python/itk/support/template_class.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/Wrapping/Generators/Python/itk/support/extras.py b/Wrapping/Generators/Python/itk/support/extras.py index fae908161db..d1e95325313 100644 --- a/Wrapping/Generators/Python/itk/support/extras.py +++ b/Wrapping/Generators/Python/itk/support/extras.py @@ -1165,6 +1165,24 @@ def imwrite( The writer is instantiated with the image type of the image in parameter (or, again, with the output image of the filter in parameter). """ + + # Check if output path exists + msg = "" + if not os.path.isdir(os.path.dirname(filename)): + msg += "\nThe output dir doesn't exist. \n" + f"Filename = {filename}" + raise RuntimeError( + f"Could not create IO object for writing file {filename}" + msg + ) + # Check if path contains unsupported special characters + else: + try: + filename.encode('ascii') + except UnicodeEncodeError: + msg += "\nThe output path contains not supported special characters. \n" + f"Filename = {filename}" + raise RuntimeError( + f"Could not create IO object for writing file {filename}" + msg + ) + import itk img = itk.output(image_or_filter) diff --git a/Wrapping/Generators/Python/itk/support/template_class.py b/Wrapping/Generators/Python/itk/support/template_class.py index 9601df7976c..717e4a2e93d 100644 --- a/Wrapping/Generators/Python/itk/support/template_class.py +++ b/Wrapping/Generators/Python/itk/support/template_class.py @@ -27,6 +27,7 @@ import itkConfig + from itk.support import base from itk.support.extras import output from itk.support.types import itkCType @@ -157,6 +158,12 @@ def firstIfList(arg): msg = "" if not os.path.isfile(inputFileName): msg += "\nThe file doesn't exist. \n" + f"Filename = {inputFileName}" + # Check if image path contains not supported special characters. + else: + try: + inputFileName.encode('ascii') + except UnicodeEncodeError: + msg += "\nThe image path contains not supported special characters. \n" + f"Filename = {inputFileName}" raise RuntimeError( f"Could not create IO object for reading file {inputFileName}" + msg ) From 73042677988a95e3c68ff6aa6b33d87464d841fa Mon Sep 17 00:00:00 2001 From: Adrian pfleiderer Date: Wed, 10 Jan 2024 13:09:17 +0100 Subject: [PATCH 2/2] BUG: delete empty line --- Wrapping/Generators/Python/itk/support/template_class.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Wrapping/Generators/Python/itk/support/template_class.py b/Wrapping/Generators/Python/itk/support/template_class.py index 717e4a2e93d..cda7d2f8a64 100644 --- a/Wrapping/Generators/Python/itk/support/template_class.py +++ b/Wrapping/Generators/Python/itk/support/template_class.py @@ -27,7 +27,6 @@ import itkConfig - from itk.support import base from itk.support.extras import output from itk.support.types import itkCType