Guest User

Untitled

a guest
Jul 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <Python.h>
  2.  
  3. void testfunc(PyObject *self, PyObject *args)
  4. {
  5. PyObject *obj;
  6.  
  7. if (!PyArg_ParseTuple(args, "O", &obj))
  8. return NULL;
  9.  
  10. PyStringObject *x;
  11. printf("A\n");
  12. x = PyObject_GetAttrString(obj, "x");
  13. printf("B %s\n",x->ob_sval);
  14. PyErr_Print();
  15. }
  16.  
  17. static PyMethodDef testMethods[] = {
  18. {"myfunc", (PyCFunction)testfunc, METH_VARARGS, "."},
  19. {NULL, NULL},
  20. };
  21.  
  22. PyMODINIT_FUNC
  23.  
  24. inittestmod(void)
  25. {
  26. (void) Py_InitModule("testmod", testMethods);
  27. }
  28. -------------------------
  29. import new
  30. import testmod
  31.  
  32. class Something(object):
  33. def __init__(self):
  34. self.func = new.instancemethod(testmod.myfunc, self, self.__class__)
  35. self.x = 'something'
  36.  
  37.  
  38. #Something.func = testmod.myfunc
  39. a = Something()
  40. print "Value returned should be:",a.x
  41. #testmod.myfunc(a)
  42. a.func()
  43. ------------------------
  44. Value returned should be: something
  45. A
  46. B something
Add Comment
Please, Sign In to add comment