Changeset 738 for trunk/zoo-project/zoo-kernel/service_internal_python.c
- Timestamp:
- Jul 13, 2015, 3:42:39 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/service_internal_python.c
r682 r738 563 563 564 564 /** 565 * Convert a Python dictionary to a maps 566 * 567 * @param t the PyDictObject to convert 568 * @return a new maps containing the converted PyDictObject 569 * @warning make sure to free resources returned by this function 570 */ 571 maps* _mapsFromPyDict(PyDictObject* t){ 572 573 PyObject* list = PyDict_Keys((PyObject*)t); // new ref 574 int nb = PyList_Size(list); 575 576 if (nb < 1) { 577 Py_DECREF(list); 578 return NULL; 579 } 580 581 maps* ptr = (maps*) malloc(MAPS_SIZE); 582 maps* res = ptr; 583 584 PyObject* key; 585 PyObject* value; 586 587 for(int i = 0; i < nb; i++) { 588 589 key = PyList_GetItem(list,i); // borrowed ref 590 value = PyDict_GetItem((PyObject*) t, key); // borrowed ref 591 592 ptr->name = zStrdup(PyString_AsString(key)); 593 ptr->content = mapFromPyDict((PyDictObject*) value); 594 595 ptr->next = i < nb - 1 ? (maps*) malloc(MAPS_SIZE) : NULL; 596 ptr = ptr->next; 597 } 598 Py_DECREF(list); 599 600 return res; 601 } // mapsFromPyDict 602 603 /** 565 604 * Convert a Python dictionary to a map 566 605 * … … 597 636 } 598 637 else{ 638 #ifdef DEBUG 599 639 fprintf(stderr,"Unsupported return value."); 640 #endif 600 641 return NULL; 601 642 } 602 643 #else 603 644 PyString_AsStringAndSize(value,&buffer,&size); 604 #endif 605 645 #endif 646 res = addToMapWithSize(res,PyString_AsString(key),buffer,size); 606 647 }else{ 607 648 char* lkey=PyString_AsString(key); … … 622 663 623 664 /** 665 * Convert a Python dictionary to a map 666 * 667 * @param t the PyDictObject to convert 668 * @return a new map containing the converted PyDictObject 669 * @warning make sure to free resources returned by this function 670 */ 671 map* _mapFromPyDict(PyDictObject* t) { 672 673 PyObject* list = PyDict_Keys((PyObject*) t); // new ref 674 int nb = PyList_Size(list); 675 676 if (nb < 1) { 677 Py_DECREF(list); 678 return NULL; 679 } 680 681 map* ptr = (map*) malloc(MAP_SIZE); 682 map* res = ptr; 683 684 PyObject* key; 685 PyObject* value; 686 char *buffer = NULL; 687 Py_ssize_t size; 688 for(int i = 0; i < nb; i++) { 689 690 key = PyList_GetItem(list, i); // borrowed ref 691 value = PyDict_GetItem((PyObject*) t, key); // borrowed ref 692 693 ptr->name = zStrdup(PyString_AsString(key)); 694 map* msize = NULL; 695 696 #if PY_MAJOR_VERSION >= 3 697 if (PyBytes_Check(value)) { 698 // value is byte array 699 size = PyBytes_Size(value); 700 buffer = PyBytes_AsString(value); // pointer to internal buffer 701 char sz[32]; 702 sprintf(sz, "%d", (int) size); 703 msize = createMap("size", sz); 704 } 705 else if (PyUnicode_Check(value) && PyUnicode_READY(value) == 0) { 706 // value is string object 707 buffer = PyUnicode_AsUTF8AndSize(value, &size); 708 size++; 709 } 710 else { 711 printf("Type not recognized\n"); 712 // error handling 713 // ... 714 } 715 #else 716 PyString_AsStringAndSize(value, &buffer, &size); 717 size++; 718 // to do: handle byte arrays 719 #endif 720 721 ptr->value = (char*) malloc(size); // check for NULL pointer 722 memmove(ptr->value, buffer, size); 723 724 if (msize != NULL) { 725 ptr->next = msize; 726 ptr = ptr->next; 727 } 728 729 ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL; 730 ptr = ptr->next; 731 } 732 Py_DECREF(list); 733 734 return res; 735 } // mapFromPyDict 736 737 /** 624 738 * Use the ZOO-Services messages translation function from the Python 625 739 * environment
Note: See TracChangeset
for help on using the changeset viewer.