From f0e3ea24be216bd98e8f2b75d722565a68461fb8 Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Fri, 20 Dec 2019 14:58:53 +1100
Subject: [PATCH] Fix T72413: FBX import error on missing files

While it's an error case, the ascii detection caused
the missing file case to raise a full exception before running
code which handles this case more gracefully.
---
 io_scene_fbx/import_fbx.py | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py
index 751a2b177..32b887c1d 100644
--- a/io_scene_fbx/import_fbx.py
+++ b/io_scene_fbx/import_fbx.py
@@ -2307,17 +2307,6 @@ class FbxImportHelperNode:
             return None
 
 
-def is_ascii(filepath, size):
-    with open(filepath, 'r', encoding="utf-8") as f:
-        try:
-            f.read(size)
-            return True
-        except UnicodeDecodeError:
-            pass
-
-    return False
-
-
 def load(operator, context, filepath="",
          use_manual_orientation=False,
          axis_forward='-Z',
@@ -2358,10 +2347,24 @@ def load(operator, context, filepath="",
     perfmon.step("FBX Import: start importing %s" % filepath)
     perfmon.level_up()
 
-    # detect ascii files
-    if is_ascii(filepath, 24):
+    # Detect ASCII files.
+
+    # Typically it's bad practice to fail silently on any error,
+    # however the file may fail to read for many reasons,
+    # and this situation is handled later in the code,
+    # right now we only want to know if the file successfully reads as ascii.
+    try:
+        with open(filepath, 'r', encoding="utf-8") as fh:
+            fh.read(24)
+        is_ascii = True
+    except Exception:
+        is_ascii = False
+
+    if is_ascii:
         operator.report({'ERROR'}, "ASCII FBX files are not supported %r" % filepath)
         return {'CANCELLED'}
+    del is_ascii
+    # End ascii detection.
 
     try:
         elem_root, version = parse_fbx.parse(filepath)
-- 
GitLab