ext/rubypython_bridge/rtop.c in rubypython-0.2.8 vs ext/rubypython_bridge/rtop.c in rubypython-0.2.9
- old
+ new
@@ -1,163 +1,212 @@
#include "rtop.h"
RUBY_EXTERN VALUE cRubyPyObject;
-RUBY_EXTERN PyObject* rp_obj_pobject(VALUE self);
+RUBY_EXTERN PyObject* rpObjectGetPyObject(VALUE self);
-PyObject* rtop_string(VALUE rString)
+/*
+* Note: For the builtin types rubypython creates a copy of the ruby
+* object to pass into python. Builtin types are passed by VALUE not
+* by REFERENCE.
+*/
+
+PyObject* rtopString(VALUE rString)
{
+
PyObject* pString;
- char *cString;
- char *cStringCopy;
- cString=STR2CSTR(rString);
- cStringCopy=malloc(strlen(cString)*sizeof(char));
- strcpy(cStringCopy,cString);
- pString=PyString_FromString(cStringCopy);
+ char* cString;
+ char* cStringCopy;
+
+ cString = STR2CSTR(rString);
+ cStringCopy = malloc(strlen(cString) * sizeof(char));
+ strcpy(cStringCopy, cString);
+
+ pString = PyString_FromString(cStringCopy);
+
return pString;
}
-PyObject* rtop_array_list(VALUE rArray)
+PyObject* rtopArrayToList(VALUE rArray)
{
PyObject* pList;
- int size=RARRAY_LEN(rArray);
- pList=PyList_New(size);
int i;
- for(i=0;i<size;i++)
+ int size = RARRAY_LEN(rArray);
+
+ pList = PyList_New(size);
+
+ for(i = 0; i < size; i++)
{
- PyList_SetItem(pList,i,rtop_obj(rb_ary_entry(rArray,i),0));
+ PyList_SetItem(pList, i, rtopObject(rb_ary_entry(rArray, i), 0));
}
+
return pList;
}
-PyObject* rtop_array_tuple(VALUE rArray)
+PyObject* rtopArrayToTuple(VALUE rArray)
{
PyObject *pTuple,*pList;
- pList=rtop_array_list(rArray);
- pTuple=PySequence_Tuple(pList);
+
+ pList = rtopArrayToList(rArray);
+ pTuple = PySequence_Tuple(pList);
Py_XDECREF(pList);
+
return pTuple;
}
-PyObject* rtop_hash(VALUE rHash)
+PyObject* rtopHash(VALUE rHash)
{
PyObject *pDict,*pKey,*pVal;
- VALUE rKeys=rb_funcall(rHash,rb_intern("keys"),0);
- VALUE rKey,rVal;
+ VALUE rKeys;
+ VALUE rKey, rVal;
int i;
- pDict=PyDict_New();
+ pDict = PyDict_New();
+
+ rKeys = rb_funcall(rHash, rb_intern("keys"), 0);
- for(i=0;i<RARRAY_LEN(rKeys);i++)
+ for(i = 0; i < RARRAY_LEN(rKeys); i++)
{
- rKey=rb_ary_entry(rKeys,i);
- rVal=rb_hash_aref(rHash,rKey);
- PyDict_SetItem(pDict,rtop_obj(rKey,1),rtop_obj(rVal,0));
+ rKey = rb_ary_entry(rKeys, i);
+ rVal = rb_hash_aref(rHash, rKey);
+ PyDict_SetItem(pDict, rtopObject(rKey, 1), rtopObject(rVal, 0));
}
+
return pDict;
}
-PyObject* rtop_fixnum(VALUE rNum)
+PyObject* rtopFixnum(VALUE rNum)
{
PyObject* pNum;
- long cNum=NUM2LONG(rNum);
- pNum=PyInt_FromLong(cNum);
+ long cNum;
+
+ cNum = NUM2LONG(rNum);
+ pNum = PyInt_FromLong(cNum);
+
return pNum;
}
-PyObject* rtop_bignum(VALUE rNum)
+PyObject* rtopBignum(VALUE rNum)
{
PyObject* pNum;
- long cNum=NUM2LONG(rNum);
- pNum=PyLong_FromLong(cNum);
+ long cNum;
+
+ cNum = NUM2LONG(rNum);
+ pNum = PyLong_FromLong(cNum);
+
return pNum;
}
-PyObject* rtop_float(VALUE rNum)
+PyObject* rtopFloat(VALUE rNum)
{
PyObject* pNum;
- double cNum=NUM2DBL(rNum);
- pNum=PyFloat_FromDouble(cNum);
+ double cNum;
+
+ cNum = NUM2DBL(rNum);
+ pNum = PyFloat_FromDouble(cNum);
+
return pNum;
}
-PyObject* rtop_false()
+PyObject* rtopFalse()
{
Py_RETURN_FALSE;
}
-PyObject* rtop_true()
+PyObject* rtopTrue()
{
Py_RETURN_TRUE;
}
-PyObject* rtop_symbol(VALUE rSymbol)
+PyObject* rtopSymbol(VALUE rSymbol)
{
PyObject* pString;
- pString=PyString_FromString(STR2CSTR(rb_funcall(rSymbol,rb_intern("to_s"),0)));
+ char* cStr;
+
+ cStr = STR2CSTR(rb_funcall(rSymbol, rb_intern("to_s"), 0));
+ pString = PyString_FromString(cStr);
+
return pString;
}
-PyObject* rtop_obj(VALUE rObj,int is_key)
+PyObject* rtopObject(VALUE rObj, int is_key)
{
+ // The above is_key parameter determines whether the object
+ // created show be immutable if possible
+
PyObject *pObj;
VALUE rInspect;
+
+ // Check the object for its type and apply the appropriate
+ // conversion function
+
switch(TYPE(rObj))
{
case T_STRING:
- pObj=rtop_string(rObj);
+ pObj = rtopString(rObj);
break;
case T_ARRAY:
- if(is_key) pObj=rtop_array_tuple(rObj);
+ // If this object is going to be used as a
+ // hash key we should make it a tuple instead
+ // of a list
+ if(is_key) pObj = rtopArrayToTuple(rObj);
else
{
- pObj=rtop_array_list(rObj);
+ pObj = rtopArrayToList(rObj);
}
break;
case T_HASH:
- pObj=rtop_hash(rObj);
+ pObj = rtopHash(rObj);
break;
case T_FIXNUM:
- pObj=rtop_fixnum(rObj);
+ pObj = rtopFixnum(rObj);
break;
case T_BIGNUM:
- pObj=rtop_bignum(rObj);
+ pObj = rtopBignum(rObj);
break;
case T_FLOAT:
- pObj=rtop_float(rObj);
+ pObj = rtopFloat(rObj);
break;
case T_NIL:
- pObj=Py_None;
+ pObj = Py_None;
break;
case T_TRUE:
- pObj=rtop_true();
+ pObj = rtopTrue();
break;
case T_FALSE:
- pObj=rtop_false();
+ pObj = rtopFalse();
break;
case T_SYMBOL:
- pObj=rtop_symbol(rObj);
+ pObj = rtopSymbol(rObj);
break;
default:
- if(rb_obj_is_kind_of(rObj,cRubyPyObject)==Qtrue)
+ if(rb_obj_is_kind_of(rObj, cRubyPyObject) == Qtrue)
{
- pObj=rp_obj_pobject(rObj);
+ // rObj is a wrapped python object. We
+ // just take the object it wraps. In
+ // this case we are effectively passing
+ // a python object by reference
+ pObj = rpObjectGetPyObject(rObj);
}
else
{
- rInspect=rb_inspect(rObj);
- pObj=rtop_string(rInspect);
+ // If we can't figure out what else to
+ // do with the ruby object we just pass
+ // a string representation of it
+ rInspect = rb_inspect(rObj);
+ pObj = rtopString(rInspect);
}
}
+
return pObj;
-}
\ No newline at end of file
+}