23
23
# @date May 2023
24
24
#
25
25
26
- import sys , os
26
+ import sys , os , io
27
27
from typing import Union , Dict , Literal , List
28
28
import importlib
29
29
import importlib .util
43
43
)
44
44
evalOpts = { 'filename' : __file__ , 'fromPythonFrame' : True } # type: pm.EvalOptions
45
45
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
+
46
52
# Add some python functions to the global python object for code in this file to use.
47
53
globalThis = pm .eval ("globalThis;" , evalOpts )
48
54
pm .eval ("globalThis.python = { pythonMonkey: {}, stdout: {}, stderr: {} }" , evalOpts );
60
66
globalThis .python .eval = eval
61
67
globalThis .python .exec = exec
62
68
globalThis .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
65
70
66
71
globalThis .python .exit = pm .eval ("""'use strict';
67
72
(exit) => function pythonExitWrapper(exitCode) {
@@ -269,6 +274,7 @@ def _createRequireInner(*args):
269
274
*/
270
275
function createRequire(filename, bootstrap_broken, extraPaths, isMain)
271
276
{
277
+ filename = filename.split('\\ \\ ').join('/');
272
278
const bootstrap = globalThis.bootstrap; /** @bug PM-65 */
273
279
const CtxModule = bootstrap.modules['ctx-module'].CtxModule;
274
280
const moduleCache = globalThis.require?.cache || {};
@@ -283,7 +289,7 @@ def _createRequireInner(*args):
283
289
284
290
const module = new CtxModule(globalThis, filename, moduleCache);
285
291
moduleCache[filename] = module;
286
- for (let path of python.paths)
292
+ for (let path of Array.from( python.paths) )
287
293
module.paths.push(path + '/node_modules');
288
294
module.require.path.push(python.pythonMonkey.dir + '/builtin_modules');
289
295
module.require.path.push(python.pythonMonkey.nodeModules);
@@ -300,7 +306,7 @@ def _createRequireInner(*args):
300
306
}
301
307
302
308
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(', ')));
304
310
305
311
return module.require;
306
312
})""" , evalOpts )(* args )
0 commit comments