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__) */
|
/* Class.__str__() (and Class.__repr__) */
|
||||||
static KrkValue _class_to_str(int argc, KrkValue argv[], int hasKw) {
|
static KrkValue _class_to_str(int argc, KrkValue argv[], int hasKw) {
|
||||||
if (!IS_CLASS(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "expected class");
|
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);
|
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);
|
KrkString * out = krk_copyString(tmp,l);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
return OBJECT_VAL(out);
|
return OBJECT_VAL(out);
|
||||||
|
@ -26,7 +26,7 @@ Function is defined, creating it...
|
|||||||
And executing the result...
|
And executing the result...
|
||||||
outside
|
outside
|
||||||
Let's do some classes.
|
Let's do some classes.
|
||||||
<type 'Test'>
|
<class '__main__.Test'>
|
||||||
yay: bax
|
yay: bax
|
||||||
bar
|
bar
|
||||||
<method Test.doAThing>
|
<method Test.doAThing>
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
e
|
e
|
||||||
<type 'Test'>
|
<class '__main__.Test'>
|
||||||
37
|
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
|
a, b, 1, 2, 3, 7, 1, 2, 3test
|
||||||
1 apples <type 'object'>
|
1 apples <class 'object'>
|
||||||
got multiple values for argument 'a'
|
got multiple values for argument 'a'
|
||||||
|
@ -2,7 +2,7 @@ You like bacon right?
|
|||||||
<function test>
|
<function test>
|
||||||
You like sports right?
|
You like sports right?
|
||||||
1 True c None 2.71828
|
1 True c None 2.71828
|
||||||
one 2 test None <type 'object'>
|
one 2 test None <class 'object'>
|
||||||
l= []
|
l= []
|
||||||
l*= [1]
|
l*= [1]
|
||||||
l= []
|
l= []
|
||||||
|
@ -2,5 +2,5 @@ Regular string
|
|||||||
3
|
3
|
||||||
3 with a string after
|
3 with a string after
|
||||||
with a string before 3
|
with a string before 3
|
||||||
with 3<type 'object'> nothing in between
|
with 3<class 'object'> nothing in between
|
||||||
37[]{} with some fun expressions
|
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
|
1
|
||||||
['__class__', '__str__', '__dir__', '__repr__', '__hash__', '__func__', 'butts']
|
['__class__', '__str__', '__dir__', '__repr__', '__hash__', '__func__', '__module__', 'butts']
|
||||||
|
@ -5,4 +5,4 @@ True
|
|||||||
True
|
True
|
||||||
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
|
48
|
||||||
Called as a setter: [102]
|
Called as a setter: [102]
|
||||||
102
|
102
|
||||||
<type 'Foo'> test
|
<class '__main__.Foo'> test
|
||||||
<type 'Foo'> test
|
<class '__main__.Foo'> test
|
||||||
<type 'Bar'> test
|
<class '__main__.Bar'> test
|
||||||
|
Loading…
Reference in New Issue
Block a user