13
13
import re
14
14
from socket import socket , AF_INET , SOCK_STREAM
15
15
import traceback
16
+ import types
17
+ from typing import Any , Mapping
16
18
17
19
18
20
def deaddress (text : str ) -> str :
19
21
return re .sub (r"0x[0-9a-f]+" , "0x..." , text )
20
22
21
23
22
- def try_exec (data : bytes ) -> dict [str , str | dict [str , str ]]:
24
+ def get_ns (ns : Mapping [str , Any ]) -> dict [str , Any ]:
25
+ ret = {}
26
+ for k , v in ns .items ():
27
+ child_ns : Mapping [str , Any ] | None = None
28
+ if isinstance (v , type ):
29
+ child_ns = v .__dict__
30
+ elif isinstance (v , types .FunctionType ):
31
+ try :
32
+ child_ns = v ()
33
+ except Exception as e :
34
+ child_ns = {"error" : "run" , "message" : repr (e )}
35
+ child : Any
36
+ if child_ns is not None :
37
+ child = get_ns (child_ns )
38
+ else :
39
+ child = deaddress (repr (v ))
40
+ ret [k ] = child
41
+ return ret
42
+
43
+
44
+ def try_exec (data : bytes ) -> dict [str , Any ]:
23
45
try :
24
46
data_str = data .decode ("utf-8" )
25
47
except UnicodeDecodeError :
@@ -29,17 +51,12 @@ def try_exec(data: bytes) -> dict[str, str | dict[str, str]]:
29
51
except Exception as e :
30
52
return {"error" : "compile" , "message" : repr (e )}
31
53
try :
32
- ns = {}
54
+ ns : dict [ str , Any ] = {}
33
55
exec (code , ns , ns )
34
56
del ns ["__builtins__" ]
35
57
except Exception as e :
36
58
return {"error" : "run" , "message" : repr (e )}
37
- classdicts = {}
38
- for k , v in ns .items ():
39
- if isinstance (v , type ):
40
- classdict = repr (v .__dict__ )
41
- classdicts [k ] = deaddress (classdict )
42
- return {"result" : deaddress (repr (ns )), "classdicts" : classdicts }
59
+ return get_ns (ns )
43
60
44
61
45
62
@dataclass
0 commit comments