object.__str__ should call repr; remove extraneous __str__/__repr__ aliases

This commit is contained in:
K. Lange 2024-01-01 19:31:54 +09:00
parent 98bd980c2a
commit 7f397b2c2c
14 changed files with 32 additions and 46 deletions

View File

@ -233,7 +233,7 @@ KRK_StaticMethod(object,__init_subclass__) {
* all types should have a string representation available through
* those methods.
*/
KRK_Method(object,__str__) {
KRK_Method(object,__repr__) {
KrkClass * type = krk_getType(self);
KrkValue module = NONE_VAL();
@ -262,6 +262,13 @@ _error:
return NONE_VAL();
}
KRK_Method(object,__str__) {
KrkClass * type = krk_getType(self);
if (unlikely(!type->_reprer)) return krk_runtimeError(vm.exceptions->typeError, "object is not representable");
krk_push(self);
return krk_callDirect(type->_reprer, 1);
}
KRK_Method(object,__format__) {
METHOD_TAKES_EXACTLY(1);
if (!IS_STRING(argv[1])) return TYPE_ERROR(str,argv[1]);
@ -1439,6 +1446,7 @@ void _createAndBind_builtins(void) {
KrkClass * object = vm.baseClasses->objectClass;
BIND_METHOD(object,__dir__);
BIND_METHOD(object,__str__);
BIND_METHOD(object,__repr__);
BIND_METHOD(object,__hash__);
BIND_METHOD(object,__eq__);
BIND_METHOD(object,__format__);
@ -1446,7 +1454,6 @@ void _createAndBind_builtins(void) {
BIND_STATICMETHOD(object,__new__);
BIND_METHOD(object,__init__);
BIND_CLASSMETHOD(object,__init_subclass__);
krk_defineNative(&object->methods, "__repr__", FUNC_NAME(object,__str__));
krk_finalizeClass(object);
KRK_DOC(object,
"@brief Base class for all types.\n\n"
@ -1463,7 +1470,6 @@ void _createAndBind_builtins(void) {
krk_push(OBJECT_VAL(module));
BIND_METHOD(module,__repr__);
krk_defineNative(&module->methods, "__str__", FUNC_NAME(module,__repr__));
krk_finalizeClass(module);
KRK_DOC(module, "Type of imported modules and packages.");

View File

@ -98,7 +98,7 @@ KRK_Function(open) {
#define BLOCK_SIZE 1024
KRK_Method(File,__str__) {
KRK_Method(File,__repr__) {
METHOD_TAKES_NONE();
KrkValue filename;
KrkValue modestr;
@ -520,12 +520,11 @@ KRK_Module(fileio) {
"Writes the contents of @p data to the stream.");
KRK_DOC(BIND_METHOD(File,close), "@brief Close the stream and flush any remaining buffered writes.");
KRK_DOC(BIND_METHOD(File,flush), "@brief Flush unbuffered writes to the stream.");
BIND_METHOD(File,__str__);
BIND_METHOD(File,__repr__);
KRK_DOC(BIND_METHOD(File,__init__), "@bsnote{%File objects can not be initialized using this constructor. "
"Use the <a class=\"el\" href=\"#open\">open()</a> function instead.}");
BIND_METHOD(File,__enter__);
BIND_METHOD(File,__exit__);
krk_defineNative(&File->methods, "__repr__", FUNC_NAME(File,__str__));
krk_finalizeClass(File);
KrkClass * BinaryFile = krk_makeClass(module, &fileio_BinaryFile, "BinaryFile", File);

View File

@ -570,7 +570,6 @@ KRK_Module(socket) {
BIND_PROP(socket,type);
BIND_PROP(socket,proto);
krk_defineNative(&socket->methods,"__str__", FUNC_NAME(socket,__repr__));
krk_finalizeClass(SocketClass);
BIND_FUNC(module, htons);

View File

@ -135,7 +135,7 @@ KRK_Method(type,__file__) {
return self->filename ? OBJECT_VAL(self->filename) : NONE_VAL();
}
KRK_Method(type,__str__) {
KRK_Method(type,__repr__) {
/* Determine if this class has a module */
KrkValue module = NONE_VAL();
krk_tableGet(&self->methods, OBJECT_VAL(S("__module__")), &module);
@ -234,12 +234,11 @@ void _createAndBind_type(void) {
BIND_PROP(type,__file__);
BIND_PROP(type,__name__);
BIND_METHOD(type,__str__);
BIND_METHOD(type,__repr__);
BIND_METHOD(type,__subclasses__);
BIND_METHOD(type,__getitem__);
BIND_METHOD(type,__call__);
BIND_STATICMETHOD(type,__new__);
krk_defineNative(&type->methods,"__repr__",FUNC_NAME(type,__str__));
krk_finalizeClass(type);
KRK_DOC(type, "Obtain the object representation of the class of an object.");

View File

@ -436,7 +436,6 @@ void _createAndBind_bytesClass(void) {
BIND_METHOD(bytes,__hash__);
BIND_METHOD(bytes,decode);
BIND_METHOD(bytes,join);
krk_defineNative(&bytes->methods,"__str__",FUNC_NAME(bytes,__repr__)); /* alias */
krk_finalizeClass(bytes);
KrkClass * bytesiterator = ADD_BASE_CLASS(vm.baseClasses->bytesiteratorClass, "bytesiterator", vm.baseClasses->objectClass);
@ -461,6 +460,5 @@ void _createAndBind_bytesClass(void) {
BIND_METHOD(bytearray,__eq__);
BIND_METHOD(bytearray,__iter__);
BIND_METHOD(bytearray,decode);
krk_defineNative(&bytearray->methods,"__str__",FUNC_NAME(bytearray,__repr__)); /* alias */
krk_finalizeClass(bytearray);
}

View File

@ -547,7 +547,6 @@ void _createAndBind_dictClass(void) {
BIND_METHOD(dict,setdefault);
BIND_METHOD(dict,update);
krk_defineNative(&dict->methods, "__iter__", FUNC_NAME(dict,keys));
krk_defineNative(&dict->methods, "__str__", FUNC_NAME(dict,__repr__));
krk_defineNative(&dict->methods, "__class_getitem__", krk_GenericAlias)->obj.flags |= KRK_OBJ_FLAGS_FUNCTION_IS_CLASS_METHOD;
krk_attachNamedValue(&dict->methods, "__hash__", NONE_VAL());
krk_finalizeClass(dict);

View File

@ -137,7 +137,7 @@ KRK_Method(function,_ip_to_line) {
return INTEGER_VAL(line);
}
KRK_Method(function,__str__) {
KRK_Method(function,__repr__) {
METHOD_TAKES_NONE();
/* Do we have a qualified name? */
@ -215,7 +215,7 @@ KRK_Method(codeobject,__name__) {
return self->name ? OBJECT_VAL(self->name) : OBJECT_VAL(S(""));
}
KRK_Method(codeobject,__str__) {
KRK_Method(codeobject,__repr__) {
METHOD_TAKES_NONE();
KrkValue s = FUNC_NAME(codeobject,__name__)(1,argv,0);
if (!IS_STRING(s)) return NONE_VAL();
@ -334,7 +334,7 @@ KRK_Method(method,_ip_to_line) {
return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,_ip_to_line)(2,(KrkValue[]){OBJECT_VAL(self->method),argv[1]},0) : OBJECT_VAL(S("?"));
}
KRK_Method(method,__str__) {
KRK_Method(method,__repr__) {
METHOD_TAKES_NONE();
KrkValue s = FUNC_NAME(method,__qualname__)(1,argv,0);
if (!IS_STRING(s)) s = FUNC_NAME(method,__name__)(1,argv,0);
@ -408,7 +408,7 @@ void _createAndBind_functionClass(void) {
codeobject->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
codeobject->allocSize = 0;
BIND_STATICMETHOD(codeobject,__new__);
BIND_METHOD(codeobject,__str__);
BIND_METHOD(codeobject,__repr__);
BIND_METHOD(codeobject,_ip_to_line);
BIND_PROP(codeobject,__constants__);
BIND_PROP(codeobject,__name__);
@ -420,14 +420,13 @@ void _createAndBind_functionClass(void) {
BIND_PROP(codeobject,__locals__);
BIND_PROP(codeobject,__args__);
BIND_PROP(codeobject,__file__);
krk_defineNative(&codeobject->methods, "__repr__", FUNC_NAME(codeobject,__str__));
krk_finalizeClass(codeobject);
KrkClass * function = ADD_BASE_CLASS(vm.baseClasses->functionClass, "function", vm.baseClasses->objectClass);
function->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
function->allocSize = 0;
BIND_STATICMETHOD(function,__new__);
BIND_METHOD(function,__str__);
BIND_METHOD(function,__repr__);
BIND_METHOD(function,_ip_to_line);
BIND_PROP(function,__doc__);
BIND_PROP(function,__name__);
@ -438,7 +437,6 @@ void _createAndBind_functionClass(void) {
BIND_PROP(function,__code__);
BIND_PROP(function,__globals__);
BIND_PROP(function,__closure__);
krk_defineNative(&function->methods, "__repr__", FUNC_NAME(function,__str__));
krk_defineNative(&function->methods, "__class_getitem__", krk_GenericAlias)->obj.flags |= KRK_OBJ_FLAGS_FUNCTION_IS_CLASS_METHOD;
krk_finalizeClass(function);
@ -446,7 +444,7 @@ void _createAndBind_functionClass(void) {
method->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
method->allocSize = 0;
BIND_STATICMETHOD(method,__new__);
BIND_METHOD(method,__str__);
BIND_METHOD(method,__repr__);
BIND_METHOD(method,_ip_to_line);
BIND_PROP(method,__doc__);
BIND_PROP(method,__name__);
@ -457,7 +455,6 @@ void _createAndBind_functionClass(void) {
BIND_PROP(method,__self__);
BIND_PROP(method,__func__);
BIND_PROP(method,__code__);
krk_defineNative(&method->methods, "__repr__", FUNC_NAME(method,__str__));
krk_finalizeClass(method);
BUILTIN_FUNCTION("staticmethod", FUNC_NAME(krk,staticmethod), "A static method does not take an implicit self or cls argument.");

View File

@ -272,6 +272,5 @@ void _createAndBind_generatorClass(void) {
BIND_METHOD(generator,__finish__);
BIND_METHOD(generator,send);
BIND_PROP(generator,gi_running);
krk_defineNative(&generator->methods, "__str__", FUNC_NAME(generator,__repr__));
krk_finalizeClass(generator);
}

View File

@ -640,7 +640,6 @@ void _createAndBind_listClass(void) {
"@brief Sort the contents of a list.\n\n"
"Performs an in-place sort of the elements in the list, returning @c None as a gentle reminder "
"that the sort is in-place. If a sorted copy is desired, use @ref sorted instead.");
krk_defineNative(&list->methods, "__str__", FUNC_NAME(list,__repr__));
krk_defineNative(&list->methods, "__class_getitem__", krk_GenericAlias)->obj.flags |= KRK_OBJ_FLAGS_FUNCTION_IS_CLASS_METHOD;
krk_attachNamedValue(&list->methods, "__hash__", NONE_VAL());
krk_finalizeClass(list);

View File

@ -1404,7 +1404,7 @@ KRK_Method(long,__rtruediv__) {
return OBJECT_VAL(krk_takeStringVetted(rev,size,size,KRK_OBJ_FLAGS_STRING_ASCII,hash)); \
}
PRINTER(str,10,"")
PRINTER(repr,10,"")
PRINTER(hex,16,"x0")
PRINTER(oct,8,"o0")
PRINTER(bin,2,"b0")
@ -2058,7 +2058,7 @@ void _createAndBind_longClass(void) {
_long->_ongcsweep = _long_gcsweep;
BIND_STATICMETHOD(long,__new__);
BIND_METHOD(long,__str__);
BIND_METHOD(long,__repr__);
BIND_METHOD(long,__eq__);
BIND_METHOD(long,__hash__);
BIND_METHOD(long,__hex__);
@ -2067,7 +2067,6 @@ void _createAndBind_longClass(void) {
BIND_METHOD(long,__int__);
BIND_METHOD(long,__len__);
BIND_METHOD(long,__pos__);
krk_defineNative(&_long->methods,"__repr__", FUNC_NAME(long,__str__));
BIND_TRIPLET(long,add);
BIND_TRIPLET(long,sub);

View File

@ -61,7 +61,7 @@ KRK_StaticMethod(int,__new__) {
return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%T'", "int", x);
}
KRK_Method(int,__str__) {
KRK_Method(int,__repr__) {
char tmp[100];
size_t l = snprintf(tmp, 100, PRIkrk_int, self);
return OBJECT_VAL(krk_copyString(tmp, l));
@ -628,7 +628,7 @@ static int isDigits(const char * c) {
return 1;
}
KRK_Method(float,__str__) {
KRK_Method(float,__repr__) {
char tmp[100];
size_t l = snprintf(tmp, 97, "%.16g", self);
if (!strstr(tmp,".") && isDigits(tmp)) {
@ -747,7 +747,7 @@ KRK_StaticMethod(bool,__new__) {
return BOOLEAN_VAL(!krk_isFalsey(argv[1]));
}
KRK_Method(bool,__str__) {
KRK_Method(bool,__repr__) {
return OBJECT_VAL((self ? S("True") : S("False")));
}
@ -756,7 +756,7 @@ KRK_Method(bool,__format__) {
CHECK_ARG(1,str,KrkString*,format_spec);
if (!format_spec->length) {
return FUNC_NAME(bool,__str__)(argc,argv,hasKw);
return FUNC_NAME(bool,__repr__)(argc,argv,hasKw);
} else {
return FUNC_NAME(int,__format__)(argc,argv,hasKw);
}
@ -767,7 +767,7 @@ KRK_StaticMethod(NoneType,__new__) {
return NONE_VAL();
}
KRK_Method(NoneType,__str__) {
KRK_Method(NoneType,__repr__) {
return OBJECT_VAL(S("None"));
}
@ -789,7 +789,7 @@ KRK_StaticMethod(NotImplementedType,__new__) {
return NOTIMPL_VAL();
}
KRK_Method(NotImplementedType,__str__) {
KRK_Method(NotImplementedType,__repr__) {
return OBJECT_VAL(S("NotImplemented"));
}
@ -818,7 +818,7 @@ void _createAndBind_numericClasses(void) {
_int->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
_int->allocSize = 0;
BIND_STATICMETHOD(int,__new__);
BIND_METHOD(int,__str__);
BIND_METHOD(int,__repr__);
BIND_METHOD(int,__int__);
BIND_METHOD(int,__chr__);
BIND_METHOD(int,__eq__);
@ -855,7 +855,6 @@ void _createAndBind_numericClasses(void) {
BIND_METHOD(int,__abs__);
BIND_METHOD(int,__pos__);
krk_defineNative(&_int->methods, "__repr__", FUNC_NAME(int,__str__));
krk_finalizeClass(_int);
KRK_DOC(_int, "Convert a number or string type to an integer representation.");
@ -866,7 +865,7 @@ void _createAndBind_numericClasses(void) {
BIND_STATICMETHOD(float,__new__);
BIND_METHOD(float,__int__);
BIND_METHOD(float,__float__);
BIND_METHOD(float,__str__);
BIND_METHOD(float,__repr__);
BIND_METHOD(float,__eq__);
BIND_METHOD(float,__hash__);
BIND_TRIPLET(float,add);
@ -881,7 +880,6 @@ void _createAndBind_numericClasses(void) {
BIND_METHOD(float,__neg__);
BIND_METHOD(float,__abs__);
BIND_METHOD(float,__pos__);
krk_defineNative(&_float->methods, "__repr__", FUNC_NAME(float,__str__));
#endif
krk_finalizeClass(_float);
KRK_DOC(_float, "Convert a number or string type to a float representation.");
@ -889,9 +887,8 @@ void _createAndBind_numericClasses(void) {
KrkClass * _bool = ADD_BASE_CLASS(vm.baseClasses->boolClass, "bool", vm.baseClasses->intClass);
_bool->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_STATICMETHOD(bool,__new__);
BIND_METHOD(bool,__str__);
BIND_METHOD(bool,__repr__);
BIND_METHOD(bool,__format__);
krk_defineNative(&_bool->methods, "__repr__", FUNC_NAME(bool,__str__));
krk_finalizeClass(_bool);
KRK_DOC(_bool, "Returns False if the argument is 'falsey', otherwise True.");
@ -899,20 +896,18 @@ void _createAndBind_numericClasses(void) {
_NoneType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
_NoneType->allocSize = 0;
BIND_STATICMETHOD(NoneType, __new__);
BIND_METHOD(NoneType, __str__);
BIND_METHOD(NoneType, __repr__);
BIND_METHOD(NoneType, __hash__);
BIND_METHOD(NoneType, __eq__);
krk_defineNative(&_NoneType->methods, "__repr__", FUNC_NAME(NoneType,__str__));
krk_finalizeClass(_NoneType);
KrkClass * _NotImplementedType = ADD_BASE_CLASS(vm.baseClasses->notImplClass, "NotImplementedType", vm.baseClasses->objectClass);
_NotImplementedType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
_NotImplementedType->allocSize = 0;
BIND_STATICMETHOD(NotImplementedType, __new__);
BIND_METHOD(NotImplementedType, __str__);
BIND_METHOD(NotImplementedType, __repr__);
BIND_METHOD(NotImplementedType, __hash__);
BIND_METHOD(NotImplementedType, __eq__);
krk_defineNative(&_NotImplementedType->methods, "__repr__", FUNC_NAME(NotImplementedType,__str__));
krk_finalizeClass(_NotImplementedType);
krk_attachNamedValue(&vm.builtins->fields, "NotImplemented", NOTIMPL_VAL());

View File

@ -391,7 +391,6 @@ void _createAndBind_setClass(void) {
"@brief Empty the set.\n\n"
"Removes all elements from the set, in-place.");
BIND_METHOD(set,update);
krk_defineNative(&set->methods, "__str__", FUNC_NAME(set,__repr__));
krk_attachNamedValue(&set->methods, "__hash__", NONE_VAL());
krk_finalizeClass(set);

View File

@ -177,7 +177,6 @@ void _createAndBind_sliceClass(void) {
BIND_PROP(slice,start);
BIND_PROP(slice,end);
BIND_PROP(slice,step);
krk_defineNative(&slice->methods, "__str__", FUNC_NAME(slice,__repr__));
krk_attachNamedValue(&slice->methods, "__hash__", NONE_VAL());
krk_finalizeClass(slice);
}

View File

@ -283,7 +283,6 @@ void _createAndBind_tupleClass(void) {
BIND_METHOD(tuple,__hash__);
BIND_METHOD(tuple,__add__);
BIND_METHOD(tuple,__mul__);
krk_defineNative(&tuple->methods, "__str__", FUNC_NAME(tuple,__repr__));
krk_finalizeClass(tuple);
ADD_BASE_CLASS(vm.baseClasses->tupleiteratorClass, "tupleiterator", vm.baseClasses->objectClass);