spacepaste

  1.  
  2. def test_tp_call(self):
  3. module = self.import_extension('foo', [
  4. ("tp_call", "METH_VARARGS",
  5. '''
  6. PyTypeObject *type = (PyTypeObject *)PyTuple_GET_ITEM(args, 0);
  7. PyObject *obj = PyTuple_GET_ITEM(args, 1);
  8. PyObject *c_args = PyTuple_GET_ITEM(args, 2);
  9. if (!type->tp_call)
  10. {
  11. PyErr_SetNone(PyExc_ValueError);
  12. return NULL;
  13. }
  14. return type->tp_call(obj, c_args, NULL);
  15. '''
  16. )
  17. ])
  18. class C:
  19. def __call__(self, *args):
  20. return args
  21. assert module.tp_call(type(C()), C(), ('x', 2)) == ('x', 2)
  22. class D(type):
  23. def __call__(self, *args):
  24. return "foo! %r" % (args,)
  25. typ1 = D('d', (), {})
  26. #assert module.tp_call(D, typ1, ()) == "foo! ()" XXX not working so far
  27. assert isinstance(module.tp_call(type, typ1, ()), typ1)
  28.