def print_out(message, color, file):
stack_ = stack()
# Interestingly the if statement below is not executed when excepting KeyboardInterrupts. Weird.
# To prevent a crash we assume the module's name is 'Unknown'
module = "Unknown"
if getmodule(stack_[2][0]) == None:
for i in stack_[2:]:
if getmodule(i[0]) != None:
try:
module = CACHED_MODULES[i[0].f_code]
except KeyError:
module = getmodule(i[0]).__name__
CACHED_MODULES[i[0].f_code] = module
else:
try:
module = CACHED_MODULES[stack_[2][0].f_code]
except KeyError:
module = getmodule(stack_[2][0]).__name__
CACHED_MODULES[stack_[2][0].f_code] = module
print("\033[32m%s\033[0m:%i ? %s%s\033[0m" % (
module, stack_[2][0].f_lineno, color,
message), file=file)
file.flush()
评论列表
文章目录