增加计时

This commit is contained in:
shenjack 2023-10-03 21:30:36 +08:00
parent e55c85a5ad
commit b6d58cf9fc
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 11 additions and 4 deletions

6
.gitignore vendored
View File

@ -1,2 +1,6 @@
/venv
config.toml
config.toml
# Added by cargo
/target

View File

@ -1,3 +1,4 @@
import time
import random
import asyncio
import traceback
@ -127,12 +128,13 @@ async def safe_eval(code: str) -> str:
code = code.replace('exit', '好坏!\n')
code = code.replace('eval', '啊哈!\n')
code = code.replace('exec', '抓住!\n')
start_time = time.time()
try:
import os
import math
import random
import decimal
global_val = {'math': math,
global_val = {'time': time,
'math': math,
'decimal': decimal,
'random': random,
'__import__': '<built-in function __import__>',
@ -151,6 +153,7 @@ async def safe_eval(code: str) -> str:
result = result[:limit]
except:
result = traceback.format_exc()
end_time = time.time()
result = result.replace(KEY, '***')
result = result.replace(HOST, '***')
@ -159,7 +162,7 @@ async def safe_eval(code: str) -> str:
if result == '6' or result == 6:
result = '他确实等于 6'
result = f'{code}\neval result:\n{result}'
result = f'{code}\neval result:\n{result}\n耗时: {end_time - start_time} s'
return result
except:
error = traceback.format_exc()