|
From: David B. <dav...@us...> - 2004-12-19 21:46:44
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28287 Modified Files: Tag: sidewinder-branch langpython.c Log Message: added two-way type conversion for frontier records and lists. Records become Python Dictionaries, and vice versa. Lists become Python lists and vice versa. Index: langpython.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langpython.c,v retrieving revision 1.2.4.30 retrieving revision 1.2.4.31 diff -C2 -d -r1.2.4.30 -r1.2.4.31 *** langpython.c 18 Dec 2004 22:38:02 -0000 1.2.4.30 --- langpython.c 19 Dec 2004 21:46:34 -0000 1.2.4.31 *************** *** 39,43 **** #include "tablestructure.h" //#include "tableverbs.h" ! //#include "op.h" //#include "opinternal.h" #include "oplist.h" --- 39,43 ---- #include "tablestructure.h" //#include "tableverbs.h" ! #include "op.h" //#include "opinternal.h" #include "oplist.h" *************** *** 98,101 **** --- 98,103 ---- extern boolean langpushlistval (hdllistrecord hlist, ptrstring pkey, tyvaluerecord *val); + extern boolean langgetlistsize (const tyvaluerecord *, long *); + extern boolean langgetlistitem (const tyvaluerecord *, long, ptrstring, tyvaluerecord *); #if defined(WIN95VERSION) || (defined(MACVERSION) && TARGET_RT_MAC_MACHO) *************** *** 427,430 **** --- 429,433 ---- fl = fl && IMPORTSYMBOL (hm, PySeqIter_Type); + fl = fl && IMPORTSYMBOL (hm, PyList_Type); fl = fl && IMPORTSYMBOL (hm, PyList_Append); fl = fl && IMPORTSYMBOL (hm, PyList_GetItem); *************** *** 671,677 **** } else if (ob_type == &PyDict_Type) { ! //return convertPythonDict(obj, hdl, name); ! return false; } else if (ob_type == &PyComplex_Type) { --- 674,699 ---- } else if (ob_type == &PyDict_Type) { ! int pos = 0; ! PyObject *key; ! PyObject *value; + // get a new frontier list + setnilvalue(tvr); + if (!coercevalue(tvr, recordvaluetype)) + return false; + + // for each python element, convert and push a new value + while (PyDict_Next(obj, &pos, &key, &value)) { + char *name = PyString_AsString(PyObject_Str(key)); + bigstring bsname; + tyvaluerecord arg; + + copyctopstring(name, bsname); + if (!pythontofrontier(value, &arg)) + return false; + if (!langpushlistval((*tvr).data.listvalue, bsname, &arg)) + return false; + } + } else if (ob_type == &PyComplex_Type) { *************** *** 684,687 **** --- 706,727 ---- } else if (ob_type == &PyList_Type) { + int len, i; + + // get a new frontier list + setnilvalue(tvr); + if (!coercevalue(tvr, listvaluetype)) + return false; + + // for each python element, convert and push a new value + len = PyList_GET_SIZE(obj); + for (i = 0; i < len; i++) { + PyObject *o = PyList_GET_ITEM(obj, i); + tyvaluerecord arg; + if (!pythontofrontier(o, &arg)) + return false; + if (!langpushlistval((*tvr).data.listvalue, nil, &arg)) + return false; + } + //printf("list\n"); } else if (ob_type == &PyFile_Type) { *************** *** 739,744 **** } else if (ob_type == &PyDict_Type) { ! return convertPythonDict(obj, hdl, name); } else if (ob_type == &PyComplex_Type) { --- 779,804 ---- } else if (ob_type == &PyDict_Type) { ! int pos = 0; ! PyObject *key; ! PyObject *value; + // get a new frontier list + setnilvalue(&tvr); + if (!coercevalue(&tvr, recordvaluetype)) + return false; + + // for each python element, convert and push a new value + while (PyDict_Next(obj, &pos, &key, &value)) { + char *name = PyString_AsString(PyObject_Str(key)); + bigstring bsname; + tyvaluerecord arg; + + copyctopstring(name, bsname); + if (!pythontofrontier(value, &arg)) + return false; + if (!langpushlistval(tvr.data.listvalue, bsname, &arg)) + return false; + } + } else if (ob_type == &PyComplex_Type) { *************** *** 781,784 **** --- 841,862 ---- } else if (ob_type == &PyList_Type) { + int len, i; + + // get a new frontier list + setnilvalue(&tvr); + if (!coercevalue(&tvr, listvaluetype)) + return false; + + // for each python element, convert and push a new value + len = PyList_GET_SIZE(obj); + for (i = 0; i < len; i++) { + PyObject *o = PyList_GET_ITEM(obj, i); + tyvaluerecord arg; + if (!pythontofrontier(o, &arg)) + return false; + if (!langpushlistval(tvr.data.listvalue, nil, &arg)) + return false; + } + //printf("list\n"); } else if (ob_type == &PyFile_Type) { *************** *** 898,901 **** --- 976,1030 ---- break; + case listvaluetype: { + long len, i; + + if (!langgetlistsize(tvr, &len)) + return false; + + *pptr = PyList_New(len); + for (i = 1; i <= len; i++) { + tyvaluerecord item; + bigstring key; + PyObject *pobj; + + if (!langgetlistitem(tvr, i, (ptrstring)&key, &item)) + return false; + + if (!convertfrontiervalue(nil, zerostring, &item, &pobj)) + return false; + + PyList_SET_ITEM(*pptr, i - 1, pobj); + } + + break; + } + + case recordvaluetype: { + long len, i; + + if (!langgetlistsize(tvr, &len)) + return false; + + *pptr = PyDict_New(); + for (i = 1; i <= len; i++) { + tyvaluerecord item; + bigstring key; + char keyStr[sizeof(bigstring)]; + PyObject *pobj; + + if (!langgetlistitem(tvr, i, (ptrstring)&key, &item)) + return false; + + if (!convertfrontiervalue(nil, zerostring, &item, &pobj)) + return false; + + copyptocstring(key, keyStr); + + PyDict_SetItemString(*pptr, keyStr, pobj); + } + + break; + } + case externalvaluetype: { *************** *** 956,961 **** // //case binaryvaluetype: - //case listvaluetype: - //case recordvaluetype: // maybe support these --- 1085,1088 ---- |