home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Languages / python / PyObjC-0.47-MIHS / pyobjc-0.47-src / Modules / OC_PythonObject.m < prev    next >
Encoding:
Text File  |  1996-11-13  |  17.9 KB  |  896 lines

  1. /* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
  2.  *
  3.  * This software may be used and distributed freely for any purpose
  4.  * provided that this notice is included unchanged on any and all
  5.  * copies. The author does not warrant or guarantee this software in
  6.  * any way.
  7.  *
  8.  * This file is part of the PyObjC package.
  9.  *
  10.  * $RCSfile: OC_PythonObject.m,v $
  11.  * $Revision: 1.1.1.4 $
  12.  * $Date: 1996/11/14 01:50:15 $
  13.  *
  14.  * Created Wed Sep  4 19:57:44 1996.
  15.  */
  16.  
  17. #include "objc_support.h"
  18. #include "abstract.h"
  19. #include "compile.h"
  20. #include <stdarg.h>
  21.  
  22. #define CLASS_VERSION 0
  23.  
  24. #ifndef MAX
  25. #define MAX(x,y) ({ unsigned int __x=(x), __y=(y); (__x > __y ? __x : __y); })
  26. #define MIN(x,y) ({ unsigned int __x=(x), __y=(y); (__x < __y ? __x : __y); })
  27. #endif
  28.  
  29. @implementation OC_PythonObject
  30.  
  31. + (void) initialize
  32. {
  33.   if (self == [OC_PythonObject class])
  34.     {
  35.       [OC_PythonObject setVersion:CLASS_VERSION];
  36.     }
  37. }
  38.  
  39. + newWithObject:(PyObject *) obj
  40. {
  41.   if (ObjCObject_Check (obj))
  42.     return ((ObjCObject *) obj)->oc_object;
  43.   else
  44.     {
  45.       id instance = [[self alloc] initWithObject:obj];
  46.       
  47. #ifdef WITH_FOUNDATION
  48.       [instance autorelease];
  49. #endif
  50.  
  51.       return instance;
  52.     }
  53. }
  54.  
  55. - initWithObject:(PyObject *) obj
  56. {
  57.   [super init];
  58.  
  59. #ifndef WITH_FOUNDATION
  60.   Py_INCREF (obj);
  61. #endif
  62.   
  63.   pyObject = obj;
  64.  
  65.   return self;
  66. }
  67.  
  68. #ifdef WITH_FOUNDATION
  69.  
  70. - (void) retain
  71. {
  72.   [super retain];
  73.   Py_INCREF (pyObject);
  74. }
  75.  
  76. - (oneway void) release
  77. {
  78.   [super release];
  79.   Py_DECREF (pyObject);
  80. }
  81.  
  82. #else
  83.  
  84. - copyFromZone:(NXZone *) zone
  85. {
  86.   id copy;
  87.  
  88.   Py_INCREF (pyObject);
  89.   copy = [super copyFromZone:zone];
  90.   
  91.   return copy;
  92. }
  93.  
  94. - free
  95. {
  96.   Py_DECREF (pyObject);
  97.  
  98.   return [super free];
  99. }
  100.  
  101. #endif
  102.  
  103. #define RETURN_RESULT(result) do {                    \
  104.   if (result)                                \
  105.     {                                    \
  106.       OC_PythonObject *ret = [[self class] newWithObject:result];    \
  107.                                           \
  108.       Py_DECREF (result);                        \
  109.       return ret;                            \
  110.     }                                    \
  111.   else                                    \
  112.     return nil;                                     \
  113. } while(0)
  114.  
  115. - doesNotRecognize:(SEL) aSelector
  116. {
  117. #define ERRORMSG " does not recognize -"
  118.   const char *whoiam = NAMEOF(self);
  119.   const char *selname = SELNAME(aSelector);
  120.   char buffer[strlen (whoiam) + sizeof ERRORMSG + strlen (selname) + 1];
  121.  
  122.   strcpy (buffer, whoiam);
  123.   strcat (buffer, ERRORMSG);
  124.   strcat (buffer, selname);
  125.   PyErr_SetString (ObjC_Error, buffer);
  126.   return nil;
  127. }
  128.  
  129. /*#F Check the argument count of the method/function @var{pymethod},
  130.   returning the method itself if it matches @var{argcount}, NULL
  131.   otherwise. */
  132. static inline PyObject *
  133. check_argcount (PyObject *pymethod, unsigned int argcount)
  134. {
  135.   if (pymethod && (PyFunction_Check (pymethod) || PyMethod_Check (pymethod)))
  136.     {
  137.       PyCodeObject *func_code;
  138.       
  139.       if (PyMethod_Check (pymethod))
  140.     {
  141.       func_code = (PyCodeObject *) PyFunction_GetCode (PyMethod_Function (pymethod));
  142.       argcount++;        // for `self'
  143.     }
  144.       else
  145.     func_code = (PyCodeObject *) PyFunction_GetCode (pymethod);
  146.       
  147.       if (func_code->co_argcount == argcount)
  148.     return pymethod;
  149.     }
  150.  
  151.   return NULL;
  152. }
  153.  
  154. /*#F If the Python object @var{obj} implements a method whose name matches
  155.   the Objective-C selector @var{aSelector} and accepts the correct number
  156.   of arguments, return that method, otherwise NULL. */
  157. static PyObject *
  158. get_method_for_selector (PyObject *obj, SEL aSelector)
  159. {
  160.   if (aSelector)
  161.     {
  162.       const char *meth_name = SELNAME(aSelector);
  163.       int len = strlen (meth_name);
  164.       char *pymeth_name;
  165.       unsigned int argcount;
  166.       PyObject *pymethod;
  167.       register const char *p;
  168.       
  169.       for (argcount=0, p=meth_name; *p; p++)
  170.     if (*p == ':')
  171.       argcount++;
  172.   
  173.       pymeth_name = alloca (PYTHONIFIED_LENGTH(meth_name, len, argcount));
  174. #if PYTHONIFICATION_METHOD != WITH_BOTH
  175.       pythonify_objc_message (meth_name, pymeth_name);
  176.  
  177.       pymethod = PyObject_GetAttrString (obj, pymeth_name);
  178.       return check_argcount (pymethod, argcount);
  179. #else
  180.       {
  181.     PyObject *meth;
  182.  
  183.     pythonify_objc_message (meth_name, pymeth_name, PYTHONIFICATION_FIRST_TRY);
  184.     pymethod = PyObject_GetAttrString (obj, pymeth_name);
  185.     meth = check_argcount (pymethod, argcount);
  186.     if (meth)
  187.       return meth;
  188.  
  189. #if PYTHONIFICATION_FIRST_TRY == WITH_DOUBLE_UNDERSCORE
  190.     pythonify_objc_message (meth_name, pymeth_name, WITH_SINGLE_UNDERSCORE);
  191. #else
  192.     pythonify_objc_message (meth_name, pymeth_name, WITH_DOUBLE_UNDERSCORE);
  193. #endif
  194.     pymethod = PyObject_GetAttrString (obj, pymeth_name);
  195.     meth = check_argcount (pymethod, argcount);
  196.     if (meth)
  197.       {
  198.         if (Py_VerboseFlag)
  199.           {
  200.         char faster_name[200];
  201.         
  202.         fprintf (stderr, "PyObjC Warning: method `%s' matches `%s',\n\t", pymeth_name, meth_name);
  203.         
  204.         pythonify_objc_message (meth_name, faster_name, PYTHONIFICATION_FIRST_TRY);
  205.         
  206.         fprintf (stderr, "but `%s' would be faster.\n", faster_name);
  207.           }
  208.  
  209.         return meth;
  210.       }
  211.       }
  212. #endif /* PYTHONIFICATION_METHOD != WITH_BOTH */
  213.     }
  214.   return NULL;    
  215. }
  216.  
  217. #ifdef WITH_FOUNDATION
  218. - (BOOL) respondsToSelector:(SEL) aSelector
  219. #else
  220. - (BOOL) respondsTo:(SEL) aSelector
  221. #endif
  222. {
  223. #ifdef WITH_FOUNDATION
  224.   if ([super respondsToSelector:aSelector])
  225. #else
  226.   if ([super respondsTo:aSelector])
  227. #endif
  228.     return YES;
  229.   else
  230.     {
  231.       PyObject *m = get_method_for_selector ([self object], aSelector);
  232.  
  233.       if (m)
  234.     return YES;
  235.       else
  236.     {
  237.       PyErr_Clear();
  238.       return NO;
  239.     }
  240.     }
  241. }
  242.  
  243. - forward:(SEL) aSelector :(marg_list) argFrame
  244. {
  245.   PyObject *pymethod = get_method_for_selector ([self object], aSelector);
  246.  
  247.   if (pymethod)
  248.     {
  249.       PyObject *args = NULL;
  250.       unsigned int i, argcount;      
  251.       PyCodeObject *func_code;
  252.       
  253.       if (PyMethod_Check (pymethod))
  254.     {
  255.       func_code = (PyCodeObject *) PyFunction_GetCode (PyMethod_Function (pymethod));
  256.       argcount = func_code->co_argcount-1; // adjust for `self'
  257.     }
  258.       else
  259.     {
  260.       func_code = (PyCodeObject *) PyFunction_GetCode (pymethod);
  261.       argcount = func_code->co_argcount;
  262.     }
  263.       
  264.       args = PyTuple_New (argcount);
  265.       if (args)
  266.     {
  267.       unsigned int offset = sizeof (id *) + sizeof (SEL *);
  268.       PyObject *result;
  269.       
  270.       for (i=0; i<argcount; i++)
  271.         {
  272. #ifdef GNU_RUNTIME
  273.           /* XXX is this right? What about arguments in register? */
  274.           id argument = *(id *) (argFrame->arg_ptr + offset + i*sizeof (id *));
  275. #else
  276.           id argument = marg_getValue (argFrame, offset + i*sizeof (id *), id);
  277. #endif
  278.           if ([argument isKindOf:[OC_PythonObject class]])
  279.         {
  280.           PyObject *pyarg = [argument object];
  281.           
  282.           Py_INCREF(pyarg);
  283.           PyTuple_SET_ITEM (args, i, pyarg);
  284.         }
  285.           else
  286.         {
  287.           ObjCObject *pyarg = ObjCObject_new (argument);
  288.           
  289.           PyTuple_SET_ITEM (args, i, (PyObject *) pyarg);
  290.         }
  291.         }
  292.  
  293.       result = PyObject_CallObject (pymethod, args);
  294.   
  295.       Py_DECREF(args);
  296.  
  297.       RETURN_RESULT(result);
  298.     }
  299.     }
  300.  
  301.   return [self doesNotRecognize:aSelector];
  302. }    
  303.  
  304.  
  305. // NUMBER PROTOCOL
  306.  
  307. - (int) numberCheck
  308. {
  309.   return PyNumber_Check ([self object]);
  310. }
  311.  
  312. - (id <PythonObject>) numberAdd:(id <PythonObject>) o2
  313. {
  314.   PyObject *pyo1 = [self object];
  315.   PyObject *pyo2 = [o2 object];
  316.   PyObject *result = PyNumber_Add (pyo1, pyo2);
  317.  
  318.   RETURN_RESULT(result);
  319. }
  320.  
  321. - (id <PythonObject>) numberSubtract:(id <PythonObject>) o2
  322. {
  323.   PyObject *pyo1 = [self object];
  324.   PyObject *pyo2 = [o2 object];
  325.   PyObject *result = PyNumber_Subtract (pyo1, pyo2);
  326.  
  327.   RETURN_RESULT(result);
  328. }
  329.  
  330. - (id <PythonObject>) numberMultiply:(id <PythonObject>) o2
  331. {
  332.   PyObject *pyo1 = [self object];
  333.   PyObject *pyo2 = [o2 object];
  334.   PyObject *result = PyNumber_Multiply (pyo1, pyo2);
  335.  
  336.   RETURN_RESULT(result);
  337. }
  338.  
  339. - (id <PythonObject>) numberDivide:(id <PythonObject>) o2
  340. {
  341.   PyObject *pyo1 = [self object];
  342.   PyObject *pyo2 = [o2 object];
  343.   PyObject *result = PyNumber_Divide (pyo1, pyo2);
  344.  
  345.   RETURN_RESULT(result);
  346. }
  347.  
  348. - (id <PythonObject>) numberRemainder:(id <PythonObject>) o2
  349. {
  350.   PyObject *pyo1 = [self object];
  351.   PyObject *pyo2 = [o2 object];
  352.   PyObject *result = PyNumber_Remainder (pyo1, pyo2);
  353.  
  354.   RETURN_RESULT(result);
  355. }
  356.  
  357. - (id <PythonObject>) numberDivmod:(id <PythonObject>) o2
  358. {
  359.   PyObject *pyo1 = [self object];
  360.   PyObject *pyo2 = [o2 object];
  361.   PyObject *result = PyNumber_Divmod (pyo1, pyo2);
  362.  
  363.   RETURN_RESULT(result);
  364. }
  365.  
  366. - (id <PythonObject>) numberPower:(id <PythonObject>) o2
  367.                  :(id <PythonObject>) o3
  368. {
  369.   PyObject *pyo1 = [self object];
  370.   PyObject *pyo2 = [o2 object];
  371.   PyObject *pyo3 = [o3 object];
  372.   PyObject *result = PyNumber_Power (pyo1, pyo2, pyo3);
  373.  
  374.   RETURN_RESULT(result);
  375. }
  376.  
  377. - (id <PythonObject>) numberNegative
  378. {
  379.   PyObject *pyo = [self object];
  380.   PyObject *result = PyNumber_Negative (pyo);
  381.  
  382.   RETURN_RESULT(result);
  383. }
  384.  
  385. - (id <PythonObject>) numberPositive
  386. {
  387.   PyObject *pyo = [self object];
  388.   PyObject *result = PyNumber_Positive (pyo);
  389.  
  390.   RETURN_RESULT(result);
  391. }
  392.  
  393. - (id <PythonObject>) numberAbsolute
  394. {
  395.   PyObject *pyo = [self object];
  396.   PyObject *result = PyNumber_Absolute (pyo);
  397.  
  398.   RETURN_RESULT(result);
  399. }
  400.  
  401. - (id <PythonObject>) numberInvert
  402. {
  403.   PyObject *pyo = [self object];
  404.   PyObject *result = PyNumber_Invert (pyo);
  405.  
  406.   RETURN_RESULT(result);
  407. }
  408.  
  409. - (id <PythonObject>) numberLshift:(id <PythonObject>) o2
  410. {
  411.   PyObject *pyo1 = [self object];
  412.   PyObject *pyo2 = [o2 object];
  413.   PyObject *result = PyNumber_Lshift (pyo1, pyo2);
  414.  
  415.   RETURN_RESULT(result);
  416. }
  417.  
  418. - (id <PythonObject>) numberRshift:(id <PythonObject>) o2
  419. {
  420.   PyObject *pyo1 = [self object];
  421.   PyObject *pyo2 = [o2 object];
  422.   PyObject *result = PyNumber_Rshift (pyo1, pyo2);
  423.  
  424.   RETURN_RESULT(result);
  425. }
  426.  
  427. - (id <PythonObject>) numberAnd:(id <PythonObject>) o2
  428. {
  429.   PyObject *pyo1 = [self object];
  430.   PyObject *pyo2 = [o2 object];
  431.   PyObject *result = PyNumber_And (pyo1, pyo2);
  432.  
  433.   RETURN_RESULT(result);
  434. }
  435.  
  436. - (id <PythonObject>) numberXor:(id <PythonObject>) o2
  437. {
  438.   PyObject *pyo1 = [self object];
  439.   PyObject *pyo2 = [o2 object];
  440.   PyObject *result = PyNumber_Xor (pyo1, pyo2);
  441.  
  442.   RETURN_RESULT(result);
  443. }
  444.  
  445. - (id <PythonObject>) numberOr:(id <PythonObject>) o2
  446. {
  447.   PyObject *pyo1 = [self object];
  448.   PyObject *pyo2 = [o2 object];
  449.   PyObject *result = PyNumber_Or (pyo1, pyo2);
  450.  
  451.   RETURN_RESULT(result);
  452. }
  453.  
  454. - (id <PythonObject>) numberInt
  455. {
  456.   PyObject *pyo = [self object];
  457.   PyObject *result = PyNumber_Int (pyo);
  458.  
  459.   RETURN_RESULT(result);
  460. }
  461.  
  462. - (id <PythonObject>) numberLong
  463. {
  464.   PyObject *pyo = [self object];
  465.   PyObject *result = PyNumber_Long (pyo);
  466.  
  467.   RETURN_RESULT(result);
  468. }
  469.  
  470. - (id <PythonObject>) numberFloat
  471. {
  472.   PyObject *pyo = [self object];
  473.   PyObject *result = PyNumber_Float (pyo);
  474.  
  475.   RETURN_RESULT(result);
  476. }
  477.  
  478. // SEQUENCE PROTOCOL
  479.  
  480. - (int) sequenceCheck
  481. {
  482.   return PySequence_Check ([self object]);
  483. }
  484.  
  485. - (int) sequenceLength
  486. {
  487.   return PySequence_Length ([self object]);
  488. }
  489.  
  490. - (id <PythonObject>) sequenceConcat:(id <PythonObject>) o2
  491. {
  492.   PyObject *pyo1 = [self object];
  493.   PyObject *pyo2 = [o2 object];
  494.   PyObject *result = PySequence_Concat (pyo1, pyo2);
  495.  
  496.   RETURN_RESULT(result);
  497. }
  498.  
  499. - (id <PythonObject>) sequenceRepeat:(int) count
  500. {
  501.   PyObject *pyo = [self object];
  502.   PyObject *result = PySequence_Repeat (pyo, count);
  503.  
  504.   RETURN_RESULT(result);
  505. }
  506.  
  507. - (id <PythonObject>) sequenceGetItem:(int) count
  508. {
  509.   PyObject *pyo = [self object];
  510.   PyObject *result = PySequence_GetItem (pyo, count);
  511.  
  512.   RETURN_RESULT(result);
  513. }
  514.   
  515. - (id <PythonObject>) sequenceGetSliceFrom:(int) i1 to:(int) i2
  516. {
  517.   PyObject *pyo = [self object];
  518.   PyObject *result = PySequence_GetSlice (pyo, i1, i2);
  519.  
  520.   RETURN_RESULT(result);
  521. }
  522.  
  523. - (int) sequenceSetItem:(int) i value:(id <PythonObject>) v
  524. {
  525.   
  526.   PyObject *pyo = [self object];
  527.   PyObject *pyv = [v object];
  528.  
  529.   return PySequence_SetItem (pyo, i, pyv);
  530. }
  531.  
  532. - (int) sequenceDelItem:(int) i
  533. {
  534.   
  535.   PyObject *pyo = [self object];
  536.  
  537.   return PySequence_DelItem (pyo, i);
  538. }
  539.  
  540. - (int) sequenceSetSliceFrom:(int) i1
  541.               to:(int) i2
  542.                value:(id <PythonObject>) v
  543. {
  544.   PyObject *pyo = [self object];
  545.   PyObject *pyv = [v object];
  546.  
  547.   return PySequence_SetSlice (pyo, i1, i2, pyv);
  548. }
  549.  
  550. - (int) sequenceDelSliceFrom:(int) i1
  551.               to:(int) i2
  552. {
  553.   PyObject *pyo = [self object];
  554.  
  555.   return PySequence_DelSlice (pyo, i1, i2);
  556. }
  557.  
  558. - (id <PythonObject>) sequenceTuple
  559. {
  560.   PyObject *pyo = [self object];
  561.   PyObject *result = PySequence_Tuple (pyo);
  562.  
  563.   RETURN_RESULT(result);
  564. }
  565.  
  566. - (int) sequenceCount:(id <PythonObject>) value
  567. {
  568.   PyObject *pyo = [self object];
  569.   PyObject *pyv = [value object];
  570.  
  571.   return PySequence_Count (pyo, pyv);
  572. }
  573.  
  574. - (int) sequenceIn:(id <PythonObject>) value
  575. {
  576.   PyObject *pyo = [self object];
  577.   PyObject *pyv = [value object];
  578.  
  579.   return PySequence_In (pyo, pyv);
  580. }
  581.  
  582. - (int) sequenceIndex:(id <PythonObject>) value
  583. {
  584.   PyObject *pyo = [self object];
  585.   PyObject *pyv = [value object];
  586.  
  587.   return PySequence_Index (pyo, pyv);
  588. }
  589.  
  590. // CALLING PROTOCOL
  591.  
  592. - (int) callableCheck
  593. {
  594.   return PyCallable_Check ([self object]);
  595. }
  596.  
  597. - (id <PythonObject>) callableCall:(id <PythonObject>) args
  598. {
  599.   PyObject *pyo = [self object];
  600.   PyObject *pyargs = [args object];
  601.   PyObject *result = PyObject_CallObject (pyo, pyargs);
  602.  
  603.   RETURN_RESULT(result);
  604. }
  605.  
  606. - (id <PythonObject>) callableCallFunction:(char *) format, ...
  607. {
  608.   va_list va;
  609.   PyObject *args;
  610.   PyObject *result;
  611.  
  612.   va_start (va, format);
  613.  
  614.   if (format)
  615.     args = Py_VaBuildValue (format, va);
  616.   else
  617.     args = PyTuple_New (0);
  618.   
  619.   va_end(va);
  620.   
  621.   if (! args)
  622.     return nil;
  623.  
  624.   if(! PyTuple_Check (args))
  625.     {
  626.       PyObject *a = PyTuple_New (1);
  627.  
  628.       if (!a)
  629.     return nil;
  630.       PyTuple_SET_ITEM (a, 0, args);
  631.  
  632.       args = a;
  633.     }
  634.  
  635.   result = PyObject_CallObject ([self object], args);
  636.   
  637.   Py_DECREF(args);
  638.   
  639.   RETURN_RESULT(result);
  640. }
  641.  
  642. - (id <PythonObject>) callableCallMethod:(char *) m
  643.                 withArgs:(char *) format, ...
  644. {
  645.   va_list va;
  646.   PyObject *args;
  647.   PyObject *method;
  648.   PyObject *result;
  649.  
  650.   method = PyObject_GetAttrString ([self object], m);
  651.   if (! method)
  652.     {
  653.       PyErr_SetString (PyExc_AttributeError, m);
  654.       return nil;
  655.     }
  656.   else if (! PyCallable_Check (method))
  657.     {
  658.       PyErr_SetString(PyExc_TypeError,"call of non-callable attribute");
  659.       return nil;
  660.     }
  661.   
  662.   va_start (va, format);
  663.  
  664.   if (format)
  665.     args = Py_VaBuildValue (format, va);
  666.   else
  667.     args = PyTuple_New (0);
  668.   
  669.   va_end(va);
  670.   
  671.   if (! args)
  672.     return nil;
  673.  
  674.   if(! PyTuple_Check (args))
  675.     {
  676.       PyObject *a = PyTuple_New (1);
  677.  
  678.       if (!a)
  679.     return nil;
  680.       PyTuple_SET_ITEM (a, 0, args);
  681.  
  682.       args = a;
  683.     }
  684.  
  685.   result = PyObject_CallObject (method, args);
  686.   
  687.   Py_DECREF(args);
  688.   
  689.   RETURN_RESULT(result);
  690. }
  691.  
  692.  
  693. // MAPPING PROTOCOL
  694.  
  695. - (int) mappingCheck
  696. {
  697.   return PyMapping_Check ([self object]);
  698. }
  699.  
  700. - (int) mappingLength
  701. {
  702.   return PyMapping_Length ([self object]);
  703. }
  704.  
  705. - (int) mappingDelItemString:(char *) key
  706. {
  707. #ifndef PyMapping_DelItemString
  708.   return PyDict_DelItemString ([self object], key);
  709. #else
  710.   return PyMapping_DelItemString ([self object], key);
  711. #endif
  712. }
  713.  
  714. - (int) mappingDelItem:(id <PythonObject>) key
  715. {
  716. #ifndef PyMapping_DelItem
  717.   return PyDict_DelItem ([self object], [key object]);
  718. #else
  719.   return PyMapping_DelItem ([self object], [key object]);
  720. #endif
  721. }
  722.  
  723. - (int) mappingHasKeyString:(char *) key
  724. {
  725.   return PyMapping_HasKeyString ([self object], key);
  726. }
  727.  
  728. - (int) mappingHasKey:(id <PythonObject>) key
  729. {
  730.   return PyMapping_HasKey ([self object], [key object]);
  731. }
  732.  
  733. - (id <PythonObject>) mappingKeys
  734. {
  735.   PyObject *pykeys = PyMapping_Keys ([self object]);
  736.  
  737.   RETURN_RESULT(pykeys);
  738. }
  739.  
  740. - (id <PythonObject>) mappingItems
  741. {
  742.   PyObject *pyitems = PyMapping_Items ([self object]);
  743.  
  744.   RETURN_RESULT(pyitems);
  745. }
  746.  
  747. - (id <PythonObject>) mappingValues
  748. {
  749.   PyObject *pyvalues = PyMapping_Values ([self object]);
  750.  
  751.   RETURN_RESULT(pyvalues);
  752. }
  753.  
  754. - (id <PythonObject>) mappingGetItemString:(char *) key
  755. {
  756.   PyObject *result = PyMapping_GetItemString ([self object], key);
  757.  
  758.   RETURN_RESULT(result);
  759. }
  760.  
  761. - (int) mappingSetItemString:(char *) key value:(id <PythonObject>) value
  762. {
  763.   return PyMapping_SetItemString ([self object], key, [value object]);
  764. }
  765.  
  766.  
  767. // PYTHON OBJECT PROTOCOL
  768.  
  769. - (PyObject *) object
  770. {
  771.   return pyObject;
  772. }
  773.  
  774. - (int) print:(FILE *) fp flags:(int) flags
  775. {
  776.   return PyObject_Print ([self object], fp, flags);
  777. }
  778.  
  779. - (int) hasAttrString:(char *) attr_name
  780. {
  781.   return PyObject_HasAttrString ([self object], attr_name);
  782. }
  783.  
  784. - (id <PythonObject>) getAttrString:(char *) attr_name
  785. {
  786.   PyObject *result = PyObject_GetAttrString ([self object], attr_name);
  787.  
  788.   RETURN_RESULT(result);
  789. }
  790.  
  791. - (int) hasAttr:(id <PythonObject>) attr_name
  792. {
  793.   [self notImplemented:_cmd];
  794.   return 0;
  795.   // XXX this is not yet implemented as of 1.4b3
  796.   // return PyObject_HasAttr ([self object], [attr_name object]);
  797. }
  798.  
  799. - (id <PythonObject>) getAttr:(id <PythonObject>) attr_name
  800. {
  801.   PyObject *result = PyObject_GetAttr ([self object], [attr_name object]);
  802.  
  803.   RETURN_RESULT(result);
  804. }
  805.  
  806. - (int) setAttrString:(char *) attr_name value:(id <PythonObject>) value
  807. {
  808.   return PyObject_SetAttrString ([self object], attr_name, [value object]);
  809. }
  810.  
  811. - (int) setAttr:(id <PythonObject>) attr_name value:(id <PythonObject>) value
  812. {
  813.   return PyObject_SetAttr ([self object], [attr_name object], [value object]);
  814. }
  815.  
  816. - (int) delAttrString:(char *) attr_name
  817. {
  818.   return PyObject_DelAttrString ([self object], attr_name);
  819. }
  820.  
  821. - (int) delAttr:(id <PythonObject>) attr_name
  822. {
  823.   return PyObject_DelAttr ([self object], [attr_name object]);
  824. }
  825.  
  826. - (int) cmp:(id <PythonObject>) o2 result:(int *) result
  827. {
  828.   return PyObject_Cmp ([self object], [o2 object], result);
  829. }
  830.  
  831. - (int) compare:(id <PythonObject>) o2
  832. {
  833.   return PyObject_Compare ([self object], [o2 object]);
  834. }
  835.  
  836. - (id <PythonObject>) repr
  837. {
  838.   RETURN_RESULT(PyObject_Repr ([self object]));
  839. }
  840.  
  841. - (id <PythonObject>) str
  842. {
  843.   RETURN_RESULT(PyObject_Str ([self object]));
  844. }
  845.  
  846. - (long) hash
  847. {
  848.   return PyObject_Hash ([self object]);
  849. }
  850.  
  851. - (int) isTrue
  852. {
  853.   return PyObject_IsTrue ([self object]);
  854. }
  855.  
  856. - (id <PythonObject>) type
  857. {
  858.   RETURN_RESULT(PyObject_Type ([self object]));
  859. }
  860.  
  861. - (int) length
  862. {
  863.   return PyObject_Length ([self object]);
  864. }
  865.  
  866. - (int) count
  867. {
  868.   return PyObject_Length ([self object]);
  869. }
  870.  
  871. - (id <PythonObject>) getItem:(id <PythonObject>) key
  872. {
  873.   RETURN_RESULT(PyObject_GetItem ([self object], [key object]));
  874. }
  875.  
  876. - (int) setItem:(id <PythonObject>) key value:(id <PythonObject>) v
  877. {
  878.   return PyObject_SetItem ([self object],
  879.                [key object],
  880.                [v object]);
  881. }
  882.  
  883. - (int) delItem:(id <PythonObject>) key
  884. {
  885.   return PyObject_DelItem ([self object], [key object]);
  886. }
  887.  
  888.  
  889. @end /* OC_PythonObject class implementation */
  890.  
  891. /*
  892. ** Local Variables:
  893. ** change-log-default-name:"../ChangeLog.PyObjC"
  894. ** End:
  895. */
  896.