Skip to content

Commit 31a8750

Browse files
authored
Merge pull request #70 from Distributive-Network/Xmader/feat/console-api-update
Make `console` globally available
2 parents f27b51c + a8b5666 commit 31a8750

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

python/pythonmonkey/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
# Expose the package version
66
import importlib.metadata
77
__version__= importlib.metadata.version(__name__)
8+
9+
# Load the module by default to make `console` globally available
10+
require("console")

python/pythonmonkey/builtin_modules/internal-binding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"""
88
See function declarations in ./internal-binding.d.ts
99
"""
10-
exports = pm.internalBinding
10+
exports = pm.internalBinding # type: ignore

python/pythonmonkey/global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ declare const python: {
2424
/** see `pm.eval` */
2525
declare function pmEval(code: string): any;
2626

27+
// Expose our own `console` as a property of the global object
28+
declare const console: import("console").Console;
29+
2730
// Keep this in sync with both https://hg.mozilla.org/releases/mozilla-esr102/file/a03fde6/js/public/Promise.h#l331
2831
// and https://github.com/nodejs/node/blob/v20.2.0/deps/v8/include/v8-promise.h#L30
2932
declare enum PromiseState { Pending = 0, Fulfilled = 1, Rejected = 2 }

python/pythonmonkey/require.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def statSync_inner(filename: str) -> Union[Dict[str, int], bool]:
154154
Union[Dict[str, int], False]: The mode of the file or False if the file doesn't exist.
155155
"""
156156
from os import stat
157+
filename = os.path.normpath(filename)
157158
if (os.path.exists(filename)):
158159
sb = stat(filename)
159160
return { 'mode': sb.st_mode }
@@ -166,12 +167,17 @@ def readFileSync(filename, charset) -> str:
166167
Returns:
167168
str: The contents of the file
168169
"""
170+
filename = os.path.normpath(filename)
169171
with open(filename, "r", encoding=charset) as fileHnd:
170172
return fileHnd.read()
171173

174+
def existsSync(filename: str) -> bool:
175+
filename = os.path.normpath(filename)
176+
return os.path.exists(filename)
177+
172178
bootstrap.modules.fs.statSync_inner = statSync_inner
173179
bootstrap.modules.fs.readFileSync = readFileSync
174-
bootstrap.modules.fs.existsSync = os.path.exists
180+
bootstrap.modules.fs.existsSync = existsSync
175181

176182
# Read ctx-module module from disk and invoke so that this file is the "main module" and ctx-module has
177183
# require and exports symbols injected from the bootstrap object above. Current PythonMonkey bugs
@@ -201,6 +207,7 @@ def load(filename: str) -> Dict:
201207
: The loaded python module
202208
"""
203209

210+
filename = os.path.normpath(filename)
204211
name = os.path.basename(filename)
205212
if name not in sys.modules:
206213
sourceFileLoader = machinery.SourceFileLoader(name, filename)

0 commit comments

Comments
 (0)