Rename __get__, __set__ to match Python's __getitem__, __setitem__ and make room for future addition of descriptors
This commit is contained in:
parent
aaac0c168a
commit
c9aa17e119
@ -13,12 +13,12 @@ class defaultdict(dict):
|
|||||||
'''Automatically called to create default values when @p key is not found.'''
|
'''Automatically called to create default values when @p key is not found.'''
|
||||||
if not self.default_factory: raise KeyError(key)
|
if not self.default_factory: raise KeyError(key)
|
||||||
let result = self.default_factory()
|
let result = self.default_factory()
|
||||||
self.__set__(key, result)
|
self.__setitem__(key, result)
|
||||||
return result
|
return result
|
||||||
def __get__(self, key):
|
def __getitem__(self, key):
|
||||||
if key not in self:
|
if key not in self:
|
||||||
return self.__missing__(key)
|
return self.__missing__(key)
|
||||||
return super().__get__(key)
|
return super().__getitem__(key)
|
||||||
|
|
||||||
class deque():
|
class deque():
|
||||||
'''
|
'''
|
||||||
@ -184,7 +184,7 @@ class deque():
|
|||||||
s = s[2]
|
s = s[2]
|
||||||
return out
|
return out
|
||||||
return _
|
return _
|
||||||
def __get__(self, index):
|
def __getitem__(self, index):
|
||||||
if index >= self._size or index < -self._size: raise IndexError("Invalid index")
|
if index >= self._size or index < -self._size: raise IndexError("Invalid index")
|
||||||
if index == 0: return self._head[1]
|
if index == 0: return self._head[1]
|
||||||
if index > 0:
|
if index > 0:
|
||||||
|
@ -100,7 +100,7 @@ KRK_METHOD(bytes,__repr__,{
|
|||||||
return finishStringBuilder(&sb);
|
return finishStringBuilder(&sb);
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(bytes,__get__,{
|
KRK_METHOD(bytes,__getitem__,{
|
||||||
METHOD_TAKES_EXACTLY(1);
|
METHOD_TAKES_EXACTLY(1);
|
||||||
CHECK_ARG(1,int,krk_integer_type,asInt);
|
CHECK_ARG(1,int,krk_integer_type,asInt);
|
||||||
|
|
||||||
@ -223,7 +223,7 @@ void _createAndBind_bytesClass(void) {
|
|||||||
BIND_METHOD(bytes,__repr__);
|
BIND_METHOD(bytes,__repr__);
|
||||||
BIND_METHOD(bytes,__len__);
|
BIND_METHOD(bytes,__len__);
|
||||||
BIND_METHOD(bytes,__contains__);
|
BIND_METHOD(bytes,__contains__);
|
||||||
BIND_METHOD(bytes,__get__);
|
BIND_METHOD(bytes,__getitem__);
|
||||||
BIND_METHOD(bytes,__eq__);
|
BIND_METHOD(bytes,__eq__);
|
||||||
BIND_METHOD(bytes,__add__);
|
BIND_METHOD(bytes,__add__);
|
||||||
BIND_METHOD(bytes,__iter__);
|
BIND_METHOD(bytes,__iter__);
|
||||||
|
@ -43,7 +43,7 @@ KRK_METHOD(dict,__init__,{
|
|||||||
return argv[0];
|
return argv[0];
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(dict,__get__,{
|
KRK_METHOD(dict,__getitem__,{
|
||||||
METHOD_TAKES_EXACTLY(1);
|
METHOD_TAKES_EXACTLY(1);
|
||||||
KrkValue out;
|
KrkValue out;
|
||||||
if (!krk_tableGet(&self->entries, argv[1], &out))
|
if (!krk_tableGet(&self->entries, argv[1], &out))
|
||||||
@ -51,7 +51,7 @@ KRK_METHOD(dict,__get__,{
|
|||||||
return out;
|
return out;
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(dict,__set__,{
|
KRK_METHOD(dict,__setitem__,{
|
||||||
METHOD_TAKES_EXACTLY(2);
|
METHOD_TAKES_EXACTLY(2);
|
||||||
krk_tableSet(&self->entries, argv[1], argv[2]);
|
krk_tableSet(&self->entries, argv[1], argv[2]);
|
||||||
})
|
})
|
||||||
@ -371,8 +371,8 @@ void _createAndBind_dictClass(void) {
|
|||||||
dict->_ongcsweep = _dict_gcsweep;
|
dict->_ongcsweep = _dict_gcsweep;
|
||||||
BIND_METHOD(dict,__init__);
|
BIND_METHOD(dict,__init__);
|
||||||
BIND_METHOD(dict,__repr__);
|
BIND_METHOD(dict,__repr__);
|
||||||
BIND_METHOD(dict,__get__);
|
BIND_METHOD(dict,__getitem__);
|
||||||
BIND_METHOD(dict,__set__);
|
BIND_METHOD(dict,__setitem__);
|
||||||
BIND_METHOD(dict,__or__);
|
BIND_METHOD(dict,__or__);
|
||||||
BIND_METHOD(dict,__delitem__);
|
BIND_METHOD(dict,__delitem__);
|
||||||
BIND_METHOD(dict,__len__);
|
BIND_METHOD(dict,__len__);
|
||||||
|
@ -47,7 +47,7 @@ KrkValue krk_list_of(int argc, KrkValue argv[], int hasKw) {
|
|||||||
#define CURRENT_CTYPE KrkList *
|
#define CURRENT_CTYPE KrkList *
|
||||||
#define CURRENT_NAME self
|
#define CURRENT_NAME self
|
||||||
|
|
||||||
KRK_METHOD(list,__get__,{
|
KRK_METHOD(list,__getitem__,{
|
||||||
METHOD_TAKES_EXACTLY(1);
|
METHOD_TAKES_EXACTLY(1);
|
||||||
CHECK_ARG(1,int,krk_integer_type,index);
|
CHECK_ARG(1,int,krk_integer_type,index);
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
pthread_rwlock_rdlock(&self->rwlock);
|
||||||
@ -57,7 +57,7 @@ KRK_METHOD(list,__get__,{
|
|||||||
return result;
|
return result;
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(list,__set__,{
|
KRK_METHOD(list,__setitem__,{
|
||||||
METHOD_TAKES_EXACTLY(2);
|
METHOD_TAKES_EXACTLY(2);
|
||||||
CHECK_ARG(1,int,krk_integer_type,index);
|
CHECK_ARG(1,int,krk_integer_type,index);
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
pthread_rwlock_rdlock(&self->rwlock);
|
||||||
@ -479,8 +479,8 @@ void _createAndBind_listClass(void) {
|
|||||||
list->_ongcscan = _list_gcscan;
|
list->_ongcscan = _list_gcscan;
|
||||||
list->_ongcsweep = _list_gcsweep;
|
list->_ongcsweep = _list_gcsweep;
|
||||||
BIND_METHOD(list,__init__);
|
BIND_METHOD(list,__init__);
|
||||||
BIND_METHOD(list,__get__);
|
BIND_METHOD(list,__getitem__);
|
||||||
BIND_METHOD(list,__set__);
|
BIND_METHOD(list,__setitem__);
|
||||||
BIND_METHOD(list,__len__);
|
BIND_METHOD(list,__len__);
|
||||||
BIND_METHOD(list,__repr__);
|
BIND_METHOD(list,__repr__);
|
||||||
BIND_METHOD(list,__contains__);
|
BIND_METHOD(list,__contains__);
|
||||||
|
@ -66,7 +66,7 @@ KRK_METHOD(str,__len__,{
|
|||||||
return INTEGER_VAL(self->codesLength);
|
return INTEGER_VAL(self->codesLength);
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(str,__set__,{
|
KRK_METHOD(str,__setitem__,{
|
||||||
return krk_runtimeError(vm.exceptions->typeError, "Strings are not mutable.");
|
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));
|
return FLOATING_VAL(strtod(AS_CSTRING(argv[0]),NULL));
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(str,__get__,{
|
KRK_METHOD(str,__getitem__,{
|
||||||
METHOD_TAKES_EXACTLY(1);
|
METHOD_TAKES_EXACTLY(1);
|
||||||
CHECK_ARG(1,int,krk_integer_type,asInt);
|
CHECK_ARG(1,int,krk_integer_type,asInt);
|
||||||
if (asInt < 0) asInt += (int)AS_STRING(argv[0])->codesLength;
|
if (asInt < 0) asInt += (int)AS_STRING(argv[0])->codesLength;
|
||||||
@ -867,7 +867,7 @@ KRK_METHOD(striterator,__call__,{
|
|||||||
return argv[0];
|
return argv[0];
|
||||||
} else {
|
} else {
|
||||||
krk_attachNamedValue(&self->fields, "i", INTEGER_VAL(AS_INTEGER(_counter)+1));
|
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:
|
_corrupt:
|
||||||
return krk_runtimeError(vm.exceptions->typeError, "Corrupt str iterator: %s", errorStr);
|
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,__int__);
|
||||||
BIND_METHOD(str,__float__);
|
BIND_METHOD(str,__float__);
|
||||||
BIND_METHOD(str,__getslice__);
|
BIND_METHOD(str,__getslice__);
|
||||||
BIND_METHOD(str,__get__);
|
BIND_METHOD(str,__getitem__);
|
||||||
BIND_METHOD(str,__set__);
|
BIND_METHOD(str,__setitem__);
|
||||||
BIND_METHOD(str,__add__);
|
BIND_METHOD(str,__add__);
|
||||||
BIND_METHOD(str,__len__);
|
BIND_METHOD(str,__len__);
|
||||||
BIND_METHOD(str,__mul__);
|
BIND_METHOD(str,__mul__);
|
||||||
@ -920,9 +920,9 @@ void _createAndBind_strClass(void) {
|
|||||||
BIND_METHOD(str,upper);
|
BIND_METHOD(str,upper);
|
||||||
BIND_METHOD(str,title);
|
BIND_METHOD(str,title);
|
||||||
|
|
||||||
krk_defineNative(&str->methods,".__setslice__",FUNC_NAME(str,__set__));
|
krk_defineNative(&str->methods,".__setslice__",FUNC_NAME(str,__setitem__));
|
||||||
krk_defineNative(&str->methods,".__delslice__",FUNC_NAME(str,__set__));
|
krk_defineNative(&str->methods,".__delslice__",FUNC_NAME(str,__setitem__));
|
||||||
krk_defineNative(&str->methods,".__delitem__",FUNC_NAME(str,__set__));
|
krk_defineNative(&str->methods,".__delitem__",FUNC_NAME(str,__setitem__));
|
||||||
krk_finalizeClass(str);
|
krk_finalizeClass(str);
|
||||||
KRK_DOC(str, "Obtain a string representation of an object.");
|
KRK_DOC(str, "Obtain a string representation of an object.");
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ KRK_METHOD(tuple,__len__,{
|
|||||||
return INTEGER_VAL(self->values.count);
|
return INTEGER_VAL(self->values.count);
|
||||||
})
|
})
|
||||||
|
|
||||||
KRK_METHOD(tuple,__get__,{
|
KRK_METHOD(tuple,__getitem__,{
|
||||||
METHOD_TAKES_EXACTLY(1);
|
METHOD_TAKES_EXACTLY(1);
|
||||||
CHECK_ARG(1,int,krk_integer_type,index);
|
CHECK_ARG(1,int,krk_integer_type,index);
|
||||||
TUPLE_WRAP_INDEX();
|
TUPLE_WRAP_INDEX();
|
||||||
@ -157,7 +157,7 @@ _noexport
|
|||||||
void _createAndBind_tupleClass(void) {
|
void _createAndBind_tupleClass(void) {
|
||||||
KrkClass * tuple = ADD_BASE_CLASS(vm.baseClasses->tupleClass, "tuple", vm.baseClasses->objectClass);
|
KrkClass * tuple = ADD_BASE_CLASS(vm.baseClasses->tupleClass, "tuple", vm.baseClasses->objectClass);
|
||||||
BIND_METHOD(tuple,__repr__);
|
BIND_METHOD(tuple,__repr__);
|
||||||
BIND_METHOD(tuple,__get__);
|
BIND_METHOD(tuple,__getitem__);
|
||||||
BIND_METHOD(tuple,__len__);
|
BIND_METHOD(tuple,__len__);
|
||||||
BIND_METHOD(tuple,__contains__);
|
BIND_METHOD(tuple,__contains__);
|
||||||
BIND_METHOD(tuple,__iter__);
|
BIND_METHOD(tuple,__iter__);
|
||||||
|
4
src/os.c
4
src/os.c
@ -94,7 +94,7 @@ static KrkClass * environClass;
|
|||||||
KrkValue krk_os_setenviron(int argc, KrkValue argv[], int hasKw) {
|
KrkValue krk_os_setenviron(int argc, KrkValue argv[], int hasKw) {
|
||||||
if (argc < 3 || !krk_isInstanceOf(argv[0], environClass) ||
|
if (argc < 3 || !krk_isInstanceOf(argv[0], environClass) ||
|
||||||
!IS_STRING(argv[1]) || !IS_STRING(argv[2])) {
|
!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 */
|
/* Set environment variable */
|
||||||
size_t len = AS_STRING(argv[1])->length + AS_STRING(argv[2])->length + 2;
|
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 */
|
krk_pop(); /* className */
|
||||||
|
|
||||||
/* Add our set method that should also call dict's set method */
|
/* 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_defineNative(&environClass->methods, ".__delitem__", krk_os_unsetenviron);
|
||||||
krk_finalizeClass(environClass);
|
krk_finalizeClass(environClass);
|
||||||
|
|
||||||
|
31
src/vm.c
31
src/vm.c
@ -1151,32 +1151,41 @@ void krk_initVM(int flags) {
|
|||||||
struct { const char * s; size_t len; } _methods[] = {
|
struct { const char * s; size_t len; } _methods[] = {
|
||||||
#define _(m,s) [m] = {s,sizeof(s)-1}
|
#define _(m,s) [m] = {s,sizeof(s)-1}
|
||||||
_(METHOD_INIT, "__init__"),
|
_(METHOD_INIT, "__init__"),
|
||||||
|
/* String conversion */
|
||||||
_(METHOD_STR, "__str__"),
|
_(METHOD_STR, "__str__"),
|
||||||
_(METHOD_REPR, "__repr__"),
|
_(METHOD_REPR, "__repr__"),
|
||||||
_(METHOD_GET, "__get__"),
|
/* Subscripting / Indexing */
|
||||||
_(METHOD_SET, "__set__"),
|
_(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_CLASS, "__class__"),
|
||||||
_(METHOD_NAME, "__name__"),
|
_(METHOD_NAME, "__name__"),
|
||||||
_(METHOD_FILE, "__file__"),
|
_(METHOD_FILE, "__file__"),
|
||||||
|
_(METHOD_DOC, "__doc__"),
|
||||||
|
_(METHOD_BASE, "__base__"),
|
||||||
|
/* Numeric conversions */
|
||||||
_(METHOD_INT, "__int__"),
|
_(METHOD_INT, "__int__"),
|
||||||
_(METHOD_CHR, "__chr__"),
|
_(METHOD_CHR, "__chr__"),
|
||||||
_(METHOD_ORD, "__ord__"),
|
_(METHOD_ORD, "__ord__"),
|
||||||
_(METHOD_FLOAT, "__float__"),
|
_(METHOD_FLOAT, "__float__"),
|
||||||
_(METHOD_LEN, "__len__"),
|
/* General overridable methods */
|
||||||
_(METHOD_DOC, "__doc__"),
|
|
||||||
_(METHOD_BASE, "__base__"),
|
|
||||||
_(METHOD_CALL, "__call__"),
|
_(METHOD_CALL, "__call__"),
|
||||||
_(METHOD_GETSLICE, "__getslice__"),
|
|
||||||
_(METHOD_SETSLICE, "__setslice__"),
|
|
||||||
_(METHOD_DELSLICE, "__delslice__"),
|
|
||||||
_(METHOD_EQ, "__eq__"),
|
_(METHOD_EQ, "__eq__"),
|
||||||
|
/* Iterables */
|
||||||
|
_(METHOD_ITER, "__iter__"),
|
||||||
|
_(METHOD_CONTAINS, "__contains__"),
|
||||||
|
/* Context managers */
|
||||||
_(METHOD_ENTER, "__enter__"),
|
_(METHOD_ENTER, "__enter__"),
|
||||||
_(METHOD_EXIT, "__exit__"),
|
_(METHOD_EXIT, "__exit__"),
|
||||||
_(METHOD_DELITEM, "__delitem__"),
|
/* Attribute access */
|
||||||
_(METHOD_ITER, "__iter__"),
|
|
||||||
_(METHOD_GETATTR, "__getattr__"),
|
_(METHOD_GETATTR, "__getattr__"),
|
||||||
_(METHOD_DIR, "__dir__"),
|
_(METHOD_DIR, "__dir__"),
|
||||||
_(METHOD_CONTAINS, "__contains__"),
|
|
||||||
#undef _
|
#undef _
|
||||||
};
|
};
|
||||||
for (size_t i = 0; i < METHOD__MAX; ++i) {
|
for (size_t i = 0; i < METHOD__MAX; ++i) {
|
||||||
|
4
src/vm.h
4
src/vm.h
@ -777,8 +777,8 @@ extern void krk_addObjects(void);
|
|||||||
* FIXME This stuff needs to be moved to another header! FIXME
|
* FIXME This stuff needs to be moved to another header! FIXME
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern KrkValue _str___get__(int argc, KrkValue argv[], int hasKw);
|
extern KrkValue _str___getitem__(int argc, KrkValue argv[], int hasKw);
|
||||||
#define krk_string_get _str___get__
|
#define krk_string_get _str___getitem__
|
||||||
extern KrkValue _str___int__(int argc, KrkValue argv[], int hasKw);
|
extern KrkValue _str___int__(int argc, KrkValue argv[], int hasKw);
|
||||||
#define krk_string_int _str___int__
|
#define krk_string_int _str___int__
|
||||||
extern KrkValue _str___float__(int argc, KrkValue argv[], int hasKw);
|
extern KrkValue _str___float__(int argc, KrkValue argv[], int hasKw);
|
||||||
|
@ -125,9 +125,9 @@ class SuperClass():
|
|||||||
return "(I am a " + self.a + ")"
|
return "(I am a " + self.a + ")"
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "(I am a " + self.a + ")"
|
return "(I am a " + self.a + ")"
|
||||||
def __get__(self, ind):
|
def __getitem__(self, ind):
|
||||||
return "(get[" + str(ind) + "])"
|
return "(get[" + str(ind) + "])"
|
||||||
def __set__(self, ind, val):
|
def __setitem__(self, ind, val):
|
||||||
print("(set[" + str(ind) + "] = " + val + ")")
|
print("(set[" + str(ind) + "] = " + val + ")")
|
||||||
|
|
||||||
class SubClass(SuperClass):
|
class SubClass(SuperClass):
|
||||||
@ -148,7 +148,7 @@ subclass.aMethod()
|
|||||||
|
|
||||||
print("Subclass says:", subclass)
|
print("Subclass says:", subclass)
|
||||||
|
|
||||||
subclass.__get__(123)
|
subclass.__getitem__(123)
|
||||||
print(subclass[123])
|
print(subclass[123])
|
||||||
subclass[456] = "test"
|
subclass[456] = "test"
|
||||||
|
|
||||||
|
@ -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])
|
print("Test"[1])
|
||||||
|
|
||||||
class Test:
|
class Test:
|
||||||
|
@ -25,7 +25,7 @@ let blacklistedMethods = [
|
|||||||
let specialMethods = {
|
let specialMethods = {
|
||||||
'__contains__': lambda cls, args: f'{args or "<i>needle</i>"} <b>in</b> {cls}',
|
'__contains__': lambda cls, args: f'{args or "<i>needle</i>"} <b>in</b> {cls}',
|
||||||
'__init__': lambda cls, args: f'let <i>x</i> = <b>{cls}</b>({args})',
|
'__init__': lambda cls, args: f'let <i>x</i> = <b>{cls}</b>({args})',
|
||||||
'__get__': lambda cls, args: f'{cls}[{args or "<i>key</i>"}]',
|
'__getitem__': lambda cls, args: f'{cls}[{args or "<i>key</i>"}]',
|
||||||
'__delitem__': lambda cls, args: f'<b>del</b> {cls}[{args or "<i>key</i>"}]',
|
'__delitem__': lambda cls, args: f'<b>del</b> {cls}[{args or "<i>key</i>"}]',
|
||||||
'__add__': lambda cls, args: f'{cls} + {args or "<i>other</i>"}',
|
'__add__': lambda cls, args: f'{cls} + {args or "<i>other</i>"}',
|
||||||
'__len__': lambda cls, args: f'<b>len</b>({cls})',
|
'__len__': lambda cls, args: f'<b>len</b>({cls})',
|
||||||
|
Loading…
Reference in New Issue
Block a user