home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Python / modsupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  9.4 KB  |  486 lines

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