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

  1.  
  2. /* Module new -- create new objects of various types */
  3.  
  4. #include "Python.h"
  5. #include "compile.h"
  6.  
  7. static char new_instance_doc[] =
  8. "Create an instance object from (CLASS, DICT) without calling its __init__().";
  9.  
  10. static PyObject *
  11. new_instance(PyObject* unused, PyObject* args)
  12. {
  13.     PyObject* klass;
  14.     PyObject *dict;
  15.     PyInstanceObject *inst;
  16.     if (!PyArg_ParseTuple(args, "O!O!:instance",
  17.                   &PyClass_Type, &klass,
  18.                   &PyDict_Type, &dict))
  19.         return NULL;
  20.     inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
  21.     if (inst == NULL)
  22.         return NULL;
  23.     Py_INCREF(klass);
  24.     Py_INCREF(dict);
  25.     inst->in_class = (PyClassObject *)klass;
  26.     inst->in_dict = dict;
  27.     PyObject_GC_Init(inst);
  28.     return (PyObject *)inst;
  29. }
  30.  
  31. static char new_im_doc[] =
  32. "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
  33.  
  34. static PyObject *
  35. new_instancemethod(PyObject* unused, PyObject* args)
  36. {
  37.     PyObject* func;
  38.     PyObject* self;
  39.     PyObject* classObj;
  40.  
  41.     if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
  42.                   &func,
  43.                   &self,
  44.                   &PyClass_Type, &classObj))
  45.         return NULL;
  46.     if (!PyCallable_Check(func)) {
  47.         PyErr_SetString(PyExc_TypeError,
  48.                 "first argument must be callable");
  49.         return NULL;
  50.     }
  51.     if (self == Py_None)
  52.         self = NULL;
  53.     else if (!PyInstance_Check(self)) {
  54.         PyErr_SetString(PyExc_TypeError,
  55.                 "second argument must be instance or None");
  56.         return NULL;
  57.     }
  58.     return PyMethod_New(func, self, classObj);
  59. }
  60.  
  61. static char new_function_doc[] =
  62. "Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).";
  63.  
  64. static PyObject *
  65. new_function(PyObject* unused, PyObject* args)
  66. {
  67.     PyObject* code;
  68.     PyObject* globals;
  69.     PyObject* name = Py_None;
  70.     PyObject* defaults = Py_None;
  71.     PyFunctionObject* newfunc;
  72.  
  73.     if (!PyArg_ParseTuple(args, "O!O!|SO!:function",
  74.                   &PyCode_Type, &code,
  75.                   &PyDict_Type, &globals,
  76.                   &name,
  77.                   &PyTuple_Type, &defaults))
  78.         return NULL;
  79.  
  80.     newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
  81.     if (newfunc == NULL)
  82.         return NULL;
  83.  
  84.     if (name != Py_None) {
  85.         Py_XINCREF(name);
  86.         Py_XDECREF(newfunc->func_name);
  87.         newfunc->func_name = name;
  88.     }
  89.     if (defaults != Py_None) {
  90.         Py_XINCREF(defaults);
  91.         Py_XDECREF(newfunc->func_defaults);
  92.         newfunc->func_defaults  = defaults;
  93.     }
  94.  
  95.     return (PyObject *)newfunc;
  96. }
  97.  
  98. static char new_code_doc[] =
  99. "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
  100.  
  101. static PyObject *
  102. new_code(PyObject* unused, PyObject* args)
  103. {
  104.     int argcount;
  105.     int nlocals;
  106.     int stacksize;
  107.     int flags;
  108.     PyObject* code;
  109.     PyObject* consts;
  110.     PyObject* names;
  111.     PyObject* varnames;
  112.     PyObject* filename;
  113.     PyObject* name;
  114.     int firstlineno;
  115.     PyObject* lnotab;
  116.     PyBufferProcs *pb;
  117.  
  118.     if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code",
  119.                   &argcount, &nlocals, &stacksize, &flags,
  120.                   &code,
  121.                   &PyTuple_Type, &consts,
  122.                   &PyTuple_Type, &names,
  123.                   &PyTuple_Type, &varnames,
  124.                   &filename, &name,
  125.                   &firstlineno, &lnotab))
  126.         return NULL;
  127.  
  128.     pb = code->ob_type->tp_as_buffer;
  129.     if (pb == NULL ||
  130.         pb->bf_getreadbuffer == NULL ||
  131.         pb->bf_getsegcount == NULL ||
  132.         (*pb->bf_getsegcount)(code, NULL) != 1)
  133.     {
  134.         PyErr_SetString(PyExc_TypeError,
  135.           "bytecode object must be a single-segment read-only buffer");
  136.         return NULL;
  137.     }
  138.  
  139.     return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
  140.                       code, consts, names, varnames,
  141.                       filename, name, firstlineno, lnotab);
  142. }
  143.  
  144. static char new_module_doc[] =
  145. "Create a module object from (NAME).";
  146.  
  147. static PyObject *
  148. new_module(PyObject* unused, PyObject* args)
  149. {
  150.     char *name;
  151.   
  152.     if (!PyArg_ParseTuple(args, "s:module", &name))
  153.         return NULL;
  154.     return PyModule_New(name);
  155. }
  156.  
  157. static char new_class_doc[] =
  158. "Create a class object from (NAME, BASE_CLASSES, DICT).";
  159.  
  160. static PyObject *
  161. new_class(PyObject* unused, PyObject* args)
  162. {
  163.     PyObject * name;
  164.     PyObject * classes;
  165.     PyObject * dict;
  166.   
  167.     if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
  168.                   &PyDict_Type, &dict))
  169.         return NULL;
  170.     return PyClass_New(classes, dict, name);
  171. }
  172.  
  173. static PyMethodDef new_methods[] = {
  174.     {"instance",        new_instance,        
  175.      METH_VARARGS, new_instance_doc},
  176.     {"instancemethod",    new_instancemethod,    
  177.      METH_VARARGS, new_im_doc},
  178.     {"function",        new_function,        
  179.      METH_VARARGS, new_function_doc},
  180.     {"code",        new_code,        
  181.      METH_VARARGS, new_code_doc},
  182.     {"module",        new_module,        
  183.      METH_VARARGS, new_module_doc},
  184.     {"classobj",        new_class,        
  185.      METH_VARARGS, new_class_doc},
  186.     {NULL,            NULL}        /* sentinel */
  187. };
  188.  
  189. char new_doc[] =
  190. "Functions to create new objects used by the interpreter.\n\
  191. \n\
  192. You need to know a great deal about the interpreter to use this!";
  193.  
  194. DL_EXPORT(void)
  195. initnew(void)
  196. {
  197.     Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
  198.                PYTHON_API_VERSION);
  199. }
  200.