summaryrefslogtreecommitdiffstatshomepage
path: root/py/objset.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objset.c')
-rw-r--r--py/objset.c28
1 files changed, 27 insertions, 1 deletions
diff --git a/py/objset.c b/py/objset.c
index f225ca7f66..826f8bdc86 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -5,7 +5,9 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
+#include "mpqstr.h"
#include "obj.h"
+#include "runtime.h"
#include "map.h"
typedef struct _mp_obj_set_t {
@@ -29,10 +31,34 @@ void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
print(env, "}");
}
-static const mp_obj_type_t set_type = {
+static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
+ switch (n_args) {
+ case 0:
+ // return a new, empty set
+ return mp_obj_new_set(0, NULL);
+
+ case 1:
+ {
+ // 1 argument, an iterable from which we make a new set
+ mp_obj_t set = mp_obj_new_set(0, NULL);
+ mp_obj_t iterable = rt_getiter(args[0]);
+ mp_obj_t item;
+ while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
+ mp_obj_set_store(set, item);
+ }
+ return set;
+ }
+
+ default:
+ nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "set takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
+ }
+}
+
+const mp_obj_type_t set_type = {
{ &mp_const_type },
"set",
set_print, // print
+ set_make_new, // make_new
NULL, // call_n
NULL, // unary_op
NULL, // binary_op