def run() -> None: pip_directory = os.path.expanduser("~/Documents/modules") Python.shared.isScriptRunning = True os.chdir(currentDir) try: sys.path.remove(pip_directory) except: pass sys.path.insert(-1, currentDir) sys.path.insert(-1, pip_directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) if debug and "widget" not in os.environ: try: console except: import console console.__are_breakpoints_set__ = False console.__breakpoints__ = breakpoints console.__i__ = -1 old_input = input def debugger_input(prompt): try: console except: import console if not console.__are_breakpoints_set__: breakpoints = console.__breakpoints__ console.__i__ += 1 if len(breakpoints) < console.__i__: console.__are_breakpoints_set__ = True return "" try: breakpoints[console.__i__ + 1] except: console.__are_breakpoints_set__ = True return "b " + str(breakpoints[console.__i__]) else: console.__should_inspect__ = True return old_input(prompt) if len(breakpoints) > 0: builtins.input = debugger_input pdb.main(["pdb", path]) builtins.input = old_input else: spec.loader.exec_module(__script__) except SystemExit: pass except KeyboardInterrupt: pass except Exception as e: if not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(traceback.format_exc()) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(sys.exc_info()[2]) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] Python.shared.errorType = exc_type.__name__ Python.shared.errorReason = str(e) for console in ConsoleViewController.visibles: if console.editorSplitViewController.editor.document.fileURL.path != path: continue console.editorSplitViewController.editor.showErrorAtLine( lineNumber) error = traceback.format_exc(limit=-count) try: PyOutputHelper.printError( error, script=threading.current_thread().script_path) except AttributeError: PyOutputHelper.printError(error, script=None) if "cv2.error" in error and "!_src.empty()" in error: string = "\nOn Pyto, 'cv2.VideoCapture.read' may return an invalid value the first time. If you are running a loop for capturing a video from the camera, check if the return value is valid before using it. See the 'OpenCV/face_detection.py' example.\n" try: PyOutputHelper.printError( string, script=threading.current_thread().script_path) except AttributeError: PyOutputHelper.printError(string, script=None) sys.path.remove(currentDir) if debug: pdb.post_mortem(exc_tb) if __isMainApp__(): EditorViewController.runningLine = 0 ConsoleViewController.enableDoneButton() ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview()
def run_script(path, replMode=False, debug=False, breakpoints=[], runREPL=True): """ Run the script at given path catching exceptions. This function should only be used internally by Pyto. Args: path: The path of the script. replMode: If set to `True`, errors will not be handled. debug: Set to `True` for debugging. breakpoints: Lines to break if debugging. runREPL: Set it to `True` for running the REPL. """ __repl_namespace__[path.split("/")[-1]] = {} __clear_mods__() python = Python.shared python.addScriptToList(path) if PyCallbackHelper is not None: PyCallbackHelper.exception = None is_watch_script = False if path == str(Python.watchScriptURL.path): is_watch_script = True currentDir = "" try: currentDir = str(python.currentWorkingDirectory) except: currentDir = os.path.expanduser(os.path.dirname(path)) try: del os.environ["ps1"] except KeyError: pass try: del os.environ["ps2"] except KeyError: pass sys.argv = [path] for arg in python.args: if arg != "": sys.argv.append(str(arg)) d = os.path.expanduser("~/tmp") filesToRemove = [] try: filesToRemove = [os.path.join(d, f) for f in os.listdir(d)] except: pass try: filesToRemove.remove(d + "/Script.py") except: pass try: filesToRemove.remove(d + "/Watch.py") except: pass for f in filesToRemove: if f.endswith(".repl.py"): continue if f.endswith(".tmp"): continue try: os.remove(f) except PermissionError: pass # Kill the REPL running for this script global __repl_threads__ if path in __repl_threads__: thread = __repl_threads__[path] for tid, tobj in threading._active.items(): if tobj is thread: try: stopit.async_raise(tid, SystemExit) break except: pass del __repl_threads__[path] def run(): def add_signal_handler(s, f): return loop = asyncio.new_event_loop() loop.add_signal_handler = add_signal_handler asyncio.set_event_loop(loop) pip_directory = os.path.expanduser("~/Documents/site-packages") Python.shared.isScriptRunning = True os.chdir(currentDir) try: sys.path.remove(pip_directory) except: pass sys.path.insert(-1, currentDir) sys.path.insert(-1, pip_directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) sys.modules["__main__"] = __script__ if debug and "widget" not in os.environ: try: console except: import console console.__are_breakpoints_set__ = False console.__breakpoints__ = breakpoints console.__i__ = -1 old_input = input def debugger_input(prompt): try: console except: import console if not console.__are_breakpoints_set__: breakpoints = console.__breakpoints__ console.__i__ += 1 if len(breakpoints) < console.__i__: console.__are_breakpoints_set__ = True return "" try: breakpoints[console.__i__ + 1] except: console.__are_breakpoints_set__ = True return "b " + str(breakpoints[console.__i__]) else: console.__should_inspect__ = True return old_input(prompt) if len(breakpoints) > 0: builtins.input = debugger_input pdb.main(["pdb", path]) builtins.input = old_input loop.close() else: spec.loader.exec_module(__script__) loop.close() return (path, vars(__script__), None) except SystemExit: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), SystemExit) except KeyboardInterrupt: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), KeyboardInterrupt) except Exception as e: if PyCallbackHelper is not None: PyCallbackHelper.exception = str(e) loop.close() if not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(traceback.format_exc()) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(exc_tb) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] Python.shared.errorType = exc_type.__name__ Python.shared.errorReason = str(e) for console in ConsoleViewController.objcVisibles: if ( console.editorSplitViewController.editor.document.fileURL.path != path ): continue console.editorSplitViewController.editor.showErrorAtLine( lineNumber ) excepthook(exc_type, exc_obj, exc_tb, -count) try: PyOutputHelper.printError( "", script=threading.current_thread().script_path ) except AttributeError: PyOutputHelper.printError("", script=None) error = traceback.format_exc(limit=-count) if "cv2.error" in error and "!_src.empty()" in error: string = "\nOn Pyto, 'cv2.VideoCapture.read' may return an invalid value the first time. If you are running a loop for capturing a video from the camera, check if the return value is valid before using it. See the 'OpenCV/face_detection.py' example.\n" try: PyOutputHelper.printError( string, script=threading.current_thread().script_path ) except AttributeError: PyOutputHelper.printError(string, script=None) sys.path.remove(currentDir) if debug: pdb.post_mortem(exc_tb) return (path, vars(__script__), e) if __isMainApp__(): EditorViewController.runningLine = 0 ConsoleViewController.enableDoneButton() ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview() Python.shared.isScriptRunning = True Python.shared._isScriptRunning = True def run_repl(t): global __repl_threads__ Python.shared._isScriptRunning = False Python.shared.isScriptRunning = False Python.shared.removeScriptFromList(path) if path.endswith(".repl.py") or not runREPL: return if type(t) is tuple and len(t) == 3 and not is_watch_script: __repl_threads__[t[0]] = threading.current_thread() __runREPL__(t[0].split("/")[-1], t[1], "") _script = None if "__editor_delegate__" in dir(builtins) and builtins.__editor_delegate__ is not None: delegate = builtins.__editor_delegate__ def _run(): import builtins delegate = builtins.__editor_delegate__ t = run() if type(t) is tuple and len(t) == 3: try: delegate.did_run_script(t[0], t[1], t[2]) except NotImplementedError: run_repl(t) except SystemExit: run_repl(t) except KeyboardInterrupt: run_repl(t) except Exception: traceback.print_tb() try: delegate.run_script(path, _run) except NotImplementedError: run_repl(_run()) else: # Return the script's __dict__ for the Xcode template t = run() if __isMainApp__(): run_repl(t) else: _script = t[1] Python.shared._isScriptRunning = False Python.shared.isScriptRunning = False Python.shared.removeScriptFromList(path) sys.path = list(dict.fromkeys(sys.path)) # I don't remember why 😠if "widget" not in os.environ: import watch watch.__show_ui_if_needed__() __clear_mods__() #time.sleep(0.2) return _script
def run(): def add_signal_handler(s, f): return loop = asyncio.new_event_loop() loop.add_signal_handler = add_signal_handler asyncio.set_event_loop(loop) pip_directory = os.path.expanduser("~/Documents/site-packages") os.chdir(currentDir) try: sys.path.remove(pip_directory) except: pass sys.path.insert(-1, currentDir) sys.path.insert(-1, pip_directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) sys.__class__.main[path] = weakref.ref(__script__) sys.modules["__main__"] = __import__("__main__") if debug and "widget" not in os.environ: try: console except: import console console._has_started_debugging = False console._are_breakpoints_set = False console._breakpoints = json.loads(breakpoints) console._are_breakpoints_cleared = False console.__i__ = -1 old_input = input def debugger_input(prompt): try: console except: import console if not console._are_breakpoints_cleared: console._are_breakpoints_cleared = True return "clear" elif not console._are_breakpoints_set: breakpoints = console._breakpoints console.__i__ += 1 if len(breakpoints) < console.__i__ or len( breakpoints) == 0: console._are_breakpoints_set = True return "" try: breakpoints[console.__i__ + 1] except: console._are_breakpoints_set = True return "b " + str(breakpoints[ console.__i__]["file_path"]) + ":" + str( breakpoints[console.__i__]["lineno"]) elif not console._has_started_debugging: console._has_started_debugging = True return "c" else: console.__should_inspect__ = True _input = old_input(prompt) if _input == "<WILL INTERRUPT>": return "exit" else: return _input if len(breakpoints) > 0: builtins.input = debugger_input pdb.main(["pdb", path]) builtins.input = old_input loop.close() else: spec.loader.exec_module(__script__) loop.close() return (path, vars(__script__), None) except SystemExit: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), SystemExit) except KeyboardInterrupt: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), KeyboardInterrupt) except Exception as e: if PyCallbackHelper is not None: PyCallbackHelper.exception = str(e) loop.close() if not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(traceback.format_exc()) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(exc_tb) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] as_text = return_excepthook(exc_type, exc_obj, exc_tb, -count) offset = 0 end_offset = 0 if isinstance(e, SyntaxError): offset = e.offset end_offset = e.end_offset count = 0 _json = get_json(exc_tb, exc_obj, as_text, count, offset, end_offset, vars(__script__)) traceback_shown = False for console in ConsoleViewController.objcVisibles: if (console.editorSplitViewController.editor.document. fileURL.path != path): continue console.editorSplitViewController.editor.showTraceback( _json) traceback_shown = True if not traceback_shown: builtins.print(as_text) try: PyOutputHelper.printError( "", script=threading.current_thread().script_path) except AttributeError: PyOutputHelper.printError("", script=None) error = traceback.format_exc(limit=-count) if "cv2.error" in error and "!_src.empty()" in error: string = "\nOn Pyto, 'cv2.VideoCapture.read' may return an invalid value the first time. If you are running a loop for capturing a video from the camera, check if the return value is valid before using it. See the 'OpenCV/face_detection.py' example.\n" try: PyOutputHelper.printError( string, script=threading.current_thread().script_path) except AttributeError: PyOutputHelper.printError(string, script=None) return (path, vars(__script__), e) finally: Python.shared.removeScriptFromList(path) if __isMainApp__(): EditorViewController.runningLine = 0 ConsoleViewController.enableDoneButton() ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview()
def run() -> None: if __platform__ is iOS: Python.shared.isScriptRunning = True os.system = Python.shared.system directory = os.path.expanduser(os.path.dirname(path)) os.chdir(directory) sys.path.insert(0, directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) if debug and __platform__ is iOS and __host__ is not widget: try: console except: import console console.__are_breakpoints_set__ = False console.__should_inspect__ = True console.__breakpoints__ = breakpoints console.__i__ = -1 old_input = input def debugger_input(prompt): try: console except: import console if not console.__are_breakpoints_set__: breakpoints = console.__breakpoints__ console.__i__ += 1 if len(breakpoints) < console.__i__: console.__are_breakpoints_set__ = True return "" try: breakpoints[console.__i__ + 1] except: console.__are_breakpoints_set__ = True return "b " + str(breakpoints[console.__i__]) elif not console.__should_inspect__: console.__should_inspect__ = True return old_input(prompt) else: console.__should_inspect__ = False return "from pyto import ConsoleViewController; from _get_variables_hierarchy import get_variables_hierarchy; ConsoleViewController.variables = get_variables_hierarchy(locals())" if len(breakpoints) > 0: builtins.input = debugger_input pdb.main(["pdb", path]) builtins.input = old_input else: spec.loader.exec_module(__script__) except SystemExit: pass except KeyboardInterrupt: pass except Exception as e: if __platform__ is iOS and not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(str(e)) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(sys.exc_info()[2]) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] if __platform__ is iOS: Python.shared.errorType = exc_type.__name__ Python.shared.errorReason = str(e) EditorSplitViewController.visible.editor.showErrorAtLine( lineNumber) elif __platform__ is macOS: print("Pyto.error_at_line;" + str(lineNumber) + ";") error = traceback.format_exc(limit=-count) if __platform__ is iOS: PyOutputHelper.printError(error) sys.path.remove(directory) else: sys.stderr.write(error + "\n") if debug: pdb.post_mortem(exc_tb) if __platform__ is iOS and __isMainApp__(): EditorViewController.runningLine = 0 ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview()
def run() -> None: Python.shared.isScriptRunning = True os.system = Python.shared.system directory = os.path.expanduser(os.path.dirname(path)) os.chdir(directory) sys.path.insert(0, directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) spec.loader.exec_module(__script__) Python.shared.values = [ item for item in dir(__script__) if not item.startswith("__") ] except SystemExit: pass except KeyboardInterrupt: pass except Exception as e: if not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(str(e)) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(sys.exc_info()[2]) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] Python.shared.errorType = exc_type.__name__ Python.shared.errorReason = str(e) if __platform__ is iOS: EditorSplitViewController.visible.editor.showErrorAtLine( lineNumber) else: pass #EditorViewController.showErrorAtLine(lineNumber) print(traceback.format_exc(limit=-count)) sys.path.remove(directory) if (__isMainApp__() and __platform__ is iOS): ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview() Python.shared.isScriptRunning = False Python.shared._isScriptRunning = False