From c9aa17e11974d393c4e18c8fac88409863da42e2 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Wed, 10 Mar 2021 14:24:22 +0900 Subject: [PATCH] Rename __get__, __set__ to match Python's __getitem__, __setitem__ and make room for future addition of descriptors --- modules/collections.krk | 8 ++++---- src/obj_bytes.c | 4 ++-- src/obj_dict.c | 8 ++++---- src/obj_list.c | 8 ++++---- src/obj_str.c | 16 ++++++++-------- src/obj_tuple.c | 4 ++-- src/os.c | 4 ++-- src/vm.c | 31 ++++++++++++++++++++----------- src/vm.h | 4 ++-- test/test.krk | 6 +++--- test/testBindNativeMethod.krk | 2 +- tools/gendoc.krk | 2 +- 12 files changed, 53 insertions(+), 44 deletions(-) diff --git a/modules/collections.krk b/modules/collections.krk index 000b8ed..2229381 100644 --- a/modules/collections.krk +++ b/modules/collections.krk @@ -13,12 +13,12 @@ class defaultdict(dict): '''Automatically called to create default values when @p key is not found.''' if not self.default_factory: raise KeyError(key) let result = self.default_factory() - self.__set__(key, result) + self.__setitem__(key, result) return result - def __get__(self, key): + def __getitem__(self, key): if key not in self: return self.__missing__(key) - return super().__get__(key) + return super().__getitem__(key) class deque(): ''' @@ -184,7 +184,7 @@ class deque(): s = s[2] return out return _ - def __get__(self, index): + def __getitem__(self, index): if index >= self._size or index < -self._size: raise IndexError("Invalid index") if index == 0: return self._head[1] if index > 0: diff --git a/src/obj_bytes.c b/src/obj_bytes.c index 31c7cd4..3dc7396 100644 --- a/src/obj_bytes.c +++ b/src/obj_bytes.c @@ -100,7 +100,7 @@ KRK_METHOD(bytes,__repr__,{ return finishStringBuilder(&sb); }) -KRK_METHOD(bytes,__get__,{ +KRK_METHOD(bytes,__getitem__,{ METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,asInt); @@ -223,7 +223,7 @@ void _createAndBind_bytesClass(void) { BIND_METHOD(bytes,__repr__); BIND_METHOD(bytes,__len__); BIND_METHOD(bytes,__contains__); - BIND_METHOD(bytes,__get__); + BIND_METHOD(bytes,__getitem__); BIND_METHOD(bytes,__eq__); BIND_METHOD(bytes,__add__); BIND_METHOD(bytes,__iter__); diff --git a/src/obj_dict.c b/src/obj_dict.c index 9ef8b4a..1524942 100644 --- a/src/obj_dict.c +++ b/src/obj_dict.c @@ -43,7 +43,7 @@ KRK_METHOD(dict,__init__,{ return argv[0]; }) -KRK_METHOD(dict,__get__,{ +KRK_METHOD(dict,__getitem__,{ METHOD_TAKES_EXACTLY(1); KrkValue out; if (!krk_tableGet(&self->entries, argv[1], &out)) @@ -51,7 +51,7 @@ KRK_METHOD(dict,__get__,{ return out; }) -KRK_METHOD(dict,__set__,{ +KRK_METHOD(dict,__setitem__,{ METHOD_TAKES_EXACTLY(2); krk_tableSet(&self->entries, argv[1], argv[2]); }) @@ -371,8 +371,8 @@ void _createAndBind_dictClass(void) { dict->_ongcsweep = _dict_gcsweep; BIND_METHOD(dict,__init__); BIND_METHOD(dict,__repr__); - BIND_METHOD(dict,__get__); - BIND_METHOD(dict,__set__); + BIND_METHOD(dict,__getitem__); + BIND_METHOD(dict,__setitem__); BIND_METHOD(dict,__or__); BIND_METHOD(dict,__delitem__); BIND_METHOD(dict,__len__); diff --git a/src/obj_list.c b/src/obj_list.c index 9050d84..e262d37 100644 --- a/src/obj_list.c +++ b/src/obj_list.c @@ -47,7 +47,7 @@ KrkValue krk_list_of(int argc, KrkValue argv[], int hasKw) { #define CURRENT_CTYPE KrkList * #define CURRENT_NAME self -KRK_METHOD(list,__get__,{ +KRK_METHOD(list,__getitem__,{ METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,index); pthread_rwlock_rdlock(&self->rwlock); @@ -57,7 +57,7 @@ KRK_METHOD(list,__get__,{ return result; }) -KRK_METHOD(list,__set__,{ +KRK_METHOD(list,__setitem__,{ METHOD_TAKES_EXACTLY(2); CHECK_ARG(1,int,krk_integer_type,index); pthread_rwlock_rdlock(&self->rwlock); @@ -479,8 +479,8 @@ void _createAndBind_listClass(void) { list->_ongcscan = _list_gcscan; list->_ongcsweep = _list_gcsweep; BIND_METHOD(list,__init__); - BIND_METHOD(list,__get__); - BIND_METHOD(list,__set__); + BIND_METHOD(list,__getitem__); + BIND_METHOD(list,__setitem__); BIND_METHOD(list,__len__); BIND_METHOD(list,__repr__); BIND_METHOD(list,__contains__); diff --git a/src/obj_str.c b/src/obj_str.c index e73e98a..5e64090 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -66,7 +66,7 @@ KRK_METHOD(str,__len__,{ return INTEGER_VAL(self->codesLength); }) -KRK_METHOD(str,__set__,{ +KRK_METHOD(str,__setitem__,{ return krk_runtimeError(vm.exceptions->typeError, "Strings are not mutable."); }) @@ -135,7 +135,7 @@ KRK_METHOD(str,__float__,{ return FLOATING_VAL(strtod(AS_CSTRING(argv[0]),NULL)); }) -KRK_METHOD(str,__get__,{ +KRK_METHOD(str,__getitem__,{ METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,asInt); if (asInt < 0) asInt += (int)AS_STRING(argv[0])->codesLength; @@ -867,7 +867,7 @@ KRK_METHOD(striterator,__call__,{ return argv[0]; } else { krk_attachNamedValue(&self->fields, "i", INTEGER_VAL(AS_INTEGER(_counter)+1)); - return FUNC_NAME(str,__get__)(2,(KrkValue[]){_str,_counter},3); + return FUNC_NAME(str,__getitem__)(2,(KrkValue[]){_str,_counter},3); } _corrupt: return krk_runtimeError(vm.exceptions->typeError, "Corrupt str iterator: %s", errorStr); @@ -882,8 +882,8 @@ void _createAndBind_strClass(void) { BIND_METHOD(str,__int__); BIND_METHOD(str,__float__); BIND_METHOD(str,__getslice__); - BIND_METHOD(str,__get__); - BIND_METHOD(str,__set__); + BIND_METHOD(str,__getitem__); + BIND_METHOD(str,__setitem__); BIND_METHOD(str,__add__); BIND_METHOD(str,__len__); BIND_METHOD(str,__mul__); @@ -920,9 +920,9 @@ void _createAndBind_strClass(void) { BIND_METHOD(str,upper); BIND_METHOD(str,title); - krk_defineNative(&str->methods,".__setslice__",FUNC_NAME(str,__set__)); - krk_defineNative(&str->methods,".__delslice__",FUNC_NAME(str,__set__)); - krk_defineNative(&str->methods,".__delitem__",FUNC_NAME(str,__set__)); + krk_defineNative(&str->methods,".__setslice__",FUNC_NAME(str,__setitem__)); + krk_defineNative(&str->methods,".__delslice__",FUNC_NAME(str,__setitem__)); + krk_defineNative(&str->methods,".__delitem__",FUNC_NAME(str,__setitem__)); krk_finalizeClass(str); KRK_DOC(str, "Obtain a string representation of an object."); diff --git a/src/obj_tuple.c b/src/obj_tuple.c index 18134db..3d9f158 100644 --- a/src/obj_tuple.c +++ b/src/obj_tuple.c @@ -66,7 +66,7 @@ KRK_METHOD(tuple,__len__,{ return INTEGER_VAL(self->values.count); }) -KRK_METHOD(tuple,__get__,{ +KRK_METHOD(tuple,__getitem__,{ METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,index); TUPLE_WRAP_INDEX(); @@ -157,7 +157,7 @@ _noexport void _createAndBind_tupleClass(void) { KrkClass * tuple = ADD_BASE_CLASS(vm.baseClasses->tupleClass, "tuple", vm.baseClasses->objectClass); BIND_METHOD(tuple,__repr__); - BIND_METHOD(tuple,__get__); + BIND_METHOD(tuple,__getitem__); BIND_METHOD(tuple,__len__); BIND_METHOD(tuple,__contains__); BIND_METHOD(tuple,__iter__); diff --git a/src/os.c b/src/os.c index fe8a661..3b8d2c6 100644 --- a/src/os.c +++ b/src/os.c @@ -94,7 +94,7 @@ static KrkClass * environClass; KrkValue krk_os_setenviron(int argc, KrkValue argv[], int hasKw) { if (argc < 3 || !krk_isInstanceOf(argv[0], environClass) || !IS_STRING(argv[1]) || !IS_STRING(argv[2])) { - return krk_runtimeError(vm.exceptions->argumentError, "Invalid arguments to environ.__set__"); + return krk_runtimeError(vm.exceptions->argumentError, "Invalid arguments to environ.__setitem__"); } /* Set environment variable */ size_t len = AS_STRING(argv[1])->length + AS_STRING(argv[2])->length + 2; @@ -140,7 +140,7 @@ static void _loadEnviron(KrkInstance * module) { krk_pop(); /* className */ /* Add our set method that should also call dict's set method */ - krk_defineNative(&environClass->methods, ".__set__", krk_os_setenviron); + krk_defineNative(&environClass->methods, ".__setitem__", krk_os_setenviron); krk_defineNative(&environClass->methods, ".__delitem__", krk_os_unsetenviron); krk_finalizeClass(environClass); diff --git a/src/vm.c b/src/vm.c index d18aa21..eaa4052 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1151,32 +1151,41 @@ void krk_initVM(int flags) { struct { const char * s; size_t len; } _methods[] = { #define _(m,s) [m] = {s,sizeof(s)-1} _(METHOD_INIT, "__init__"), + /* String conversion */ _(METHOD_STR, "__str__"), _(METHOD_REPR, "__repr__"), - _(METHOD_GET, "__get__"), - _(METHOD_SET, "__set__"), + /* Subscripting / Indexing */ + _(METHOD_LEN, "__len__"), + _(METHOD_GET, "__getitem__"), + _(METHOD_SET, "__setitem__"), + _(METHOD_DELITEM, "__delitem__"), + /* Slice subscripting */ + _(METHOD_GETSLICE, "__getslice__"), + _(METHOD_SETSLICE, "__setslice__"), + _(METHOD_DELSLICE, "__delslice__"), + /* Dynamic properties */ _(METHOD_CLASS, "__class__"), _(METHOD_NAME, "__name__"), _(METHOD_FILE, "__file__"), + _(METHOD_DOC, "__doc__"), + _(METHOD_BASE, "__base__"), + /* Numeric conversions */ _(METHOD_INT, "__int__"), _(METHOD_CHR, "__chr__"), _(METHOD_ORD, "__ord__"), _(METHOD_FLOAT, "__float__"), - _(METHOD_LEN, "__len__"), - _(METHOD_DOC, "__doc__"), - _(METHOD_BASE, "__base__"), + /* General overridable methods */ _(METHOD_CALL, "__call__"), - _(METHOD_GETSLICE, "__getslice__"), - _(METHOD_SETSLICE, "__setslice__"), - _(METHOD_DELSLICE, "__delslice__"), _(METHOD_EQ, "__eq__"), + /* Iterables */ + _(METHOD_ITER, "__iter__"), + _(METHOD_CONTAINS, "__contains__"), + /* Context managers */ _(METHOD_ENTER, "__enter__"), _(METHOD_EXIT, "__exit__"), - _(METHOD_DELITEM, "__delitem__"), - _(METHOD_ITER, "__iter__"), + /* Attribute access */ _(METHOD_GETATTR, "__getattr__"), _(METHOD_DIR, "__dir__"), - _(METHOD_CONTAINS, "__contains__"), #undef _ }; for (size_t i = 0; i < METHOD__MAX; ++i) { diff --git a/src/vm.h b/src/vm.h index 8c665b3..5cfc799 100644 --- a/src/vm.h +++ b/src/vm.h @@ -777,8 +777,8 @@ extern void krk_addObjects(void); * FIXME This stuff needs to be moved to another header! FIXME */ -extern KrkValue _str___get__(int argc, KrkValue argv[], int hasKw); -#define krk_string_get _str___get__ +extern KrkValue _str___getitem__(int argc, KrkValue argv[], int hasKw); +#define krk_string_get _str___getitem__ extern KrkValue _str___int__(int argc, KrkValue argv[], int hasKw); #define krk_string_int _str___int__ extern KrkValue _str___float__(int argc, KrkValue argv[], int hasKw); diff --git a/test/test.krk b/test/test.krk index 3d17f59..57ea027 100755 --- a/test/test.krk +++ b/test/test.krk @@ -125,9 +125,9 @@ class SuperClass(): return "(I am a " + self.a + ")" def __repr__(self): return "(I am a " + self.a + ")" - def __get__(self, ind): + def __getitem__(self, ind): return "(get[" + str(ind) + "])" - def __set__(self, ind, val): + def __setitem__(self, ind, val): print("(set[" + str(ind) + "] = " + val + ")") class SubClass(SuperClass): @@ -148,7 +148,7 @@ subclass.aMethod() print("Subclass says:", subclass) -subclass.__get__(123) +subclass.__getitem__(123) print(subclass[123]) subclass[456] = "test" diff --git a/test/testBindNativeMethod.krk b/test/testBindNativeMethod.krk index e640216..6b6ad7d 100644 --- a/test/testBindNativeMethod.krk +++ b/test/testBindNativeMethod.krk @@ -1,4 +1,4 @@ -# Strings have a syntehesized __get__ so they can be indexed... +# Strings have a syntehesized __getitem__ so they can be indexed... print("Test"[1]) class Test: diff --git a/tools/gendoc.krk b/tools/gendoc.krk index 755d44e..cd069a3 100755 --- a/tools/gendoc.krk +++ b/tools/gendoc.krk @@ -25,7 +25,7 @@ let blacklistedMethods = [ let specialMethods = { '__contains__': lambda cls, args: f'{args or "needle"} in {cls}', '__init__': lambda cls, args: f'let x = {cls}({args})', - '__get__': lambda cls, args: f'{cls}[{args or "key"}]', + '__getitem__': lambda cls, args: f'{cls}[{args or "key"}]', '__delitem__': lambda cls, args: f'del {cls}[{args or "key"}]', '__add__': lambda cls, args: f'{cls} + {args or "other"}', '__len__': lambda cls, args: f'len({cls})',