;; Fu Jie Huang  9/2002

;; the dummiest demo: 
;; to emulate the python code: (with line number)
;;  1. Y = 2
;;  2. X = 3
;;  3. X = X+Y
;;  4. print X

;; first, load in the python C API, and initialize the interpreter
(libload "python/python")
(Py_Initialize)

;; each python process has an implicit dictionary for bookkeeping
;; to emulate this process, you need to create your own dictionary
;; later, you can retrieve data from this dictionary (so-called dereference)
;; as well as plant new data into this dictionary (so-called assignment)
(setq pdict (PyDict_New))
(PyDict_SetItemString pdict "__builtins__" (PyEval_GetBuiltins))

;; obviously, we need two assignments done first
(PyDict_SetItemString pdict "Y" (PyInt_FromLong 2))
(PyDict_SetItemString pdict "X" (PyInt_FromLong 3))

;; then run the "X=X+Y" statement, this is run within the context(dictionary)
;; the constant 257 means the string contains a statement, not an expression
(PyRun_String "X=X+Y" 257 pdict pdict)
(PyRun_String "print X" 257 pdict pdict)

;; reclaim the memory (decrease the reference count of the dictionary)
(Py_DECREF pdict)