GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — PythonMonkey/python/pythonmonkey/cli/pmjs.py at main · Distributive-Network/PythonMonkey Expand file tree/ Copy pathpmjs.pyMore file actions executable file ·429 lines (381 loc) · 14.3 KB / Copy pathpmjs.pyFile metadata and controlsexecutable file ·429 lines (381 loc) · 14.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 #! /usr/bin/env python3 # @file pmjs - PythonMonkey REPL # @author Wes Garland, [email protected] # @date June 2023 # @copyright Copyright (c) 2023 Distributive Corp. import sys import os import signal import getopt import readline import asyncio import pythonmonkey as pm from pythonmonkey.lib import pmdb, wtfpm globalThis = pm.eval("globalThis") evalOpts = {'filename': __file__, 'fromPythonFrame': True, 'strict': False} # type: pm.EvalOptions if (os.getenv('PMJS_PATH')): requirePath = list(map(os.path.abspath, os.getenv('PMJS_PATH').split(','))) else: requirePath = False pm.eval("""'use strict'; const cmds = {}; cmds.help = function help() { return '.' + `exit Exit the REPL .help Print this help message .load Load JS from a file into the REPL session .save Save all evaluated commands in this REPL session to a file .python Evaluate a Python statement, returning result as global variable $n. Use '.python reset' to reset back to $1. Statement starting with 'from' or 'import' are silently executed. Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL` }; cmds.exit = python.exit; cmds.python = function pythonCmd(...args) { var retval; const cmd = args.join(' ').trim(); if (cmd === 'reset') { for (let i=0; i < pythonCmd.serial; i++) delete globalThis['$' + i]; pythonCmd.serial = 0; return; } if (cmd === '') return; try { if (arguments[0] === 'from' || arguments[0] === 'import') return python.exec(cmd); retval = python.eval(cmd); } catch(error) { globalThis._error = error; return util.inspect(error); } pythonCmd.serial = (pythonCmd.serial || 0) + 1; globalThis['$' + pythonCmd.serial] = retval; python.stdout.write('$' + pythonCmd.serial + ' = '); return util.inspect(retval); }; /** * Handle a .xyz repl command. Invokes function cmds[XXX], passing arguments that the user typed as the * function arguments. The function arguments are space-delimited arguments; arguments surrounded by * quotes can include spaces, similar to how bash parses arguments. Argument parsing cribbed from * stackoverflow user Tsuneo Yoshioka, question 4031900. * * @param {string} cmdLine the command the user typed, without the leading . * @returns {string} to display */ globalThis.replCmd = function replCmd(cmdLine) { const cmdName = (cmdLine.match(/^[^ ]+/) || ['help'])[0]; const args = cmdLine.slice(cmdName.length).trim(); const argv = args.match(/\\\\?.|^$/g).reduce((p, c) => { if (c === '"') p.quote ^= 1; else if (!p.quote && c === ' ') p.a.push(''); else p.a[p.a.length-1] += c.replace(/\\\\(.)/,"$1"); return p; }, {a: ['']}).a; if (!cmds.hasOwnProperty(cmdName)) return `Invalid REPL keyword`; return cmds[cmdName].apply(null, argv); } /** * Evaluate a complete statement, built by the Python readline loop. */ globalThis.replEval = function replEval(statement) { const indirectEval = eval; var originalStatement = statement; var result; var mightBeObjectLiteral = false; /* A statement which starts with a { and does not end with a ; is treated as an object literal, * and to get the parser in to Expression mode instead of Statement mode, we surround any expression * like that which is also a valid compilation unit with parens, then if that is a syntax error, * we re-evaluate without the parens. */ if (/^\\s*\\{.*[^;\\s]\\s*$/.test(statement)) { const testStatement = `(${statement})`; if (globalThis.python.pythonMonkey.isCompilableUnit(testStatement)) statement = testStatement; } try { try { result = indirectEval(statement); } catch(evalError) { /* Don't retry paren-wrapped statements which weren't syntax errors, as they might have * side-effects. Never retry if we didn't paren-wrap. */ if (!(evalError instanceof SyntaxError) || originalStatement === statement) throw evalError; globalThis._swallowed_error = evalError; result = indirectEval(originalStatement); } globalThis._ = result; return util.inspect(result); } catch(error) { globalThis._error = error; return util.inspect(error); } } """, evalOpts) async def repl(): """ Start a REPL to evaluate JavaScript code in the extra-module environment. Multi-line statements and readline history are supported. ^C support is sketchy. Exit the REPL with ^D or ".quit". """ print('Welcome to PythonMonkey v' + pm.__version__ + '.') print('Type ".help" for more information.') readline.parse_and_bind('set editing-mode emacs') histfile = os.getenv('PMJS_REPL_HISTORY') or os.path.expanduser('~/.pmjs_history') if (os.path.exists(histfile)): try: readline.read_history_file(histfile) except BaseException: pass got_sigint = 0 statement = '' readline_skip_chars = 0 inner_loop = False def save_history(): nonlocal histfile readline.write_history_file(histfile) import atexit atexit.register(save_history) def quit(): """ Quit the REPL. Repl saved by atexit handler. """ globalThis.python.exit() # need for python.exit.code in require.py def sigint_handler(signum, frame): """ Handle ^C by aborting the entry of the current statement and quitting when double-struck. Sometimes this happens in the main input() function. When that happens statement is "", because we have not yet returned from input(). Sometimes it happens in the middle of the inner loop's input() - in that case, statement is the beginning of a multiline expression. Hitting ^C in the middle of a multiline express cancels its input, but readline's input() doesn't return, so we have to print the extra > prompt and fake it by later getting rid of the first readline_skip_chars characters from the input buffer. """ nonlocal got_sigint nonlocal statement nonlocal readline_skip_chars nonlocal inner_loop got_sigint = got_sigint + 1 if (got_sigint > 1): raise EOFError if (not inner_loop): if (got_sigint == 1 and len(readline.get_line_buffer()) == readline_skip_chars): # First ^C with nothing in the input buffer sys.stdout.write("\n(To exit, press Ctrl+C again or Ctrl+D or type .exit)") elif (got_sigint == 1 and readline.get_line_buffer() != ""): # Input buffer has text - clear it got_sigint = 0 readline_skip_chars = len(readline.get_line_buffer()) else: if (got_sigint == 1 and statement == "" and len(readline.get_line_buffer()) == readline_skip_chars): # statement == "" means that the inner loop has already seen ^C and is now faking the outer loop sys.stdout.write("\n(To exit, press Ctrl+C again or Ctrl+D or type .exit)") elif (got_sigint == 1 and statement != ""): # ^C happened on inner loop while it was still thinking we were doing a multiline-expression; since # we can't break the input() function, we set it up to return an outer expression and fake the outer loop got_sigint = 0 readline_skip_chars = len(readline.get_line_buffer()) sys.stdout.write("\n> ") statement = "" signal.signal(signal.SIGINT, sigint_handler) # Main Loop # # Read lines entered by the user and collect them in a statement. Once the statement is a candiate # for JavaScript evaluation (determined by pm.isCompilableUnit(), send it to replEval(). Statements # beginning with a . are interpreted as REPL commands and sent to replCmd(). # # Beware - extremely tricky interplay between readline and the SIGINT handler. This is largely because we # we can't clear the pending line buffer, so we have to fake it by re-displaying the prompt and subtracting # characters. Another complicating factor is that the handler will suspend and resume readline, but there # is no mechanism to force readline to return before the user presses enter. # while got_sigint < 2: try: await asyncio.sleep(0) inner_loop = False if (statement == ""): statement = input('> ')[readline_skip_chars:] readline_skip_chars = 0 if (len(statement) == 0): continue if (statement[0] == '.'): cmd_output = globalThis.replCmd(statement[1:]) if (cmd_output is not None): print(cmd_output) statement = "" continue if (pm.isCompilableUnit(statement)): print(globalThis.replEval(statement)) statement = "" got_sigint = 0 else: got_sigint = 0 # This loop builds a multi-line statement, but if the user hits ^C during this build, we # abort the statement. The tricky part here is that the input('... ') doesn't quit when # SIGINT is received, so we have to patch things up so that the next-entered line is # treated as the input at the top of the loop. while (got_sigint == 0): await asyncio.sleep(0) inner_loop = True lineBuffer = input('... ') more = lineBuffer[readline_skip_chars:] readline_skip_chars = 0 if (got_sigint > 0): statement = more break statement = statement + '\n' + more if (pm.isCompilableUnit(statement)): print(globalThis.replEval(statement)) statement = "" break except EOFError: print() quit() def usage(): print("""Usage: pmjs [options] [ script.js ] [arguments] Options: - script read from stdin (default if no file name is provided, interactive mode if a tty) -- indicate the end of node options -e, --eval=... evaluate script -h, --help print pnode command line options (currently set) -i, --interactive always enter the REPL even if stdin does not appear to be a terminal -p, --print [...] evaluate script and print result -r, --require... module to preload (option can be repeated) -v, --version print PythonMonkey version --use-strict evaluate -e, -p, and REPL code in strict mode --inspect enable pmdb, a gdb-like JavaScript debugger interface --wtf enable WTFPythonMonkey, a tool that can detect hanging timers when Ctrl-C is hit Environment variables: TZ specify the timezone configuration PMJS_PATH ':'-separated list of directories prefixed to the module search path PMJS_REPL_HISTORY path to the persistent REPL history file""" ) def initGlobalThis(): """ Initialize globalThis for pmjs use in the extra-module context (eg -r, -e, -p). This context needs a require function which resolves modules relative to the current working directory at pmjs launch. The global require is to the JS function using a trick instead of a JS-wrapped-Python-wrapped function """ global requirePath require = pm.createRequire(os.path.abspath(os.getcwd() + '/__pmjs_virtual__'), requirePath) globalThis.require = require globalInitModule = require( os.path.realpath( os.path.dirname(__file__) + "/../lib/pmjs/global-init")) # module load has side-effects globalThis.arguments = sys.argv return globalInitModule def main(): """ Main program entry point """ enterRepl = sys.stdin.isatty() forceRepl = False globalInitModule = initGlobalThis() global requirePath try: opts, args = getopt.getopt(sys.argv[1:], "hie:p:r:v", ["help", "eval=", "print=", "require=", "version", "interactive", "use-strict", "inspect", "wtf"]) except getopt.GetoptError as err: # print help information and exit: print(err) # will print something like "option -a not recognized" usage() sys.exit(2) output = None verbose = False enableWTF = False for o, a in opts: if o in ("-v", "--version"): print(pm.__version__) sys.exit() elif o in ("--use-strict"): evalOpts['strict'] = True elif o in ("-h", "--help"): usage() sys.exit() elif o in ("-i", "--interactive"): forceRepl = True elif o in ("-e", "--eval"): async def runEval(): pm.eval(a, evalOpts) await pm.wait() asyncio.run(runEval()) enterRepl = False elif o in ("-p", "--print"): async def runEvalPrint(): ret = pm.eval(a, evalOpts) pm.eval("ret => console.log(ret)", evalOpts)(ret) await pm.wait() asyncio.run(runEvalPrint()) enterRepl = False elif o in ("-r", "--require"): globalThis.require(a) elif o in ("--inspect"): pmdb.enable() elif o in ("--wtf"): enableWTF = True else: assert False, "unhandled option" if (len(args) > 0): async def runJS(): hasUncaughtException = False loop = asyncio.get_running_loop() def exceptionHandler(loop, context): "See https://docs.python.org/3.11/library/asyncio-eventloop.html#error-handling-api" error = context["exception"] try: globalInitModule.uncaughtExceptionHandler(error) except SystemExit: # the "exception" raised by `sys.exit()` call pass finally: pm.stop() # unblock `await pm.wait()` to gracefully exit the program nonlocal hasUncaughtException hasUncaughtException = True loop.set_exception_handler(exceptionHandler) def cleanupExit(code=0): pm.stop() realExit(code) realExit = globalThis.python.exit globalThis.python.exit = cleanupExit try: globalInitModule.patchGlobalRequire() pm.runProgramModule(args[0], args, requirePath) await pm.wait() # blocks until all asynchronous calls finish if hasUncaughtException: sys.exit(1) except Exception as error: print(error, file=sys.stderr) sys.exit(1) try: asyncio.run(runJS()) except KeyboardInterrupt: print() # silently going to end the program instead of printing out the Python traceback if enableWTF: wtfpm.printTimersDebugInfo() elif (enterRepl or forceRepl): async def runREPL(): globalInitModule.initReplLibs() await repl() await pm.wait() asyncio.run(runREPL()) globalThis.python.exit() # need for python.exit.code in require.py if __name__ == "__main__": main() |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |