rubypython 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/History.txt +4 -1
  2. data/Manifest.txt +16 -1
  3. data/PostInstall.txt +1 -1
  4. data/README.txt +7 -2
  5. data/config/hoe.rb +12 -12
  6. data/ext/rubypython_bridge/cbridge.c +92 -90
  7. data/ext/rubypython_bridge/cbridge.h +6 -16
  8. data/ext/rubypython_bridge/ptor.c +110 -56
  9. data/ext/rubypython_bridge/ptor.h +9 -25
  10. data/ext/rubypython_bridge/rp_blankobject.c +42 -0
  11. data/ext/rubypython_bridge/rp_blankobject.h +10 -0
  12. data/ext/rubypython_bridge/rp_class.c +56 -0
  13. data/ext/rubypython_bridge/rp_class.h +6 -0
  14. data/ext/rubypython_bridge/rp_error.c +14 -4
  15. data/ext/rubypython_bridge/rp_error.h +3 -1
  16. data/ext/rubypython_bridge/rp_function.c +31 -0
  17. data/ext/rubypython_bridge/rp_function.h +6 -0
  18. data/ext/rubypython_bridge/rp_instance.c +165 -0
  19. data/ext/rubypython_bridge/rp_instance.h +6 -0
  20. data/ext/rubypython_bridge/rp_module.c +159 -0
  21. data/ext/rubypython_bridge/rp_module.h +7 -0
  22. data/ext/rubypython_bridge/rp_object.c +93 -427
  23. data/ext/rubypython_bridge/rp_object.h +8 -54
  24. data/ext/rubypython_bridge/rp_util.c +61 -0
  25. data/ext/rubypython_bridge/rp_util.h +11 -0
  26. data/ext/rubypython_bridge/rtop.c +103 -54
  27. data/ext/rubypython_bridge/rtop.h +11 -16
  28. data/ext/rubypython_bridge/rubypython_bridge.c +48 -20
  29. data/ext/rubypython_bridge/rubypython_bridge.h +5 -6
  30. data/lib/rubypython.rb +2 -2
  31. data/lib/rubypython/session.rb +4 -0
  32. data/lib/rubypython/version.rb +1 -1
  33. data/test/python_helpers/objects.py +12 -0
  34. data/test/python_helpers/objects.pyc +0 -0
  35. data/test/test_rubypython.rb +123 -19
  36. data/test/test_rubypython_bridge_extn.rb +52 -19
  37. data/test/test_session.rb +1 -1
  38. data/website/index.html +25 -9
  39. data/website/index.txt +26 -3
  40. metadata +20 -5
  41. data/.autotest +0 -9
@@ -2,67 +2,21 @@
2
2
  #include "ptor.h"
3
3
  #include "rtop.h"
4
4
  #include "cbridge.h"
5
+ #include "rp_error.h"
5
6
 
6
7
  #ifndef _RP_OBJECT_H_
7
8
  #define _RP_OBJECT_H_
8
9
 
9
- struct RubyPyObj
10
+ typedef struct RubyPyObj
10
11
  {
11
12
  PyObject* pObject;
12
- };
13
+ } PObj;
13
14
 
14
- typedef struct RubyPyObj PObj;
15
+ PyObject* rpObjectGetPyObject(VALUE);
15
16
 
16
- void rp_obj_mark(PObj* self);
17
+ VALUE rpObjectFromPyObject(PyObject*);
17
18
 
18
- void rp_obj_free(PObj* self);
19
+ int rpHasSymbol(VALUE, ID);
19
20
 
20
- VALUE rp_obj_alloc(VALUE klass);
21
-
22
- PyObject* rp_obj_pobject(VALUE self);
23
-
24
- VALUE rp_obj_name(VALUE self);
25
-
26
-
27
- //Ruby wrapper for Python Modules
28
- VALUE rp_mod_init(VALUE self,VALUE mname);
29
-
30
- int rp_has_attr(VALUE self,ID func_hame);
31
-
32
- VALUE rp_mod_call_func(VALUE self,VALUE func_name,VALUE args);
33
-
34
- VALUE rp_mod_delegate(VALUE self,VALUE args);
35
-
36
- //Ruby wrapper for Python classes
37
-
38
- VALUE rp_cla_from_class(PyObject *pClass);
39
-
40
- VALUE rp_func_from_function(PyObject *pFunc);
41
-
42
- int rp_is_func(VALUE pObj);
43
-
44
- VALUE rp_obj_from_pyobject(PyObject *pObj);
45
-
46
- VALUE rp_inst_from_instance(PyObject *pInst);
47
-
48
- VALUE rp_inst_delegate(VALUE self,VALUE args);
49
-
50
- VALUE rp_cla_new_inst(VALUE self,VALUE args);
51
-
52
- VALUE rp_obj_responds(VALUE self,VALUE mname);
53
-
54
- VALUE blank_undef_if(VALUE mname,VALUE klass);
55
-
56
- VALUE blank_obj_prep(VALUE self);
57
-
58
- int rp_equal(VALUE args);
59
-
60
- int rp_double_bang(VALUE args);
61
-
62
- VALUE rp_mod_attr_set(VALUE self,VALUE args);
63
-
64
- VALUE rp_inst_attr_set(VALUE self, VALUE args);
65
-
66
- VALUE rp_obj_wrap(PyObject* pObj);
67
-
68
- #endif /* _RP_OBJECT_H_ */
21
+ VALUE rpRespondsTo(VALUE, VALUE);
22
+ #endif
@@ -0,0 +1,61 @@
1
+ #include "rp_util.h"
2
+
3
+ #include "rp_object.h"
4
+ #include "rp_function.h"
5
+
6
+ RUBY_EXTERN VALUE mRubyPythonBridge;
7
+ RUBY_EXTERN VALUE ePythonError;
8
+ RUBY_EXTERN VALUE cRubyPyObject;
9
+ RUBY_EXTERN VALUE cBlankObject;
10
+ RUBY_EXTERN VALUE cRubyPyClass;
11
+ RUBY_EXTERN VALUE cRubyPyFunction;
12
+ RUBY_EXTERN VALUE cRubyPyInstance;
13
+
14
+ VALUE rpObjectWrap(PyObject* pObj)
15
+ {
16
+ VALUE rObj;
17
+
18
+ if(PyFunction_Check(pObj)||PyMethod_Check(pObj)||!PyObject_HasAttrString(pObj,"__dict__"))
19
+ {
20
+ return rpFunctionFromPyObject(pObj);
21
+
22
+ }
23
+
24
+ if(PyInstance_Check(pObj))
25
+ {
26
+ rObj = rpInstanceFromPyObject(pObj);
27
+ return rObj;
28
+ }
29
+
30
+ return rpClassFromPyObject(pObj);
31
+ }
32
+
33
+
34
+ //Pass this function the argument list for a function call. Checks to see
35
+ //if the first parameter (the method symbol name here because this is called
36
+ //from within method_missing) ends with an equals
37
+ int rpSymbolIsSetter(VALUE args)
38
+ {
39
+ VALUE mname;
40
+ VALUE name_string;
41
+ VALUE isSetter_;
42
+
43
+ mname = rb_ary_entry(args, 0);
44
+ name_string = rb_funcall(mname, rb_intern("to_s"), 0);
45
+
46
+ isSetter_ = rb_funcall(name_string, rb_intern("end_with?"), 1, rb_str_new2("="));
47
+
48
+ return Qtrue == isSetter_;
49
+ }
50
+
51
+
52
+
53
+ //Tests if the first argument ends with !!. See the comment for
54
+ //rpSymbolIsSetter
55
+ int rpSymbolIsDoubleBang(VALUE args)
56
+ {
57
+ VALUE mname = rb_ary_entry(args, 0);
58
+ VALUE name_string = rb_funcall(mname, rb_intern("to_s"), 0);
59
+
60
+ return Qtrue == rb_funcall(name_string, rb_intern("end_with?"), 1, rb_str_new2("!!"));
61
+ }
@@ -0,0 +1,11 @@
1
+ #include "config.h"
2
+
3
+ #ifndef _RP_UTIL_H_
4
+ #define _RP_UTIL_H_
5
+ int rpSymbolIsSetter();
6
+
7
+ int rpSymbolIsDoubleBang(VALUE);
8
+
9
+ VALUE rpObjectWrap(PyObject*);
10
+
11
+ #endif /* _RP_UTIL_H_ */
@@ -1,163 +1,212 @@
1
1
  #include "rtop.h"
2
2
 
3
3
  RUBY_EXTERN VALUE cRubyPyObject;
4
- RUBY_EXTERN PyObject* rp_obj_pobject(VALUE self);
4
+ RUBY_EXTERN PyObject* rpObjectGetPyObject(VALUE self);
5
5
 
6
- PyObject* rtop_string(VALUE rString)
6
+ /*
7
+ * Note: For the builtin types rubypython creates a copy of the ruby
8
+ * object to pass into python. Builtin types are passed by VALUE not
9
+ * by REFERENCE.
10
+ */
11
+
12
+ PyObject* rtopString(VALUE rString)
7
13
  {
14
+
8
15
  PyObject* pString;
9
- char *cString;
10
- char *cStringCopy;
11
- cString=STR2CSTR(rString);
12
- cStringCopy=malloc(strlen(cString)*sizeof(char));
13
- strcpy(cStringCopy,cString);
14
- pString=PyString_FromString(cStringCopy);
16
+ char* cString;
17
+ char* cStringCopy;
18
+
19
+ cString = STR2CSTR(rString);
20
+ cStringCopy = malloc(strlen(cString) * sizeof(char));
21
+ strcpy(cStringCopy, cString);
22
+
23
+ pString = PyString_FromString(cStringCopy);
24
+
15
25
  return pString;
16
26
  }
17
27
 
18
28
 
19
- PyObject* rtop_array_list(VALUE rArray)
29
+ PyObject* rtopArrayToList(VALUE rArray)
20
30
  {
21
31
  PyObject* pList;
22
- int size=RARRAY_LEN(rArray);
23
- pList=PyList_New(size);
24
32
  int i;
25
- for(i=0;i<size;i++)
33
+ int size = RARRAY_LEN(rArray);
34
+
35
+ pList = PyList_New(size);
36
+
37
+ for(i = 0; i < size; i++)
26
38
  {
27
- PyList_SetItem(pList,i,rtop_obj(rb_ary_entry(rArray,i),0));
39
+ PyList_SetItem(pList, i, rtopObject(rb_ary_entry(rArray, i), 0));
28
40
  }
41
+
29
42
  return pList;
30
43
  }
31
44
 
32
- PyObject* rtop_array_tuple(VALUE rArray)
45
+ PyObject* rtopArrayToTuple(VALUE rArray)
33
46
  {
34
47
  PyObject *pTuple,*pList;
35
- pList=rtop_array_list(rArray);
36
- pTuple=PySequence_Tuple(pList);
48
+
49
+ pList = rtopArrayToList(rArray);
50
+ pTuple = PySequence_Tuple(pList);
37
51
  Py_XDECREF(pList);
52
+
38
53
  return pTuple;
39
54
  }
40
55
 
41
- PyObject* rtop_hash(VALUE rHash)
56
+ PyObject* rtopHash(VALUE rHash)
42
57
  {
43
58
  PyObject *pDict,*pKey,*pVal;
44
- VALUE rKeys=rb_funcall(rHash,rb_intern("keys"),0);
45
- VALUE rKey,rVal;
59
+ VALUE rKeys;
60
+ VALUE rKey, rVal;
46
61
  int i;
47
62
 
48
- pDict=PyDict_New();
63
+ pDict = PyDict_New();
64
+
65
+ rKeys = rb_funcall(rHash, rb_intern("keys"), 0);
49
66
 
50
- for(i=0;i<RARRAY_LEN(rKeys);i++)
67
+ for(i = 0; i < RARRAY_LEN(rKeys); i++)
51
68
  {
52
- rKey=rb_ary_entry(rKeys,i);
53
- rVal=rb_hash_aref(rHash,rKey);
54
- PyDict_SetItem(pDict,rtop_obj(rKey,1),rtop_obj(rVal,0));
69
+ rKey = rb_ary_entry(rKeys, i);
70
+ rVal = rb_hash_aref(rHash, rKey);
71
+ PyDict_SetItem(pDict, rtopObject(rKey, 1), rtopObject(rVal, 0));
55
72
  }
73
+
56
74
  return pDict;
57
75
  }
58
76
 
59
- PyObject* rtop_fixnum(VALUE rNum)
77
+ PyObject* rtopFixnum(VALUE rNum)
60
78
  {
61
79
  PyObject* pNum;
62
- long cNum=NUM2LONG(rNum);
63
- pNum=PyInt_FromLong(cNum);
80
+ long cNum;
81
+
82
+ cNum = NUM2LONG(rNum);
83
+ pNum = PyInt_FromLong(cNum);
84
+
64
85
  return pNum;
65
86
  }
66
87
 
67
- PyObject* rtop_bignum(VALUE rNum)
88
+ PyObject* rtopBignum(VALUE rNum)
68
89
  {
69
90
  PyObject* pNum;
70
- long cNum=NUM2LONG(rNum);
71
- pNum=PyLong_FromLong(cNum);
91
+ long cNum;
92
+
93
+ cNum = NUM2LONG(rNum);
94
+ pNum = PyLong_FromLong(cNum);
95
+
72
96
  return pNum;
73
97
  }
74
98
 
75
- PyObject* rtop_float(VALUE rNum)
99
+ PyObject* rtopFloat(VALUE rNum)
76
100
  {
77
101
  PyObject* pNum;
78
- double cNum=NUM2DBL(rNum);
79
- pNum=PyFloat_FromDouble(cNum);
102
+ double cNum;
103
+
104
+ cNum = NUM2DBL(rNum);
105
+ pNum = PyFloat_FromDouble(cNum);
106
+
80
107
  return pNum;
81
108
  }
82
109
 
83
- PyObject* rtop_false()
110
+ PyObject* rtopFalse()
84
111
  {
85
112
  Py_RETURN_FALSE;
86
113
  }
87
114
 
88
- PyObject* rtop_true()
115
+ PyObject* rtopTrue()
89
116
  {
90
117
  Py_RETURN_TRUE;
91
118
  }
92
119
 
93
- PyObject* rtop_symbol(VALUE rSymbol)
120
+ PyObject* rtopSymbol(VALUE rSymbol)
94
121
  {
95
122
  PyObject* pString;
96
- pString=PyString_FromString(STR2CSTR(rb_funcall(rSymbol,rb_intern("to_s"),0)));
123
+ char* cStr;
124
+
125
+ cStr = STR2CSTR(rb_funcall(rSymbol, rb_intern("to_s"), 0));
126
+ pString = PyString_FromString(cStr);
127
+
97
128
  return pString;
98
129
 
99
130
  }
100
131
 
101
- PyObject* rtop_obj(VALUE rObj,int is_key)
132
+ PyObject* rtopObject(VALUE rObj, int is_key)
102
133
  {
134
+ // The above is_key parameter determines whether the object
135
+ // created show be immutable if possible
136
+
103
137
  PyObject *pObj;
104
138
  VALUE rInspect;
139
+
140
+ // Check the object for its type and apply the appropriate
141
+ // conversion function
142
+
105
143
  switch(TYPE(rObj))
106
144
  {
107
145
  case T_STRING:
108
- pObj=rtop_string(rObj);
146
+ pObj = rtopString(rObj);
109
147
  break;
110
148
 
111
149
  case T_ARRAY:
112
- if(is_key) pObj=rtop_array_tuple(rObj);
150
+ // If this object is going to be used as a
151
+ // hash key we should make it a tuple instead
152
+ // of a list
153
+ if(is_key) pObj = rtopArrayToTuple(rObj);
113
154
  else
114
155
  {
115
- pObj=rtop_array_list(rObj);
156
+ pObj = rtopArrayToList(rObj);
116
157
  }
117
158
  break;
118
159
 
119
160
  case T_HASH:
120
- pObj=rtop_hash(rObj);
161
+ pObj = rtopHash(rObj);
121
162
  break;
122
163
 
123
164
  case T_FIXNUM:
124
- pObj=rtop_fixnum(rObj);
165
+ pObj = rtopFixnum(rObj);
125
166
  break;
126
167
 
127
168
  case T_BIGNUM:
128
- pObj=rtop_bignum(rObj);
169
+ pObj = rtopBignum(rObj);
129
170
  break;
130
171
 
131
172
  case T_FLOAT:
132
- pObj=rtop_float(rObj);
173
+ pObj = rtopFloat(rObj);
133
174
  break;
134
175
 
135
176
  case T_NIL:
136
- pObj=Py_None;
177
+ pObj = Py_None;
137
178
  break;
138
179
 
139
180
  case T_TRUE:
140
- pObj=rtop_true();
181
+ pObj = rtopTrue();
141
182
  break;
142
183
 
143
184
  case T_FALSE:
144
- pObj=rtop_false();
185
+ pObj = rtopFalse();
145
186
  break;
146
187
 
147
188
  case T_SYMBOL:
148
- pObj=rtop_symbol(rObj);
189
+ pObj = rtopSymbol(rObj);
149
190
  break;
150
191
 
151
192
  default:
152
- if(rb_obj_is_kind_of(rObj,cRubyPyObject)==Qtrue)
193
+ if(rb_obj_is_kind_of(rObj, cRubyPyObject) == Qtrue)
153
194
  {
154
- pObj=rp_obj_pobject(rObj);
195
+ // rObj is a wrapped python object. We
196
+ // just take the object it wraps. In
197
+ // this case we are effectively passing
198
+ // a python object by reference
199
+ pObj = rpObjectGetPyObject(rObj);
155
200
  }
156
201
  else
157
202
  {
158
- rInspect=rb_inspect(rObj);
159
- pObj=rtop_string(rInspect);
203
+ // If we can't figure out what else to
204
+ // do with the ruby object we just pass
205
+ // a string representation of it
206
+ rInspect = rb_inspect(rObj);
207
+ pObj = rtopString(rInspect);
160
208
  }
161
209
  }
210
+
162
211
  return pObj;
163
- }
212
+ }
@@ -1,22 +1,17 @@
1
1
  #include "config.h"
2
2
 
3
- #ifndef _RP_ERROR_H_
4
- #include "rp_error.h"
5
- #endif
6
-
7
3
  #ifndef _RTOP_H_
8
4
  #define _RTOP_H_
9
- PyObject* rtop_string(VALUE rString);
10
- PyObject* rtop_array_list(VALUE rArray);
11
- PyObject* rtop_array_tuple(VALUE rArray);
12
- PyObject* rtop_hash(VALUE rHash);
13
- PyObject* rtop_fixnum(VALUE rNum);
14
- PyObject* rtop_bignum(VALUE rNum);
15
- PyObject* rtop_float(VALUE rNum);
16
- PyObject* rtop_false(void);
17
- PyObject* rtop_true(void);
18
- PyObject* rtop_symbol(VALUE rSymbol);
19
-
20
- PyObject* rtop_obj(VALUE rObj,int is_key);
5
+ PyObject* rtopString(VALUE);
6
+ PyObject* rtopArrayToList(VALUE);
7
+ PyObject* rtopArrayToTuple(VALUE);
8
+ PyObject* rtopHash(VALUE);
9
+ PyObject* rtopFixnum(VALUE);
10
+ PyObject* rtopBignum(VALUE);
11
+ PyObject* rtopFloat(VALUE);
12
+ PyObject* rtopFalse(void);
13
+ PyObject* rtopTrue(void);
14
+ PyObject* rtopSymbol(VALUE);
15
+ PyObject* rtopObject(VALUE, int);
21
16
 
22
17
  #endif /* _RTOP_H_ */