rubypython 0.2.11 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data.tar.gz.sig +0 -0
  2. data/{History.txt → History.markdown} +34 -28
  3. data/Manifest.txt +26 -40
  4. data/PostInstall.txt +2 -1
  5. data/README.markdown +103 -0
  6. data/Rakefile +19 -3
  7. data/lib/rubypython.rb +118 -114
  8. data/lib/rubypython/blankobject.rb +21 -0
  9. data/lib/rubypython/conversion.rb +198 -0
  10. data/lib/rubypython/core_ext/string.rb +7 -0
  11. data/lib/rubypython/legacy.rb +15 -0
  12. data/lib/rubypython/macros.rb +47 -0
  13. data/lib/rubypython/operators.rb +111 -0
  14. data/lib/rubypython/pymainclass.rb +51 -0
  15. data/lib/rubypython/pyobject.rb +203 -0
  16. data/lib/rubypython/python.rb +111 -0
  17. data/lib/rubypython/pythonerror.rb +78 -0
  18. data/lib/rubypython/rubypyproxy.rb +214 -0
  19. data/lib/rubypython/version.rb +4 -3
  20. data/spec/conversion_spec.rb +66 -0
  21. data/spec/legacy_spec.rb +22 -0
  22. data/spec/pymainclass_spec.rb +26 -0
  23. data/spec/pyobject_spec.rb +264 -0
  24. data/spec/python_helpers/objects.py +41 -0
  25. data/spec/pythonerror_spec.rb +43 -0
  26. data/spec/refcnt_spec.rb +68 -0
  27. data/spec/rubypyclass_spec.rb +13 -0
  28. data/spec/rubypyproxy_spec.rb +249 -0
  29. data/spec/rubypython_spec.rb +62 -0
  30. data/spec/spec.opts +2 -0
  31. data/spec/spec_helper.rb +51 -0
  32. metadata +79 -73
  33. metadata.gz.sig +0 -0
  34. data/README.txt +0 -60
  35. data/ext/rubypython_bridge/cbridge.c +0 -150
  36. data/ext/rubypython_bridge/cbridge.h +0 -15
  37. data/ext/rubypython_bridge/config.h +0 -14
  38. data/ext/rubypython_bridge/extconf.rb +0 -43
  39. data/ext/rubypython_bridge/ptor.c +0 -242
  40. data/ext/rubypython_bridge/ptor.h +0 -15
  41. data/ext/rubypython_bridge/rp_blankobject.c +0 -42
  42. data/ext/rubypython_bridge/rp_blankobject.h +0 -11
  43. data/ext/rubypython_bridge/rp_class.c +0 -56
  44. data/ext/rubypython_bridge/rp_class.h +0 -7
  45. data/ext/rubypython_bridge/rp_error.c +0 -34
  46. data/ext/rubypython_bridge/rp_error.h +0 -11
  47. data/ext/rubypython_bridge/rp_function.c +0 -31
  48. data/ext/rubypython_bridge/rp_function.h +0 -7
  49. data/ext/rubypython_bridge/rp_instance.c +0 -164
  50. data/ext/rubypython_bridge/rp_instance.h +0 -7
  51. data/ext/rubypython_bridge/rp_module.c +0 -160
  52. data/ext/rubypython_bridge/rp_module.h +0 -8
  53. data/ext/rubypython_bridge/rp_object.c +0 -194
  54. data/ext/rubypython_bridge/rp_object.h +0 -23
  55. data/ext/rubypython_bridge/rp_util.c +0 -63
  56. data/ext/rubypython_bridge/rp_util.h +0 -11
  57. data/ext/rubypython_bridge/rtop.c +0 -212
  58. data/ext/rubypython_bridge/rtop.h +0 -17
  59. data/ext/rubypython_bridge/rubypython_bridge.c +0 -125
  60. data/ext/rubypython_bridge/rubypython_bridge.h +0 -10
  61. data/lib/rubypython/session.rb +0 -4
  62. data/lib/rubypython/wrapper_extensions.rb +0 -83
  63. data/setup.rb +0 -1585
  64. data/tasks/environment.rake +0 -7
  65. data/tasks/extconf.rake +0 -13
  66. data/tasks/extconf/rubypython_bridge.rake +0 -49
  67. data/test/python_helpers/objects.py +0 -12
  68. data/test/test.wav +0 -0
  69. data/test/test_helper.rb +0 -2
  70. data/test/test_rubypython.rb +0 -215
  71. data/test/test_rubypython_bridge_extn.rb +0 -133
  72. data/test/test_session.rb +0 -6
@@ -1,14 +0,0 @@
1
- #ifndef _PYTHON_H_
2
- #define _PYTHON_H_
3
- #include "Python.h"
4
- #endif /* _PYTHON_H_ */
5
-
6
- #ifndef _STDLIB_H_
7
- #define _STDLIB_H_
8
- #include "stdlib.h"
9
- #endif /* _STDLIB_H_ */
10
-
11
- #ifndef _RUBY_H_
12
- #define _RUBY_H_
13
- #include "ruby.h"
14
- #endif /* _RUBY_H_ */
@@ -1,43 +0,0 @@
1
- require 'mkmf'
2
- require 'open3'
3
-
4
- begin
5
- if (Open3.popen3("python --version") { |i,o,e| e.read}.chomp.split[1].to_f < 2.4)
6
- puts "Rubypython seem to think you don't have python 2.4 or higher."
7
- puts "Try install anyway? (y/n)"
8
- c=gets.chomp
9
- while !["y","n"].member? c do
10
- puts "Please type y or n"
11
- end
12
- if(c=="n")
13
- exit -1
14
- end
15
- end
16
- rescue
17
- puts "Could not check python version. Do you have Python 2.4 or higher? (y/n)"
18
- if(gets.chomp == "n")
19
- puts "Please install Python 2.4 or higher"
20
- exit
21
- end
22
- puts "Okay."
23
- end
24
- dir_config("rubypython_bridge")
25
- if(!system("which python-config"))
26
- print "Can't configure with python_config"
27
- exit -1
28
- end
29
-
30
- #unless find_library("python2.6",nil)||find_library("python2.5",nil)||find_library("python2.4",nil)
31
- # puts "Could not find python libraries"
32
- # exit -1
33
- #end
34
- $LDFLAGS << " " + `python-config --ldflags`
35
- $CPPFLAGS << " " + `python-config --includes`.chomp
36
-
37
- if RUBY_VERSION=~/1\.9/ then
38
- puts "Building for Ruby 1.9"
39
- $CPPFLAGS += " -DRUBY_19"
40
- end
41
-
42
-
43
- create_makefile("rubypython_bridge")
@@ -1,242 +0,0 @@
1
- #include "ptor.h"
2
-
3
- #include "rp_error.h"
4
- #include "rp_function.h"
5
- #include "rp_instance.h"
6
- #include "rp_class.h"
7
-
8
-
9
- /* Note:
10
- The conversion functions for the builtin types are just that,
11
- conversion functions. They create a new Ruby object equivalent to
12
- the given Python object, they do not wrap the Python object.
13
-
14
- */
15
-
16
- VALUE ptorString(PyObject* pString)
17
- {
18
- // Make sure pString is actually a string
19
- if(!PyString_Check(pString)) return Qnil;
20
-
21
- // Allocate a new string for Ruby and return it.
22
- // Note that this is a new object, not a wrapper around a
23
- // python object
24
- char* cStr;
25
- char* cStrCopy;
26
-
27
- cStr = PyString_AsString(pString);
28
-
29
- cStrCopy = malloc(PyString_Size(pString) * sizeof(char));
30
-
31
- strcpy(cStrCopy, cStr);
32
-
33
-
34
- return rb_str_new2(cStrCopy);
35
- }
36
-
37
- VALUE ptorList(PyObject* pList)
38
- {
39
- // Verify that pList is a python list
40
- if(!PyList_Check(pList)) return Qnil;
41
-
42
- VALUE rArray;
43
- VALUE rElement;
44
- PyObject* element;
45
-
46
- int i = 0;
47
-
48
- // Allocate a new Ruby array
49
- rArray = rb_ary_new();
50
-
51
- // Iteratively add converted elements to the new Ruby list
52
- int list_size = PyList_Size(pList);
53
- for(i = 0; i < list_size; i++)
54
- {
55
- element = PyList_GetItem(pList, i);
56
- Py_INCREF(element);
57
- rElement = ptorObject(element);
58
- rb_ary_push(rArray, rElement);
59
- }
60
- return rArray;
61
- }
62
-
63
- VALUE ptorInt(PyObject* pNum)
64
- {
65
- if(!PyInt_Check(pNum)) return Qnil;
66
-
67
- VALUE rNum;
68
-
69
- rNum = INT2NUM(PyInt_AsLong(pNum));
70
- return rNum;
71
-
72
- }
73
-
74
- VALUE ptorLong(PyObject* pNum)
75
- {
76
- if(!PyLong_Check(pNum)) return Qnil;
77
-
78
- VALUE rNum;
79
- long cNum;
80
-
81
- cNum = PyLong_AsLong(pNum);
82
-
83
- if(PyErr_ExceptionMatches(PyExc_OverflowError))
84
- {
85
- rpPythonError();
86
- return Qnil;
87
- }
88
-
89
- rNum = INT2NUM(cNum);
90
-
91
- return rNum;
92
-
93
- }
94
-
95
- VALUE ptorFloat(PyObject* pNum)
96
- {
97
- if(!PyFloat_Check(pNum)) return Qnil;
98
-
99
- VALUE rNum;
100
-
101
- rNum = rb_float_new(PyFloat_AsDouble(pNum));
102
-
103
- return rNum;
104
- }
105
-
106
- VALUE ptorTuple(PyObject* pTuple)
107
- {
108
- if(!PyTuple_Check(pTuple)) return Qnil;
109
-
110
- VALUE rArray;
111
- PyObject* pList;
112
-
113
- pList = PySequence_List(pTuple);
114
- rArray = ptorList(pList);
115
- Py_DECREF(pList);
116
-
117
- return rArray;
118
- }
119
-
120
-
121
- VALUE ptorDict(PyObject* pDict)
122
- {
123
- if(!PyDict_Check(pDict)) return Qnil;
124
-
125
- VALUE rHash;
126
- VALUE rKey, rVal;
127
- PyObject *key,*val;
128
- Py_ssize_t pos = 0;
129
-
130
- rHash = rb_hash_new();
131
-
132
- while(PyDict_Next(pDict,&pos,&key,&val))
133
- {
134
- Py_XINCREF(key);
135
- Py_XINCREF(val);
136
- rKey = ptorObject(key);
137
- rVal = ptorObject(val);
138
- if(rKey == Qnil) continue;
139
- rb_hash_aset(rHash, rKey, rVal);
140
- }
141
-
142
- return rHash;
143
- }
144
-
145
- static VALUE ptorObjectBasic(PyObject *pObj, int destructive)
146
- {
147
- VALUE rObj;
148
-
149
- // Test the Python object vs various types and convert / wrap it
150
- // appropriately. If the destructive flag is set, we destroy
151
- // the original.
152
-
153
- if(PyObject_TypeCheck(pObj,&PyString_Type))
154
- {
155
- rObj = ptorString(pObj);
156
- if(destructive) Py_DECREF(pObj);
157
- return rObj;
158
- }
159
-
160
- if(PyObject_TypeCheck(pObj,&PyList_Type))
161
- {
162
- rObj = ptorList(pObj);
163
- if(destructive) Py_DECREF(pObj);
164
- return rObj;
165
- }
166
- if(PyObject_TypeCheck(pObj,&PyInt_Type))
167
- {
168
- rObj = ptorInt(pObj);
169
- if(destructive) Py_DECREF(pObj);
170
- return rObj;
171
- }
172
- if(PyObject_TypeCheck(pObj,&PyLong_Type))
173
- {
174
- rObj = ptorLong(pObj);
175
- if(destructive) Py_DECREF(pObj);
176
- return rObj;
177
- }
178
- if(PyObject_TypeCheck(pObj,&PyFloat_Type))
179
- {
180
- rObj = ptorFloat(pObj);
181
- if(destructive) Py_DECREF(pObj);
182
- return rObj;
183
- }
184
- if(PyObject_TypeCheck(pObj,&PyTuple_Type))
185
- {
186
- rObj = ptorTuple(pObj);
187
- if(destructive) Py_DECREF(pObj);
188
- return rObj;
189
- }
190
- if(PyObject_TypeCheck(pObj,&PyDict_Type))
191
- {
192
- rObj = ptorDict(pObj);
193
- if(destructive) Py_DECREF(pObj);
194
- return rObj;
195
- }
196
-
197
- if(pObj == Py_True)
198
- {
199
- if(destructive) Py_DECREF(Py_True);
200
- return Qtrue;
201
- }
202
- if(pObj == Py_False)
203
- {
204
- if(destructive) Py_DECREF(Py_False);
205
- return Qfalse;
206
- }
207
- if(pObj == Py_None)
208
- {
209
- return Qnil;
210
- }
211
- if(PyFunction_Check(pObj)||PyMethod_Check(pObj)||!PyObject_HasAttrString(pObj,"__dict__"))
212
- {
213
- return rpFunctionFromPyObject(pObj);
214
-
215
- }
216
- if(PyInstance_Check(pObj))
217
- {
218
- rObj = rpInstanceFromPyObject(pObj);
219
- return rObj;
220
- }
221
-
222
- // Fallthrough behavior: The object is a class which should be wrapped
223
- return rpClassFromPyObject(pObj);
224
- }
225
-
226
- // Convert a Python object to a Ruby object and destroy the original
227
- VALUE ptorObjectKeep(PyObject *pObj)
228
- {
229
- VALUE rObj;
230
- rObj = ptorObjectBasic(pObj, 0);
231
- return rObj;
232
- }
233
-
234
-
235
- // Convert a Python object to a Ruby object while keeping the original
236
- VALUE ptorObject(PyObject* pObj)
237
- {
238
- VALUE rObj;
239
- rObj = ptorObjectBasic(pObj, 1);
240
- return rObj;
241
- }
242
-
@@ -1,15 +0,0 @@
1
- #include "config.h"
2
-
3
- #ifndef _PTOR_H_
4
- #define _PTOR_H_
5
- //Python to Ruby Conversion
6
- VALUE ptorString(PyObject* pString);
7
- VALUE ptorList(PyObject* pList);
8
- VALUE ptorInt(PyObject* pNum);
9
- VALUE ptorLong(PyObject* pNum);
10
- VALUE ptorFloat(PyObject* pNum);
11
- VALUE ptorTuple(PyObject* pTuple);
12
- VALUE ptorDict(PyObject* pDict);
13
- VALUE ptorObject(PyObject* pObj);
14
- VALUE ptorObjectKeep(PyObject *pObj);
15
- #endif /* _PTOR_H_ */
@@ -1,42 +0,0 @@
1
- #include "rp_blankobject.h"
2
-
3
- RUBY_EXTERN VALUE mRubyPythonBridge;
4
-
5
- VALUE cBlankObject;
6
-
7
- // :nodoc:
8
- /* This functions is used as a predicate function. Every function name
9
- for which it returns true will be removed from the blank object
10
- dictionary.
11
- */
12
- VALUE blank_undef_if(VALUE name, VALUE klass)
13
- {
14
- VALUE mname = rb_funcall(name, rb_intern("to_s"), 0);
15
- VALUE methodRe = rb_str_new2("(?:^__)|(?:\\?$)|(?:^send$)|(?:^class$)");
16
-
17
- if(rb_funcall(mname, rb_intern("match"), 1, methodRe) == Qnil)
18
- {
19
- rb_undef_method(klass, STR2CSTR(mname));
20
- return Qtrue;
21
- }
22
- else
23
- {
24
- return Qfalse;
25
- }
26
- }
27
-
28
- // :nodoc:
29
- VALUE blank_obj_prep(VALUE self)
30
- {
31
- VALUE instance_methods = rb_funcall(self, rb_intern("instance_methods"), 0);
32
-
33
- rb_iterate(rb_each, instance_methods, blank_undef_if, self);
34
- return self;
35
- }
36
-
37
- // :nodoc:
38
- inline void Init_BlankObject()
39
- {
40
- cBlankObject = rb_define_class_under(mRubyPythonBridge,"BlankObject", rb_cObject);
41
- blank_obj_prep(cBlankObject);
42
- }
@@ -1,11 +0,0 @@
1
- #include "config.h"
2
-
3
- #ifndef _RP_BLANKOBJECT_H_
4
- #define _RP_BLANKOBJECT_H_
5
-
6
-
7
- VALUE blank_undef_if(VALUE, VALUE);
8
- VALUE blank_obj_prep(VALUE);
9
- inline void Init_BlankObject();
10
-
11
- #endif
@@ -1,56 +0,0 @@
1
- #include "rp_class.h"
2
-
3
- #include "rp_module.h"
4
- #include "rp_object.h"
5
-
6
- RUBY_EXTERN VALUE mRubyPythonBridge;
7
- RUBY_EXTERN VALUE ePythonError;
8
- RUBY_EXTERN VALUE cRubyPyObject;
9
- RUBY_EXTERN VALUE cBlankObject;
10
-
11
- VALUE cRubyPyClass;
12
-
13
- VALUE rpClassFromPyObject(PyObject *pClass)
14
- {
15
- PObj* self;
16
- PyObject* pClassDict;
17
- VALUE rDict;
18
- VALUE rClass = rb_class_new_instance(0, NULL, cRubyPyClass);
19
-
20
- Data_Get_Struct(rClass, PObj, self);
21
- self->pObject = pClass;
22
-
23
- pClassDict = PyObject_GetAttrString(pClass,"__dict__");
24
- Py_XINCREF(pClassDict);
25
-
26
- rDict = rpObjectFromPyObject
27
- (pClassDict);
28
- rb_iv_set(rClass,"@pdict", rDict);
29
-
30
- return rClass;
31
- }
32
-
33
- static
34
- VALUE rpClassNew(VALUE self, VALUE args)
35
- {
36
- PyObject* pSelf;
37
-
38
- pSelf = rpObjectGetPyObject(self);
39
-
40
- return rpCall(pSelf, args);
41
- }
42
-
43
- /*
44
- A wrapper class for Python classes and instances.
45
-
46
- This allows objects which cannot easily be converted to native Ruby types to still be accessible
47
- from within ruby. Most users need not concern themselves with anything about this class except
48
- its existence.
49
-
50
- */
51
- inline void Init_RubyPyClass()
52
- {
53
- cRubyPyClass = rb_define_class_under(mRubyPythonBridge,"RubyPyClass", cRubyPyObject);
54
- rb_define_method(cRubyPyClass,"method_missing", rpModuleDelegate,- 2);
55
- rb_define_method(cRubyPyClass,"new", rpClassNew,- 2);
56
- }
@@ -1,7 +0,0 @@
1
- #include "config.h"
2
-
3
- #ifndef _RP_CLASS_H_
4
- #define _RP_CLASS_H_
5
- VALUE rpClassFromPyObject(PyObject*);
6
- inline void Init_RubyPyClass();
7
- #endif
@@ -1,34 +0,0 @@
1
- #include "rp_error.h"
2
- #include "ptor.h"
3
-
4
- VALUE ePythonError;
5
- VALUE eRubyPyError;
6
-
7
- void rpPythonError()
8
- {
9
- PyObject *pType,*pValue,*pTraceback;
10
- PyObject *pTypeName;
11
-
12
- PyErr_Fetch(&pType,&pValue,&pTraceback);
13
-
14
- pTypeName = PyObject_GetAttrString(pType,"__name__");
15
- Py_XDECREF(pType);
16
-
17
- rb_raise(ePythonError,"%s:(%s)\n", STR2CSTR(ptorObject(pTypeName)), STR2CSTR(rb_inspect(ptorObject(pValue))));
18
-
19
- Py_XDECREF(pTraceback);
20
- }
21
-
22
- void rpRubyPyError(char* eString) {
23
- rb_raise(eRubyPyError, eString);
24
- }
25
-
26
- /*
27
- Used to pass error information back into Ruby should an error occur in the embedded Python
28
- interpreter.
29
- */
30
- inline void Init_RubyPyError()
31
- {
32
- ePythonError = rb_define_class("PythonError", rb_eException);
33
- eRubyPyError = rb_define_class("RubyPyError", rb_eException);
34
- }