-
-
Notifications
You must be signed in to change notification settings - Fork 668
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.