import sys
from collections import defaultdict
import time


timeouts = defaultdict(list)
top_timeout_id = 0
cancelled_timeouts = set()


def windows(*args, **kwargs):
    return []


def set_timeout(func, timeout, *args, **kwargs):
    global top_timeout_id
    timeout_id = top_timeout_id
    top_timeout_id + 1
    if top_timeout_id > 100000:
        top_timeout_id = 0

    def timeout_func():
        if timeout_id in cancelled_timeouts:
            cancelled_timeouts.remove(timeout_id)
            return
        func(*args, **kwargs)

    then = time.time() + (timeout / 1000.0)
    timeouts[then].append(timeout_func)
    return timeout_id


def cancel_timeout(timeout_id):
    if timeout_id in timeouts:
        cancelled_timeouts.add(timeout_id)


def call_timeouts():
    now = time.time()
    to_remove = []
    for t, tos in timeouts.items():
        if now >= t:
            for timeout in tos:
                timeout()
            to_remove.append(t)
    for k in to_remove:
        del timeouts[k]


def error_message(*args, **kwargs):
    print(args, kwargs)


class Region(object):
    def __init__(*args, **kwargs):
        pass


def platform():
    return sys.platform

@@ -129,16 +129,41 @@
 = set()%0A
+calling_timeouts = False%0A
 %0A%0Adef wi
@@ -794,24 +794,120 @@
 timeouts():%0A
+    global calling_timeouts%0A    if calling_timeouts:%0A        return%0A    calling_timeouts = True%0A
     now = ti
@@ -1130,16 +1130,45 @@
 outs%5Bk%5D%0A
+    calling_timeouts = False%0A
 %0A%0Adef er

Stop exception if someone calls timeouts from a timeout.