objtype.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2018 Damien P. George
  7. * Copyright (c) 2014-2016 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <stddef.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include "py/objtype.h"
  32. #include "py/runtime.h"
  33. #if MICROPY_DEBUG_VERBOSE // print debugging info
  34. #define DEBUG_PRINT (1)
  35. #define DEBUG_printf DEBUG_printf
  36. #else // don't print debugging info
  37. #define DEBUG_PRINT (0)
  38. #define DEBUG_printf(...) (void)0
  39. #endif
  40. #define ENABLE_SPECIAL_ACCESSORS \
  41. (MICROPY_PY_DESCRIPTORS || MICROPY_PY_DELATTR_SETATTR || MICROPY_PY_BUILTINS_PROPERTY)
  42. #define TYPE_FLAG_IS_SUBCLASSED (0x0001)
  43. #define TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
  44. STATIC mp_obj_t static_class_method_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
  45. /******************************************************************************/
  46. // instance object
  47. STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_type_t **last_native_base) {
  48. int count = 0;
  49. for (;;) {
  50. if (type == &mp_type_object) {
  51. // Not a "real" type, end search here.
  52. return count;
  53. } else if (mp_obj_is_native_type(type)) {
  54. // Native types don't have parents (at least not from our perspective) so end.
  55. *last_native_base = type;
  56. return count + 1;
  57. } else if (type->parent == NULL) {
  58. // No parents so end search here.
  59. return count;
  60. #if MICROPY_MULTIPLE_INHERITANCE
  61. } else if (((mp_obj_base_t*)type->parent)->type == &mp_type_tuple) {
  62. // Multiple parents, search through them all recursively.
  63. const mp_obj_tuple_t *parent_tuple = type->parent;
  64. const mp_obj_t *item = parent_tuple->items;
  65. const mp_obj_t *top = item + parent_tuple->len;
  66. for (; item < top; ++item) {
  67. assert(MP_OBJ_IS_TYPE(*item, &mp_type_type));
  68. const mp_obj_type_t *bt = (const mp_obj_type_t *)MP_OBJ_TO_PTR(*item);
  69. count += instance_count_native_bases(bt, last_native_base);
  70. }
  71. return count;
  72. #endif
  73. } else {
  74. // A single parent, use iteration to continue the search.
  75. type = type->parent;
  76. }
  77. }
  78. }
  79. // This wrapper function is allows a subclass of a native type to call the
  80. // __init__() method (corresponding to type->make_new) of the native type.
  81. STATIC mp_obj_t native_base_init_wrapper(size_t n_args, const mp_obj_t *args) {
  82. mp_obj_instance_t *self = MP_OBJ_TO_PTR(args[0]);
  83. const mp_obj_type_t *native_base = NULL;
  84. instance_count_native_bases(self->base.type, &native_base);
  85. self->subobj[0] = native_base->make_new(native_base, n_args - 1, 0, args + 1);
  86. return mp_const_none;
  87. }
  88. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(native_base_init_wrapper_obj, 1, MP_OBJ_FUN_ARGS_MAX, native_base_init_wrapper);
  89. #if !MICROPY_CPYTHON_COMPAT
  90. STATIC
  91. #endif
  92. mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_type_t **native_base) {
  93. size_t num_native_bases = instance_count_native_bases(class, native_base);
  94. assert(num_native_bases < 2);
  95. mp_obj_instance_t *o = m_new_obj_var(mp_obj_instance_t, mp_obj_t, num_native_bases);
  96. o->base.type = class;
  97. mp_map_init(&o->members, 0);
  98. // Initialise the native base-class slot (should be 1 at most) with a valid
  99. // object. It doesn't matter which object, so long as it can be uniquely
  100. // distinguished from a native class that is initialised.
  101. if (num_native_bases != 0) {
  102. o->subobj[0] = MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj);
  103. }
  104. return o;
  105. }
  106. // TODO
  107. // This implements depth-first left-to-right MRO, which is not compliant with Python3 MRO
  108. // http://python-history.blogspot.com/2010/06/method-resolution-order.html
  109. // https://www.python.org/download/releases/2.3/mro/
  110. //
  111. // will keep lookup->dest[0]'s value (should be MP_OBJ_NULL on invocation) if attribute
  112. // is not found
  113. // will set lookup->dest[0] to MP_OBJ_SENTINEL if special method was found in a native
  114. // type base via slot id (as specified by lookup->meth_offset). As there can be only one
  115. // native base, it's known that it applies to instance->subobj[0]. In most cases, we also
  116. // don't need to know which type it was - because instance->subobj[0] is of that type.
  117. // The only exception is when object is not yet constructed, then we need to know base
  118. // native type to construct its instance->subobj[0] from. But this case is handled via
  119. // instance_count_native_bases(), which returns a native base which it saw.
  120. struct class_lookup_data {
  121. mp_obj_instance_t *obj;
  122. qstr attr;
  123. size_t meth_offset;
  124. mp_obj_t *dest;
  125. bool is_type;
  126. };
  127. STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_type_t *type) {
  128. assert(lookup->dest[0] == MP_OBJ_NULL);
  129. assert(lookup->dest[1] == MP_OBJ_NULL);
  130. for (;;) {
  131. DEBUG_printf("mp_obj_class_lookup: Looking up %s in %s\n", qstr_str(lookup->attr), qstr_str(type->name));
  132. // Optimize special method lookup for native types
  133. // This avoids extra method_name => slot lookup. On the other hand,
  134. // this should not be applied to class types, as will result in extra
  135. // lookup either.
  136. if (lookup->meth_offset != 0 && mp_obj_is_native_type(type)) {
  137. if (*(void**)((char*)type + lookup->meth_offset) != NULL) {
  138. DEBUG_printf("mp_obj_class_lookup: Matched special meth slot (off=%d) for %s\n",
  139. lookup->meth_offset, qstr_str(lookup->attr));
  140. lookup->dest[0] = MP_OBJ_SENTINEL;
  141. return;
  142. }
  143. }
  144. if (type->locals_dict != NULL) {
  145. // search locals_dict (the set of methods/attributes)
  146. assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
  147. mp_map_t *locals_map = &type->locals_dict->map;
  148. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(lookup->attr), MP_MAP_LOOKUP);
  149. if (elem != NULL) {
  150. if (lookup->is_type) {
  151. // If we look up a class method, we need to return original type for which we
  152. // do a lookup, not a (base) type in which we found the class method.
  153. const mp_obj_type_t *org_type = (const mp_obj_type_t*)lookup->obj;
  154. mp_convert_member_lookup(MP_OBJ_NULL, org_type, elem->value, lookup->dest);
  155. } else {
  156. mp_obj_instance_t *obj = lookup->obj;
  157. mp_obj_t obj_obj;
  158. if (obj != NULL && mp_obj_is_native_type(type) && type != &mp_type_object /* object is not a real type */) {
  159. // If we're dealing with native base class, then it applies to native sub-object
  160. obj_obj = obj->subobj[0];
  161. } else {
  162. obj_obj = MP_OBJ_FROM_PTR(obj);
  163. }
  164. mp_convert_member_lookup(obj_obj, type, elem->value, lookup->dest);
  165. }
  166. #if DEBUG_PRINT
  167. DEBUG_printf("mp_obj_class_lookup: Returning: ");
  168. mp_obj_print_helper(MICROPY_DEBUG_PRINTER, lookup->dest[0], PRINT_REPR);
  169. if (lookup->dest[1] != MP_OBJ_NULL) {
  170. // Don't try to repr() lookup->dest[1], as we can be called recursively
  171. DEBUG_printf(" <%s @%p>", mp_obj_get_type_str(lookup->dest[1]), MP_OBJ_TO_PTR(lookup->dest[1]));
  172. }
  173. DEBUG_printf("\n");
  174. #endif
  175. return;
  176. }
  177. }
  178. // Previous code block takes care about attributes defined in .locals_dict,
  179. // but some attributes of native types may be handled using .load_attr method,
  180. // so make sure we try to lookup those too.
  181. if (lookup->obj != NULL && !lookup->is_type && mp_obj_is_native_type(type) && type != &mp_type_object /* object is not a real type */) {
  182. mp_load_method_maybe(lookup->obj->subobj[0], lookup->attr, lookup->dest);
  183. if (lookup->dest[0] != MP_OBJ_NULL) {
  184. return;
  185. }
  186. }
  187. // attribute not found, keep searching base classes
  188. if (type->parent == NULL) {
  189. DEBUG_printf("mp_obj_class_lookup: No more parents\n");
  190. return;
  191. #if MICROPY_MULTIPLE_INHERITANCE
  192. } else if (((mp_obj_base_t*)type->parent)->type == &mp_type_tuple) {
  193. const mp_obj_tuple_t *parent_tuple = type->parent;
  194. const mp_obj_t *item = parent_tuple->items;
  195. const mp_obj_t *top = item + parent_tuple->len - 1;
  196. for (; item < top; ++item) {
  197. assert(MP_OBJ_IS_TYPE(*item, &mp_type_type));
  198. mp_obj_type_t *bt = (mp_obj_type_t*)MP_OBJ_TO_PTR(*item);
  199. if (bt == &mp_type_object) {
  200. // Not a "real" type
  201. continue;
  202. }
  203. mp_obj_class_lookup(lookup, bt);
  204. if (lookup->dest[0] != MP_OBJ_NULL) {
  205. return;
  206. }
  207. }
  208. // search last base (simple tail recursion elimination)
  209. assert(MP_OBJ_IS_TYPE(*item, &mp_type_type));
  210. type = (mp_obj_type_t*)MP_OBJ_TO_PTR(*item);
  211. #endif
  212. } else {
  213. type = type->parent;
  214. }
  215. if (type == &mp_type_object) {
  216. // Not a "real" type
  217. return;
  218. }
  219. }
  220. }
  221. STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  222. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  223. qstr meth = (kind == PRINT_STR) ? MP_QSTR___str__ : MP_QSTR___repr__;
  224. mp_obj_t member[2] = {MP_OBJ_NULL};
  225. struct class_lookup_data lookup = {
  226. .obj = self,
  227. .attr = meth,
  228. .meth_offset = offsetof(mp_obj_type_t, print),
  229. .dest = member,
  230. .is_type = false,
  231. };
  232. mp_obj_class_lookup(&lookup, self->base.type);
  233. if (member[0] == MP_OBJ_NULL && kind == PRINT_STR) {
  234. // If there's no __str__, fall back to __repr__
  235. lookup.attr = MP_QSTR___repr__;
  236. lookup.meth_offset = 0;
  237. mp_obj_class_lookup(&lookup, self->base.type);
  238. }
  239. if (member[0] == MP_OBJ_SENTINEL) {
  240. // Handle Exception subclasses specially
  241. if (mp_obj_is_native_exception_instance(self->subobj[0])) {
  242. if (kind != PRINT_STR) {
  243. mp_print_str(print, qstr_str(self->base.type->name));
  244. }
  245. mp_obj_print_helper(print, self->subobj[0], kind | PRINT_EXC_SUBCLASS);
  246. } else {
  247. mp_obj_print_helper(print, self->subobj[0], kind);
  248. }
  249. return;
  250. }
  251. if (member[0] != MP_OBJ_NULL) {
  252. mp_obj_t r = mp_call_function_1(member[0], self_in);
  253. mp_obj_print_helper(print, r, PRINT_STR);
  254. return;
  255. }
  256. // TODO: CPython prints fully-qualified type name
  257. mp_printf(print, "<%s object at %p>", mp_obj_get_type_str(self_in), self);
  258. }
  259. mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  260. assert(mp_obj_is_instance_type(self));
  261. // look for __new__ function
  262. mp_obj_t init_fn[2] = {MP_OBJ_NULL};
  263. struct class_lookup_data lookup = {
  264. .obj = NULL,
  265. .attr = MP_QSTR___new__,
  266. .meth_offset = offsetof(mp_obj_type_t, make_new),
  267. .dest = init_fn,
  268. .is_type = false,
  269. };
  270. mp_obj_class_lookup(&lookup, self);
  271. const mp_obj_type_t *native_base = NULL;
  272. mp_obj_instance_t *o;
  273. if (init_fn[0] == MP_OBJ_NULL || init_fn[0] == MP_OBJ_SENTINEL) {
  274. // Either there is no __new__() method defined or there is a native
  275. // constructor. In both cases create a blank instance.
  276. o = mp_obj_new_instance(self, &native_base);
  277. // Since type->make_new() implements both __new__() and __init__() in
  278. // one go, of which the latter may be overridden by the Python subclass,
  279. // we defer (see the end of this function) the call of the native
  280. // constructor to give a chance for the Python __init__() method to call
  281. // said native constructor.
  282. } else {
  283. // Call Python class __new__ function with all args to create an instance
  284. mp_obj_t new_ret;
  285. if (n_args == 0 && n_kw == 0) {
  286. mp_obj_t args2[1] = {MP_OBJ_FROM_PTR(self)};
  287. new_ret = mp_call_function_n_kw(init_fn[0], 1, 0, args2);
  288. } else {
  289. mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_args + 2 * n_kw);
  290. args2[0] = MP_OBJ_FROM_PTR(self);
  291. memcpy(args2 + 1, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
  292. new_ret = mp_call_function_n_kw(init_fn[0], n_args + 1, n_kw, args2);
  293. m_del(mp_obj_t, args2, 1 + n_args + 2 * n_kw);
  294. }
  295. // https://docs.python.org/3.4/reference/datamodel.html#object.__new__
  296. // "If __new__() does not return an instance of cls, then the new
  297. // instance's __init__() method will not be invoked."
  298. if (mp_obj_get_type(new_ret) != self) {
  299. return new_ret;
  300. }
  301. // The instance returned by __new__() becomes the new object
  302. o = MP_OBJ_TO_PTR(new_ret);
  303. }
  304. // now call Python class __init__ function with all args
  305. // This method has a chance to call super().__init__() to construct a
  306. // possible native base class.
  307. init_fn[0] = init_fn[1] = MP_OBJ_NULL;
  308. lookup.obj = o;
  309. lookup.attr = MP_QSTR___init__;
  310. lookup.meth_offset = 0;
  311. mp_obj_class_lookup(&lookup, self);
  312. if (init_fn[0] != MP_OBJ_NULL) {
  313. mp_obj_t init_ret;
  314. if (n_args == 0 && n_kw == 0) {
  315. init_ret = mp_call_method_n_kw(0, 0, init_fn);
  316. } else {
  317. mp_obj_t *args2 = m_new(mp_obj_t, 2 + n_args + 2 * n_kw);
  318. args2[0] = init_fn[0];
  319. args2[1] = init_fn[1];
  320. memcpy(args2 + 2, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
  321. init_ret = mp_call_method_n_kw(n_args, n_kw, args2);
  322. m_del(mp_obj_t, args2, 2 + n_args + 2 * n_kw);
  323. }
  324. if (init_ret != mp_const_none) {
  325. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  326. mp_raise_TypeError("__init__() should return None");
  327. } else {
  328. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  329. "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret)));
  330. }
  331. }
  332. }
  333. // If the type had a native base that was not explicitly initialised
  334. // (constructed) by the Python __init__() method then construct it now.
  335. if (native_base != NULL && o->subobj[0] == MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj)) {
  336. o->subobj[0] = native_base->make_new(native_base, n_args, n_kw, args);
  337. }
  338. return MP_OBJ_FROM_PTR(o);
  339. }
  340. // Qstrs for special methods are guaranteed to have a small value, so we use byte
  341. // type to represent them.
  342. const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
  343. [MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
  344. [MP_UNARY_OP_LEN] = MP_QSTR___len__,
  345. [MP_UNARY_OP_HASH] = MP_QSTR___hash__,
  346. [MP_UNARY_OP_INT] = MP_QSTR___int__,
  347. #if MICROPY_PY_ALL_SPECIAL_METHODS
  348. [MP_UNARY_OP_POSITIVE] = MP_QSTR___pos__,
  349. [MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
  350. [MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
  351. [MP_UNARY_OP_ABS] = MP_QSTR___abs__,
  352. #endif
  353. #if MICROPY_PY_SYS_GETSIZEOF
  354. [MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__,
  355. #endif
  356. };
  357. STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  358. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  359. #if MICROPY_PY_SYS_GETSIZEOF
  360. if (MP_UNLIKELY(op == MP_UNARY_OP_SIZEOF)) {
  361. // TODO: This doesn't count inherited objects (self->subobj)
  362. const mp_obj_type_t *native_base;
  363. size_t num_native_bases = instance_count_native_bases(mp_obj_get_type(self_in), &native_base);
  364. size_t sz = sizeof(*self) + sizeof(*self->subobj) * num_native_bases
  365. + sizeof(*self->members.table) * self->members.alloc;
  366. return MP_OBJ_NEW_SMALL_INT(sz);
  367. }
  368. #endif
  369. qstr op_name = mp_unary_op_method_name[op];
  370. /* Still try to lookup native slot
  371. if (op_name == 0) {
  372. return MP_OBJ_NULL;
  373. }
  374. */
  375. mp_obj_t member[2] = {MP_OBJ_NULL};
  376. struct class_lookup_data lookup = {
  377. .obj = self,
  378. .attr = op_name,
  379. .meth_offset = offsetof(mp_obj_type_t, unary_op),
  380. .dest = member,
  381. .is_type = false,
  382. };
  383. mp_obj_class_lookup(&lookup, self->base.type);
  384. if (member[0] == MP_OBJ_SENTINEL) {
  385. return mp_unary_op(op, self->subobj[0]);
  386. } else if (member[0] != MP_OBJ_NULL) {
  387. mp_obj_t val = mp_call_function_1(member[0], self_in);
  388. switch (op) {
  389. case MP_UNARY_OP_HASH:
  390. // __hash__ must return a small int
  391. val = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_truncated(val));
  392. break;
  393. case MP_UNARY_OP_INT:
  394. // Must return int
  395. if (!MP_OBJ_IS_INT(val)) {
  396. mp_raise_TypeError(NULL);
  397. }
  398. break;
  399. default:
  400. // No need to do anything
  401. ;
  402. }
  403. return val;
  404. } else {
  405. if (op == MP_UNARY_OP_HASH) {
  406. lookup.attr = MP_QSTR___eq__;
  407. mp_obj_class_lookup(&lookup, self->base.type);
  408. if (member[0] == MP_OBJ_NULL) {
  409. // https://docs.python.org/3/reference/datamodel.html#object.__hash__
  410. // "User-defined classes have __eq__() and __hash__() methods by default;
  411. // with them, all objects compare unequal (except with themselves) and
  412. // x.__hash__() returns an appropriate value such that x == y implies
  413. // both that x is y and hash(x) == hash(y)."
  414. return MP_OBJ_NEW_SMALL_INT((mp_uint_t)self_in);
  415. }
  416. // "A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None.
  417. // When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError"
  418. }
  419. return MP_OBJ_NULL; // op not supported
  420. }
  421. }
  422. // Binary-op enum values not listed here will have the default value of 0 in the
  423. // table, corresponding to MP_QSTR_NULL, and are therefore unsupported (a lookup will
  424. // fail). They can be added at the expense of code size for the qstr.
  425. // Qstrs for special methods are guaranteed to have a small value, so we use byte
  426. // type to represent them.
  427. const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
  428. [MP_BINARY_OP_LESS] = MP_QSTR___lt__,
  429. [MP_BINARY_OP_MORE] = MP_QSTR___gt__,
  430. [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__,
  431. [MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__,
  432. [MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__,
  433. // MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
  434. [MP_BINARY_OP_CONTAINS] = MP_QSTR___contains__,
  435. // If an inplace method is not found a normal method will be used as a fallback
  436. [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
  437. [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
  438. #if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
  439. [MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__,
  440. [MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__,
  441. [MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__,
  442. [MP_BINARY_OP_INPLACE_MODULO] = MP_QSTR___imod__,
  443. [MP_BINARY_OP_INPLACE_POWER] = MP_QSTR___ipow__,
  444. [MP_BINARY_OP_INPLACE_OR] = MP_QSTR___ior__,
  445. [MP_BINARY_OP_INPLACE_XOR] = MP_QSTR___ixor__,
  446. [MP_BINARY_OP_INPLACE_AND] = MP_QSTR___iand__,
  447. [MP_BINARY_OP_INPLACE_LSHIFT] = MP_QSTR___ilshift__,
  448. [MP_BINARY_OP_INPLACE_RSHIFT] = MP_QSTR___irshift__,
  449. #endif
  450. [MP_BINARY_OP_ADD] = MP_QSTR___add__,
  451. [MP_BINARY_OP_SUBTRACT] = MP_QSTR___sub__,
  452. #if MICROPY_PY_ALL_SPECIAL_METHODS
  453. [MP_BINARY_OP_MULTIPLY] = MP_QSTR___mul__,
  454. [MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__,
  455. [MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__,
  456. [MP_BINARY_OP_MODULO] = MP_QSTR___mod__,
  457. [MP_BINARY_OP_DIVMOD] = MP_QSTR___divmod__,
  458. [MP_BINARY_OP_POWER] = MP_QSTR___pow__,
  459. [MP_BINARY_OP_OR] = MP_QSTR___or__,
  460. [MP_BINARY_OP_XOR] = MP_QSTR___xor__,
  461. [MP_BINARY_OP_AND] = MP_QSTR___and__,
  462. [MP_BINARY_OP_LSHIFT] = MP_QSTR___lshift__,
  463. [MP_BINARY_OP_RSHIFT] = MP_QSTR___rshift__,
  464. #endif
  465. #if MICROPY_PY_REVERSE_SPECIAL_METHODS
  466. [MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__,
  467. [MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__,
  468. #if MICROPY_PY_ALL_SPECIAL_METHODS
  469. [MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__,
  470. [MP_BINARY_OP_REVERSE_FLOOR_DIVIDE] = MP_QSTR___rfloordiv__,
  471. [MP_BINARY_OP_REVERSE_TRUE_DIVIDE] = MP_QSTR___rtruediv__,
  472. [MP_BINARY_OP_REVERSE_MODULO] = MP_QSTR___rmod__,
  473. [MP_BINARY_OP_REVERSE_POWER] = MP_QSTR___rpow__,
  474. [MP_BINARY_OP_REVERSE_OR] = MP_QSTR___ror__,
  475. [MP_BINARY_OP_REVERSE_XOR] = MP_QSTR___rxor__,
  476. [MP_BINARY_OP_REVERSE_AND] = MP_QSTR___rand__,
  477. [MP_BINARY_OP_REVERSE_LSHIFT] = MP_QSTR___rlshift__,
  478. [MP_BINARY_OP_REVERSE_RSHIFT] = MP_QSTR___rrshift__,
  479. #endif
  480. #endif
  481. };
  482. STATIC mp_obj_t instance_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  483. // Note: For ducktyping, CPython does not look in the instance members or use
  484. // __getattr__ or __getattribute__. It only looks in the class dictionary.
  485. mp_obj_instance_t *lhs = MP_OBJ_TO_PTR(lhs_in);
  486. retry:;
  487. qstr op_name = mp_binary_op_method_name[op];
  488. /* Still try to lookup native slot
  489. if (op_name == 0) {
  490. return MP_OBJ_NULL;
  491. }
  492. */
  493. mp_obj_t dest[3] = {MP_OBJ_NULL};
  494. struct class_lookup_data lookup = {
  495. .obj = lhs,
  496. .attr = op_name,
  497. .meth_offset = offsetof(mp_obj_type_t, binary_op),
  498. .dest = dest,
  499. .is_type = false,
  500. };
  501. mp_obj_class_lookup(&lookup, lhs->base.type);
  502. mp_obj_t res;
  503. if (dest[0] == MP_OBJ_SENTINEL) {
  504. res = mp_binary_op(op, lhs->subobj[0], rhs_in);
  505. } else if (dest[0] != MP_OBJ_NULL) {
  506. dest[2] = rhs_in;
  507. res = mp_call_method_n_kw(1, 0, dest);
  508. } else {
  509. // If this was an inplace method, fallback to normal method
  510. // https://docs.python.org/3/reference/datamodel.html#object.__iadd__ :
  511. // "If a specific method is not defined, the augmented assignment
  512. // falls back to the normal methods."
  513. if (op >= MP_BINARY_OP_INPLACE_OR && op <= MP_BINARY_OP_INPLACE_POWER) {
  514. op -= MP_BINARY_OP_INPLACE_OR - MP_BINARY_OP_OR;
  515. goto retry;
  516. }
  517. return MP_OBJ_NULL; // op not supported
  518. }
  519. #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
  520. // NotImplemented means "try other fallbacks (like calling __rop__
  521. // instead of __op__) and if nothing works, raise TypeError". As
  522. // MicroPython doesn't implement any fallbacks, signal to raise
  523. // TypeError right away.
  524. if (res == mp_const_notimplemented) {
  525. return MP_OBJ_NULL; // op not supported
  526. }
  527. #endif
  528. return res;
  529. }
  530. STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  531. // logic: look in instance members then class locals
  532. assert(mp_obj_is_instance_type(mp_obj_get_type(self_in)));
  533. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  534. mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
  535. if (elem != NULL) {
  536. // object member, always treated as a value
  537. dest[0] = elem->value;
  538. return;
  539. }
  540. #if MICROPY_CPYTHON_COMPAT
  541. if (attr == MP_QSTR___dict__) {
  542. // Create a new dict with a copy of the instance's map items.
  543. // This creates, unlike CPython, a 'read-only' __dict__: modifying
  544. // it will not result in modifications to the actual instance members.
  545. mp_map_t *map = &self->members;
  546. mp_obj_t attr_dict = mp_obj_new_dict(map->used);
  547. for (size_t i = 0; i < map->alloc; ++i) {
  548. if (MP_MAP_SLOT_IS_FILLED(map, i)) {
  549. mp_obj_dict_store(attr_dict, map->table[i].key, map->table[i].value);
  550. }
  551. }
  552. dest[0] = attr_dict;
  553. return;
  554. }
  555. #endif
  556. struct class_lookup_data lookup = {
  557. .obj = self,
  558. .attr = attr,
  559. .meth_offset = 0,
  560. .dest = dest,
  561. .is_type = false,
  562. };
  563. mp_obj_class_lookup(&lookup, self->base.type);
  564. mp_obj_t member = dest[0];
  565. if (member != MP_OBJ_NULL) {
  566. if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  567. // Class doesn't have any special accessors to check so return straightaway
  568. return;
  569. }
  570. #if MICROPY_PY_BUILTINS_PROPERTY
  571. if (MP_OBJ_IS_TYPE(member, &mp_type_property)) {
  572. // object member is a property; delegate the load to the property
  573. // Note: This is an optimisation for code size and execution time.
  574. // The proper way to do it is have the functionality just below
  575. // in a __get__ method of the property object, and then it would
  576. // be called by the descriptor code down below. But that way
  577. // requires overhead for the nested mp_call's and overhead for
  578. // the code.
  579. const mp_obj_t *proxy = mp_obj_property_get(member);
  580. if (proxy[0] == mp_const_none) {
  581. mp_raise_msg(&mp_type_AttributeError, "unreadable attribute");
  582. } else {
  583. dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self_in);
  584. }
  585. return;
  586. }
  587. #endif
  588. #if MICROPY_PY_DESCRIPTORS
  589. // found a class attribute; if it has a __get__ method then call it with the
  590. // class instance and class as arguments and return the result
  591. // Note that this is functionally correct but very slow: each load_attr
  592. // requires an extra mp_load_method_maybe to check for the __get__.
  593. mp_obj_t attr_get_method[4];
  594. mp_load_method_maybe(member, MP_QSTR___get__, attr_get_method);
  595. if (attr_get_method[0] != MP_OBJ_NULL) {
  596. attr_get_method[2] = self_in;
  597. attr_get_method[3] = MP_OBJ_FROM_PTR(mp_obj_get_type(self_in));
  598. dest[0] = mp_call_method_n_kw(2, 0, attr_get_method);
  599. }
  600. #endif
  601. return;
  602. }
  603. // try __getattr__
  604. if (attr != MP_QSTR___getattr__) {
  605. #if MICROPY_PY_DELATTR_SETATTR
  606. // If the requested attr is __setattr__/__delattr__ then don't delegate the lookup
  607. // to __getattr__. If we followed CPython's behaviour then __setattr__/__delattr__
  608. // would have already been found in the "object" base class.
  609. if (attr == MP_QSTR___setattr__ || attr == MP_QSTR___delattr__) {
  610. return;
  611. }
  612. #endif
  613. mp_obj_t dest2[3];
  614. mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
  615. if (dest2[0] != MP_OBJ_NULL) {
  616. // __getattr__ exists, call it and return its result
  617. dest2[2] = MP_OBJ_NEW_QSTR(attr);
  618. dest[0] = mp_call_method_n_kw(1, 0, dest2);
  619. return;
  620. }
  621. }
  622. }
  623. STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
  624. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  625. if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  626. // Class doesn't have any special accessors so skip their checks
  627. goto skip_special_accessors;
  628. }
  629. #if MICROPY_PY_BUILTINS_PROPERTY || MICROPY_PY_DESCRIPTORS
  630. // With property and/or descriptors enabled we need to do a lookup
  631. // first in the class dict for the attribute to see if the store should
  632. // be delegated.
  633. mp_obj_t member[2] = {MP_OBJ_NULL};
  634. struct class_lookup_data lookup = {
  635. .obj = self,
  636. .attr = attr,
  637. .meth_offset = 0,
  638. .dest = member,
  639. .is_type = false,
  640. };
  641. mp_obj_class_lookup(&lookup, self->base.type);
  642. if (member[0] != MP_OBJ_NULL) {
  643. #if MICROPY_PY_BUILTINS_PROPERTY
  644. if (MP_OBJ_IS_TYPE(member[0], &mp_type_property)) {
  645. // attribute exists and is a property; delegate the store/delete
  646. // Note: This is an optimisation for code size and execution time.
  647. // The proper way to do it is have the functionality just below in
  648. // a __set__/__delete__ method of the property object, and then it
  649. // would be called by the descriptor code down below. But that way
  650. // requires overhead for the nested mp_call's and overhead for
  651. // the code.
  652. const mp_obj_t *proxy = mp_obj_property_get(member[0]);
  653. mp_obj_t dest[2] = {self_in, value};
  654. if (value == MP_OBJ_NULL) {
  655. // delete attribute
  656. if (proxy[2] == mp_const_none) {
  657. // TODO better error message?
  658. return false;
  659. } else {
  660. mp_call_function_n_kw(proxy[2], 1, 0, dest);
  661. return true;
  662. }
  663. } else {
  664. // store attribute
  665. if (proxy[1] == mp_const_none) {
  666. // TODO better error message?
  667. return false;
  668. } else {
  669. mp_call_function_n_kw(proxy[1], 2, 0, dest);
  670. return true;
  671. }
  672. }
  673. }
  674. #endif
  675. #if MICROPY_PY_DESCRIPTORS
  676. // found a class attribute; if it has a __set__/__delete__ method then
  677. // call it with the class instance (and value) as arguments
  678. if (value == MP_OBJ_NULL) {
  679. // delete attribute
  680. mp_obj_t attr_delete_method[3];
  681. mp_load_method_maybe(member[0], MP_QSTR___delete__, attr_delete_method);
  682. if (attr_delete_method[0] != MP_OBJ_NULL) {
  683. attr_delete_method[2] = self_in;
  684. mp_call_method_n_kw(1, 0, attr_delete_method);
  685. return true;
  686. }
  687. } else {
  688. // store attribute
  689. mp_obj_t attr_set_method[4];
  690. mp_load_method_maybe(member[0], MP_QSTR___set__, attr_set_method);
  691. if (attr_set_method[0] != MP_OBJ_NULL) {
  692. attr_set_method[2] = self_in;
  693. attr_set_method[3] = value;
  694. mp_call_method_n_kw(2, 0, attr_set_method);
  695. return true;
  696. }
  697. }
  698. #endif
  699. }
  700. #endif
  701. #if MICROPY_PY_DELATTR_SETATTR
  702. if (value == MP_OBJ_NULL) {
  703. // delete attribute
  704. // try __delattr__ first
  705. mp_obj_t attr_delattr_method[3];
  706. mp_load_method_maybe(self_in, MP_QSTR___delattr__, attr_delattr_method);
  707. if (attr_delattr_method[0] != MP_OBJ_NULL) {
  708. // __delattr__ exists, so call it
  709. attr_delattr_method[2] = MP_OBJ_NEW_QSTR(attr);
  710. mp_call_method_n_kw(1, 0, attr_delattr_method);
  711. return true;
  712. }
  713. } else {
  714. // store attribute
  715. // try __setattr__ first
  716. mp_obj_t attr_setattr_method[4];
  717. mp_load_method_maybe(self_in, MP_QSTR___setattr__, attr_setattr_method);
  718. if (attr_setattr_method[0] != MP_OBJ_NULL) {
  719. // __setattr__ exists, so call it
  720. attr_setattr_method[2] = MP_OBJ_NEW_QSTR(attr);
  721. attr_setattr_method[3] = value;
  722. mp_call_method_n_kw(2, 0, attr_setattr_method);
  723. return true;
  724. }
  725. }
  726. #endif
  727. skip_special_accessors:
  728. if (value == MP_OBJ_NULL) {
  729. // delete attribute
  730. mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
  731. return elem != NULL;
  732. } else {
  733. // store attribute
  734. mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
  735. return true;
  736. }
  737. }
  738. STATIC void mp_obj_instance_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  739. if (dest[0] == MP_OBJ_NULL) {
  740. mp_obj_instance_load_attr(self_in, attr, dest);
  741. } else {
  742. if (mp_obj_instance_store_attr(self_in, attr, dest[1])) {
  743. dest[0] = MP_OBJ_NULL; // indicate success
  744. }
  745. }
  746. }
  747. STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
  748. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  749. mp_obj_t member[4] = {MP_OBJ_NULL, MP_OBJ_NULL, index, value};
  750. struct class_lookup_data lookup = {
  751. .obj = self,
  752. .meth_offset = offsetof(mp_obj_type_t, subscr),
  753. .dest = member,
  754. .is_type = false,
  755. };
  756. if (value == MP_OBJ_NULL) {
  757. // delete item
  758. lookup.attr = MP_QSTR___delitem__;
  759. } else if (value == MP_OBJ_SENTINEL) {
  760. // load item
  761. lookup.attr = MP_QSTR___getitem__;
  762. } else {
  763. // store item
  764. lookup.attr = MP_QSTR___setitem__;
  765. }
  766. mp_obj_class_lookup(&lookup, self->base.type);
  767. if (member[0] == MP_OBJ_SENTINEL) {
  768. return mp_obj_subscr(self->subobj[0], index, value);
  769. } else if (member[0] != MP_OBJ_NULL) {
  770. size_t n_args = value == MP_OBJ_NULL || value == MP_OBJ_SENTINEL ? 1 : 2;
  771. mp_obj_t ret = mp_call_method_n_kw(n_args, 0, member);
  772. if (value == MP_OBJ_SENTINEL) {
  773. return ret;
  774. } else {
  775. return mp_const_none;
  776. }
  777. } else {
  778. return MP_OBJ_NULL; // op not supported
  779. }
  780. }
  781. STATIC mp_obj_t mp_obj_instance_get_call(mp_obj_t self_in, mp_obj_t *member) {
  782. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  783. struct class_lookup_data lookup = {
  784. .obj = self,
  785. .attr = MP_QSTR___call__,
  786. .meth_offset = offsetof(mp_obj_type_t, call),
  787. .dest = member,
  788. .is_type = false,
  789. };
  790. mp_obj_class_lookup(&lookup, self->base.type);
  791. return member[0];
  792. }
  793. bool mp_obj_instance_is_callable(mp_obj_t self_in) {
  794. mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
  795. return mp_obj_instance_get_call(self_in, member) != MP_OBJ_NULL;
  796. }
  797. mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  798. mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
  799. mp_obj_t call = mp_obj_instance_get_call(self_in, member);
  800. if (call == MP_OBJ_NULL) {
  801. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  802. mp_raise_TypeError("object not callable");
  803. } else {
  804. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  805. "'%s' object isn't callable", mp_obj_get_type_str(self_in)));
  806. }
  807. }
  808. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  809. if (call == MP_OBJ_SENTINEL) {
  810. return mp_call_function_n_kw(self->subobj[0], n_args, n_kw, args);
  811. }
  812. return mp_call_method_self_n_kw(member[0], member[1], n_args, n_kw, args);
  813. }
  814. STATIC mp_obj_t instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
  815. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  816. mp_obj_t member[2] = {MP_OBJ_NULL};
  817. struct class_lookup_data lookup = {
  818. .obj = self,
  819. .attr = MP_QSTR___iter__,
  820. .meth_offset = offsetof(mp_obj_type_t, getiter),
  821. .dest = member,
  822. .is_type = false,
  823. };
  824. mp_obj_class_lookup(&lookup, self->base.type);
  825. if (member[0] == MP_OBJ_NULL) {
  826. return MP_OBJ_NULL;
  827. } else if (member[0] == MP_OBJ_SENTINEL) {
  828. mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
  829. return type->getiter(self->subobj[0], iter_buf);
  830. } else {
  831. return mp_call_method_n_kw(0, 0, member);
  832. }
  833. }
  834. STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  835. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  836. mp_obj_t member[2] = {MP_OBJ_NULL};
  837. struct class_lookup_data lookup = {
  838. .obj = self,
  839. .attr = MP_QSTR_, // don't actually look for a method
  840. .meth_offset = offsetof(mp_obj_type_t, buffer_p.get_buffer),
  841. .dest = member,
  842. .is_type = false,
  843. };
  844. mp_obj_class_lookup(&lookup, self->base.type);
  845. if (member[0] == MP_OBJ_SENTINEL) {
  846. mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
  847. return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags);
  848. } else {
  849. return 1; // object does not support buffer protocol
  850. }
  851. }
  852. /******************************************************************************/
  853. // type object
  854. // - the struct is mp_obj_type_t and is defined in obj.h so const types can be made
  855. // - there is a constant mp_obj_type_t (called mp_type_type) for the 'type' object
  856. // - creating a new class (a new type) creates a new mp_obj_type_t
  857. #if ENABLE_SPECIAL_ACCESSORS
  858. STATIC bool check_for_special_accessors(mp_obj_t key, mp_obj_t value) {
  859. #if MICROPY_PY_DELATTR_SETATTR
  860. if (key == MP_OBJ_NEW_QSTR(MP_QSTR___setattr__) || key == MP_OBJ_NEW_QSTR(MP_QSTR___delattr__)) {
  861. return true;
  862. }
  863. #endif
  864. #if MICROPY_PY_BUILTINS_PROPERTY
  865. if (MP_OBJ_IS_TYPE(value, &mp_type_property)) {
  866. return true;
  867. }
  868. #endif
  869. #if MICROPY_PY_DESCRIPTORS
  870. static const uint8_t to_check[] = {
  871. MP_QSTR___get__, MP_QSTR___set__, MP_QSTR___delete__,
  872. };
  873. for (size_t i = 0; i < MP_ARRAY_SIZE(to_check); ++i) {
  874. mp_obj_t dest_temp[2];
  875. mp_load_method_protected(value, to_check[i], dest_temp, true);
  876. if (dest_temp[0] != MP_OBJ_NULL) {
  877. return true;
  878. }
  879. }
  880. #endif
  881. return false;
  882. }
  883. #endif
  884. STATIC void type_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  885. (void)kind;
  886. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  887. mp_printf(print, "<class '%q'>", self->name);
  888. }
  889. STATIC mp_obj_t type_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  890. (void)type_in;
  891. mp_arg_check_num(n_args, n_kw, 1, 3, false);
  892. switch (n_args) {
  893. case 1:
  894. return MP_OBJ_FROM_PTR(mp_obj_get_type(args[0]));
  895. case 3:
  896. // args[0] = name
  897. // args[1] = bases tuple
  898. // args[2] = locals dict
  899. return mp_obj_new_type(mp_obj_str_get_qstr(args[0]), args[1], args[2]);
  900. default:
  901. mp_raise_TypeError("type takes 1 or 3 arguments");
  902. }
  903. }
  904. STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  905. // instantiate an instance of a class
  906. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  907. if (self->make_new == NULL) {
  908. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  909. mp_raise_TypeError("cannot create instance");
  910. } else {
  911. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  912. "cannot create '%q' instances", self->name));
  913. }
  914. }
  915. // make new instance
  916. mp_obj_t o = self->make_new(self, n_args, n_kw, args);
  917. // return new instance
  918. return o;
  919. }
  920. STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  921. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_type));
  922. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  923. if (dest[0] == MP_OBJ_NULL) {
  924. // load attribute
  925. #if MICROPY_CPYTHON_COMPAT
  926. if (attr == MP_QSTR___name__) {
  927. dest[0] = MP_OBJ_NEW_QSTR(self->name);
  928. return;
  929. }
  930. #endif
  931. struct class_lookup_data lookup = {
  932. .obj = (mp_obj_instance_t*)self,
  933. .attr = attr,
  934. .meth_offset = 0,
  935. .dest = dest,
  936. .is_type = true,
  937. };
  938. mp_obj_class_lookup(&lookup, self);
  939. } else {
  940. // delete/store attribute
  941. if (self->locals_dict != NULL) {
  942. assert(self->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
  943. mp_map_t *locals_map = &self->locals_dict->map;
  944. if (locals_map->is_fixed) {
  945. // can't apply delete/store to a fixed map
  946. return;
  947. }
  948. if (dest[1] == MP_OBJ_NULL) {
  949. // delete attribute
  950. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
  951. if (elem != NULL) {
  952. dest[0] = MP_OBJ_NULL; // indicate success
  953. }
  954. } else {
  955. #if ENABLE_SPECIAL_ACCESSORS
  956. // Check if we add any special accessor methods with this store
  957. if (!(self->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  958. if (check_for_special_accessors(MP_OBJ_NEW_QSTR(attr), dest[1])) {
  959. if (self->flags & TYPE_FLAG_IS_SUBCLASSED) {
  960. // This class is already subclassed so can't have special accessors added
  961. mp_raise_msg(&mp_type_AttributeError, "can't add special method to already-subclassed class");
  962. }
  963. self->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  964. }
  965. }
  966. #endif
  967. // store attribute
  968. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  969. elem->value = dest[1];
  970. dest[0] = MP_OBJ_NULL; // indicate success
  971. }
  972. }
  973. }
  974. }
  975. const mp_obj_type_t mp_type_type = {
  976. { &mp_type_type },
  977. .name = MP_QSTR_type,
  978. .print = type_print,
  979. .make_new = type_make_new,
  980. .call = type_call,
  981. .unary_op = mp_generic_unary_op,
  982. .attr = type_attr,
  983. };
  984. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {
  985. // Verify input objects have expected type
  986. if (!MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)) {
  987. mp_raise_TypeError(NULL);
  988. }
  989. if (!MP_OBJ_IS_TYPE(locals_dict, &mp_type_dict)) {
  990. mp_raise_TypeError(NULL);
  991. }
  992. // TODO might need to make a copy of locals_dict; at least that's how CPython does it
  993. // Basic validation of base classes
  994. uint16_t base_flags = 0;
  995. size_t bases_len;
  996. mp_obj_t *bases_items;
  997. mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items);
  998. for (size_t i = 0; i < bases_len; i++) {
  999. if (!MP_OBJ_IS_TYPE(bases_items[i], &mp_type_type)) {
  1000. mp_raise_TypeError(NULL);
  1001. }
  1002. mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]);
  1003. // TODO: Verify with CPy, tested on function type
  1004. if (t->make_new == NULL) {
  1005. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  1006. mp_raise_TypeError("type isn't an acceptable base type");
  1007. } else {
  1008. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  1009. "type '%q' isn't an acceptable base type", t->name));
  1010. }
  1011. }
  1012. #if ENABLE_SPECIAL_ACCESSORS
  1013. if (mp_obj_is_instance_type(t)) {
  1014. t->flags |= TYPE_FLAG_IS_SUBCLASSED;
  1015. base_flags |= t->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  1016. }
  1017. #endif
  1018. }
  1019. mp_obj_type_t *o = m_new0(mp_obj_type_t, 1);
  1020. o->base.type = &mp_type_type;
  1021. o->flags = base_flags;
  1022. o->name = name;
  1023. o->print = instance_print;
  1024. o->make_new = mp_obj_instance_make_new;
  1025. o->call = mp_obj_instance_call;
  1026. o->unary_op = instance_unary_op;
  1027. o->binary_op = instance_binary_op;
  1028. o->attr = mp_obj_instance_attr;
  1029. o->subscr = instance_subscr;
  1030. o->getiter = instance_getiter;
  1031. //o->iternext = ; not implemented
  1032. o->buffer_p.get_buffer = instance_get_buffer;
  1033. if (bases_len > 0) {
  1034. // Inherit protocol from a base class. This allows to define an
  1035. // abstract base class which would translate C-level protocol to
  1036. // Python method calls, and any subclass inheriting from it will
  1037. // support this feature.
  1038. o->protocol = ((mp_obj_type_t*)MP_OBJ_TO_PTR(bases_items[0]))->protocol;
  1039. if (bases_len >= 2) {
  1040. #if MICROPY_MULTIPLE_INHERITANCE
  1041. o->parent = MP_OBJ_TO_PTR(bases_tuple);
  1042. #else
  1043. mp_raise_NotImplementedError("multiple inheritance not supported");
  1044. #endif
  1045. } else {
  1046. o->parent = MP_OBJ_TO_PTR(bases_items[0]);
  1047. }
  1048. }
  1049. o->locals_dict = MP_OBJ_TO_PTR(locals_dict);
  1050. #if ENABLE_SPECIAL_ACCESSORS
  1051. // Check if the class has any special accessor methods
  1052. if (!(o->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  1053. for (size_t i = 0; i < o->locals_dict->map.alloc; i++) {
  1054. if (MP_MAP_SLOT_IS_FILLED(&o->locals_dict->map, i)) {
  1055. const mp_map_elem_t *elem = &o->locals_dict->map.table[i];
  1056. if (check_for_special_accessors(elem->key, elem->value)) {
  1057. o->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. #endif
  1064. const mp_obj_type_t *native_base;
  1065. size_t num_native_bases = instance_count_native_bases(o, &native_base);
  1066. if (num_native_bases > 1) {
  1067. mp_raise_TypeError("multiple bases have instance lay-out conflict");
  1068. }
  1069. mp_map_t *locals_map = &o->locals_dict->map;
  1070. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(MP_QSTR___new__), MP_MAP_LOOKUP);
  1071. if (elem != NULL) {
  1072. // __new__ slot exists; check if it is a function
  1073. if (MP_OBJ_IS_FUN(elem->value)) {
  1074. // __new__ is a function, wrap it in a staticmethod decorator
  1075. elem->value = static_class_method_make_new(&mp_type_staticmethod, 1, 0, &elem->value);
  1076. }
  1077. }
  1078. return MP_OBJ_FROM_PTR(o);
  1079. }
  1080. /******************************************************************************/
  1081. // super object
  1082. typedef struct _mp_obj_super_t {
  1083. mp_obj_base_t base;
  1084. mp_obj_t type;
  1085. mp_obj_t obj;
  1086. } mp_obj_super_t;
  1087. STATIC void super_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  1088. (void)kind;
  1089. mp_obj_super_t *self = MP_OBJ_TO_PTR(self_in);
  1090. mp_print_str(print, "<super: ");
  1091. mp_obj_print_helper(print, self->type, PRINT_STR);
  1092. mp_print_str(print, ", ");
  1093. mp_obj_print_helper(print, self->obj, PRINT_STR);
  1094. mp_print_str(print, ">");
  1095. }
  1096. STATIC mp_obj_t super_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  1097. (void)type_in;
  1098. // 0 arguments are turned into 2 in the compiler
  1099. // 1 argument is not yet implemented
  1100. mp_arg_check_num(n_args, n_kw, 2, 2, false);
  1101. if (!MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
  1102. mp_raise_TypeError(NULL);
  1103. }
  1104. mp_obj_super_t *o = m_new_obj(mp_obj_super_t);
  1105. *o = (mp_obj_super_t){{type_in}, args[0], args[1]};
  1106. return MP_OBJ_FROM_PTR(o);
  1107. }
  1108. STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  1109. if (dest[0] != MP_OBJ_NULL) {
  1110. // not load attribute
  1111. return;
  1112. }
  1113. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_super));
  1114. mp_obj_super_t *self = MP_OBJ_TO_PTR(self_in);
  1115. assert(MP_OBJ_IS_TYPE(self->type, &mp_type_type));
  1116. mp_obj_type_t *type = MP_OBJ_TO_PTR(self->type);
  1117. struct class_lookup_data lookup = {
  1118. .obj = MP_OBJ_TO_PTR(self->obj),
  1119. .attr = attr,
  1120. .meth_offset = 0,
  1121. .dest = dest,
  1122. .is_type = false,
  1123. };
  1124. // Allow a call super().__init__() to reach any native base classes
  1125. if (attr == MP_QSTR___init__) {
  1126. lookup.meth_offset = offsetof(mp_obj_type_t, make_new);
  1127. }
  1128. if (type->parent == NULL) {
  1129. // no parents, do nothing
  1130. #if MICROPY_MULTIPLE_INHERITANCE
  1131. } else if (((mp_obj_base_t*)type->parent)->type == &mp_type_tuple) {
  1132. const mp_obj_tuple_t *parent_tuple = type->parent;
  1133. size_t len = parent_tuple->len;
  1134. const mp_obj_t *items = parent_tuple->items;
  1135. for (size_t i = 0; i < len; i++) {
  1136. assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type));
  1137. if (MP_OBJ_TO_PTR(items[i]) == &mp_type_object) {
  1138. // The "object" type will be searched at the end of this function,
  1139. // and we don't want to lookup native methods in object.
  1140. continue;
  1141. }
  1142. mp_obj_class_lookup(&lookup, (mp_obj_type_t*)MP_OBJ_TO_PTR(items[i]));
  1143. if (dest[0] != MP_OBJ_NULL) {
  1144. break;
  1145. }
  1146. }
  1147. #endif
  1148. } else if (type->parent != &mp_type_object) {
  1149. mp_obj_class_lookup(&lookup, type->parent);
  1150. }
  1151. if (dest[0] != MP_OBJ_NULL) {
  1152. if (dest[0] == MP_OBJ_SENTINEL) {
  1153. // Looked up native __init__ so defer to it
  1154. dest[0] = MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj);
  1155. dest[1] = self->obj;
  1156. }
  1157. return;
  1158. }
  1159. // Reset meth_offset so we don't look up any native methods in object,
  1160. // because object never takes up the native base-class slot.
  1161. lookup.meth_offset = 0;
  1162. mp_obj_class_lookup(&lookup, &mp_type_object);
  1163. }
  1164. const mp_obj_type_t mp_type_super = {
  1165. { &mp_type_type },
  1166. .name = MP_QSTR_super,
  1167. .print = super_print,
  1168. .make_new = super_make_new,
  1169. .attr = super_attr,
  1170. };
  1171. void mp_load_super_method(qstr attr, mp_obj_t *dest) {
  1172. mp_obj_super_t super = {{&mp_type_super}, dest[1], dest[2]};
  1173. mp_load_method(MP_OBJ_FROM_PTR(&super), attr, dest);
  1174. }
  1175. /******************************************************************************/
  1176. // subclassing and built-ins specific to types
  1177. // object and classinfo should be type objects
  1178. // (but the function will fail gracefully if they are not)
  1179. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) {
  1180. for (;;) {
  1181. if (object == classinfo) {
  1182. return true;
  1183. }
  1184. // not equivalent classes, keep searching base classes
  1185. // object should always be a type object, but just return false if it's not
  1186. if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) {
  1187. return false;
  1188. }
  1189. const mp_obj_type_t *self = MP_OBJ_TO_PTR(object);
  1190. if (self->parent == NULL) {
  1191. // type has no parents
  1192. return false;
  1193. #if MICROPY_MULTIPLE_INHERITANCE
  1194. } else if (((mp_obj_base_t*)self->parent)->type == &mp_type_tuple) {
  1195. // get the base objects (they should be type objects)
  1196. const mp_obj_tuple_t *parent_tuple = self->parent;
  1197. const mp_obj_t *item = parent_tuple->items;
  1198. const mp_obj_t *top = item + parent_tuple->len - 1;
  1199. // iterate through the base objects
  1200. for (; item < top; ++item) {
  1201. if (mp_obj_is_subclass_fast(*item, classinfo)) {
  1202. return true;
  1203. }
  1204. }
  1205. // search last base (simple tail recursion elimination)
  1206. object = *item;
  1207. #endif
  1208. } else {
  1209. // type has 1 parent
  1210. object = MP_OBJ_FROM_PTR(self->parent);
  1211. }
  1212. }
  1213. }
  1214. STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
  1215. size_t len;
  1216. mp_obj_t *items;
  1217. if (MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) {
  1218. len = 1;
  1219. items = &classinfo;
  1220. } else if (MP_OBJ_IS_TYPE(classinfo, &mp_type_tuple)) {
  1221. mp_obj_tuple_get(classinfo, &len, &items);
  1222. } else {
  1223. mp_raise_TypeError("issubclass() arg 2 must be a class or a tuple of classes");
  1224. }
  1225. for (size_t i = 0; i < len; i++) {
  1226. // We explicitly check for 'object' here since no-one explicitly derives from it
  1227. if (items[i] == MP_OBJ_FROM_PTR(&mp_type_object) || mp_obj_is_subclass_fast(object, items[i])) {
  1228. return mp_const_true;
  1229. }
  1230. }
  1231. return mp_const_false;
  1232. }
  1233. STATIC mp_obj_t mp_builtin_issubclass(mp_obj_t object, mp_obj_t classinfo) {
  1234. if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) {
  1235. mp_raise_TypeError("issubclass() arg 1 must be a class");
  1236. }
  1237. return mp_obj_is_subclass(object, classinfo);
  1238. }
  1239. MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_issubclass_obj, mp_builtin_issubclass);
  1240. STATIC mp_obj_t mp_builtin_isinstance(mp_obj_t object, mp_obj_t classinfo) {
  1241. return mp_obj_is_subclass(MP_OBJ_FROM_PTR(mp_obj_get_type(object)), classinfo);
  1242. }
  1243. MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
  1244. mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type) {
  1245. mp_obj_type_t *self_type = mp_obj_get_type(self_in);
  1246. if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(self_type), native_type)) {
  1247. return MP_OBJ_NULL;
  1248. }
  1249. mp_obj_instance_t *self = (mp_obj_instance_t*)MP_OBJ_TO_PTR(self_in);
  1250. return self->subobj[0];
  1251. }
  1252. /******************************************************************************/
  1253. // staticmethod and classmethod types (probably should go in a different file)
  1254. STATIC mp_obj_t static_class_method_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  1255. assert(self == &mp_type_staticmethod || self == &mp_type_classmethod);
  1256. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  1257. mp_obj_static_class_method_t *o = m_new_obj(mp_obj_static_class_method_t);
  1258. *o = (mp_obj_static_class_method_t){{self}, args[0]};
  1259. return MP_OBJ_FROM_PTR(o);
  1260. }
  1261. const mp_obj_type_t mp_type_staticmethod = {
  1262. { &mp_type_type },
  1263. .name = MP_QSTR_staticmethod,
  1264. .make_new = static_class_method_make_new,
  1265. };
  1266. const mp_obj_type_t mp_type_classmethod = {
  1267. { &mp_type_type },
  1268. .name = MP_QSTR_classmethod,
  1269. .make_new = static_class_method_make_new,
  1270. };