From ea96d853cd26c52bccff139514cde19198f2b696 Mon Sep 17 00:00:00 2001 From: Roy-043 Date: Thu, 29 Aug 2024 19:18:35 +0200 Subject: [PATCH] BIM: Fix Python 3.12 run time error with importers Fixes #16137 Similar to this fix: https://github.com/FreeCAD/FreeCAD/pull/13337#discussion_r1564806896 --- src/Mod/BIM/importers/exportIFC.py | 5 +---- src/Mod/BIM/importers/importIFC.py | 4 ---- src/Mod/BIM/importers/importIFClegacy.py | 4 +--- src/Mod/BIM/importers/importJSON.py | 7 ++----- src/Mod/BIM/importers/importOBJ.py | 11 ++++------- src/Mod/BIM/importers/importSH3D.py | 4 +--- src/Mod/BIM/importers/importSHP.py | 7 +++---- src/Mod/BIM/importers/importWebGL.py | 5 ++--- 8 files changed, 14 insertions(+), 33 deletions(-) diff --git a/src/Mod/BIM/importers/exportIFC.py b/src/Mod/BIM/importers/exportIFC.py index d5b890c442..d92466ad36 100644 --- a/src/Mod/BIM/importers/exportIFC.py +++ b/src/Mod/BIM/importers/exportIFC.py @@ -32,6 +32,7 @@ import os import time import tempfile import math +from builtins import open as pyopen import FreeCAD import Part @@ -54,10 +55,6 @@ __title__ = "FreeCAD IFC export" __author__ = ("Yorik van Havre", "Jonathan Wiedemann", "Bernd Hahnebach") __url__ = "https://www.freecad.org" -# Save the Python open function because it will be redefined -if open.__module__ in ['__builtin__', 'io']: - pyopen = open - # Templates and other definitions **** # Specific FreeCAD <-> IFC slang translations translationtable = { diff --git a/src/Mod/BIM/importers/importIFC.py b/src/Mod/BIM/importers/importIFC.py index 4f6b8b6a21..e6ad2a94ba 100644 --- a/src/Mod/BIM/importers/importIFC.py +++ b/src/Mod/BIM/importers/importIFC.py @@ -55,10 +55,6 @@ __url__ = "https://www.freecad.org" DEBUG = False # Set to True to see debug messages. Otherwise, totally silent ZOOMOUT = True # Set to False to not zoom extents after import -# Save the Python open function because it will be redefined -if open.__module__ in ['__builtin__', 'io']: - pyopen = open - # Templates and other definitions **** # which IFC type must create which FreeCAD type diff --git a/src/Mod/BIM/importers/importIFClegacy.py b/src/Mod/BIM/importers/importIFClegacy.py index ef398cf7d2..5b4c452463 100644 --- a/src/Mod/BIM/importers/importIFClegacy.py +++ b/src/Mod/BIM/importers/importIFClegacy.py @@ -27,6 +27,7 @@ import FreeCAD, Arch, Draft, os, sys, time, Part, DraftVecUtils, uuid, math, re +from builtins import open as pyopen from draftutils import params from draftutils.translate import translate @@ -54,9 +55,6 @@ supportedIfcTypes = ["IfcSite", "IfcBuilding", "IfcBuildingStorey", "IfcBeam", " "IfcPile", "IfcFooting", "IfcReinforcingBar", "IfcTendon"] # TODO : shading device not supported? -if open.__module__ in ['__builtin__','io']: - pyopen = open # because we'll redefine open below - def open(filename,skip=None): "called when freecad opens a file" docname = os.path.splitext(os.path.basename(filename))[0] diff --git a/src/Mod/BIM/importers/importJSON.py b/src/Mod/BIM/importers/importJSON.py index 32133898f9..74e6e27aff 100644 --- a/src/Mod/BIM/importers/importJSON.py +++ b/src/Mod/BIM/importers/importJSON.py @@ -22,6 +22,7 @@ """FreeCAD JSON exporter""" import json +from builtins import open as pyopen import FreeCAD import Draft @@ -37,10 +38,6 @@ else: def translate(ctxt, txt): return txt -if open.__module__ in ['__builtin__','io']: - pythonopen = open - - def export(exportList, filename): "exports the given objects to a .json file" @@ -52,7 +49,7 @@ def export(exportList, filename): } # Write file - outfile = pythonopen(filename, "w") + outfile = pyopen(filename, "w") json.dump(data, outfile, separators = (',', ':')) outfile.close() diff --git a/src/Mod/BIM/importers/importOBJ.py b/src/Mod/BIM/importers/importOBJ.py index 022790b9b4..214eeced36 100644 --- a/src/Mod/BIM/importers/importOBJ.py +++ b/src/Mod/BIM/importers/importOBJ.py @@ -22,7 +22,7 @@ import os import codecs import ntpath -# import numpy as np +from builtins import open as pyopen import FreeCAD import Arch @@ -50,9 +50,6 @@ else: # and supports exporting faces with more than 3 vertices # and supports object colors / materials -if open.__module__ in ['__builtin__','io']: - pythonopen = open - def findVert(aVertex,aList): "finds aVertex in aList, returns index" p = Draft.precision() @@ -260,7 +257,7 @@ def export(exportList,filename,colors=None): outfile.close() FreeCAD.Console.PrintMessage(translate("Arch","Successfully written") + " " + filename + "\n") if materials: - outfile = pythonopen(filenamemtl,"w") + outfile = pyopen(filenamemtl,"w") outfile.write("# FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + " Arch module\n") outfile.write("# https://www.freecad.org\n") kinds = {"AmbientColor":"Ka ","DiffuseColor":"Kd ","SpecularColor":"Ks ","EmissiveColor":"Ke ","Transparency":"Tr ","Dissolve":"d "} @@ -306,7 +303,7 @@ def insert(filename,docname): doc = FreeCAD.newDocument(docname) FreeCAD.ActiveDocument = doc - with pythonopen(filename,"r",encoding="utf8") as infile: + with pyopen(filename,"r",encoding="utf8") as infile: verts = [] facets = [] activeobject = None @@ -327,7 +324,7 @@ def insert(filename,docname): if line[:7] == "mtllib ": matlib = os.path.join(os.path.dirname(filename),line[7:]) if os.path.exists(matlib): - with pythonopen(matlib,"r") as matfile: + with pyopen(matlib,"r") as matfile: mname = None color = None trans = None diff --git a/src/Mod/BIM/importers/importSH3D.py b/src/Mod/BIM/importers/importSH3D.py index 6ae6f9c25e..01f8ef282b 100644 --- a/src/Mod/BIM/importers/importSH3D.py +++ b/src/Mod/BIM/importers/importSH3D.py @@ -28,6 +28,7 @@ import os import tempfile import xml.sax import zipfile +from builtins import open as pyopen import FreeCAD import Arch @@ -43,9 +44,6 @@ import Part DEBUG = True -if open.__module__ in ['__builtin__','io']: - pyopen = open # because we'll redefine open below - def open(filename): "called when freecad wants to open a file" diff --git a/src/Mod/BIM/importers/importSHP.py b/src/Mod/BIM/importers/importSHP.py index 2fcb54416f..1ead995ccf 100644 --- a/src/Mod/BIM/importers/importSHP.py +++ b/src/Mod/BIM/importers/importSHP.py @@ -21,12 +21,11 @@ #*************************************************************************** import os +from builtins import open as pyopen + import FreeCAD translate = FreeCAD.Qt.translate -if open.__module__ in ['__builtin__','io']: - pythonopen = open - def open(filename): """opens a SHP/SHX/DBF file in a new FreeCAD document""" @@ -142,7 +141,7 @@ def checkShapeFileLibrary(): return False b = u.read() fp = os.path.join(FreeCAD.getUserMacroDir(True),"shapefile.py") - f = pythonopen(fp,"wb") + f = pyopen(fp,"wb") f.write(b) f.close() try: diff --git a/src/Mod/BIM/importers/importWebGL.py b/src/Mod/BIM/importers/importWebGL.py index c0c0b2df3d..2a596e24f1 100644 --- a/src/Mod/BIM/importers/importWebGL.py +++ b/src/Mod/BIM/importers/importWebGL.py @@ -44,6 +44,7 @@ import Part import OfflineRenderingUtils import json import textwrap +from builtins import open as pyopen if FreeCAD.GuiUp: import FreeCADGui @@ -52,8 +53,6 @@ else: FreeCADGui = None def translate(ctxt, txt): return txt -if open.__module__ in ['__builtin__','io']: pythonopen = open - ## @package importWebGL # \ingroup ARCH # \brief FreeCAD WebGL Exporter @@ -868,7 +867,7 @@ def export( exportList, filename, colors = None, camera = None ): html = html.replace('$data', json.dumps(data, separators=(',', ':')) ) # Shape Data - outfile = pythonopen(filename, "w") + outfile = pyopen(filename, "w") outfile.write( html ) outfile.close() FreeCAD.Console.PrintMessage( translate("Arch", "Successfully written") + ' ' + filename + "\n" )