2323# @date May 2023
2424#
2525
26- import sys , os
26+ import sys , os , io
2727from typing import Union , Dict , Literal , List
2828import importlib
2929import importlib .util
4343)
4444evalOpts = { 'filename' : __file__ , 'fromPythonFrame' : True } # type: pm.EvalOptions
4545
46+ # Force to use UTF-8 encoding
47+ # Windows may use other encodings / code pages that have many characters missing/unrepresentable
48+ # Error: Python UnicodeEncodeError: 'charmap' codec can't encode characters in position xx-xx: character maps to <undefined>
49+ sys .stdout .reconfigure (encoding = 'utf-8' )
50+ sys .stderr .reconfigure (encoding = 'utf-8' )
51+
4652# Add some python functions to the global python object for code in this file to use.
4753globalThis = pm .eval ("globalThis;" , evalOpts )
4854pm .eval ("globalThis.python = { pythonMonkey: {}, stdout: {}, stderr: {} }" , evalOpts );
6066globalThis .python .eval = eval
6167globalThis .python .exec = exec
6268globalThis .python .getenv = os .getenv
63- globalThis .python .paths = ':' .join (sys .path )
64- pm .eval ("python.paths = python.paths.split(':');" , evalOpts ); # fix when pm supports arrays
69+ globalThis .python .paths = sys .path
6570
6671globalThis .python .exit = pm .eval ("""'use strict';
6772(exit) => function pythonExitWrapper(exitCode) {
@@ -269,6 +274,7 @@ def _createRequireInner(*args):
269274 */
270275function createRequire(filename, bootstrap_broken, extraPaths, isMain)
271276{
277+ filename = filename.split('\\ \\ ').join('/');
272278 const bootstrap = globalThis.bootstrap; /** @bug PM-65 */
273279 const CtxModule = bootstrap.modules['ctx-module'].CtxModule;
274280 const moduleCache = globalThis.require?.cache || {};
@@ -283,7 +289,7 @@ def _createRequireInner(*args):
283289
284290 const module = new CtxModule(globalThis, filename, moduleCache);
285291 moduleCache[filename] = module;
286- for (let path of python.paths)
292+ for (let path of Array.from( python.paths) )
287293 module.paths.push(path + '/node_modules');
288294 module.require.path.push(python.pythonMonkey.dir + '/builtin_modules');
289295 module.require.path.push(python.pythonMonkey.nodeModules);
@@ -300,7 +306,7 @@ def _createRequireInner(*args):
300306 }
301307
302308 if (extraPaths)
303- module.require.path.splice(module.require.path.length, 0, ...(extraPaths.split(': ')));
309+ module.require.path.splice(module.require.path.length, 0, ...(extraPaths.split(', ')));
304310
305311 return module.require;
306312})""" , evalOpts )(* args )
0 commit comments