obj.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <assert.h>
  30. #include "py/obj.h"
  31. #include "py/objtype.h"
  32. #include "py/objint.h"
  33. #include "py/objstr.h"
  34. #include "py/runtime.h"
  35. #include "py/stackctrl.h"
  36. #include "py/stream.h" // for mp_obj_print
  37. const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
  38. #if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
  39. if (mp_obj_is_obj(o_in)) {
  40. const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
  41. return o->type;
  42. } else {
  43. static const mp_obj_type_t *const types[] = {
  44. NULL, &mp_type_int, &mp_type_str, &mp_type_int,
  45. NULL, &mp_type_int, &mp_type_NoneType, &mp_type_int,
  46. NULL, &mp_type_int, &mp_type_str, &mp_type_int,
  47. NULL, &mp_type_int, &mp_type_bool, &mp_type_int,
  48. };
  49. return types[(uintptr_t)o_in & 0xf];
  50. }
  51. #elif MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
  52. if (mp_obj_is_small_int(o_in)) {
  53. return &mp_type_int;
  54. } else if (mp_obj_is_obj(o_in)) {
  55. const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
  56. return o->type;
  57. #if MICROPY_PY_BUILTINS_FLOAT
  58. } else if ((((mp_uint_t)(o_in)) & 0xff800007) != 0x00000006) {
  59. return &mp_type_float;
  60. #endif
  61. } else {
  62. static const mp_obj_type_t *const types[] = {
  63. &mp_type_str, &mp_type_NoneType, &mp_type_str, &mp_type_bool,
  64. };
  65. return types[((uintptr_t)o_in >> 3) & 3];
  66. }
  67. #else
  68. if (mp_obj_is_small_int(o_in)) {
  69. return &mp_type_int;
  70. } else if (mp_obj_is_qstr(o_in)) {
  71. return &mp_type_str;
  72. #if MICROPY_PY_BUILTINS_FLOAT && ( \
  73. MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D)
  74. } else if (mp_obj_is_float(o_in)) {
  75. return &mp_type_float;
  76. #endif
  77. #if MICROPY_OBJ_IMMEDIATE_OBJS
  78. } else if (mp_obj_is_immediate_obj(o_in)) {
  79. static const mp_obj_type_t *const types[2] = {&mp_type_NoneType, &mp_type_bool};
  80. return types[MP_OBJ_IMMEDIATE_OBJ_VALUE(o_in) & 1];
  81. #endif
  82. } else {
  83. const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
  84. return o->type;
  85. }
  86. #endif
  87. }
  88. const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
  89. return qstr_str(mp_obj_get_type(o_in)->name);
  90. }
  91. void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  92. // There can be data structures nested too deep, or just recursive
  93. MP_STACK_CHECK();
  94. #ifndef NDEBUG
  95. if (o_in == MP_OBJ_NULL) {
  96. mp_print_str(print, "(nil)");
  97. return;
  98. }
  99. #endif
  100. const mp_obj_type_t *type = mp_obj_get_type(o_in);
  101. if (type->print != NULL) {
  102. type->print((mp_print_t *)print, o_in, kind);
  103. } else {
  104. mp_printf(print, "<%q>", type->name);
  105. }
  106. }
  107. void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
  108. mp_obj_print_helper(MP_PYTHON_PRINTER, o_in, kind);
  109. }
  110. // helper function to print an exception with traceback
  111. void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
  112. if (mp_obj_is_exception_instance(exc)) {
  113. size_t n, *values;
  114. mp_obj_exception_get_traceback(exc, &n, &values);
  115. if (n > 0) {
  116. assert(n % 3 == 0);
  117. mp_print_str(print, "Traceback (most recent call last):\n");
  118. for (int i = n - 3; i >= 0; i -= 3) {
  119. #if MICROPY_ENABLE_SOURCE_LINE
  120. mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
  121. #else
  122. mp_printf(print, " File \"%q\"", values[i]);
  123. #endif
  124. // the block name can be NULL if it's unknown
  125. qstr block = values[i + 2];
  126. if (block == MP_QSTRnull) {
  127. mp_print_str(print, "\n");
  128. } else {
  129. mp_printf(print, ", in %q\n", block);
  130. }
  131. }
  132. }
  133. }
  134. mp_obj_print_helper(print, exc, PRINT_EXC);
  135. mp_print_str(print, "\n");
  136. }
  137. bool mp_obj_is_true(mp_obj_t arg) {
  138. if (arg == mp_const_false) {
  139. return 0;
  140. } else if (arg == mp_const_true) {
  141. return 1;
  142. } else if (arg == mp_const_none) {
  143. return 0;
  144. } else if (mp_obj_is_small_int(arg)) {
  145. if (arg == MP_OBJ_NEW_SMALL_INT(0)) {
  146. return 0;
  147. } else {
  148. return 1;
  149. }
  150. } else {
  151. const mp_obj_type_t *type = mp_obj_get_type(arg);
  152. if (type->unary_op != NULL) {
  153. mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
  154. if (result != MP_OBJ_NULL) {
  155. return result == mp_const_true;
  156. }
  157. }
  158. mp_obj_t len = mp_obj_len_maybe(arg);
  159. if (len != MP_OBJ_NULL) {
  160. // obj has a length, truth determined if len != 0
  161. return len != MP_OBJ_NEW_SMALL_INT(0);
  162. } else {
  163. // any other obj is true per Python semantics
  164. return 1;
  165. }
  166. }
  167. }
  168. bool mp_obj_is_callable(mp_obj_t o_in) {
  169. const mp_call_fun_t call = mp_obj_get_type(o_in)->call;
  170. if (call != mp_obj_instance_call) {
  171. return call != NULL;
  172. }
  173. return mp_obj_instance_is_callable(o_in);
  174. }
  175. // This function implements the '==' and '!=' operators.
  176. //
  177. // From the Python language reference:
  178. // (https://docs.python.org/3/reference/expressions.html#not-in)
  179. // "The objects need not have the same type. If both are numbers, they are converted
  180. // to a common type. Otherwise, the == and != operators always consider objects of
  181. // different types to be unequal."
  182. //
  183. // This means that False==0 and True==1 are true expressions.
  184. //
  185. // Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
  186. // comparison returns NotImplemented, == and != are decided by comparing the object
  187. // pointer."
  188. mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
  189. mp_obj_t local_true = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_false : mp_const_true;
  190. mp_obj_t local_false = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_true : mp_const_false;
  191. int pass_number = 0;
  192. // Shortcut for very common cases
  193. if (o1 == o2 &&
  194. (mp_obj_is_small_int(o1) || !(mp_obj_get_type(o1)->flags & MP_TYPE_FLAG_EQ_NOT_REFLEXIVE))) {
  195. return local_true;
  196. }
  197. // fast path for strings
  198. if (mp_obj_is_str(o1)) {
  199. if (mp_obj_is_str(o2)) {
  200. // both strings, use special function
  201. return mp_obj_str_equal(o1, o2) ? local_true : local_false;
  202. #if MICROPY_PY_STR_BYTES_CMP_WARN
  203. } else if (mp_obj_is_type(o2, &mp_type_bytes)) {
  204. str_bytes_cmp:
  205. mp_warning(MP_WARN_CAT(BytesWarning), "Comparison between bytes and str");
  206. return local_false;
  207. #endif
  208. } else {
  209. goto skip_one_pass;
  210. }
  211. #if MICROPY_PY_STR_BYTES_CMP_WARN
  212. } else if (mp_obj_is_str(o2) && mp_obj_is_type(o1, &mp_type_bytes)) {
  213. // o1 is not a string (else caught above), so the objects are not equal
  214. goto str_bytes_cmp;
  215. #endif
  216. }
  217. // fast path for small ints
  218. if (mp_obj_is_small_int(o1)) {
  219. if (mp_obj_is_small_int(o2)) {
  220. // both SMALL_INT, and not equal if we get here
  221. return local_false;
  222. } else {
  223. goto skip_one_pass;
  224. }
  225. }
  226. // generic type, call binary_op(MP_BINARY_OP_EQUAL)
  227. while (pass_number < 2) {
  228. const mp_obj_type_t *type = mp_obj_get_type(o1);
  229. // If a full equality test is not needed and the other object is a different
  230. // type then we don't need to bother trying the comparison.
  231. if (type->binary_op != NULL &&
  232. ((type->flags & MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE) || mp_obj_get_type(o2) == type)) {
  233. // CPython is asymmetric: it will try __eq__ if there's no __ne__ but not the
  234. // other way around. If the class doesn't need a full test we can skip __ne__.
  235. if (op == MP_BINARY_OP_NOT_EQUAL && (type->flags & MP_TYPE_FLAG_EQ_HAS_NEQ_TEST)) {
  236. mp_obj_t r = type->binary_op(MP_BINARY_OP_NOT_EQUAL, o1, o2);
  237. if (r != MP_OBJ_NULL) {
  238. return r;
  239. }
  240. }
  241. // Try calling __eq__.
  242. mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
  243. if (r != MP_OBJ_NULL) {
  244. if (op == MP_BINARY_OP_EQUAL) {
  245. return r;
  246. } else {
  247. return mp_obj_is_true(r) ? local_true : local_false;
  248. }
  249. }
  250. }
  251. skip_one_pass:
  252. // Try the other way around if none of the above worked
  253. ++pass_number;
  254. mp_obj_t temp = o1;
  255. o1 = o2;
  256. o2 = temp;
  257. }
  258. // equality not implemented, so fall back to pointer conparison
  259. return (o1 == o2) ? local_true : local_false;
  260. }
  261. bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
  262. return mp_obj_is_true(mp_obj_equal_not_equal(MP_BINARY_OP_EQUAL, o1, o2));
  263. }
  264. mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
  265. // This function essentially performs implicit type conversion to int
  266. // Note that Python does NOT provide implicit type conversion from
  267. // float to int in the core expression language, try some_list[1.0].
  268. if (arg == mp_const_false) {
  269. return 0;
  270. } else if (arg == mp_const_true) {
  271. return 1;
  272. } else if (mp_obj_is_small_int(arg)) {
  273. return MP_OBJ_SMALL_INT_VALUE(arg);
  274. } else if (mp_obj_is_type(arg, &mp_type_int)) {
  275. return mp_obj_int_get_checked(arg);
  276. } else {
  277. mp_obj_t res = mp_unary_op(MP_UNARY_OP_INT, (mp_obj_t)arg);
  278. return mp_obj_int_get_checked(res);
  279. }
  280. }
  281. mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
  282. if (mp_obj_is_int(arg)) {
  283. return mp_obj_int_get_truncated(arg);
  284. } else {
  285. return mp_obj_get_int(arg);
  286. }
  287. }
  288. // returns false if arg is not of integral type
  289. // returns true and sets *value if it is of integral type
  290. // can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
  291. bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
  292. if (arg == mp_const_false) {
  293. *value = 0;
  294. } else if (arg == mp_const_true) {
  295. *value = 1;
  296. } else if (mp_obj_is_small_int(arg)) {
  297. *value = MP_OBJ_SMALL_INT_VALUE(arg);
  298. } else if (mp_obj_is_type(arg, &mp_type_int)) {
  299. *value = mp_obj_int_get_checked(arg);
  300. } else {
  301. return false;
  302. }
  303. return true;
  304. }
  305. #if MICROPY_PY_BUILTINS_FLOAT
  306. bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
  307. mp_float_t val;
  308. if (arg == mp_const_false) {
  309. val = 0;
  310. } else if (arg == mp_const_true) {
  311. val = 1;
  312. } else if (mp_obj_is_small_int(arg)) {
  313. val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
  314. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  315. } else if (mp_obj_is_type(arg, &mp_type_int)) {
  316. val = mp_obj_int_as_float_impl(arg);
  317. #endif
  318. } else if (mp_obj_is_float(arg)) {
  319. val = mp_obj_float_get(arg);
  320. } else {
  321. return false;
  322. }
  323. *value = val;
  324. return true;
  325. }
  326. mp_float_t mp_obj_get_float(mp_obj_t arg) {
  327. mp_float_t val;
  328. if (!mp_obj_get_float_maybe(arg, &val)) {
  329. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  330. mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
  331. #else
  332. mp_raise_msg_varg(&mp_type_TypeError,
  333. MP_ERROR_TEXT("can't convert %s to float"), mp_obj_get_type_str(arg));
  334. #endif
  335. }
  336. return val;
  337. }
  338. #if MICROPY_PY_BUILTINS_COMPLEX
  339. bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
  340. if (arg == mp_const_false) {
  341. *real = 0;
  342. *imag = 0;
  343. } else if (arg == mp_const_true) {
  344. *real = 1;
  345. *imag = 0;
  346. } else if (mp_obj_is_small_int(arg)) {
  347. *real = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
  348. *imag = 0;
  349. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  350. } else if (mp_obj_is_type(arg, &mp_type_int)) {
  351. *real = mp_obj_int_as_float_impl(arg);
  352. *imag = 0;
  353. #endif
  354. } else if (mp_obj_is_float(arg)) {
  355. *real = mp_obj_float_get(arg);
  356. *imag = 0;
  357. } else if (mp_obj_is_type(arg, &mp_type_complex)) {
  358. mp_obj_complex_get(arg, real, imag);
  359. } else {
  360. return false;
  361. }
  362. return true;
  363. }
  364. void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
  365. if (!mp_obj_get_complex_maybe(arg, real, imag)) {
  366. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  367. mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
  368. #else
  369. mp_raise_msg_varg(&mp_type_TypeError,
  370. MP_ERROR_TEXT("can't convert %s to complex"), mp_obj_get_type_str(arg));
  371. #endif
  372. }
  373. }
  374. #endif
  375. #endif
  376. // note: returned value in *items may point to the interior of a GC block
  377. void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
  378. if (mp_obj_is_type(o, &mp_type_tuple)) {
  379. mp_obj_tuple_get(o, len, items);
  380. } else if (mp_obj_is_type(o, &mp_type_list)) {
  381. mp_obj_list_get(o, len, items);
  382. } else {
  383. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  384. mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
  385. #else
  386. mp_raise_msg_varg(&mp_type_TypeError,
  387. MP_ERROR_TEXT("object '%s' isn't a tuple or list"), mp_obj_get_type_str(o));
  388. #endif
  389. }
  390. }
  391. // note: returned value in *items may point to the interior of a GC block
  392. void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
  393. size_t seq_len;
  394. mp_obj_get_array(o, &seq_len, items);
  395. if (seq_len != len) {
  396. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  397. mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
  398. #else
  399. mp_raise_msg_varg(&mp_type_ValueError,
  400. MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
  401. #endif
  402. }
  403. }
  404. // is_slice determines whether the index is a slice index
  405. size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
  406. mp_int_t i;
  407. if (mp_obj_is_small_int(index)) {
  408. i = MP_OBJ_SMALL_INT_VALUE(index);
  409. } else if (!mp_obj_get_int_maybe(index, &i)) {
  410. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  411. mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
  412. #else
  413. mp_raise_msg_varg(&mp_type_TypeError,
  414. MP_ERROR_TEXT("%q indices must be integers, not %s"),
  415. type->name, mp_obj_get_type_str(index));
  416. #endif
  417. }
  418. if (i < 0) {
  419. i += len;
  420. }
  421. if (is_slice) {
  422. if (i < 0) {
  423. i = 0;
  424. } else if ((mp_uint_t)i > len) {
  425. i = len;
  426. }
  427. } else {
  428. if (i < 0 || (mp_uint_t)i >= len) {
  429. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  430. mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
  431. #else
  432. mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
  433. #endif
  434. }
  435. }
  436. // By this point 0 <= i <= len and so fits in a size_t
  437. return (size_t)i;
  438. }
  439. mp_obj_t mp_obj_id(mp_obj_t o_in) {
  440. mp_int_t id = (mp_int_t)o_in;
  441. if (!mp_obj_is_obj(o_in)) {
  442. return mp_obj_new_int(id);
  443. } else if (id >= 0) {
  444. // Many OSes and CPUs have affinity for putting "user" memories
  445. // into low half of address space, and "system" into upper half.
  446. // We're going to take advantage of that and return small int
  447. // (signed) for such "user" addresses.
  448. return MP_OBJ_NEW_SMALL_INT(id);
  449. } else {
  450. // If that didn't work, well, let's return long int, just as
  451. // a (big) positive value, so it will never clash with the range
  452. // of small int returned in previous case.
  453. return mp_obj_new_int_from_uint((mp_uint_t)id);
  454. }
  455. }
  456. // will raise a TypeError if object has no length
  457. mp_obj_t mp_obj_len(mp_obj_t o_in) {
  458. mp_obj_t len = mp_obj_len_maybe(o_in);
  459. if (len == MP_OBJ_NULL) {
  460. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  461. mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
  462. #else
  463. mp_raise_msg_varg(&mp_type_TypeError,
  464. MP_ERROR_TEXT("object of type '%s' has no len()"), mp_obj_get_type_str(o_in));
  465. #endif
  466. } else {
  467. return len;
  468. }
  469. }
  470. // may return MP_OBJ_NULL
  471. mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
  472. if (
  473. #if !MICROPY_PY_BUILTINS_STR_UNICODE
  474. // It's simple - unicode is slow, non-unicode is fast
  475. mp_obj_is_str(o_in) ||
  476. #endif
  477. mp_obj_is_type(o_in, &mp_type_bytes)) {
  478. GET_STR_LEN(o_in, l);
  479. return MP_OBJ_NEW_SMALL_INT(l);
  480. } else {
  481. const mp_obj_type_t *type = mp_obj_get_type(o_in);
  482. if (type->unary_op != NULL) {
  483. return type->unary_op(MP_UNARY_OP_LEN, o_in);
  484. } else {
  485. return MP_OBJ_NULL;
  486. }
  487. }
  488. }
  489. mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
  490. const mp_obj_type_t *type = mp_obj_get_type(base);
  491. if (type->subscr != NULL) {
  492. mp_obj_t ret = type->subscr(base, index, value);
  493. if (ret != MP_OBJ_NULL) {
  494. return ret;
  495. }
  496. // TODO: call base classes here?
  497. }
  498. if (value == MP_OBJ_NULL) {
  499. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  500. mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
  501. #else
  502. mp_raise_msg_varg(&mp_type_TypeError,
  503. MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
  504. #endif
  505. } else if (value == MP_OBJ_SENTINEL) {
  506. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  507. mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
  508. #else
  509. mp_raise_msg_varg(&mp_type_TypeError,
  510. MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
  511. #endif
  512. } else {
  513. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  514. mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
  515. #else
  516. mp_raise_msg_varg(&mp_type_TypeError,
  517. MP_ERROR_TEXT("'%s' object doesn't support item assignment"), mp_obj_get_type_str(base));
  518. #endif
  519. }
  520. }
  521. // Return input argument. Useful as .getiter for objects which are
  522. // their own iterators, etc.
  523. mp_obj_t mp_identity(mp_obj_t self) {
  524. return self;
  525. }
  526. MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
  527. mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
  528. (void)iter_buf;
  529. return self;
  530. }
  531. bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  532. const mp_obj_type_t *type = mp_obj_get_type(obj);
  533. if (type->buffer_p.get_buffer == NULL) {
  534. return false;
  535. }
  536. int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
  537. if (ret != 0) {
  538. return false;
  539. }
  540. return true;
  541. }
  542. void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  543. if (!mp_get_buffer(obj, bufinfo, flags)) {
  544. mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
  545. }
  546. }
  547. mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
  548. switch (op) {
  549. case MP_UNARY_OP_HASH:
  550. return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
  551. default:
  552. return MP_OBJ_NULL; // op not supported
  553. }
  554. }