objtype.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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-2018 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 qstr 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_QSTRnull, 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 qstr 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_MAT_MULTIPLY] = MP_QSTR___imatmul__,
  441. [MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__,
  442. [MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__,
  443. [MP_BINARY_OP_INPLACE_MODULO] = MP_QSTR___imod__,
  444. [MP_BINARY_OP_INPLACE_POWER] = MP_QSTR___ipow__,
  445. [MP_BINARY_OP_INPLACE_OR] = MP_QSTR___ior__,
  446. [MP_BINARY_OP_INPLACE_XOR] = MP_QSTR___ixor__,
  447. [MP_BINARY_OP_INPLACE_AND] = MP_QSTR___iand__,
  448. [MP_BINARY_OP_INPLACE_LSHIFT] = MP_QSTR___ilshift__,
  449. [MP_BINARY_OP_INPLACE_RSHIFT] = MP_QSTR___irshift__,
  450. #endif
  451. [MP_BINARY_OP_ADD] = MP_QSTR___add__,
  452. [MP_BINARY_OP_SUBTRACT] = MP_QSTR___sub__,
  453. #if MICROPY_PY_ALL_SPECIAL_METHODS
  454. [MP_BINARY_OP_MULTIPLY] = MP_QSTR___mul__,
  455. [MP_BINARY_OP_MAT_MULTIPLY] = MP_QSTR___matmul__,
  456. [MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__,
  457. [MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__,
  458. [MP_BINARY_OP_MODULO] = MP_QSTR___mod__,
  459. [MP_BINARY_OP_DIVMOD] = MP_QSTR___divmod__,
  460. [MP_BINARY_OP_POWER] = MP_QSTR___pow__,
  461. [MP_BINARY_OP_OR] = MP_QSTR___or__,
  462. [MP_BINARY_OP_XOR] = MP_QSTR___xor__,
  463. [MP_BINARY_OP_AND] = MP_QSTR___and__,
  464. [MP_BINARY_OP_LSHIFT] = MP_QSTR___lshift__,
  465. [MP_BINARY_OP_RSHIFT] = MP_QSTR___rshift__,
  466. #endif
  467. #if MICROPY_PY_REVERSE_SPECIAL_METHODS
  468. [MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__,
  469. [MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__,
  470. #if MICROPY_PY_ALL_SPECIAL_METHODS
  471. [MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__,
  472. [MP_BINARY_OP_REVERSE_MAT_MULTIPLY] = MP_QSTR___rmatmul__,
  473. [MP_BINARY_OP_REVERSE_FLOOR_DIVIDE] = MP_QSTR___rfloordiv__,
  474. [MP_BINARY_OP_REVERSE_TRUE_DIVIDE] = MP_QSTR___rtruediv__,
  475. [MP_BINARY_OP_REVERSE_MODULO] = MP_QSTR___rmod__,
  476. [MP_BINARY_OP_REVERSE_POWER] = MP_QSTR___rpow__,
  477. [MP_BINARY_OP_REVERSE_OR] = MP_QSTR___ror__,
  478. [MP_BINARY_OP_REVERSE_XOR] = MP_QSTR___rxor__,
  479. [MP_BINARY_OP_REVERSE_AND] = MP_QSTR___rand__,
  480. [MP_BINARY_OP_REVERSE_LSHIFT] = MP_QSTR___rlshift__,
  481. [MP_BINARY_OP_REVERSE_RSHIFT] = MP_QSTR___rrshift__,
  482. #endif
  483. #endif
  484. };
  485. STATIC mp_obj_t instance_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  486. // Note: For ducktyping, CPython does not look in the instance members or use
  487. // __getattr__ or __getattribute__. It only looks in the class dictionary.
  488. mp_obj_instance_t *lhs = MP_OBJ_TO_PTR(lhs_in);
  489. retry:;
  490. qstr op_name = mp_binary_op_method_name[op];
  491. /* Still try to lookup native slot
  492. if (op_name == 0) {
  493. return MP_OBJ_NULL;
  494. }
  495. */
  496. mp_obj_t dest[3] = {MP_OBJ_NULL};
  497. struct class_lookup_data lookup = {
  498. .obj = lhs,
  499. .attr = op_name,
  500. .meth_offset = offsetof(mp_obj_type_t, binary_op),
  501. .dest = dest,
  502. .is_type = false,
  503. };
  504. mp_obj_class_lookup(&lookup, lhs->base.type);
  505. mp_obj_t res;
  506. if (dest[0] == MP_OBJ_SENTINEL) {
  507. res = mp_binary_op(op, lhs->subobj[0], rhs_in);
  508. } else if (dest[0] != MP_OBJ_NULL) {
  509. dest[2] = rhs_in;
  510. res = mp_call_method_n_kw(1, 0, dest);
  511. } else {
  512. // If this was an inplace method, fallback to normal method
  513. // https://docs.python.org/3/reference/datamodel.html#object.__iadd__ :
  514. // "If a specific method is not defined, the augmented assignment
  515. // falls back to the normal methods."
  516. if (op >= MP_BINARY_OP_INPLACE_OR && op <= MP_BINARY_OP_INPLACE_POWER) {
  517. op -= MP_BINARY_OP_INPLACE_OR - MP_BINARY_OP_OR;
  518. goto retry;
  519. }
  520. return MP_OBJ_NULL; // op not supported
  521. }
  522. #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
  523. // NotImplemented means "try other fallbacks (like calling __rop__
  524. // instead of __op__) and if nothing works, raise TypeError". As
  525. // MicroPython doesn't implement any fallbacks, signal to raise
  526. // TypeError right away.
  527. if (res == mp_const_notimplemented) {
  528. return MP_OBJ_NULL; // op not supported
  529. }
  530. #endif
  531. return res;
  532. }
  533. STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  534. // logic: look in instance members then class locals
  535. assert(mp_obj_is_instance_type(mp_obj_get_type(self_in)));
  536. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  537. mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
  538. if (elem != NULL) {
  539. // object member, always treated as a value
  540. dest[0] = elem->value;
  541. return;
  542. }
  543. #if MICROPY_CPYTHON_COMPAT
  544. if (attr == MP_QSTR___dict__) {
  545. // Create a new dict with a copy of the instance's map items.
  546. // This creates, unlike CPython, a 'read-only' __dict__: modifying
  547. // it will not result in modifications to the actual instance members.
  548. mp_map_t *map = &self->members;
  549. mp_obj_t attr_dict = mp_obj_new_dict(map->used);
  550. for (size_t i = 0; i < map->alloc; ++i) {
  551. if (mp_map_slot_is_filled(map, i)) {
  552. mp_obj_dict_store(attr_dict, map->table[i].key, map->table[i].value);
  553. }
  554. }
  555. dest[0] = attr_dict;
  556. return;
  557. }
  558. #endif
  559. struct class_lookup_data lookup = {
  560. .obj = self,
  561. .attr = attr,
  562. .meth_offset = 0,
  563. .dest = dest,
  564. .is_type = false,
  565. };
  566. mp_obj_class_lookup(&lookup, self->base.type);
  567. mp_obj_t member = dest[0];
  568. if (member != MP_OBJ_NULL) {
  569. if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  570. // Class doesn't have any special accessors to check so return straightaway
  571. return;
  572. }
  573. #if MICROPY_PY_BUILTINS_PROPERTY
  574. if (mp_obj_is_type(member, &mp_type_property)) {
  575. // object member is a property; delegate the load to the property
  576. // Note: This is an optimisation for code size and execution time.
  577. // The proper way to do it is have the functionality just below
  578. // in a __get__ method of the property object, and then it would
  579. // be called by the descriptor code down below. But that way
  580. // requires overhead for the nested mp_call's and overhead for
  581. // the code.
  582. const mp_obj_t *proxy = mp_obj_property_get(member);
  583. if (proxy[0] == mp_const_none) {
  584. mp_raise_msg(&mp_type_AttributeError, "unreadable attribute");
  585. } else {
  586. dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self_in);
  587. }
  588. return;
  589. }
  590. #endif
  591. #if MICROPY_PY_DESCRIPTORS
  592. // found a class attribute; if it has a __get__ method then call it with the
  593. // class instance and class as arguments and return the result
  594. // Note that this is functionally correct but very slow: each load_attr
  595. // requires an extra mp_load_method_maybe to check for the __get__.
  596. mp_obj_t attr_get_method[4];
  597. mp_load_method_maybe(member, MP_QSTR___get__, attr_get_method);
  598. if (attr_get_method[0] != MP_OBJ_NULL) {
  599. attr_get_method[2] = self_in;
  600. attr_get_method[3] = MP_OBJ_FROM_PTR(mp_obj_get_type(self_in));
  601. dest[0] = mp_call_method_n_kw(2, 0, attr_get_method);
  602. }
  603. #endif
  604. return;
  605. }
  606. // try __getattr__
  607. if (attr != MP_QSTR___getattr__) {
  608. #if MICROPY_PY_DELATTR_SETATTR
  609. // If the requested attr is __setattr__/__delattr__ then don't delegate the lookup
  610. // to __getattr__. If we followed CPython's behaviour then __setattr__/__delattr__
  611. // would have already been found in the "object" base class.
  612. if (attr == MP_QSTR___setattr__ || attr == MP_QSTR___delattr__) {
  613. return;
  614. }
  615. #endif
  616. mp_obj_t dest2[3];
  617. mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
  618. if (dest2[0] != MP_OBJ_NULL) {
  619. // __getattr__ exists, call it and return its result
  620. dest2[2] = MP_OBJ_NEW_QSTR(attr);
  621. dest[0] = mp_call_method_n_kw(1, 0, dest2);
  622. return;
  623. }
  624. }
  625. }
  626. STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
  627. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  628. if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  629. // Class doesn't have any special accessors so skip their checks
  630. goto skip_special_accessors;
  631. }
  632. #if MICROPY_PY_BUILTINS_PROPERTY || MICROPY_PY_DESCRIPTORS
  633. // With property and/or descriptors enabled we need to do a lookup
  634. // first in the class dict for the attribute to see if the store should
  635. // be delegated.
  636. mp_obj_t member[2] = {MP_OBJ_NULL};
  637. struct class_lookup_data lookup = {
  638. .obj = self,
  639. .attr = attr,
  640. .meth_offset = 0,
  641. .dest = member,
  642. .is_type = false,
  643. };
  644. mp_obj_class_lookup(&lookup, self->base.type);
  645. if (member[0] != MP_OBJ_NULL) {
  646. #if MICROPY_PY_BUILTINS_PROPERTY
  647. if (mp_obj_is_type(member[0], &mp_type_property)) {
  648. // attribute exists and is a property; delegate the store/delete
  649. // Note: This is an optimisation for code size and execution time.
  650. // The proper way to do it is have the functionality just below in
  651. // a __set__/__delete__ method of the property object, and then it
  652. // would be called by the descriptor code down below. But that way
  653. // requires overhead for the nested mp_call's and overhead for
  654. // the code.
  655. const mp_obj_t *proxy = mp_obj_property_get(member[0]);
  656. mp_obj_t dest[2] = {self_in, value};
  657. if (value == MP_OBJ_NULL) {
  658. // delete attribute
  659. if (proxy[2] == mp_const_none) {
  660. // TODO better error message?
  661. return false;
  662. } else {
  663. mp_call_function_n_kw(proxy[2], 1, 0, dest);
  664. return true;
  665. }
  666. } else {
  667. // store attribute
  668. if (proxy[1] == mp_const_none) {
  669. // TODO better error message?
  670. return false;
  671. } else {
  672. mp_call_function_n_kw(proxy[1], 2, 0, dest);
  673. return true;
  674. }
  675. }
  676. }
  677. #endif
  678. #if MICROPY_PY_DESCRIPTORS
  679. // found a class attribute; if it has a __set__/__delete__ method then
  680. // call it with the class instance (and value) as arguments
  681. if (value == MP_OBJ_NULL) {
  682. // delete attribute
  683. mp_obj_t attr_delete_method[3];
  684. mp_load_method_maybe(member[0], MP_QSTR___delete__, attr_delete_method);
  685. if (attr_delete_method[0] != MP_OBJ_NULL) {
  686. attr_delete_method[2] = self_in;
  687. mp_call_method_n_kw(1, 0, attr_delete_method);
  688. return true;
  689. }
  690. } else {
  691. // store attribute
  692. mp_obj_t attr_set_method[4];
  693. mp_load_method_maybe(member[0], MP_QSTR___set__, attr_set_method);
  694. if (attr_set_method[0] != MP_OBJ_NULL) {
  695. attr_set_method[2] = self_in;
  696. attr_set_method[3] = value;
  697. mp_call_method_n_kw(2, 0, attr_set_method);
  698. return true;
  699. }
  700. }
  701. #endif
  702. }
  703. #endif
  704. #if MICROPY_PY_DELATTR_SETATTR
  705. if (value == MP_OBJ_NULL) {
  706. // delete attribute
  707. // try __delattr__ first
  708. mp_obj_t attr_delattr_method[3];
  709. mp_load_method_maybe(self_in, MP_QSTR___delattr__, attr_delattr_method);
  710. if (attr_delattr_method[0] != MP_OBJ_NULL) {
  711. // __delattr__ exists, so call it
  712. attr_delattr_method[2] = MP_OBJ_NEW_QSTR(attr);
  713. mp_call_method_n_kw(1, 0, attr_delattr_method);
  714. return true;
  715. }
  716. } else {
  717. // store attribute
  718. // try __setattr__ first
  719. mp_obj_t attr_setattr_method[4];
  720. mp_load_method_maybe(self_in, MP_QSTR___setattr__, attr_setattr_method);
  721. if (attr_setattr_method[0] != MP_OBJ_NULL) {
  722. // __setattr__ exists, so call it
  723. attr_setattr_method[2] = MP_OBJ_NEW_QSTR(attr);
  724. attr_setattr_method[3] = value;
  725. mp_call_method_n_kw(2, 0, attr_setattr_method);
  726. return true;
  727. }
  728. }
  729. #endif
  730. skip_special_accessors:
  731. if (value == MP_OBJ_NULL) {
  732. // delete attribute
  733. mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
  734. return elem != NULL;
  735. } else {
  736. // store attribute
  737. mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
  738. return true;
  739. }
  740. }
  741. STATIC void mp_obj_instance_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  742. if (dest[0] == MP_OBJ_NULL) {
  743. mp_obj_instance_load_attr(self_in, attr, dest);
  744. } else {
  745. if (mp_obj_instance_store_attr(self_in, attr, dest[1])) {
  746. dest[0] = MP_OBJ_NULL; // indicate success
  747. }
  748. }
  749. }
  750. STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
  751. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  752. mp_obj_t member[4] = {MP_OBJ_NULL, MP_OBJ_NULL, index, value};
  753. struct class_lookup_data lookup = {
  754. .obj = self,
  755. .meth_offset = offsetof(mp_obj_type_t, subscr),
  756. .dest = member,
  757. .is_type = false,
  758. };
  759. if (value == MP_OBJ_NULL) {
  760. // delete item
  761. lookup.attr = MP_QSTR___delitem__;
  762. } else if (value == MP_OBJ_SENTINEL) {
  763. // load item
  764. lookup.attr = MP_QSTR___getitem__;
  765. } else {
  766. // store item
  767. lookup.attr = MP_QSTR___setitem__;
  768. }
  769. mp_obj_class_lookup(&lookup, self->base.type);
  770. if (member[0] == MP_OBJ_SENTINEL) {
  771. return mp_obj_subscr(self->subobj[0], index, value);
  772. } else if (member[0] != MP_OBJ_NULL) {
  773. size_t n_args = value == MP_OBJ_NULL || value == MP_OBJ_SENTINEL ? 1 : 2;
  774. mp_obj_t ret = mp_call_method_n_kw(n_args, 0, member);
  775. if (value == MP_OBJ_SENTINEL) {
  776. return ret;
  777. } else {
  778. return mp_const_none;
  779. }
  780. } else {
  781. return MP_OBJ_NULL; // op not supported
  782. }
  783. }
  784. STATIC mp_obj_t mp_obj_instance_get_call(mp_obj_t self_in, mp_obj_t *member) {
  785. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  786. struct class_lookup_data lookup = {
  787. .obj = self,
  788. .attr = MP_QSTR___call__,
  789. .meth_offset = offsetof(mp_obj_type_t, call),
  790. .dest = member,
  791. .is_type = false,
  792. };
  793. mp_obj_class_lookup(&lookup, self->base.type);
  794. return member[0];
  795. }
  796. bool mp_obj_instance_is_callable(mp_obj_t self_in) {
  797. mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
  798. return mp_obj_instance_get_call(self_in, member) != MP_OBJ_NULL;
  799. }
  800. 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) {
  801. mp_obj_t member[2] = {MP_OBJ_NULL, MP_OBJ_NULL};
  802. mp_obj_t call = mp_obj_instance_get_call(self_in, member);
  803. if (call == MP_OBJ_NULL) {
  804. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  805. mp_raise_TypeError("object not callable");
  806. } else {
  807. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  808. "'%s' object isn't callable", mp_obj_get_type_str(self_in)));
  809. }
  810. }
  811. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  812. if (call == MP_OBJ_SENTINEL) {
  813. return mp_call_function_n_kw(self->subobj[0], n_args, n_kw, args);
  814. }
  815. return mp_call_method_self_n_kw(member[0], member[1], n_args, n_kw, args);
  816. }
  817. STATIC mp_obj_t instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
  818. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  819. mp_obj_t member[2] = {MP_OBJ_NULL};
  820. struct class_lookup_data lookup = {
  821. .obj = self,
  822. .attr = MP_QSTR___iter__,
  823. .meth_offset = offsetof(mp_obj_type_t, getiter),
  824. .dest = member,
  825. .is_type = false,
  826. };
  827. mp_obj_class_lookup(&lookup, self->base.type);
  828. if (member[0] == MP_OBJ_NULL) {
  829. return MP_OBJ_NULL;
  830. } else if (member[0] == MP_OBJ_SENTINEL) {
  831. mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
  832. return type->getiter(self->subobj[0], iter_buf);
  833. } else {
  834. return mp_call_method_n_kw(0, 0, member);
  835. }
  836. }
  837. STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  838. mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
  839. mp_obj_t member[2] = {MP_OBJ_NULL};
  840. struct class_lookup_data lookup = {
  841. .obj = self,
  842. .attr = MP_QSTR_, // don't actually look for a method
  843. .meth_offset = offsetof(mp_obj_type_t, buffer_p.get_buffer),
  844. .dest = member,
  845. .is_type = false,
  846. };
  847. mp_obj_class_lookup(&lookup, self->base.type);
  848. if (member[0] == MP_OBJ_SENTINEL) {
  849. mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
  850. return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags);
  851. } else {
  852. return 1; // object does not support buffer protocol
  853. }
  854. }
  855. /******************************************************************************/
  856. // type object
  857. // - the struct is mp_obj_type_t and is defined in obj.h so const types can be made
  858. // - there is a constant mp_obj_type_t (called mp_type_type) for the 'type' object
  859. // - creating a new class (a new type) creates a new mp_obj_type_t
  860. #if ENABLE_SPECIAL_ACCESSORS
  861. STATIC bool check_for_special_accessors(mp_obj_t key, mp_obj_t value) {
  862. #if MICROPY_PY_DELATTR_SETATTR
  863. if (key == MP_OBJ_NEW_QSTR(MP_QSTR___setattr__) || key == MP_OBJ_NEW_QSTR(MP_QSTR___delattr__)) {
  864. return true;
  865. }
  866. #endif
  867. #if MICROPY_PY_BUILTINS_PROPERTY
  868. if (mp_obj_is_type(value, &mp_type_property)) {
  869. return true;
  870. }
  871. #endif
  872. #if MICROPY_PY_DESCRIPTORS
  873. static const uint8_t to_check[] = {
  874. MP_QSTR___get__, MP_QSTR___set__, MP_QSTR___delete__,
  875. };
  876. for (size_t i = 0; i < MP_ARRAY_SIZE(to_check); ++i) {
  877. mp_obj_t dest_temp[2];
  878. mp_load_method_protected(value, to_check[i], dest_temp, true);
  879. if (dest_temp[0] != MP_OBJ_NULL) {
  880. return true;
  881. }
  882. }
  883. #endif
  884. return false;
  885. }
  886. #endif
  887. STATIC void type_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  888. (void)kind;
  889. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  890. mp_printf(print, "<class '%q'>", self->name);
  891. }
  892. 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) {
  893. (void)type_in;
  894. mp_arg_check_num(n_args, n_kw, 1, 3, false);
  895. switch (n_args) {
  896. case 1:
  897. return MP_OBJ_FROM_PTR(mp_obj_get_type(args[0]));
  898. case 3:
  899. // args[0] = name
  900. // args[1] = bases tuple
  901. // args[2] = locals dict
  902. return mp_obj_new_type(mp_obj_str_get_qstr(args[0]), args[1], args[2]);
  903. default:
  904. mp_raise_TypeError("type takes 1 or 3 arguments");
  905. }
  906. }
  907. STATIC mp_obj_t type_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  908. // instantiate an instance of a class
  909. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  910. if (self->make_new == NULL) {
  911. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  912. mp_raise_TypeError("cannot create instance");
  913. } else {
  914. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  915. "cannot create '%q' instances", self->name));
  916. }
  917. }
  918. // make new instance
  919. mp_obj_t o = self->make_new(self, n_args, n_kw, args);
  920. // return new instance
  921. return o;
  922. }
  923. STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  924. assert(mp_obj_is_type(self_in, &mp_type_type));
  925. mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
  926. if (dest[0] == MP_OBJ_NULL) {
  927. // load attribute
  928. #if MICROPY_CPYTHON_COMPAT
  929. if (attr == MP_QSTR___name__) {
  930. dest[0] = MP_OBJ_NEW_QSTR(self->name);
  931. return;
  932. }
  933. if (attr == MP_QSTR___bases__) {
  934. if (self == &mp_type_object) {
  935. dest[0] = mp_const_empty_tuple;
  936. return;
  937. }
  938. mp_obj_t parent_obj = self->parent ? MP_OBJ_FROM_PTR(self->parent) : MP_OBJ_FROM_PTR(&mp_type_object);
  939. #if MICROPY_MULTIPLE_INHERITANCE
  940. if (mp_obj_is_type(parent_obj, &mp_type_tuple)) {
  941. dest[0] = parent_obj;
  942. return;
  943. }
  944. #endif
  945. dest[0] = mp_obj_new_tuple(1, &parent_obj);
  946. return;
  947. }
  948. #endif
  949. struct class_lookup_data lookup = {
  950. .obj = (mp_obj_instance_t*)self,
  951. .attr = attr,
  952. .meth_offset = 0,
  953. .dest = dest,
  954. .is_type = true,
  955. };
  956. mp_obj_class_lookup(&lookup, self);
  957. } else {
  958. // delete/store attribute
  959. if (self->locals_dict != NULL) {
  960. assert(self->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
  961. mp_map_t *locals_map = &self->locals_dict->map;
  962. if (locals_map->is_fixed) {
  963. // can't apply delete/store to a fixed map
  964. return;
  965. }
  966. if (dest[1] == MP_OBJ_NULL) {
  967. // delete attribute
  968. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
  969. if (elem != NULL) {
  970. dest[0] = MP_OBJ_NULL; // indicate success
  971. }
  972. } else {
  973. #if ENABLE_SPECIAL_ACCESSORS
  974. // Check if we add any special accessor methods with this store
  975. if (!(self->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  976. if (check_for_special_accessors(MP_OBJ_NEW_QSTR(attr), dest[1])) {
  977. if (self->flags & TYPE_FLAG_IS_SUBCLASSED) {
  978. // This class is already subclassed so can't have special accessors added
  979. mp_raise_msg(&mp_type_AttributeError, "can't add special method to already-subclassed class");
  980. }
  981. self->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  982. }
  983. }
  984. #endif
  985. // store attribute
  986. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  987. elem->value = dest[1];
  988. dest[0] = MP_OBJ_NULL; // indicate success
  989. }
  990. }
  991. }
  992. }
  993. const mp_obj_type_t mp_type_type = {
  994. { &mp_type_type },
  995. .name = MP_QSTR_type,
  996. .print = type_print,
  997. .make_new = type_make_new,
  998. .call = type_call,
  999. .unary_op = mp_generic_unary_op,
  1000. .attr = type_attr,
  1001. };
  1002. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {
  1003. // Verify input objects have expected type
  1004. if (!mp_obj_is_type(bases_tuple, &mp_type_tuple)) {
  1005. mp_raise_TypeError(NULL);
  1006. }
  1007. if (!mp_obj_is_type(locals_dict, &mp_type_dict)) {
  1008. mp_raise_TypeError(NULL);
  1009. }
  1010. // TODO might need to make a copy of locals_dict; at least that's how CPython does it
  1011. // Basic validation of base classes
  1012. uint16_t base_flags = 0;
  1013. size_t bases_len;
  1014. mp_obj_t *bases_items;
  1015. mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items);
  1016. for (size_t i = 0; i < bases_len; i++) {
  1017. if (!mp_obj_is_type(bases_items[i], &mp_type_type)) {
  1018. mp_raise_TypeError(NULL);
  1019. }
  1020. mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]);
  1021. // TODO: Verify with CPy, tested on function type
  1022. if (t->make_new == NULL) {
  1023. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  1024. mp_raise_TypeError("type isn't an acceptable base type");
  1025. } else {
  1026. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  1027. "type '%q' isn't an acceptable base type", t->name));
  1028. }
  1029. }
  1030. #if ENABLE_SPECIAL_ACCESSORS
  1031. if (mp_obj_is_instance_type(t)) {
  1032. t->flags |= TYPE_FLAG_IS_SUBCLASSED;
  1033. base_flags |= t->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  1034. }
  1035. #endif
  1036. }
  1037. mp_obj_type_t *o = m_new0(mp_obj_type_t, 1);
  1038. o->base.type = &mp_type_type;
  1039. o->flags = base_flags;
  1040. o->name = name;
  1041. o->print = instance_print;
  1042. o->make_new = mp_obj_instance_make_new;
  1043. o->call = mp_obj_instance_call;
  1044. o->unary_op = instance_unary_op;
  1045. o->binary_op = instance_binary_op;
  1046. o->attr = mp_obj_instance_attr;
  1047. o->subscr = instance_subscr;
  1048. o->getiter = instance_getiter;
  1049. //o->iternext = ; not implemented
  1050. o->buffer_p.get_buffer = instance_get_buffer;
  1051. if (bases_len > 0) {
  1052. // Inherit protocol from a base class. This allows to define an
  1053. // abstract base class which would translate C-level protocol to
  1054. // Python method calls, and any subclass inheriting from it will
  1055. // support this feature.
  1056. o->protocol = ((mp_obj_type_t*)MP_OBJ_TO_PTR(bases_items[0]))->protocol;
  1057. if (bases_len >= 2) {
  1058. #if MICROPY_MULTIPLE_INHERITANCE
  1059. o->parent = MP_OBJ_TO_PTR(bases_tuple);
  1060. #else
  1061. mp_raise_NotImplementedError("multiple inheritance not supported");
  1062. #endif
  1063. } else {
  1064. o->parent = MP_OBJ_TO_PTR(bases_items[0]);
  1065. }
  1066. }
  1067. o->locals_dict = MP_OBJ_TO_PTR(locals_dict);
  1068. #if ENABLE_SPECIAL_ACCESSORS
  1069. // Check if the class has any special accessor methods
  1070. if (!(o->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
  1071. for (size_t i = 0; i < o->locals_dict->map.alloc; i++) {
  1072. if (mp_map_slot_is_filled(&o->locals_dict->map, i)) {
  1073. const mp_map_elem_t *elem = &o->locals_dict->map.table[i];
  1074. if (check_for_special_accessors(elem->key, elem->value)) {
  1075. o->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
  1076. break;
  1077. }
  1078. }
  1079. }
  1080. }
  1081. #endif
  1082. const mp_obj_type_t *native_base;
  1083. size_t num_native_bases = instance_count_native_bases(o, &native_base);
  1084. if (num_native_bases > 1) {
  1085. mp_raise_TypeError("multiple bases have instance lay-out conflict");
  1086. }
  1087. mp_map_t *locals_map = &o->locals_dict->map;
  1088. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(MP_QSTR___new__), MP_MAP_LOOKUP);
  1089. if (elem != NULL) {
  1090. // __new__ slot exists; check if it is a function
  1091. if (mp_obj_is_fun(elem->value)) {
  1092. // __new__ is a function, wrap it in a staticmethod decorator
  1093. elem->value = static_class_method_make_new(&mp_type_staticmethod, 1, 0, &elem->value);
  1094. }
  1095. }
  1096. return MP_OBJ_FROM_PTR(o);
  1097. }
  1098. /******************************************************************************/
  1099. // super object
  1100. typedef struct _mp_obj_super_t {
  1101. mp_obj_base_t base;
  1102. mp_obj_t type;
  1103. mp_obj_t obj;
  1104. } mp_obj_super_t;
  1105. STATIC void super_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  1106. (void)kind;
  1107. mp_obj_super_t *self = MP_OBJ_TO_PTR(self_in);
  1108. mp_print_str(print, "<super: ");
  1109. mp_obj_print_helper(print, self->type, PRINT_STR);
  1110. mp_print_str(print, ", ");
  1111. mp_obj_print_helper(print, self->obj, PRINT_STR);
  1112. mp_print_str(print, ">");
  1113. }
  1114. 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) {
  1115. (void)type_in;
  1116. // 0 arguments are turned into 2 in the compiler
  1117. // 1 argument is not yet implemented
  1118. mp_arg_check_num(n_args, n_kw, 2, 2, false);
  1119. if (!mp_obj_is_type(args[0], &mp_type_type)) {
  1120. mp_raise_TypeError(NULL);
  1121. }
  1122. mp_obj_super_t *o = m_new_obj(mp_obj_super_t);
  1123. *o = (mp_obj_super_t){{type_in}, args[0], args[1]};
  1124. return MP_OBJ_FROM_PTR(o);
  1125. }
  1126. STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  1127. if (dest[0] != MP_OBJ_NULL) {
  1128. // not load attribute
  1129. return;
  1130. }
  1131. assert(mp_obj_is_type(self_in, &mp_type_super));
  1132. mp_obj_super_t *self = MP_OBJ_TO_PTR(self_in);
  1133. assert(mp_obj_is_type(self->type, &mp_type_type));
  1134. mp_obj_type_t *type = MP_OBJ_TO_PTR(self->type);
  1135. struct class_lookup_data lookup = {
  1136. .obj = MP_OBJ_TO_PTR(self->obj),
  1137. .attr = attr,
  1138. .meth_offset = 0,
  1139. .dest = dest,
  1140. .is_type = false,
  1141. };
  1142. // Allow a call super().__init__() to reach any native base classes
  1143. if (attr == MP_QSTR___init__) {
  1144. lookup.meth_offset = offsetof(mp_obj_type_t, make_new);
  1145. }
  1146. if (type->parent == NULL) {
  1147. // no parents, do nothing
  1148. #if MICROPY_MULTIPLE_INHERITANCE
  1149. } else if (((mp_obj_base_t*)type->parent)->type == &mp_type_tuple) {
  1150. const mp_obj_tuple_t *parent_tuple = type->parent;
  1151. size_t len = parent_tuple->len;
  1152. const mp_obj_t *items = parent_tuple->items;
  1153. for (size_t i = 0; i < len; i++) {
  1154. assert(mp_obj_is_type(items[i], &mp_type_type));
  1155. if (MP_OBJ_TO_PTR(items[i]) == &mp_type_object) {
  1156. // The "object" type will be searched at the end of this function,
  1157. // and we don't want to lookup native methods in object.
  1158. continue;
  1159. }
  1160. mp_obj_class_lookup(&lookup, (mp_obj_type_t*)MP_OBJ_TO_PTR(items[i]));
  1161. if (dest[0] != MP_OBJ_NULL) {
  1162. break;
  1163. }
  1164. }
  1165. #endif
  1166. } else if (type->parent != &mp_type_object) {
  1167. mp_obj_class_lookup(&lookup, type->parent);
  1168. }
  1169. if (dest[0] != MP_OBJ_NULL) {
  1170. if (dest[0] == MP_OBJ_SENTINEL) {
  1171. // Looked up native __init__ so defer to it
  1172. dest[0] = MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj);
  1173. dest[1] = self->obj;
  1174. }
  1175. return;
  1176. }
  1177. // Reset meth_offset so we don't look up any native methods in object,
  1178. // because object never takes up the native base-class slot.
  1179. lookup.meth_offset = 0;
  1180. mp_obj_class_lookup(&lookup, &mp_type_object);
  1181. }
  1182. const mp_obj_type_t mp_type_super = {
  1183. { &mp_type_type },
  1184. .name = MP_QSTR_super,
  1185. .print = super_print,
  1186. .make_new = super_make_new,
  1187. .attr = super_attr,
  1188. };
  1189. void mp_load_super_method(qstr attr, mp_obj_t *dest) {
  1190. mp_obj_super_t super = {{&mp_type_super}, dest[1], dest[2]};
  1191. mp_load_method(MP_OBJ_FROM_PTR(&super), attr, dest);
  1192. }
  1193. /******************************************************************************/
  1194. // subclassing and built-ins specific to types
  1195. // object and classinfo should be type objects
  1196. // (but the function will fail gracefully if they are not)
  1197. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) {
  1198. for (;;) {
  1199. if (object == classinfo) {
  1200. return true;
  1201. }
  1202. // not equivalent classes, keep searching base classes
  1203. // object should always be a type object, but just return false if it's not
  1204. if (!mp_obj_is_type(object, &mp_type_type)) {
  1205. return false;
  1206. }
  1207. const mp_obj_type_t *self = MP_OBJ_TO_PTR(object);
  1208. if (self->parent == NULL) {
  1209. // type has no parents
  1210. return false;
  1211. #if MICROPY_MULTIPLE_INHERITANCE
  1212. } else if (((mp_obj_base_t*)self->parent)->type == &mp_type_tuple) {
  1213. // get the base objects (they should be type objects)
  1214. const mp_obj_tuple_t *parent_tuple = self->parent;
  1215. const mp_obj_t *item = parent_tuple->items;
  1216. const mp_obj_t *top = item + parent_tuple->len - 1;
  1217. // iterate through the base objects
  1218. for (; item < top; ++item) {
  1219. if (mp_obj_is_subclass_fast(*item, classinfo)) {
  1220. return true;
  1221. }
  1222. }
  1223. // search last base (simple tail recursion elimination)
  1224. object = *item;
  1225. #endif
  1226. } else {
  1227. // type has 1 parent
  1228. object = MP_OBJ_FROM_PTR(self->parent);
  1229. }
  1230. }
  1231. }
  1232. STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
  1233. size_t len;
  1234. mp_obj_t *items;
  1235. if (mp_obj_is_type(classinfo, &mp_type_type)) {
  1236. len = 1;
  1237. items = &classinfo;
  1238. } else if (mp_obj_is_type(classinfo, &mp_type_tuple)) {
  1239. mp_obj_tuple_get(classinfo, &len, &items);
  1240. } else {
  1241. mp_raise_TypeError("issubclass() arg 2 must be a class or a tuple of classes");
  1242. }
  1243. for (size_t i = 0; i < len; i++) {
  1244. // We explicitly check for 'object' here since no-one explicitly derives from it
  1245. if (items[i] == MP_OBJ_FROM_PTR(&mp_type_object) || mp_obj_is_subclass_fast(object, items[i])) {
  1246. return mp_const_true;
  1247. }
  1248. }
  1249. return mp_const_false;
  1250. }
  1251. STATIC mp_obj_t mp_builtin_issubclass(mp_obj_t object, mp_obj_t classinfo) {
  1252. if (!mp_obj_is_type(object, &mp_type_type)) {
  1253. mp_raise_TypeError("issubclass() arg 1 must be a class");
  1254. }
  1255. return mp_obj_is_subclass(object, classinfo);
  1256. }
  1257. MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_issubclass_obj, mp_builtin_issubclass);
  1258. STATIC mp_obj_t mp_builtin_isinstance(mp_obj_t object, mp_obj_t classinfo) {
  1259. return mp_obj_is_subclass(MP_OBJ_FROM_PTR(mp_obj_get_type(object)), classinfo);
  1260. }
  1261. MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
  1262. mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type) {
  1263. mp_obj_type_t *self_type = mp_obj_get_type(self_in);
  1264. if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(self_type), native_type)) {
  1265. return MP_OBJ_NULL;
  1266. }
  1267. mp_obj_instance_t *self = (mp_obj_instance_t*)MP_OBJ_TO_PTR(self_in);
  1268. return self->subobj[0];
  1269. }
  1270. /******************************************************************************/
  1271. // staticmethod and classmethod types (probably should go in a different file)
  1272. 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) {
  1273. assert(self == &mp_type_staticmethod || self == &mp_type_classmethod);
  1274. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  1275. mp_obj_static_class_method_t *o = m_new_obj(mp_obj_static_class_method_t);
  1276. *o = (mp_obj_static_class_method_t){{self}, args[0]};
  1277. return MP_OBJ_FROM_PTR(o);
  1278. }
  1279. const mp_obj_type_t mp_type_staticmethod = {
  1280. { &mp_type_type },
  1281. .name = MP_QSTR_staticmethod,
  1282. .make_new = static_class_method_make_new,
  1283. };
  1284. const mp_obj_type_t mp_type_classmethod = {
  1285. { &mp_type_type },
  1286. .name = MP_QSTR_classmethod,
  1287. .make_new = static_class_method_make_new,
  1288. };