Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for unsupported characters within image path and output path #4390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a useful check to occur in the C++ code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I assume that performing an equivalent check in C++ would be a lot harder. If the author has the will to explore this, it would be good.

# 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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not supported -> unsupported

Perhaps give user a hint about setting UTF-8 as the default code page?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to place this check into a function.

raise RuntimeError(
f"Could not create IO object for writing file {filename}" + msg
)

import itk

img = itk.output(image_or_filter)
Expand Down
6 changes: 6 additions & 0 deletions Wrapping/Generators/Python/itk/support/template_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferably give a hint about UTF8 code page.

raise RuntimeError(
f"Could not create IO object for reading file {inputFileName}" + msg
)
Expand Down