Simplify some container __repr__ implementations with %R
This commit is contained in:
parent
3e8bb098ff
commit
9d5c9200e8
@ -325,15 +325,10 @@ KRK_Method(bytearray,__repr__) {
|
|||||||
METHOD_TAKES_NONE();
|
METHOD_TAKES_NONE();
|
||||||
struct StringBuilder sb = {0};
|
struct StringBuilder sb = {0};
|
||||||
pushStringBuilderStr(&sb, "bytearray(", 10);
|
pushStringBuilderStr(&sb, "bytearray(", 10);
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", self->actual)) {
|
||||||
krk_push(self->actual);
|
krk_discardStringBuilder(&sb);
|
||||||
KrkValue repred_bytes = krk_callDirect(vm.baseClasses->bytesClass->_reprer, 1);
|
|
||||||
if (!IS_STRING(repred_bytes)) {
|
|
||||||
/* Invalid repr of bytes? */
|
|
||||||
discardStringBuilder(&sb);
|
|
||||||
return NONE_VAL();
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
pushStringBuilderStr(&sb, AS_STRING(repred_bytes)->chars, AS_STRING(repred_bytes)->length);
|
|
||||||
pushStringBuilder(&sb,')');
|
pushStringBuilder(&sb,')');
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
}
|
}
|
||||||
|
100
src/obj_dict.c
100
src/obj_dict.c
@ -188,35 +188,21 @@ KRK_Method(dict,__repr__) {
|
|||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
KrkTableEntry * entry = &self->entries.entries[i];
|
KrkTableEntry * entry = &self->entries.entries[i];
|
||||||
if (IS_KWARGS(entry->key)) continue;
|
if (IS_KWARGS(entry->key)) continue;
|
||||||
if (c > 0) {
|
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
|
||||||
}
|
|
||||||
c++;
|
c++;
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pushStringBuilderStr(&sb, ": ", 2);
|
pushStringBuilderStr(&sb, ": ", 2);
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pushStringBuilder(&sb,'}');
|
pushStringBuilder(&sb,'}');
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
KRK_Method(dict,copy) {
|
KRK_Method(dict,copy) {
|
||||||
@ -368,39 +354,23 @@ KRK_Method(dictitems,__repr__) {
|
|||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||||
if (IS_KWARGS(entry->key)) continue;
|
if (IS_KWARGS(entry->key)) continue;
|
||||||
if (c > 0) {
|
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
|
||||||
}
|
|
||||||
c++;
|
c++;
|
||||||
|
|
||||||
pushStringBuilder(&sb,'(');
|
pushStringBuilder(&sb,'(');
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
pushStringBuilderStr(&sb, ", ", 2);
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pushStringBuilder(&sb,')');
|
pushStringBuilder(&sb,')');
|
||||||
}
|
}
|
||||||
|
|
||||||
pushStringBuilderStr(&sb,"])",2);
|
pushStringBuilderStr(&sb,"])",2);
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef CURRENT_CTYPE
|
#undef CURRENT_CTYPE
|
||||||
@ -449,24 +419,19 @@ KRK_Method(dictkeys,__repr__) {
|
|||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||||
if (IS_KWARGS(entry->key)) continue;
|
if (IS_KWARGS(entry->key)) continue;
|
||||||
if (c > 0) {
|
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
|
||||||
}
|
|
||||||
c++;
|
c++;
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pushStringBuilderStr(&sb,"])",2);
|
pushStringBuilderStr(&sb,"])",2);
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef CURRENT_CTYPE
|
#undef CURRENT_CTYPE
|
||||||
@ -515,24 +480,19 @@ KRK_Method(dictvalues,__repr__) {
|
|||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||||
if (IS_KWARGS(entry->key)) continue;
|
if (IS_KWARGS(entry->key)) continue;
|
||||||
if (c > 0) {
|
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
|
||||||
}
|
|
||||||
c++;
|
c++;
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pushStringBuilderStr(&sb,"])",2);
|
pushStringBuilderStr(&sb,"])",2);
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
_noexport
|
_noexport
|
||||||
|
@ -138,15 +138,7 @@ KRK_Method(list,__repr__) {
|
|||||||
pushStringBuilder(&sb, '[');
|
pushStringBuilder(&sb, '[');
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
pthread_rwlock_rdlock(&self->rwlock);
|
||||||
for (size_t i = 0; i < self->values.count; ++i) {
|
for (size_t i = 0; i < self->values.count; ++i) {
|
||||||
/* repr(self[i]) */
|
if (!krk_pushStringBuilderFormat(&sb,"%R",self->values.values[i])) goto _error;
|
||||||
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 + 1 < self->values.count) {
|
if (i + 1 < self->values.count) {
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
pushStringBuilderStr(&sb, ", ", 2);
|
||||||
}
|
}
|
||||||
@ -156,6 +148,12 @@ KRK_Method(list,__repr__) {
|
|||||||
pushStringBuilder(&sb,']');
|
pushStringBuilder(&sb,']');
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
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) {
|
static int _list_extend_callback(void * context, const KrkValue * values, size_t count) {
|
||||||
|
@ -80,22 +80,19 @@ KRK_Method(set,__repr__) {
|
|||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
KrkTableEntry * entry = &self->entries.entries[i];
|
KrkTableEntry * entry = &self->entries.entries[i];
|
||||||
if (IS_KWARGS(entry->key)) continue;
|
if (IS_KWARGS(entry->key)) continue;
|
||||||
if (c > 0) {
|
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
pushStringBuilderStr(&sb, ", ", 2);
|
|
||||||
}
|
|
||||||
c++;
|
c++;
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pushStringBuilder(&sb,'}');
|
pushStringBuilder(&sb,'}');
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
KRK_Method(set,__and__) {
|
KRK_Method(set,__and__) {
|
||||||
|
@ -122,33 +122,20 @@ KRK_Method(slice,__repr__) {
|
|||||||
((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR;
|
((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR;
|
||||||
struct StringBuilder sb = {0};
|
struct StringBuilder sb = {0};
|
||||||
pushStringBuilderStr(&sb,"slice(",6);
|
pushStringBuilderStr(&sb,"slice(",6);
|
||||||
|
|
||||||
KrkClass * type;
|
|
||||||
KrkValue result;
|
|
||||||
|
|
||||||
/* start */
|
/* start */
|
||||||
type = krk_getType(self->start);
|
if (!krk_pushStringBuilderFormat(&sb, "%R", self->start)) goto _error;
|
||||||
krk_push(self->start);
|
|
||||||
result = krk_callDirect(type->_reprer, 1);
|
|
||||||
if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
|
||||||
pushStringBuilderStr(&sb,", ",2);
|
pushStringBuilderStr(&sb,", ",2);
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", self->end)) goto _error;
|
||||||
/* 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);
|
|
||||||
pushStringBuilderStr(&sb,", ",2);
|
pushStringBuilderStr(&sb,", ",2);
|
||||||
|
if (!krk_pushStringBuilderFormat(&sb, "%R", self->step)) goto _error;
|
||||||
/* 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);
|
|
||||||
|
|
||||||
pushStringBuilder(&sb,')');
|
pushStringBuilder(&sb,')');
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
KRK_Method(slice,start) {
|
KRK_Method(slice,start) {
|
||||||
|
@ -148,15 +148,8 @@ KRK_Method(tuple,__repr__) {
|
|||||||
pushStringBuilder(&sb, '(');
|
pushStringBuilder(&sb, '(');
|
||||||
|
|
||||||
for (size_t i = 0; i < self->values.count; ++i) {
|
for (size_t i = 0; i < self->values.count; ++i) {
|
||||||
KrkClass * type = krk_getType(self->values.values[i]);
|
if (i) pushStringBuilderStr(&sb, ", ", 2);
|
||||||
krk_push(self->values.values[i]);
|
if (!krk_pushStringBuilderFormat(&sb, "%R", self->values.values[i])) goto _error;
|
||||||
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 (self->values.count == 1) {
|
if (self->values.count == 1) {
|
||||||
@ -166,6 +159,11 @@ KRK_Method(tuple,__repr__) {
|
|||||||
pushStringBuilder(&sb, ')');
|
pushStringBuilder(&sb, ')');
|
||||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
|
|
||||||
|
_error:
|
||||||
|
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||||
|
krk_discardStringBuilder(&sb);
|
||||||
|
return NONE_VAL();
|
||||||
}
|
}
|
||||||
|
|
||||||
KRK_Method(tuple,__add__) {
|
KRK_Method(tuple,__add__) {
|
||||||
|
Loading…
Reference in New Issue
Block a user