Skip to content

Commit 9fa294e

Browse files
authored
Merge pull request RustPython#1982 from RustPython/coolreader18/wasm-_thread
Fix issues with wasm code missing _thread
2 parents f77e8d8 + b1ecca7 commit 9fa294e

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

Lib/_dummy_thread.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Exports only things specified by thread documentation;
1515
# skipping obsolete synonyms allocate(), start_new(), exit_thread().
1616
__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
17-
'interrupt_main', 'LockType']
17+
'interrupt_main', 'LockType', 'RLock']
1818

1919
# A dummy value
2020
TIMEOUT_MAX = 2**31
@@ -161,3 +161,35 @@ def interrupt_main():
161161
else:
162162
global _interrupt
163163
_interrupt = True
164+
165+
class RLock:
166+
def __init__(self):
167+
self.locked_count = 0
168+
169+
def acquire(self, waitflag=None, timeout=-1):
170+
self.locked_count += 1
171+
return True
172+
173+
__enter__ = acquire
174+
175+
def __exit__(self, typ, val, tb):
176+
self.release()
177+
178+
def release(self):
179+
if not self.locked_count:
180+
raise error
181+
self.locked_count -= 1
182+
return True
183+
184+
def locked(self):
185+
return self.locked_status != 0
186+
187+
def __repr__(self):
188+
return "<%s %s.%s object owner=%s count=%s at %s>" % (
189+
"locked" if self.locked_count else "unlocked",
190+
self.__class__.__module__,
191+
self.__class__.__qualname__,
192+
get_ident() if self.locked_count else 0,
193+
self.locked_count,
194+
hex(id(self))
195+
)

Lib/functools.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from collections import namedtuple
1919
# import types, weakref # Deferred to single_dispatch()
2020
from reprlib import recursive_repr
21-
from _thread import RLock
21+
try:
22+
from _thread import RLock
23+
except ModuleNotFoundError:
24+
from _dummy_thread import RLock
2225

2326

2427
################################################################################

Lib/reprlib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import builtins
66
from itertools import islice
7-
from _thread import get_ident
7+
try:
8+
from _thread import get_ident
9+
except ModuleNotFoundError:
10+
from _dummy_thread import get_ident
811

912
def recursive_repr(fillvalue='...'):
1013
'Decorator to make a repr function return fillvalue for a recursive call'

wasm/demo/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function onReady() {
148148
runCodeFromTextarea();
149149

150150
terminalVM = rp.vmStore.init('term_vm');
151-
terminalVM.setStdout(data => localEcho.println(data));
151+
terminalVM.setStdout(data => localEcho.print(data));
152152
readPrompts().catch(err => console.error(err));
153153

154154
// so that the test knows that we're ready

0 commit comments

Comments
 (0)