;; Fu Jie Huang  9/2002

;; create your own tuple, instantiate an instance from a class
;; and call the method on the tuple

;; to emulate the python code: (with line number)
;;  1. import addthem2
;;  2. p = addthemclass()
;;  2. x = p.addthree(2,3,4)
;;  3. print x

(libload "python/python")
(Py_Initialize)
(setq pdict (PyDict_New))
(PyDict_SetItemString pdict "__builtins__" (PyEval_GetBuiltins))

;; 1)********* create the tuple, 
(setq ptuple (PyTuple_New 3))
(PyTuple_SetItem  ptuple 0 (PyInt_FromLong 2))
(PyTuple_SetItem  ptuple 1 (PyInt_FromLong 3))
(PyTuple_SetItem  ptuple 2 (PyInt_FromLong 4))


;; 2)********* get the method

;; first, you import an existing module, 
;; and get the class from the imported module
(setq pmod (PyImport_ImportModule "addthem2"))
(setq pclass (PyObject_GetAttrString pmod "addthemclass"))

;; then,  you instantiate this class, with a dummy tuple argument
;; it's like running the constructor "addthemclass()" in python.
;; the interesting thing here is that a class is a callable object!
;; calling it will generate an instance.
(setq pdummy (PyTuple_New 0))
(setq pinstance (PyEval_CallObject  pclass  pdummy))

;; then, you extract the method bound with the instance
(setq pmethod (PyObject_GetAttrString pinstance "addthree"))


;; 3)******** run the method, with the tuple as its argument
(setq presult (PyEval_CallObject   pmethod   ptuple))
;; (setq presult (PyObject_CallObject pfunc ptuple))
;; (setq presult (PyObject_Call       pfunc ptuple (gptr ())))

(PyDict_SetItemString pdict "x" presult)
(PyRun_String "print x" 257 pdict pdict)
(Py_DECREF pdict)