objtype.c 55 KB

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