Changeset 928 for trunk/zoo-project
- Timestamp:
- May 28, 2019, 11:57:29 AM (7 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 PyObject* list = PyDict_Keys((PyObject*) t); // new ref761 int nb = PyList_Size(list);760 PyObject* list = PyDict_Keys((PyObject*) t); // new ref 761 int nb = PyList_Size(list); 762 762 763 if (nb < 1) {764 Py_DECREF(list);765 return NULL;766 }763 if (nb < 1) { 764 Py_DECREF(list); 765 return NULL; 766 } 767 767 768 map* ptr = (map*) malloc(MAP_SIZE);769 map* res = ptr;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 key = PyList_GetItem(list, i); // borrowed ref778 value = PyDict_GetItem((PyObject*) t, key); // borrowed ref780 key = PyList_GetItem(list, i); // borrowed ref 781 value = PyDict_GetItem((PyObject*) t, key); // borrowed ref 779 782 780 ptr->name = zStrdup(PyString_AsString(key));781 map* msize = NULL;783 ptr->name = zStrdup(PyString_AsString(key)); 784 map* msize = NULL; 782 785 783 #if PY_MAJOR_VERSION >= 3784 if (PyBytes_Check(value)) {785 // value is byte array786 size = PyBytes_Size(value);787 buffer = PyBytes_AsString(value); // pointer to internal buffer788 char sz[32];789 sprintf(sz, "%d", (int) size);790 msize = createMap("size", sz);791 }792 else if (PyUnicode_Check(value) && PyUnicode_READY(value) == 0) {793 // value is string object794 buffer = PyUnicode_AsUTF8AndSize(value, &size);795 size++;796 }797 else {798 printf("Type not recognized\n");799 // error handling800 // ...801 }802 #else803 PyString_AsStringAndSize(value, &buffer, &size);804 size++;805 // to do: handle byte arrays806 #endif786 #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 ptr->value = (char*) malloc(size); // check for NULL pointer809 memmove(ptr->value, buffer, size);811 ptr->value = (char*) malloc(size); // check for NULL pointer 812 memmove(ptr->value, buffer, size); 810 813 811 if (msize != NULL) {812 ptr->next = msize;813 ptr = ptr->next;814 }814 if (msize != NULL) { 815 ptr->next = msize; 816 ptr = ptr->next; 817 } 815 818 816 ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL;817 ptr = ptr->next;818 }819 Py_DECREF(list);819 ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL; 820 ptr = ptr->next; 821 } 822 Py_DECREF(list); 820 823 821 return res;824 return res; 822 825 } // mapFromPyDict 823 826
Note: See TracChangeset
for help on using the changeset viewer.













