repr classes as <class 'module.Class'> like Python
- We were using "<type ...>" like Python2 does for built-ins? But even Python 2 called user classes "<class ...>" - 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!
This commit is contained in:
parent
6d0e342e1b
commit
8e1f5f0565
@ -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("<type ''>") + 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("<class ''>") + 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, "<type '%s'>", AS_CLASS(argv[0])->name->chars);
|
||||
size_t l = snprintf(tmp, allocSize, "<class '%s%s%s'>",
|
||||
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);
|
||||
|
@ -26,7 +26,7 @@ Function is defined, creating it...
|
||||
And executing the result...
|
||||
outside
|
||||
Let's do some classes.
|
||||
<type 'Test'>
|
||||
<class '__main__.Test'>
|
||||
yay: bax
|
||||
bar
|
||||
<method Test.doAThing>
|
||||
|
@ -1,3 +1,3 @@
|
||||
e
|
||||
<type 'Test'>
|
||||
<class '__main__.Test'>
|
||||
37
|
||||
|
@ -1,4 +1,4 @@
|
||||
1, 2, 3, test, <type 'object'>, <function <lambda>>, 7, 9, 10;
|
||||
1, 2, 3, test, <class 'object'>, <function <lambda>>, 7, 9, 10;
|
||||
a, b, 1, 2, 3, 7, 1, 2, 3test
|
||||
1 apples <type 'object'>
|
||||
1 apples <class 'object'>
|
||||
got multiple values for argument 'a'
|
||||
|
@ -2,7 +2,7 @@ You like bacon right?
|
||||
<function test>
|
||||
You like sports right?
|
||||
1 True c None 2.71828
|
||||
one 2 test None <type 'object'>
|
||||
one 2 test None <class 'object'>
|
||||
l= []
|
||||
l*= [1]
|
||||
l= []
|
||||
|
@ -2,5 +2,5 @@ Regular string
|
||||
3
|
||||
3 with a string after
|
||||
with a string before 3
|
||||
with 3<type 'object'> nothing in between
|
||||
with 3<class 'object'> nothing in between
|
||||
37[]{} with some fun expressions
|
||||
|
@ -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']
|
||||
|
@ -5,4 +5,4 @@ True
|
||||
True
|
||||
True
|
||||
True
|
||||
['1', '2', '3', "<type 'object'>", 'None', 'True', 'test']
|
||||
['1', '2', '3', "<class 'object'>", 'None', 'True', 'test']
|
||||
|
@ -3,6 +3,6 @@ No args!
|
||||
48
|
||||
Called as a setter: [102]
|
||||
102
|
||||
<type 'Foo'> test
|
||||
<type 'Foo'> test
|
||||
<type 'Bar'> test
|
||||
<class '__main__.Foo'> test
|
||||
<class '__main__.Foo'> test
|
||||
<class '__main__.Bar'> test
|
||||
|
Loading…
Reference in New Issue
Block a user