[Bf-extensions-cvs] [f0e3ea24] master: Fix T72413: FBX import error on missing files

Campbell Barton noreply at git.blender.org
Fri Dec 20 05:01:20 CET 2019


Commit: f0e3ea24be216bd98e8f2b75d722565a68461fb8
Author: Campbell Barton
Date:   Fri Dec 20 14:58:53 2019 +1100
Branches: master
https://developer.blender.org/rBAf0e3ea24be216bd98e8f2b75d722565a68461fb8

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.

===================================================================

M	io_scene_fbx/import_fbx.py

===================================================================

diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py
index 751a2b17..32b887c1 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)



More information about the Bf-extensions-cvs mailing list