Changeset 928 for trunk/zoo-project
- Timestamp:
- May 28, 2019, 11:57:29 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/service_internal_python.c
r927 r928 758 758 map* _mapFromPyDict(PyDictObject* t) { 759 759 760 761 760 PyObject* list = PyDict_Keys((PyObject*) t); // new ref 761 int nb = PyList_Size(list); 762 762 763 764 765 766 763 if (nb < 1) { 764 Py_DECREF(list); 765 return NULL; 766 } 767 767 768 769 768 map* ptr = (map*) malloc(MAP_SIZE); 769 map* res = ptr; 770 770 771 PyObject* key; 772 PyObject* value; 773 const char *buffer = NULL; 774 Py_ssize_t size; 775 for(int i = 0; i < nb; i++) { 771 PyObject* key; 772 PyObject* value; 773 #if PY_MAJOR_VERSION >= 3 774 const 775 #endif 776 char *buffer = NULL; 777 Py_ssize_t size; 778 for(int i = 0; i < nb; i++) { 776 779 777 778 780 key = PyList_GetItem(list, i); // borrowed ref 781 value = PyDict_GetItem((PyObject*) t, key); // borrowed ref 779 782 780 781 783 ptr->name = zStrdup(PyString_AsString(key)); 784 map* msize = NULL; 782 785 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 786 #if PY_MAJOR_VERSION >= 3 787 if (PyBytes_Check(value)) { 788 // value is byte array 789 size = PyBytes_Size(value); 790 buffer = PyBytes_AsString(value); // pointer to internal buffer 791 char sz[32]; 792 sprintf(sz, "%d", (int) size); 793 msize = createMap("size", sz); 794 } 795 else if (PyUnicode_Check(value) && PyUnicode_READY(value) == 0) { 796 // value is string object 797 buffer = PyUnicode_AsUTF8AndSize(value, &size); 798 size++; 799 } 800 else { 801 printf("Type not recognized\n"); 802 // error handling 803 // ... 804 } 805 #else 806 PyString_AsStringAndSize(value, &buffer, &size); 807 size++; 808 // to do: handle byte arrays 809 #endif 807 810 808 809 811 ptr->value = (char*) malloc(size); // check for NULL pointer 812 memmove(ptr->value, buffer, size); 810 813 811 812 813 814 814 if (msize != NULL) { 815 ptr->next = msize; 816 ptr = ptr->next; 817 } 815 818 816 817 818 819 819 ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL; 820 ptr = ptr->next; 821 } 822 Py_DECREF(list); 820 823 821 824 return res; 822 825 } // mapFromPyDict 823 826
Note: See TracChangeset
for help on using the changeset viewer.