/* code to create classes C and D */ static void setup_classes() { #ifdef TAGGING pyobj zero = inject_int(0); pyobj one = inject_int(1); #else pyobj zero = create_int(0); pyobj one = create_int(1); #endif pyobj list0 = create_list(zero); pyobj list1 = create_list(one); // class C: // def m(self): // return self.f C = create_class(list0); // Add method m to the class. set_attr(C, "m", create_closure(C_m, list0)); // class D(C): // def m(self): // return self.f + 1 set_subscript(list1, zero, C); // list1[0] = C D = create_class(list1); pyobj D_m_closure = create_closure(D_m, list0); set_attr(D, "m", create_closure(D_m, list0)); pyobj D_n_closure = create_closure(D_n, list0); set_attr(D, "n", create_closure(D_n, list0)); }
int main (int argc, char *argv[]) { big_pyobj *f, *freevars; int i; /** * test1: simple closure with no free vars * f = lambda x: x * decref(list) */ pymem_init(); freevars = create_list(inject_int(0)); incref(freevars); f = create_closure(dummy_function, inject_big(freevars)); incref(f); assert (freevars->ref_ctr == 2); decref(f); assert (freevars->ref_ctr == 1); decref(freevars); pymem_print_stats(); pymem_shutdown(); }
int main (int argc, char *argv[]) { big_pyobj *A_parents; big_pyobj *A; big_pyobj *freevars; big_pyobj *f; big_pyobj *ubmethod; int i; /** * test1: retrieve unbound method from simple class * class A: * f = lambda x: x * dependencies: * f -> freevars * A -> A_parents * A -> f (via A.f) * ubmethod -> f * ubmethod -> A */ pymem_init(); freevars = create_list(inject_int(0)); incref(freevars); f = create_closure(dummy_function, inject_big(freevars)); incref(f); A_parents = create_list(inject_int(0)); incref(A_parents); A = create_class(inject_big(A_parents)); incref(A); assert (A_parents->ref_ctr == 2); set_attr(inject_big(A), "f", inject_big(f)); assert (f->ref_ctr == 2); ubmethod = project_big(get_attr(inject_big(A), "f")); incref(ubmethod); assert (A->ref_ctr == 2); assert (f->ref_ctr == 3); decref(freevars); assert (freevars->ref_ctr == 1); decref(f); assert (f->ref_ctr == 2); decref(A_parents); assert(A_parents->ref_ctr == 1); decref(A); assert(A->ref_ctr == 1); // at this point only the unbound method is keeping stuff alive. pymem_print_stats(); decref(ubmethod); pymem_print_stats(); pymem_shutdown(); }