|
| 1 | +Class IOP.Wrapper Extends %RegisteredObject |
| 2 | +{ |
| 3 | + |
| 4 | +ClassMethod Import( |
| 5 | + moduleName As %String, |
| 6 | + path As %String = "", |
| 7 | + debugPort As %Integer = 0) As %Status |
| 8 | +{ |
| 9 | + set tSC = $$$OK |
| 10 | + Try { |
| 11 | + do ##class(IOP.Common).SetPythonPath(path) |
| 12 | + // For traceback debugging |
| 13 | + do $system.Python.Debugging(1) |
| 14 | + if debugPort > 0 { |
| 15 | + |
| 16 | + set debugpy = ##class(%SYS.Python).Import("iop._debugpy") |
| 17 | + do debugpy."debugpy_in_iris"($zu(12),debugPort) |
| 18 | + } |
| 19 | + |
| 20 | + // Import the module |
| 21 | + set tModule = ##class(%SYS.Python).Import(moduleName) |
| 22 | + |
| 23 | + } |
| 24 | + Catch ex { |
| 25 | + Set tSC= ##class(IOP.Wrapper).DisplayTraceback(ex) |
| 26 | + throw ex |
| 27 | + } |
| 28 | + return tModule |
| 29 | +} |
| 30 | + |
| 31 | +ClassMethod DisplayTraceback(ex) As %Status |
| 32 | +{ |
| 33 | + set tSC = ex.AsStatus() |
| 34 | + |
| 35 | + // Import Modules |
| 36 | + set sys = ##class(%SYS.Python).Import("sys") |
| 37 | + set tracebackModule = ##class(%SYS.Python).Import("traceback") |
| 38 | + set builtins = ##class(%SYS.Python).Import("builtins") |
| 39 | + // Get the last traceback |
| 40 | + set traceback = sys."last_traceback" |
| 41 | + set exType = sys."last_type"."__name__" |
| 42 | + set exValue = sys."last_value"."__str__"() |
| 43 | + // Check if traceback is an object |
| 44 | + if $isObject(traceback) { |
| 45 | + // Format the traceback |
| 46 | + set tb = tracebackModule."format_exception"(sys."last_type", sys."last_value", traceback) |
| 47 | + set tbString = "" |
| 48 | + for i=0:1:(tb."__len__"()-1) { |
| 49 | + set tbString = tbString _ $c(10)_$c(13) _ tb."__getitem__"(i) |
| 50 | + } |
| 51 | + w tbString |
| 52 | + set tSC = $$$ERROR("Exception in Python - "_exType_" - "_exValue) |
| 53 | + } |
| 54 | + |
| 55 | + return tSC |
| 56 | +} |
| 57 | + |
| 58 | +} |
0 commit comments