home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Python / modsupport.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  11KB  |  506 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. /* Module support implementation */
  33.  
  34. #include "Python.h"
  35.  
  36. #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
  37. typedef extended va_double;
  38. #else
  39. typedef double va_double;
  40. #endif
  41.  
  42. /* Package context -- the full module name for package imports */
  43. char *_Py_PackageContext = NULL;
  44.  
  45. /* Py_InitModule4() parameters:
  46.    - name is the module name
  47.    - methods is the list of top-level functions
  48.    - doc is the documentation string
  49.    - passthrough is passed as self to functions defined in the module
  50.    - api_version is the value of PYTHON_API_VERSION at the time the
  51.      module was compiled
  52.  
  53.    Return value is a borrowed reference to the module object; or NULL
  54.    if an error occurred (in Python 1.4 and before, errors were fatal).
  55.    Errors may still leak memory.
  56. */
  57.  
  58. static char api_version_warning[] =
  59. "WARNING: Python C API version mismatch for module %s:\n\
  60.   This Python has API version %d, module %s has version %d.\n";
  61.  
  62. PyObject *
  63. Py_InitModule4(name, methods, doc, passthrough, module_api_version)
  64.     char *name;
  65.     PyMethodDef *methods;
  66.     char *doc;
  67.     PyObject *passthrough;
  68.     int module_api_version;
  69. {
  70.     PyObject *m, *d, *v;
  71.     PyMethodDef *ml;
  72.     if (module_api_version != PYTHON_API_VERSION)
  73.         fprintf(stderr, api_version_warning,
  74.             name, PYTHON_API_VERSION, name, module_api_version);
  75.     if (_Py_PackageContext != NULL) {
  76.         char *p = strrchr(_Py_PackageContext, '.');
  77.         if (p != NULL && strcmp(name, p+1) == 0) {
  78.             name = _Py_PackageContext;
  79.             _Py_PackageContext = NULL;
  80.         }
  81.     }
  82.     if ((m = PyImport_AddModule(name)) == NULL)
  83.         return NULL;
  84.     d = PyModule_GetDict(m);
  85.     for (ml = methods; ml->ml_name != NULL; ml++) {
  86.         v = PyCFunction_New(ml, passthrough);
  87.         if (v == NULL)
  88.             return NULL;
  89.         if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
  90.             return NULL;
  91.         Py_DECREF(v);
  92.     }
  93.     if (doc != NULL) {
  94.         v = PyString_FromString(doc);
  95.         if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
  96.             return NULL;
  97.         Py_DECREF(v);
  98.     }
  99.     return m;
  100. }
  101.  
  102.  
  103. /* Helper for mkvalue() to scan the length of a format */
  104.  
  105. static int countformat Py_PROTO((char *format, int endchar));
  106. static int countformat(format, endchar)
  107.     char *format;
  108.     int endchar;
  109. {
  110.     int count = 0;
  111.     int level = 0;
  112.     while (level > 0 || *format != endchar) {
  113.         switch (*format) {
  114.         case '\0':
  115.             /* Premature end */
  116.             PyErr_SetString(PyExc_SystemError,
  117.                     "unmatched paren in format");
  118.             return -1;
  119.         case '(':
  120.         case '[':
  121.         case '{':
  122.             if (level == 0)
  123.                 count++;
  124.             level++;
  125.             break;
  126.         case ')':
  127.         case ']':
  128.         case '}':
  129.             level--;
  130.             break;
  131.         case '#':
  132.         case '&':
  133.         case ',':
  134.         case ':':
  135.         case ' ':
  136.         case '\t':
  137.             break;
  138.         default:
  139.             if (level == 0)
  140.                 count++;
  141.         }
  142.         format++;
  143.     }
  144.     return count;
  145. }
  146.  
  147.  
  148. /* Generic function to create a value -- the inverse of getargs() */
  149. /* After an original idea and first implementation by Steven Miale */
  150.  
  151. static PyObject *do_mktuple Py_PROTO((char**, va_list *, int, int));
  152. static PyObject *do_mklist Py_PROTO((char**, va_list *, int, int));
  153. static PyObject *do_mkdict Py_PROTO((char**, va_list *, int, int));
  154. static PyObject *do_mkvalue Py_PROTO((char**, va_list *));
  155.  
  156.  
  157. static PyObject *
  158. do_mkdict(p_format, p_va, endchar, n)
  159.     char **p_format;
  160.     va_list *p_va;
  161.     int endchar;
  162.     int n;
  163. {
  164.     PyObject *d;
  165.     int i;
  166.     if (n < 0)
  167.         return NULL;
  168.     if ((d = PyDict_New()) == NULL)
  169.         return NULL;
  170.     for (i = 0; i < n; i+= 2) {
  171.         PyObject *k, *v;
  172.         int err;
  173.         k = do_mkvalue(p_format, p_va);
  174.         if (k == NULL) {
  175.             Py_DECREF(d);
  176.             return NULL;
  177.         }
  178.         v = do_mkvalue(p_format, p_va);
  179.         if (v == NULL) {
  180.             Py_DECREF(k);
  181.             Py_DECREF(d);
  182.             return NULL;
  183.         }
  184.         err = PyDict_SetItem(d, k, v);
  185.         Py_DECREF(k);
  186.         Py_DECREF(v);
  187.         if (err < 0) {
  188.             Py_DECREF(d);
  189.             return NULL;
  190.         }
  191.     }
  192.     if (d != NULL && **p_format != endchar) {
  193.         Py_DECREF(d);
  194.         d = NULL;
  195.         PyErr_SetString(PyExc_SystemError,
  196.                 "Unmatched paren in format");
  197.     }
  198.     else if (endchar)
  199.         ++*p_format;
  200.     return d;
  201. }
  202.  
  203. static PyObject *
  204. do_mklist(p_format, p_va, endchar, n)
  205.     char **p_format;
  206.     va_list *p_va;
  207.     int endchar;
  208.     int n;
  209. {
  210.     PyObject *v;
  211.     int i;
  212.     if (n < 0)
  213.         return NULL;
  214.     if ((v = PyList_New(n)) == NULL)
  215.         return NULL;
  216.     for (i = 0; i < n; i++) {
  217.         PyObject *w = do_mkvalue(p_format, p_va);
  218.         if (w == NULL) {
  219.             Py_DECREF(v);
  220.             return NULL;
  221.         }
  222.         PyList_SetItem(v, i, w);
  223.     }
  224.     if (v != NULL && **p_format != endchar) {
  225.         Py_DECREF(v);
  226.         v = NULL;
  227.         PyErr_SetString(PyExc_SystemError,
  228.                 "Unmatched paren in format");
  229.     }
  230.     else if (endchar)
  231.         ++*p_format;
  232.     return v;
  233. }
  234.  
  235. static PyObject *
  236. do_mktuple(p_format, p_va, endchar, n)
  237.     char **p_format;
  238.     va_list *p_va;
  239.     int endchar;
  240.     int n;
  241. {
  242.     PyObject *v;
  243.     int i;
  244.     if (n < 0)
  245.         return NULL;
  246.     if ((v = PyTuple_New(n)) == NULL)
  247.         return NULL;
  248.     for (i = 0; i < n; i++) {
  249.         PyObject *w = do_mkvalue(p_format, p_va);
  250.         if (w == NULL) {
  251.             Py_DECREF(v);
  252.             return NULL;
  253.         }
  254.         PyTuple_SetItem(v, i, w);
  255.     }
  256.     if (v != NULL && **p_format != endchar) {
  257.         Py_DECREF(v);
  258.         v = NULL;
  259.         PyErr_SetString(PyExc_SystemError,
  260.                 "Unmatched paren in format");
  261.     }
  262.     else if (endchar)
  263.         ++*p_format;
  264.     return v;
  265. }
  266.  
  267. static PyObject *
  268. do_mkvalue(p_format, p_va)
  269.     char **p_format;
  270.     va_list *p_va;
  271. {
  272.     for (;;) {
  273.         switch (*(*p_format)++) {
  274.         case '(':
  275.             return do_mktuple(p_format, p_va, ')',
  276.                       countformat(*p_format, ')'));
  277.  
  278.         case '[':
  279.             return do_mklist(p_format, p_va, ']',
  280.                      countformat(*p_format, ']'));
  281.  
  282.         case '{':
  283.             return do_mkdict(p_format, p_va, '}',
  284.                      countformat(*p_format, '}'));
  285.  
  286.         case 'b':
  287.         case 'h':
  288.         case 'i':
  289.             return PyInt_FromLong((long)va_arg(*p_va, int));
  290.  
  291.         case 'l':
  292.             return PyInt_FromLong((long)va_arg(*p_va, long));
  293.  
  294. #ifdef HAVE_LONG_LONG
  295.         case 'L':
  296.             return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
  297. #endif
  298.  
  299.         case 'f':
  300.         case 'd':
  301.             return PyFloat_FromDouble(
  302.                 (double)va_arg(*p_va, va_double));
  303.  
  304.         case 'c':
  305.         {
  306.             char p[1];
  307.             p[0] = va_arg(*p_va, int);
  308.             return PyString_FromStringAndSize(p, 1);
  309.         }
  310.  
  311.         case 's':
  312.         case 'z':
  313.         {
  314.             PyObject *v;
  315.             char *str = va_arg(*p_va, char *);
  316.             int n;
  317.             if (**p_format == '#') {
  318.                 ++*p_format;
  319.                 n = va_arg(*p_va, int);
  320.             }
  321.             else
  322.                 n = -1;
  323.             if (str == NULL) {
  324.                 v = Py_None;
  325.                 Py_INCREF(v);
  326.             }
  327.             else {
  328.                 if (n < 0)
  329.                     n = strlen(str);
  330.                 v = PyString_FromStringAndSize(str, n);
  331.             }
  332.             return v;
  333.         }
  334.  
  335.         case 'N':
  336.         case 'S':
  337.         case 'O':
  338.         if (**p_format == '&') {
  339.             typedef PyObject *(*converter) Py_PROTO((void *));
  340.             converter func = va_arg(*p_va, converter);
  341.             void *arg = va_arg(*p_va, void *);
  342.             ++*p_format;
  343.             return (*func)(arg);
  344.         }
  345.         else {
  346.             PyObject *v;
  347.             v = va_arg(*p_va, PyObject *);
  348.             if (v != NULL) {
  349.                 if (*(*p_format - 1) != 'N')
  350.                     Py_INCREF(v);
  351.             }
  352.             else if (!PyErr_Occurred())
  353.                 /* If a NULL was passed
  354.                  * because a call that should
  355.                  * have constructed a value
  356.                  * failed, that's OK, and we
  357.                  * pass the error on; but if
  358.                  * no error occurred it's not
  359.                  * clear that the caller knew
  360.                  * what she was doing. */
  361.                 PyErr_SetString(PyExc_SystemError,
  362.                     "NULL object passed to Py_BuildValue");
  363.             return v;
  364.         }
  365.  
  366.         case ':':
  367.         case ',':
  368.         case ' ':
  369.         case '\t':
  370.             break;
  371.  
  372.         default:
  373.             PyErr_SetString(PyExc_SystemError,
  374.                 "bad format char passed to Py_BuildValue");
  375.             return NULL;
  376.  
  377.         }
  378.     }
  379. }
  380.  
  381.  
  382. #ifdef HAVE_STDARG_PROTOTYPES
  383. /* VARARGS 2 */
  384. PyObject *Py_BuildValue(char *format, ...)
  385. #else
  386. /* VARARGS */
  387. PyObject *Py_BuildValue(va_alist) va_dcl
  388. #endif
  389. {
  390.     va_list va;
  391.     PyObject* retval;
  392. #ifdef HAVE_STDARG_PROTOTYPES
  393.     va_start(va, format);
  394. #else
  395.     char *format;
  396.     va_start(va);
  397.     format = va_arg(va, char *);
  398. #endif
  399.     retval = Py_VaBuildValue(format, va);
  400.     va_end(va);
  401.     return retval;
  402. }
  403.  
  404. PyObject *
  405. Py_VaBuildValue(format, va)
  406.     char *format;
  407.     va_list va;
  408. {
  409.     char *f = format;
  410.     int n = countformat(f, '\0');
  411.     va_list lva;
  412.  
  413. #ifdef VA_LIST_IS_ARRAY
  414.     memcpy(lva, va, sizeof(va_list));
  415. #else
  416.     lva = va;
  417. #endif
  418.  
  419.     if (n < 0)
  420.         return NULL;
  421.     if (n == 0) {
  422.         Py_INCREF(Py_None);
  423.         return Py_None;
  424.     }
  425.     if (n == 1)
  426.         return do_mkvalue(&f, &lva);
  427.     return do_mktuple(&f, &lva, '\0', n);
  428. }
  429.  
  430.  
  431. #ifdef HAVE_STDARG_PROTOTYPES
  432. PyObject *
  433. PyEval_CallFunction(PyObject *obj, char *format, ...)
  434. #else
  435. PyObject *
  436. PyEval_CallFunction(obj, format, va_alist)
  437.     PyObject *obj;
  438.     char *format;
  439.     va_dcl
  440. #endif
  441. {
  442.     va_list vargs;
  443.     PyObject *args;
  444.     PyObject *res;
  445.  
  446. #ifdef HAVE_STDARG_PROTOTYPES
  447.     va_start(vargs, format);
  448. #else
  449.     va_start(vargs);
  450. #endif
  451.  
  452.     args = Py_VaBuildValue(format, vargs);
  453.     va_end(vargs);
  454.  
  455.     if (args == NULL)
  456.         return NULL;
  457.  
  458.     res = PyEval_CallObject(obj, args);
  459.     Py_DECREF(args);
  460.  
  461.     return res;
  462. }
  463.  
  464.  
  465. #ifdef HAVE_STDARG_PROTOTYPES
  466. PyObject *
  467. PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
  468. #else
  469. PyObject *
  470. PyEval_CallMethod(obj, methodname, format, va_alist)
  471.     PyObject *obj;
  472.     char *methodname;
  473.     char *format;
  474.     va_dcl
  475. #endif
  476. {
  477.     va_list vargs;
  478.     PyObject *meth;
  479.     PyObject *args;
  480.     PyObject *res;
  481.  
  482.     meth = PyObject_GetAttrString(obj, methodname);
  483.     if (meth == NULL)
  484.         return NULL;
  485.  
  486. #ifdef HAVE_STDARG_PROTOTYPES
  487.     va_start(vargs, format);
  488. #else
  489.     va_start(vargs);
  490. #endif
  491.  
  492.     args = Py_VaBuildValue(format, vargs);
  493.     va_end(vargs);
  494.  
  495.     if (args == NULL) {
  496.         Py_DECREF(meth);
  497.         return NULL;
  498.     }
  499.  
  500.     res = PyEval_CallObject(meth, args);
  501.     Py_DECREF(meth);
  502.     Py_DECREF(args);
  503.  
  504.     return res;
  505. }
  506.