Clean up function, method repring

This commit is contained in:
K. Lange 2022-08-06 12:20:04 +09:00
parent 29199c9568
commit ad39476a35
5 changed files with 33 additions and 42 deletions

View File

@ -22,25 +22,15 @@ static KrkTuple * functionArgs(KrkCodeObject * _self) {
} }
for (short i = 0; i < _self->keywordArgs; ++i) { for (short i = 0; i < _self->keywordArgs; ++i) {
struct StringBuilder sb = {0}; tuple->values.values[tuple->values.count++] = krk_stringFromFormat("%S=", AS_STRING(_self->keywordArgNames.values[i]));
pushStringBuilderStr(&sb, AS_CSTRING(_self->keywordArgNames.values[i]), AS_STRING(_self->keywordArgNames.values[i])->length);
pushStringBuilder(&sb,'=');
tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb);
} }
if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS) { if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS) {
struct StringBuilder sb = {0}; tuple->values.values[tuple->values.count++] = krk_stringFromFormat("*%S", AS_STRING(_self->requiredArgNames.values[_self->requiredArgs]));
pushStringBuilder(&sb, '*');
pushStringBuilderStr(&sb, AS_CSTRING(_self->requiredArgNames.values[_self->requiredArgs]), AS_STRING(_self->requiredArgNames.values[_self->requiredArgs])->length);
tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb);
} }
if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS) { if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS) {
struct StringBuilder sb = {0}; tuple->values.values[tuple->values.count++] = krk_stringFromFormat("**%S", AS_STRING(_self->keywordArgNames.values[_self->keywordArgs]));
pushStringBuilder(&sb, '*');
pushStringBuilder(&sb, '*');
pushStringBuilderStr(&sb, AS_CSTRING(_self->keywordArgNames.values[_self->keywordArgs]), AS_STRING(_self->keywordArgNames.values[_self->keywordArgs])->length);
tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb);
} }
krk_pop(); krk_pop();
@ -151,9 +141,6 @@ KRK_Method(function,_ip_to_line) {
KRK_Method(function,__str__) { KRK_Method(function,__str__) {
METHOD_TAKES_NONE(); METHOD_TAKES_NONE();
struct StringBuilder sb = {0};
pushStringBuilderStr(&sb, "<function ", 10);
/* Do we have a qualified name? */ /* Do we have a qualified name? */
KrkValue name = FUNC_NAME(function,__qualname__)(1,&self,0); KrkValue name = FUNC_NAME(function,__qualname__)(1,&self,0);
if (IS_NONE(name)) { if (IS_NONE(name)) {
@ -162,17 +149,14 @@ KRK_Method(function,__str__) {
if (!IS_STRING(name)) name = OBJECT_VAL(S("<unnamed>")); if (!IS_STRING(name)) name = OBJECT_VAL(S("<unnamed>"));
pushStringBuilderStr(&sb, AS_CSTRING(name), AS_STRING(name)->length); krk_push(name);
pushStringBuilderStr(&sb," at ", 4); struct StringBuilder sb = {0};
krk_pushStringBuilderFormat(&sb, "<function %S at %p>", AS_STRING(name), (void*)AS_OBJECT(self));
char address[100]; krk_pop();
size_t len = snprintf(address, 100, "%p", (void*)AS_OBJECT(self));
pushStringBuilderStr(&sb, address, len);
pushStringBuilder(&sb,'>'); return krk_finishStringBuilder(&sb);
return finishStringBuilder(&sb);
} }
KRK_Method(function,__file__) { KRK_Method(function,__file__) {
@ -222,14 +206,12 @@ KRK_Method(codeobject,__str__) {
if (!IS_STRING(s)) return NONE_VAL(); if (!IS_STRING(s)) return NONE_VAL();
krk_push(s); krk_push(s);
size_t len = AS_STRING(s)->length + sizeof("<codeobject >"); struct StringBuilder sb = {0};
char * tmp = malloc(len); krk_pushStringBuilderFormat(&sb, "<codeobject %S at %p>", AS_STRING(s), (void*)self);
snprintf(tmp, len, "<codeobject %s>", AS_CSTRING(s));
s = OBJECT_VAL(krk_copyString(tmp,len-1));
free(tmp);
krk_pop(); krk_pop();
return s;
return krk_finishStringBuilder(&sb);
} }
KRK_Method(codeobject,_ip_to_line) { KRK_Method(codeobject,_ip_to_line) {
@ -329,17 +311,14 @@ KRK_Method(method,__str__) {
if (!IS_STRING(s)) return NONE_VAL(); if (!IS_STRING(s)) return NONE_VAL();
krk_push(s); krk_push(s);
KrkClass * type = krk_getType(self->receiver); struct StringBuilder sb = {0};
krk_push(self->receiver); krk_pushStringBuilderFormat(&sb, "<bound method '%S' of %T object", AS_STRING(s), self->receiver);
KrkValue reprVal = krk_callDirect(type->_reprer, 1); if (IS_OBJECT(self->receiver)) krk_pushStringBuilderFormat(&sb, " at %p", (void*)AS_OBJECT(self->receiver));
krk_pushStringBuilder(&sb, '>');
size_t len = AS_STRING(s)->length + AS_STRING(reprVal)->length + sizeof("<bound method of >") + 1;
char * tmp = malloc(len);
snprintf(tmp, len, "<bound method %s of %s>", AS_CSTRING(s), AS_CSTRING(reprVal));
s = OBJECT_VAL(krk_copyString(tmp,len-1));
free(tmp);
krk_pop(); krk_pop();
return s;
return krk_finishStringBuilder(&sb);
} }
KRK_Method(method,__file__) { KRK_Method(method,__file__) {

View File

@ -112,7 +112,7 @@ let test = Test()
test.doAThing() test.doAThing()
test.foo = "bar" test.foo = "bar"
print(test.foo) print(test.foo)
print('<bound method Test.doAThing of ' in str(test.doAThing)) print('<bound method \'Test.doAThing\' of ' in str(test.doAThing))
test.doAThing() test.doAThing()

View File

@ -1,3 +1,9 @@
# Emulate the old behavior of method.__repr__ when printing,
# which called repr() on the receiver...
def __str__(self):
return f'<bound method {self.__qualname__} of {self.__self__!r}>'
method.__str__ = __str__
class Callable(): class Callable():
def __init__(self,name): def __init__(self,name):
self.name = name self.name = name

View File

@ -14,10 +14,10 @@ def noargs():
Foo.other = other Foo.other = other
Foo.noargs = noargs Foo.noargs = noargs
print('<bound method other' in str(f.other)) print('<bound method \'other' in str(f.other))
f.other() f.other()
print('<bound method noargs' in str(f.noargs)) print('<bound method \'noargs' in str(f.noargs))
try: try:
f.noargs() f.noargs()
except Exception as e: except Exception as e:

View File

@ -1,3 +1,9 @@
# Emulate the old behavior of method.__repr__ when printing,
# which called repr() on the receiver...
def __str__(self):
return f'<bound method {self.__qualname__} of {self.__self__!r}>'
method.__str__ = __str__
class Foo(object): class Foo(object):
def __str__(self): def __str__(self):
return '<str>' return '<str>'