From 9d5c9200e8f0095ce55750d3912a424a5f760f31 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 5 Mar 2024 14:38:12 +0900 Subject: [PATCH] Simplify some container __repr__ implementations with %R --- src/obj_bytes.c | 9 +---- src/obj_dict.c | 100 +++++++++++++++--------------------------------- src/obj_list.c | 16 ++++---- src/obj_set.c | 17 ++++---- src/obj_slice.c | 29 ++++---------- src/obj_tuple.c | 16 ++++---- 6 files changed, 61 insertions(+), 126 deletions(-) diff --git a/src/obj_bytes.c b/src/obj_bytes.c index 61f5610..87e6178 100644 --- a/src/obj_bytes.c +++ b/src/obj_bytes.c @@ -325,15 +325,10 @@ KRK_Method(bytearray,__repr__) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; pushStringBuilderStr(&sb, "bytearray(", 10); - - krk_push(self->actual); - KrkValue repred_bytes = krk_callDirect(vm.baseClasses->bytesClass->_reprer, 1); - if (!IS_STRING(repred_bytes)) { - /* Invalid repr of bytes? */ - discardStringBuilder(&sb); + if (!krk_pushStringBuilderFormat(&sb, "%R", self->actual)) { + krk_discardStringBuilder(&sb); return NONE_VAL(); } - pushStringBuilderStr(&sb, AS_STRING(repred_bytes)->chars, AS_STRING(repred_bytes)->length); pushStringBuilder(&sb,')'); return finishStringBuilder(&sb); } diff --git a/src/obj_dict.c b/src/obj_dict.c index efb61bc..c7cdd9e 100644 --- a/src/obj_dict.c +++ b/src/obj_dict.c @@ -188,35 +188,21 @@ KRK_Method(dict,__repr__) { for (size_t i = 0; i < len; ++i) { KrkTableEntry * entry = &self->entries.entries[i]; if (IS_KWARGS(entry->key)) continue; - if (c > 0) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (c) pushStringBuilderStr(&sb, ", ", 2); c++; - - { - KrkClass * type = krk_getType(entry->key); - krk_push(entry->key); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } - + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error; pushStringBuilderStr(&sb, ": ", 2); - - { - KrkClass * type = krk_getType(entry->value); - krk_push(entry->value); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error; } pushStringBuilder(&sb,'}'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } KRK_Method(dict,copy) { @@ -368,39 +354,23 @@ KRK_Method(dictitems,__repr__) { for (size_t i = 0; i < len; ++i) { KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i]; if (IS_KWARGS(entry->key)) continue; - if (c > 0) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (c) pushStringBuilderStr(&sb, ", ", 2); c++; - pushStringBuilder(&sb,'('); - - { - KrkClass * type = krk_getType(entry->key); - krk_push(entry->key); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } - + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error; pushStringBuilderStr(&sb, ", ", 2); - - { - KrkClass * type = krk_getType(entry->value); - krk_push(entry->value); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } - + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error; pushStringBuilder(&sb,')'); } pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } #undef CURRENT_CTYPE @@ -449,24 +419,19 @@ KRK_Method(dictkeys,__repr__) { for (size_t i = 0; i < len; ++i) { KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i]; if (IS_KWARGS(entry->key)) continue; - if (c > 0) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (c) pushStringBuilderStr(&sb, ", ", 2); c++; - - { - KrkClass * type = krk_getType(entry->key); - krk_push(entry->key); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error; } pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } #undef CURRENT_CTYPE @@ -515,24 +480,19 @@ KRK_Method(dictvalues,__repr__) { for (size_t i = 0; i < len; ++i) { KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i]; if (IS_KWARGS(entry->key)) continue; - if (c > 0) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (c) pushStringBuilderStr(&sb, ", ", 2); c++; - - { - KrkClass * type = krk_getType(entry->value); - krk_push(entry->value); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } - } + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error; } pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } _noexport diff --git a/src/obj_list.c b/src/obj_list.c index d753439..7e1ab8d 100644 --- a/src/obj_list.c +++ b/src/obj_list.c @@ -138,15 +138,7 @@ KRK_Method(list,__repr__) { pushStringBuilder(&sb, '['); pthread_rwlock_rdlock(&self->rwlock); for (size_t i = 0; i < self->values.count; ++i) { - /* repr(self[i]) */ - KrkClass * type = krk_getType(self->values.values[i]); - krk_push(self->values.values[i]); - KrkValue result = krk_callDirect(type->_reprer, 1); - - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length); - } - + if (!krk_pushStringBuilderFormat(&sb,"%R",self->values.values[i])) goto _error; if (i + 1 < self->values.count) { pushStringBuilderStr(&sb, ", ", 2); } @@ -156,6 +148,12 @@ KRK_Method(list,__repr__) { pushStringBuilder(&sb,']'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + krk_discardStringBuilder(&sb); + pthread_rwlock_unlock(&self->rwlock); + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + return NONE_VAL(); } static int _list_extend_callback(void * context, const KrkValue * values, size_t count) { diff --git a/src/obj_set.c b/src/obj_set.c index c5d3b83..619c733 100644 --- a/src/obj_set.c +++ b/src/obj_set.c @@ -80,22 +80,19 @@ KRK_Method(set,__repr__) { for (size_t i = 0; i < len; ++i) { KrkTableEntry * entry = &self->entries.entries[i]; if (IS_KWARGS(entry->key)) continue; - if (c > 0) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (c) pushStringBuilderStr(&sb, ", ", 2); c++; - - KrkClass * type = krk_getType(entry->key); - krk_push(entry->key); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length); - } + if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error; } pushStringBuilder(&sb,'}'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } KRK_Method(set,__and__) { diff --git a/src/obj_slice.c b/src/obj_slice.c index 6456fb5..7170e26 100644 --- a/src/obj_slice.c +++ b/src/obj_slice.c @@ -122,33 +122,20 @@ KRK_Method(slice,__repr__) { ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; struct StringBuilder sb = {0}; pushStringBuilderStr(&sb,"slice(",6); - - KrkClass * type; - KrkValue result; - /* start */ - type = krk_getType(self->start); - krk_push(self->start); - result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length); + if (!krk_pushStringBuilderFormat(&sb, "%R", self->start)) goto _error; pushStringBuilderStr(&sb,", ",2); - - /* end */ - type = krk_getType(self->end); - krk_push(self->end); - result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length); + if (!krk_pushStringBuilderFormat(&sb, "%R", self->end)) goto _error; pushStringBuilderStr(&sb,", ",2); - - /* step */ - type = krk_getType(self->step); - krk_push(self->step); - result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length); - + if (!krk_pushStringBuilderFormat(&sb, "%R", self->step)) goto _error; pushStringBuilder(&sb,')'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } KRK_Method(slice,start) { diff --git a/src/obj_tuple.c b/src/obj_tuple.c index 6ab064d..d4aa2d9 100644 --- a/src/obj_tuple.c +++ b/src/obj_tuple.c @@ -148,15 +148,8 @@ KRK_Method(tuple,__repr__) { pushStringBuilder(&sb, '('); for (size_t i = 0; i < self->values.count; ++i) { - KrkClass * type = krk_getType(self->values.values[i]); - krk_push(self->values.values[i]); - KrkValue result = krk_callDirect(type->_reprer, 1); - if (IS_STRING(result)) { - pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length); - } - if (i != self->values.count - 1) { - pushStringBuilderStr(&sb, ", ", 2); - } + if (i) pushStringBuilderStr(&sb, ", ", 2); + if (!krk_pushStringBuilderFormat(&sb, "%R", self->values.values[i])) goto _error; } if (self->values.count == 1) { @@ -166,6 +159,11 @@ KRK_Method(tuple,__repr__) { pushStringBuilder(&sb, ')'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); + +_error: + ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); + krk_discardStringBuilder(&sb); + return NONE_VAL(); } KRK_Method(tuple,__add__) {