From 8e1f5f056515e8dc4c583649cf7c3578da8fc1bc Mon Sep 17 00:00:00 2001 From: K Lange Date: Wed, 24 Feb 2021 22:50:51 +0900 Subject: [PATCH] repr classes as like Python - We were using "" like Python2 does for built-ins? But even Python 2 called user classes "" - Python 3 calls both built-ins and user classes "class". - Check if there's a __module__ name and use that as well, now we look even more like Python! --- src/obj_base.c | 13 +++++++++++-- test/test.krk.expect | 2 +- test/testBindNativeMethod.krk.expect | 2 +- test/testComplexArgsNative.krk.expect | 4 ++-- test/testDefaultArgs.krk.expect | 2 +- test/testFStrings.krk.expect | 2 +- test/testGetattrDir.krk.expect | 4 ++-- test/testSetLiterals.krk.expect | 2 +- test/testSpecialDecorators.krk.expect | 6 +++--- 9 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/obj_base.c b/src/obj_base.c index 053b35a..6f8b644 100644 --- a/src/obj_base.c +++ b/src/obj_base.c @@ -36,9 +36,18 @@ static KrkValue krk_docOfClass(int argc, KrkValue argv[], int hasKw) { /* Class.__str__() (and Class.__repr__) */ static KrkValue _class_to_str(int argc, KrkValue argv[], int hasKw) { if (!IS_CLASS(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "expected class"); - size_t allocSize = sizeof("") + AS_CLASS(argv[0])->name->length; + + /* Determine if this class has a module */ + KrkValue module = NONE_VAL(); + krk_tableGet(&AS_CLASS(argv[0])->fields, OBJECT_VAL(S("__module__")), &module); + + size_t allocSize = sizeof("") + AS_CLASS(argv[0])->name->length; + if (IS_STRING(module)) allocSize += AS_STRING(module)->length + 1; char * tmp = malloc(allocSize); - size_t l = snprintf(tmp, allocSize, "", AS_CLASS(argv[0])->name->chars); + size_t l = snprintf(tmp, allocSize, "", + IS_STRING(module) ? AS_CSTRING(module) : "", + IS_STRING(module) ? "." : "", + AS_CLASS(argv[0])->name->chars); KrkString * out = krk_copyString(tmp,l); free(tmp); return OBJECT_VAL(out); diff --git a/test/test.krk.expect b/test/test.krk.expect index 154e6cc..ce4b1c9 100644 --- a/test/test.krk.expect +++ b/test/test.krk.expect @@ -26,7 +26,7 @@ Function is defined, creating it... And executing the result... outside Let's do some classes. - + yay: bax bar diff --git a/test/testBindNativeMethod.krk.expect b/test/testBindNativeMethod.krk.expect index 86df861..377f9d8 100644 --- a/test/testBindNativeMethod.krk.expect +++ b/test/testBindNativeMethod.krk.expect @@ -1,3 +1,3 @@ e - + 37 diff --git a/test/testComplexArgsNative.krk.expect b/test/testComplexArgsNative.krk.expect index f991ace..dee5166 100644 --- a/test/testComplexArgsNative.krk.expect +++ b/test/testComplexArgsNative.krk.expect @@ -1,4 +1,4 @@ -1, 2, 3, test, , >, 7, 9, 10; +1, 2, 3, test, , >, 7, 9, 10; a, b, 1, 2, 3, 7, 1, 2, 3test -1 apples +1 apples got multiple values for argument 'a' diff --git a/test/testDefaultArgs.krk.expect b/test/testDefaultArgs.krk.expect index 6d885b4..a46aba3 100644 --- a/test/testDefaultArgs.krk.expect +++ b/test/testDefaultArgs.krk.expect @@ -2,7 +2,7 @@ You like bacon right? You like sports right? 1 True c None 2.71828 -one 2 test None +one 2 test None l= [] l*= [1] l= [] diff --git a/test/testFStrings.krk.expect b/test/testFStrings.krk.expect index 60c7e9c..ae36529 100644 --- a/test/testFStrings.krk.expect +++ b/test/testFStrings.krk.expect @@ -2,5 +2,5 @@ Regular string 3 3 with a string after with a string before 3 -with 3 nothing in between +with 3 nothing in between 37[]{} with some fun expressions diff --git a/test/testGetattrDir.krk.expect b/test/testGetattrDir.krk.expect index f4ec74a..1c35290 100644 --- a/test/testGetattrDir.krk.expect +++ b/test/testGetattrDir.krk.expect @@ -1,3 +1,3 @@ -['__init__', '__str__', '__repr__', '__getattr__', '__class__', '__dir__', '__hash__', '__func__', '_dict'] +['__init__', '__str__', '__repr__', '__getattr__', '__class__', '__dir__', '__hash__', '__func__', '__module__', '_dict'] 1 -['__class__', '__str__', '__dir__', '__repr__', '__hash__', '__func__', 'butts'] +['__class__', '__str__', '__dir__', '__repr__', '__hash__', '__func__', '__module__', 'butts'] diff --git a/test/testSetLiterals.krk.expect b/test/testSetLiterals.krk.expect index 638044f..4a2ced5 100644 --- a/test/testSetLiterals.krk.expect +++ b/test/testSetLiterals.krk.expect @@ -5,4 +5,4 @@ True True True True -['1', '2', '3', "", 'None', 'True', 'test'] +['1', '2', '3', "", 'None', 'True', 'test'] diff --git a/test/testSpecialDecorators.krk.expect b/test/testSpecialDecorators.krk.expect index 8438225..e3d9f88 100644 --- a/test/testSpecialDecorators.krk.expect +++ b/test/testSpecialDecorators.krk.expect @@ -3,6 +3,6 @@ No args! 48 Called as a setter: [102] 102 - test - test - test + test + test + test