home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Python / structmember.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  8KB  |  309 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Map C struct members to Python object attributes */
  33.  
  34. #include "Python.h"
  35.  
  36. #include "structmember.h"
  37. #include "other/structmember_c.h"
  38.  
  39. static PyObject *
  40. listmembers(mlist)
  41.     struct memberlist *mlist;
  42. {
  43.     int i, n;
  44.     PyObject *v;
  45.     for (n = 0; mlist[n].name != NULL; n++)
  46.         ;
  47.     v = PyList_New(n);
  48.     if (v != NULL) {
  49.         for (i = 0; i < n; i++)
  50.             PyList_SetItem(v, i,
  51.                        PyString_FromString(mlist[i].name));
  52.         if (PyErr_Occurred()) {
  53.             Py_DECREF(v);
  54.             v = NULL;
  55.         }
  56.         else {
  57.             PyList_Sort(v);
  58.         }
  59.     }
  60.     return v;
  61. }
  62.  
  63. PyObject *
  64. PyMember_Get(addr, mlist, name)
  65.     char *addr;
  66.     struct memberlist *mlist;
  67.     char *name;
  68. {
  69.     struct memberlist *l;
  70.     
  71.     if (strcmp(name, "__members__") == 0)
  72.         return listmembers(mlist);
  73.     for (l = mlist; l->name != NULL; l++) {
  74.         if (strcmp(l->name, name) == 0) {
  75.             PyObject *v;
  76.             addr += l->offset;
  77.             switch (l->type) {
  78.             case T_BYTE:
  79.                 v = PyInt_FromLong((long)
  80.                          (((*(char*)addr & 0xff)
  81.                            ^ 0x80) - 0x80));
  82.                 break;
  83.             case T_UBYTE:
  84.                 v = PyInt_FromLong((long) *(char*)addr & 0xff);
  85.                 break;
  86.             case T_SHORT:
  87.                 v = PyInt_FromLong((long) *(short*)addr);
  88.                 break;
  89.             case T_USHORT:
  90.                 v = PyInt_FromLong((long)
  91.                          *(unsigned short*)addr);
  92.                 break;
  93.             case T_INT:
  94.                 v = PyInt_FromLong((long) *(int*)addr);
  95.                 break;
  96.             case T_UINT:
  97.                 v = PyInt_FromLong((long)
  98.                            *(unsigned int*)addr);
  99.                 break;
  100.             case T_LONG:
  101.                 v = PyInt_FromLong(*(long*)addr);
  102.                 break;
  103.             case T_ULONG:
  104.                 v = PyLong_FromDouble((double)
  105.                            *(unsigned long*)addr);
  106.                 break;
  107.             case T_FLOAT:
  108. #ifndef WITHOUT_FLOAT
  109.                 v = PyFloat_FromDouble((double)*(float*)addr);
  110. #else /* !WITHOUT_FLOAT */
  111.                 PyErr_SetString(PyExc_MissingFeatureError,
  112.                     "Float objects are not provided in this python build");
  113.                 v = NULL;
  114. #endif /* !WITHOUT_FLOAT */
  115.                 break;
  116.             case T_DOUBLE:
  117. #ifndef WITHOUT_FLOAT
  118.                 v = PyFloat_FromDouble(*(double*)addr);
  119. #else /* !WITHOUT_FLOAT */
  120.                 PyErr_SetString(PyExc_MissingFeatureError,
  121.                     "Float objects are not provided in this python build");
  122.                 v = NULL;
  123. #endif /* !WITHOUT_FLOAT */
  124.                 break;
  125.             case T_STRING:
  126.                 if (*(char**)addr == NULL) {
  127.                     Py_INCREF(Py_None);
  128.                     v = Py_None;
  129.                 }
  130.                 else
  131.                     v = PyString_FromString(*(char**)addr);
  132.                 break;
  133.             case T_STRING_INPLACE:
  134.                 v = PyString_FromString((char*)addr);
  135.                 break;
  136. #ifdef macintosh
  137.             case T_PSTRING:
  138.                 if (*(char**)addr == NULL) {
  139.                     Py_INCREF(Py_None);
  140.                     v = Py_None;
  141.                 }
  142.                 else
  143.                     v = PyString_FromStringAndSize(
  144.                         (*(char**)addr)+1,
  145.                         **(unsigned char**)addr);
  146.                 break;
  147.             case T_PSTRING_INPLACE:
  148.                 v = PyString_FromStringAndSize(
  149.                     ((char*)addr)+1,
  150.                     *(unsigned char*)addr);
  151.                 break;
  152. #endif /* macintosh */
  153.             case T_CHAR:
  154.                 v = PyString_FromStringAndSize((char*)addr, 1);
  155.                 break;
  156.             case T_OBJECT:
  157.                 v = *(PyObject **)addr;
  158.                 if (v == NULL)
  159.                     v = Py_None;
  160.                 Py_INCREF(v);
  161.                 break;
  162.             default:
  163.                 PyErr_SetString(PyExc_SystemError,
  164.                         "bad memberlist type");
  165.                 v = NULL;
  166.             }
  167.             return v;
  168.         }
  169.     }
  170.     
  171.     PyErr_SetString(PyExc_AttributeError, name);
  172.     return NULL;
  173. }
  174.  
  175. int
  176. PyMember_Set(addr, mlist, name, v)
  177.     char *addr;
  178.     struct memberlist *mlist;
  179.     char *name;
  180.     PyObject *v;
  181. {
  182.     struct memberlist *l;
  183.     PyObject *oldv;
  184.     
  185.     for (l = mlist; l->name != NULL; l++) {
  186.         if (strcmp(l->name, name) == 0) {
  187. #ifdef macintosh
  188.             if (l->readonly || l->type == T_STRING ||
  189.                 l->type == T_PSTRING)
  190.             {
  191. #else
  192.             if (l->readonly || l->type == T_STRING ) {
  193. #endif /* macintosh */
  194.                 PyErr_SetString(PyExc_TypeError,
  195.                         "readonly attribute");
  196.                 return -1;
  197.             }
  198.             if (v == NULL && l->type != T_OBJECT) {
  199.                 PyErr_SetString(PyExc_TypeError,
  200.                   "can't delete numeric/char attribute");
  201.                 return -1;
  202.             }
  203.             addr += l->offset;
  204.             switch (l->type) {
  205.             case T_BYTE:
  206.             case T_UBYTE:
  207.                 if (!PyInt_Check(v)) {
  208.                     PyErr_BadArgument();
  209.                     return -1;
  210.                 }
  211.                 *(char*)addr = (char) PyInt_AsLong(v);
  212.                 break;
  213.             case T_SHORT:
  214.             case T_USHORT:
  215.                 if (!PyInt_Check(v)) {
  216.                     PyErr_BadArgument();
  217.                     return -1;
  218.                 }
  219.                 *(short*)addr = (short) PyInt_AsLong(v);
  220.                 break;
  221.             case T_UINT:
  222.             case T_INT:
  223.                 if (!PyInt_Check(v)) {
  224.                     PyErr_BadArgument();
  225.                     return -1;
  226.                 }
  227.                 *(int*)addr = (int) PyInt_AsLong(v);
  228.                 break;
  229.             case T_LONG:
  230.                 if (!PyInt_Check(v)) {
  231.                     PyErr_BadArgument();
  232.                     return -1;
  233.                 }
  234.                 *(long*)addr = PyInt_AsLong(v);
  235.                 break;
  236.             case T_ULONG:
  237.                 if (PyInt_Check(v))
  238.                     *(long*)addr = PyInt_AsLong(v);
  239.                 else if (PyLong_Check(v))
  240.                     *(long*)addr = PyLong_AsLong(v);
  241.                 else {
  242.                     PyErr_BadArgument();
  243.                     return -1;
  244.                 }
  245.                 break;
  246.             case T_FLOAT:
  247. #ifndef WITHOUT_FLOAT
  248.                 if (PyInt_Check(v))
  249.                     *(float*)addr =
  250.                         (float) PyInt_AsLong(v);
  251.                 else if (PyFloat_Check(v))
  252.                     *(float*)addr =
  253.                         (float) PyFloat_AsDouble(v);
  254.                 else {
  255.                     PyErr_BadArgument();
  256.                     return -1;
  257.                 }
  258. #else /* !WITHOUT_FLOAT */
  259.                 PyErr_SetString(PyExc_MissingFeatureError,
  260.                     "Float objects are not provided in this python build");
  261.                 return -1;
  262. #endif /* !WITHOUT_FLOAT */
  263.                 break;
  264.             case T_DOUBLE:
  265. #ifndef WITHOUT_FLOAT
  266.                 if (PyInt_Check(v))
  267.                     *(double*)addr =
  268.                         (double) PyInt_AsLong(v);
  269.                 else if (PyFloat_Check(v))
  270.                     *(double*)addr = PyFloat_AsDouble(v);
  271.                 else {
  272.                     PyErr_BadArgument();
  273.                     return -1;
  274.                 }
  275. #else /* !WITHOUT_FLOAT */
  276.                 PyErr_SetString(PyExc_MissingFeatureError,
  277.                     "Float objects are not provided in this python build");
  278.                 return -1;
  279. #endif /* !WITHOUT_FLOAT */
  280.                 break;
  281.             case T_OBJECT:
  282.                 Py_XINCREF(v);
  283.                 oldv = *(PyObject **)addr;
  284.                 *(PyObject **)addr = v;
  285.                 Py_XDECREF(oldv);
  286.                 break;
  287.             case T_CHAR:
  288.                 if (PyString_Check(v) &&
  289.                     PyString_Size(v) == 1) {
  290.                     *(char*)addr =
  291.                         PyString_AsString(v)[0];
  292.                 }
  293.                 else {
  294.                     PyErr_BadArgument();
  295.                     return -1;
  296.                 }
  297.             default:
  298.                 PyErr_SetString(PyExc_SystemError,
  299.                         "bad memberlist type");
  300.                 return -1;
  301.             }
  302.             return 0;
  303.         }
  304.     }
  305.     
  306.     PyErr_SetString(PyExc_AttributeError, name);
  307.     return -1;
  308. }
  309.