blueloveTH 2023-04-27 15:39:21 +08:00
parent 1e3849319e
commit 897456c2f6
5 changed files with 28 additions and 1 deletions

View File

@ -203,4 +203,12 @@ class staticmethod:
return self.f(*args) return self.f(*args)
def type@__repr__(self): def type@__repr__(self):
return "<class '" + self.__name__ + "'>" return "<class '" + self.__name__ + "'>"
def help(obj):
if hasattr(obj, '__func__'):
obj = obj.__func__
if hasattr(obj, '__doc__'):
print(obj.__doc__)
else:
print("No docstring found")

View File

@ -543,6 +543,10 @@ __NEXT_STEP:;
_error(StrName(byte.arg), msg); _error(StrName(byte.arg), msg);
} DISPATCH(); } DISPATCH();
TARGET(RE_RAISE) _raise(); DISPATCH(); TARGET(RE_RAISE) _raise(); DISPATCH();
/*****************************************/
TARGET(SETUP_DOCSTRING)
TOP()->attr().set(__doc__, co_consts[byte.arg]);
DISPATCH();
#if !PK_ENABLE_COMPUTED_GOTO #if !PK_ENABLE_COMPUTED_GOTO
#if DEBUG_EXTRA_CHECK #if DEBUG_EXTRA_CHECK
default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented")); default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented"));

View File

@ -922,7 +922,20 @@ __SUBSCR_END:
} }
compile_block_body(); compile_block_body();
pop_context(); pop_context();
PyObject* docstring = nullptr;
if(decl->code->codes.size()>=2 && decl->code->codes[0].op == OP_LOAD_CONST && decl->code->codes[1].op == OP_POP_TOP){
PyObject* c = decl->code->consts[decl->code->codes[0].arg];
if(is_type(c, vm->tp_str)){
decl->code->codes[0].op = OP_NO_OP;
decl->code->codes[1].op = OP_NO_OP;
docstring = c;
}
}
ctx()->emit(OP_LOAD_FUNCTION, ctx()->add_func_decl(decl), prev().line); ctx()->emit(OP_LOAD_FUNCTION, ctx()->add_func_decl(decl), prev().line);
if(docstring != nullptr){
ctx()->emit(OP_SETUP_DOCSTRING, ctx()->add_const(docstring), prev().line);
}
// add decorators // add decorators
for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){ for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){
(*it)->emit(ctx()); (*it)->emit(ctx());

View File

@ -110,4 +110,5 @@ OPCODE(EXCEPTION_MATCH)
OPCODE(RAISE) OPCODE(RAISE)
OPCODE(RE_RAISE) OPCODE(RE_RAISE)
/**************************/ /**************************/
OPCODE(SETUP_DOCSTRING)
#endif #endif

View File

@ -394,6 +394,7 @@ const StrName __set__ = StrName::get("__set__");
const StrName __getattr__ = StrName::get("__getattr__"); const StrName __getattr__ = StrName::get("__getattr__");
const StrName __setattr__ = StrName::get("__setattr__"); const StrName __setattr__ = StrName::get("__setattr__");
const StrName __call__ = StrName::get("__call__"); const StrName __call__ = StrName::get("__call__");
const StrName __doc__ = StrName::get("__doc__");
const StrName m_eval = StrName::get("eval"); const StrName m_eval = StrName::get("eval");
const StrName m_self = StrName::get("self"); const StrName m_self = StrName::get("self");