rubypython 0.2.11 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data.tar.gz.sig +0 -0
- data/{History.txt → History.markdown} +34 -28
- data/Manifest.txt +26 -40
- data/PostInstall.txt +2 -1
- data/README.markdown +103 -0
- data/Rakefile +19 -3
- data/lib/rubypython.rb +118 -114
- data/lib/rubypython/blankobject.rb +21 -0
- data/lib/rubypython/conversion.rb +198 -0
- data/lib/rubypython/core_ext/string.rb +7 -0
- data/lib/rubypython/legacy.rb +15 -0
- data/lib/rubypython/macros.rb +47 -0
- data/lib/rubypython/operators.rb +111 -0
- data/lib/rubypython/pymainclass.rb +51 -0
- data/lib/rubypython/pyobject.rb +203 -0
- data/lib/rubypython/python.rb +111 -0
- data/lib/rubypython/pythonerror.rb +78 -0
- data/lib/rubypython/rubypyproxy.rb +214 -0
- data/lib/rubypython/version.rb +4 -3
- data/spec/conversion_spec.rb +66 -0
- data/spec/legacy_spec.rb +22 -0
- data/spec/pymainclass_spec.rb +26 -0
- data/spec/pyobject_spec.rb +264 -0
- data/spec/python_helpers/objects.py +41 -0
- data/spec/pythonerror_spec.rb +43 -0
- data/spec/refcnt_spec.rb +68 -0
- data/spec/rubypyclass_spec.rb +13 -0
- data/spec/rubypyproxy_spec.rb +249 -0
- data/spec/rubypython_spec.rb +62 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +51 -0
- metadata +79 -73
- metadata.gz.sig +0 -0
- data/README.txt +0 -60
- data/ext/rubypython_bridge/cbridge.c +0 -150
- data/ext/rubypython_bridge/cbridge.h +0 -15
- data/ext/rubypython_bridge/config.h +0 -14
- data/ext/rubypython_bridge/extconf.rb +0 -43
- data/ext/rubypython_bridge/ptor.c +0 -242
- data/ext/rubypython_bridge/ptor.h +0 -15
- data/ext/rubypython_bridge/rp_blankobject.c +0 -42
- data/ext/rubypython_bridge/rp_blankobject.h +0 -11
- data/ext/rubypython_bridge/rp_class.c +0 -56
- data/ext/rubypython_bridge/rp_class.h +0 -7
- data/ext/rubypython_bridge/rp_error.c +0 -34
- data/ext/rubypython_bridge/rp_error.h +0 -11
- data/ext/rubypython_bridge/rp_function.c +0 -31
- data/ext/rubypython_bridge/rp_function.h +0 -7
- data/ext/rubypython_bridge/rp_instance.c +0 -164
- data/ext/rubypython_bridge/rp_instance.h +0 -7
- data/ext/rubypython_bridge/rp_module.c +0 -160
- data/ext/rubypython_bridge/rp_module.h +0 -8
- data/ext/rubypython_bridge/rp_object.c +0 -194
- data/ext/rubypython_bridge/rp_object.h +0 -23
- data/ext/rubypython_bridge/rp_util.c +0 -63
- data/ext/rubypython_bridge/rp_util.h +0 -11
- data/ext/rubypython_bridge/rtop.c +0 -212
- data/ext/rubypython_bridge/rtop.h +0 -17
- data/ext/rubypython_bridge/rubypython_bridge.c +0 -125
- data/ext/rubypython_bridge/rubypython_bridge.h +0 -10
- data/lib/rubypython/session.rb +0 -4
- data/lib/rubypython/wrapper_extensions.rb +0 -83
- data/setup.rb +0 -1585
- data/tasks/environment.rake +0 -7
- data/tasks/extconf.rake +0 -13
- data/tasks/extconf/rubypython_bridge.rake +0 -49
- data/test/python_helpers/objects.py +0 -12
- data/test/test.wav +0 -0
- data/test/test_helper.rb +0 -2
- data/test/test_rubypython.rb +0 -215
- data/test/test_rubypython_bridge_extn.rb +0 -133
- data/test/test_session.rb +0 -6
@@ -1,31 +0,0 @@
|
|
1
|
-
#include "rp_function.h"
|
2
|
-
#include "rp_object.h"
|
3
|
-
|
4
|
-
RUBY_EXTERN VALUE mRubyPythonBridge;
|
5
|
-
RUBY_EXTERN VALUE cRubyPyObject;
|
6
|
-
|
7
|
-
VALUE cRubyPyFunction;
|
8
|
-
|
9
|
-
VALUE rpFunctionFromPyObject(PyObject *pFunc)
|
10
|
-
{
|
11
|
-
PObj* self;
|
12
|
-
VALUE rFunc = rb_class_new_instance(0, NULL, cRubyPyFunction);
|
13
|
-
|
14
|
-
Data_Get_Struct(rFunc, PObj, self);
|
15
|
-
|
16
|
-
self->pObject = pFunc;
|
17
|
-
|
18
|
-
return rFunc;
|
19
|
-
}
|
20
|
-
|
21
|
-
|
22
|
-
//
|
23
|
-
// A wrapper class for Python functions and methods.
|
24
|
-
//
|
25
|
-
// This is used internally to aid RubyPyClass in delegating method calls.
|
26
|
-
//
|
27
|
-
|
28
|
-
inline void Init_RubyPyFunction()
|
29
|
-
{
|
30
|
-
cRubyPyFunction = rb_define_class_under(mRubyPythonBridge,"RubyPyFunction", cRubyPyObject);
|
31
|
-
}
|
@@ -1,164 +0,0 @@
|
|
1
|
-
#include "rp_instance.h"
|
2
|
-
|
3
|
-
#include "rp_object.h"
|
4
|
-
#include "rp_function.h"
|
5
|
-
#include "rp_util.h"
|
6
|
-
|
7
|
-
RUBY_EXTERN VALUE mRubyPythonBridge;
|
8
|
-
|
9
|
-
RUBY_EXTERN VALUE cRubyPyObject;
|
10
|
-
RUBY_EXTERN VALUE cRubyPyFunction;
|
11
|
-
|
12
|
-
VALUE cRubyPyInstance;
|
13
|
-
|
14
|
-
VALUE rpInstanceFromPyObject(PyObject* pInst)
|
15
|
-
{
|
16
|
-
PObj* self;
|
17
|
-
PyObject *pClassDict,*pClass,*pInstDict;
|
18
|
-
VALUE rInstDict, rClassDict;
|
19
|
-
VALUE rInst = rb_class_new_instance(0, NULL, cRubyPyInstance);
|
20
|
-
|
21
|
-
Data_Get_Struct(rInst, PObj, self);
|
22
|
-
self->pObject = pInst;
|
23
|
-
|
24
|
-
pClass = PyObject_GetAttrString(pInst,"__class__");
|
25
|
-
pClassDict = PyObject_GetAttrString(pClass,"__dict__");
|
26
|
-
pInstDict = PyObject_GetAttrString(pInst,"__dict__");
|
27
|
-
|
28
|
-
Py_XINCREF(pClassDict);
|
29
|
-
Py_XINCREF(pInstDict);
|
30
|
-
|
31
|
-
rClassDict = rpObjectFromPyObject(pClassDict);
|
32
|
-
rInstDict = rpObjectFromPyObject(pInstDict);
|
33
|
-
|
34
|
-
rb_iv_set(rInst,"@pclassdict", rClassDict);
|
35
|
-
rb_iv_set(rInst,"@pinstdict", rInstDict);
|
36
|
-
|
37
|
-
return rInst;
|
38
|
-
}
|
39
|
-
|
40
|
-
static
|
41
|
-
VALUE rpInstanceSetAttr(VALUE self, VALUE args)
|
42
|
-
{
|
43
|
-
VALUE name, name_string, rClassDict, rInstDict;
|
44
|
-
|
45
|
-
char* cname;
|
46
|
-
|
47
|
-
PObj *pClassDict,*pInstDict,*pDict;
|
48
|
-
|
49
|
-
PyObject* pName;
|
50
|
-
|
51
|
-
name = rb_ary_shift(args);
|
52
|
-
name_string = rb_funcall(name, rb_intern("to_s"), 0);
|
53
|
-
|
54
|
-
rb_funcall(name_string, rb_intern("chop!"), 0);
|
55
|
-
|
56
|
-
if(!rpHasSymbol(self, name_string))
|
57
|
-
{
|
58
|
-
int argc;
|
59
|
-
VALUE* argv;
|
60
|
-
|
61
|
-
argc = RARRAY_LEN(args);
|
62
|
-
argv = ALLOC_N(VALUE, argc);
|
63
|
-
MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
|
64
|
-
return rb_call_super(argc, argv);
|
65
|
-
}
|
66
|
-
|
67
|
-
cname = STR2CSTR(name_string);
|
68
|
-
|
69
|
-
if((NUM2INT(rb_funcall(args, rb_intern("size"), 0)) == 1))
|
70
|
-
{
|
71
|
-
args = rb_ary_entry(args, 0);
|
72
|
-
}
|
73
|
-
|
74
|
-
rClassDict = rb_iv_get(self,"@pclassdict");
|
75
|
-
rInstDict = rb_iv_get(self,"@pinstdict");
|
76
|
-
|
77
|
-
Data_Get_Struct(rClassDict, PObj, pClassDict);
|
78
|
-
Data_Get_Struct(rInstDict, PObj, pInstDict);
|
79
|
-
|
80
|
-
pName = PyString_FromString(cname);
|
81
|
-
|
82
|
-
if(PyDict_Contains(pInstDict->pObject, pName))
|
83
|
-
{
|
84
|
-
pDict = pInstDict;
|
85
|
-
|
86
|
-
}
|
87
|
-
else
|
88
|
-
{
|
89
|
-
pDict = pClassDict;
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
Py_XDECREF(pName);
|
94
|
-
PyDict_SetItemString(pDict->pObject, STR2CSTR(name_string), rtopObject(args, 0));
|
95
|
-
|
96
|
-
return Qtrue;
|
97
|
-
}
|
98
|
-
|
99
|
-
//:nodoc:
|
100
|
-
static
|
101
|
-
VALUE rpInstanceDelegate(VALUE self, VALUE args)
|
102
|
-
{
|
103
|
-
VALUE name, name_string, rClassDict, result, rInstDict;
|
104
|
-
VALUE ret;
|
105
|
-
char* cname;
|
106
|
-
PObj *pClassDict,*pInstDict;
|
107
|
-
PyObject* pCalled;
|
108
|
-
|
109
|
-
if(rpSymbolIsSetter(args))
|
110
|
-
{
|
111
|
-
return rpInstanceSetAttr(self, args);
|
112
|
-
}
|
113
|
-
|
114
|
-
if(!rpHasSymbol(self, rb_ary_entry(args, 0)))
|
115
|
-
{
|
116
|
-
int argc;
|
117
|
-
VALUE* argv;
|
118
|
-
|
119
|
-
argc = RARRAY_LEN(args);
|
120
|
-
argv = ALLOC_N(VALUE, argc);
|
121
|
-
MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
|
122
|
-
return rb_call_super(argc, argv);
|
123
|
-
}
|
124
|
-
|
125
|
-
name = rb_ary_shift(args);
|
126
|
-
name_string = rb_funcall(name, rb_intern("to_s"), 0);
|
127
|
-
cname = STR2CSTR(name_string);
|
128
|
-
|
129
|
-
rClassDict = rb_iv_get(self,"@pclassdict");
|
130
|
-
rInstDict = rb_iv_get(self,"@pinstdict");
|
131
|
-
|
132
|
-
Data_Get_Struct(rClassDict, PObj, pClassDict);
|
133
|
-
Data_Get_Struct(rInstDict, PObj, pInstDict);
|
134
|
-
|
135
|
-
pCalled = PyDict_GetItemString(pInstDict->pObject, cname);
|
136
|
-
|
137
|
-
if(!pCalled)
|
138
|
-
{
|
139
|
-
pCalled = PyDict_GetItemString(pClassDict->pObject, cname);
|
140
|
-
}
|
141
|
-
|
142
|
-
Py_XINCREF(pCalled);
|
143
|
-
result = ptorObjectKeep(pCalled);
|
144
|
-
|
145
|
-
if(rb_obj_is_instance_of(result, cRubyPyFunction))
|
146
|
-
{
|
147
|
-
Py_XINCREF(rpObjectGetPyObject(self));
|
148
|
-
rb_ary_unshift(args, self);
|
149
|
-
ret = rpCall(pCalled, args);
|
150
|
-
return ret;
|
151
|
-
}
|
152
|
-
|
153
|
-
return result;
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
|
158
|
-
inline void Init_RubyPyInstance()
|
159
|
-
{
|
160
|
-
cRubyPyInstance = rb_define_class_under(mRubyPythonBridge,"RubyPyInstance", cRubyPyObject);
|
161
|
-
rb_define_method(cRubyPyInstance,"method_missing", rpInstanceDelegate,- 2);
|
162
|
-
}
|
163
|
-
|
164
|
-
|
@@ -1,160 +0,0 @@
|
|
1
|
-
#include "rp_module.h"
|
2
|
-
|
3
|
-
#include "rp_object.h"
|
4
|
-
#include "rp_function.h"
|
5
|
-
#include "rp_util.h"
|
6
|
-
|
7
|
-
VALUE cRubyPyModule;
|
8
|
-
|
9
|
-
RUBY_EXTERN VALUE mRubyPythonBridge;
|
10
|
-
RUBY_EXTERN VALUE cRubyPyFunction;
|
11
|
-
RUBY_EXTERN VALUE cRubyPyClass;
|
12
|
-
RUBY_EXTERN VALUE cRubyPyObject;
|
13
|
-
|
14
|
-
//static
|
15
|
-
//VALUE rpModuleCallFunction(VALUE self, VALUE func_name, VALUE args)
|
16
|
-
//{
|
17
|
-
// PyObject *pModule,*pFunc;
|
18
|
-
// VALUE rReturn;
|
19
|
-
//
|
20
|
-
// pModule = rpObjectGetPyObject(self);
|
21
|
-
//
|
22
|
-
// pFunc = rpGetFunctionWithModule(pModule, func_name);
|
23
|
-
// rReturn = rpCall(pFunc, args);
|
24
|
-
// Py_XDECREF(pFunc);
|
25
|
-
//
|
26
|
-
// return rReturn;
|
27
|
-
//
|
28
|
-
//}
|
29
|
-
|
30
|
-
//:nodoc:
|
31
|
-
static
|
32
|
-
VALUE rpModuleInit(VALUE self, VALUE mname)
|
33
|
-
{
|
34
|
-
PObj* cself;
|
35
|
-
VALUE rDict;
|
36
|
-
PyObject *pModuleDict;
|
37
|
-
|
38
|
-
Data_Get_Struct(self, PObj, cself);
|
39
|
-
cself->pObject = rpGetModule(mname);
|
40
|
-
|
41
|
-
pModuleDict = PyModule_GetDict(cself->pObject);
|
42
|
-
Py_XINCREF(pModuleDict);
|
43
|
-
|
44
|
-
rDict = rpObjectFromPyObject(pModuleDict);
|
45
|
-
|
46
|
-
rb_iv_set(self,"@pdict", rDict);
|
47
|
-
|
48
|
-
return self;
|
49
|
-
}
|
50
|
-
|
51
|
-
static
|
52
|
-
VALUE rpModuleSetAttr(VALUE self, VALUE args)
|
53
|
-
{
|
54
|
-
VALUE rDict;
|
55
|
-
PyObject* pDict;
|
56
|
-
|
57
|
-
VALUE mname = rb_ary_shift(args);
|
58
|
-
VALUE name_string = rb_funcall(mname, rb_intern("to_s"), 0);
|
59
|
-
|
60
|
-
//The method name ends with "=" because it is a setter.
|
61
|
-
//We must chop that off before we pass the string to python.
|
62
|
-
rb_funcall(name_string, rb_intern("chop!"), 0);
|
63
|
-
|
64
|
-
//The wrapped python object does not have method or attribute with the
|
65
|
-
//request named. Check for it in the Ruby superclass.
|
66
|
-
if(!rpHasSymbol(self, name_string))
|
67
|
-
{
|
68
|
-
int argc;
|
69
|
-
|
70
|
-
VALUE* argv;
|
71
|
-
argc = RARRAY_LEN(args);
|
72
|
-
|
73
|
-
argv = ALLOC_N(VALUE, argc);
|
74
|
-
MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
|
75
|
-
|
76
|
-
return rb_call_super(argc, argv);
|
77
|
-
}
|
78
|
-
|
79
|
-
if(NUM2INT(rb_funcall(args, rb_intern("size"), 0)) == 1)
|
80
|
-
{
|
81
|
-
args = rb_ary_entry(args, 0);
|
82
|
-
}
|
83
|
-
|
84
|
-
rDict = rb_iv_get(self,"@pdict");
|
85
|
-
|
86
|
-
pDict = rpObjectGetPyObject(rDict);
|
87
|
-
|
88
|
-
PyDict_SetItemString(pDict, STR2CSTR(name_string), rtopObject(args, 0));
|
89
|
-
|
90
|
-
return Qtrue;
|
91
|
-
}
|
92
|
-
|
93
|
-
//:nodoc:
|
94
|
-
VALUE rpModuleDelegate(VALUE self, VALUE args)
|
95
|
-
{
|
96
|
-
VALUE name, name_string, rDict, result;
|
97
|
-
VALUE ret;
|
98
|
-
PyObject *pCalled, *pDict;
|
99
|
-
|
100
|
-
if(rpSymbolIsSetter(args))
|
101
|
-
{
|
102
|
-
return rpModuleSetAttr(self, args);
|
103
|
-
}
|
104
|
-
|
105
|
-
// if(rpSymbolIsDoubleBang)
|
106
|
-
// {
|
107
|
-
// return rp_mod_attr_db(args);
|
108
|
-
// }
|
109
|
-
if(!rpHasSymbol(self, rb_ary_entry(args, 0)))
|
110
|
-
{
|
111
|
-
int argc;
|
112
|
-
|
113
|
-
VALUE *argv;
|
114
|
-
argc = RARRAY_LEN(args);
|
115
|
-
argv = ALLOC_N(VALUE, argc);
|
116
|
-
MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
|
117
|
-
return rb_call_super(argc, argv);
|
118
|
-
}
|
119
|
-
name = rb_ary_shift(args);
|
120
|
-
name_string = rb_funcall(name, rb_intern("to_s"), 0);
|
121
|
-
|
122
|
-
rDict = rb_iv_get(self,"@pdict");
|
123
|
-
|
124
|
-
pDict = rpObjectGetPyObject(rDict);
|
125
|
-
|
126
|
-
pCalled = PyDict_GetItemString(pDict, STR2CSTR(name_string));
|
127
|
-
Py_XINCREF(pCalled);
|
128
|
-
|
129
|
-
result = ptorObjectKeep(pCalled);
|
130
|
-
|
131
|
-
if(rb_obj_is_instance_of(result, cRubyPyFunction))
|
132
|
-
{
|
133
|
-
ret = rpCall(pCalled, args);
|
134
|
-
return ret;
|
135
|
-
}
|
136
|
-
else if(rb_obj_is_instance_of(result, cRubyPyClass)&&(rb_funcall(args, rb_intern("empty?"), 0) == Qfalse)&&PyCallable_Check(pCalled))
|
137
|
-
{
|
138
|
-
ret = rpCall(pCalled, args);
|
139
|
-
return ret;
|
140
|
-
}
|
141
|
-
|
142
|
-
return result;
|
143
|
-
|
144
|
-
}
|
145
|
-
|
146
|
-
|
147
|
-
/*
|
148
|
-
A wrapper class for Python Modules.
|
149
|
-
|
150
|
-
Methods calls are delegated to the equivalent Python methods / functions. Attribute references
|
151
|
-
return either the equivalent attribute converted to a native Ruby type, or wrapped reference
|
152
|
-
to a Python object. RubyPyModule instances should be created through the use of RubyPython.import.
|
153
|
-
|
154
|
-
*/
|
155
|
-
inline void Init_RubyPyModule()
|
156
|
-
{
|
157
|
-
cRubyPyModule = rb_define_class_under(mRubyPythonBridge,"RubyPyModule", cRubyPyObject);
|
158
|
-
rb_define_method(cRubyPyModule,"initialize", rpModuleInit, 1);
|
159
|
-
rb_define_method(cRubyPyModule,"method_missing", rpModuleDelegate,- 2);
|
160
|
-
}
|
@@ -1,194 +0,0 @@
|
|
1
|
-
#include "rp_object.h"
|
2
|
-
|
3
|
-
RUBY_EXTERN VALUE ePythonError;
|
4
|
-
RUBY_EXTERN VALUE mRubyPythonBridge;
|
5
|
-
RUBY_EXTERN VALUE cBlankObject;
|
6
|
-
|
7
|
-
VALUE cRubyPyObject;
|
8
|
-
|
9
|
-
|
10
|
-
static void rpObjectMark(PObj*);
|
11
|
-
static void rpObjectFree(PObj*);
|
12
|
-
static VALUE rpObjectAlloc(VALUE);
|
13
|
-
|
14
|
-
//Create a new RubyPyObject
|
15
|
-
static
|
16
|
-
VALUE rpObjectAlloc(VALUE klass)
|
17
|
-
{
|
18
|
-
PObj* self = ALLOC(PObj);
|
19
|
-
self->pObject = NULL;
|
20
|
-
|
21
|
-
return Data_Wrap_Struct(klass, rpObjectMark, rpObjectFree, self);
|
22
|
-
}
|
23
|
-
|
24
|
-
//Mark subsidiary objects for deletion
|
25
|
-
static
|
26
|
-
void rpObjectMark(PObj* self)
|
27
|
-
{}
|
28
|
-
|
29
|
-
//Delete a RubyPyObject
|
30
|
-
static
|
31
|
-
void rpObjectFree(PObj* self)
|
32
|
-
{
|
33
|
-
if(Py_IsInitialized() && self->pObject)
|
34
|
-
{
|
35
|
-
//Make sure we decrement the object count on our wrapped
|
36
|
-
//object before we free the ruby wrapper
|
37
|
-
Py_XDECREF(self->pObject);
|
38
|
-
}
|
39
|
-
free(self);
|
40
|
-
}
|
41
|
-
|
42
|
-
|
43
|
-
/*
|
44
|
-
Decreases the reference count on the object wrapped by this instance.
|
45
|
-
This is used for cleanup in RubyPython.stop. RubyPyObject instances automatically
|
46
|
-
decrease the reference count on their associated objects before they are garbage collected.
|
47
|
-
*/
|
48
|
-
static
|
49
|
-
VALUE rpObjectFreePobj(VALUE self)
|
50
|
-
{
|
51
|
-
PObj *cself;
|
52
|
-
|
53
|
-
Data_Get_Struct(self, PObj, cself);
|
54
|
-
|
55
|
-
if(Py_IsInitialized() && cself->pObject)
|
56
|
-
{
|
57
|
-
Py_XDECREF(cself->pObject);
|
58
|
-
cself->pObject = NULL;
|
59
|
-
return Qtrue;
|
60
|
-
}
|
61
|
-
else
|
62
|
-
{
|
63
|
-
cself->pObject = NULL;
|
64
|
-
}
|
65
|
-
|
66
|
-
return Qfalse;
|
67
|
-
}
|
68
|
-
|
69
|
-
//Fetchs the wrapped Python object from a RubyPyObject
|
70
|
-
PyObject* rpObjectGetPyObject(VALUE self)
|
71
|
-
{
|
72
|
-
PObj *cself;
|
73
|
-
|
74
|
-
Data_Get_Struct(self, PObj, cself);
|
75
|
-
|
76
|
-
if(!cself->pObject)
|
77
|
-
{
|
78
|
-
rb_raise(ePythonError,"RubyPython tried to access a freed object");
|
79
|
-
}
|
80
|
-
|
81
|
-
return cself->pObject;
|
82
|
-
}
|
83
|
-
|
84
|
-
|
85
|
-
//Creates a new RubyPyObject to wrap a python object
|
86
|
-
VALUE rpObjectFromPyObject
|
87
|
-
(PyObject* pObj)
|
88
|
-
{
|
89
|
-
PObj* self;
|
90
|
-
|
91
|
-
VALUE rObj = rb_class_new_instance(0, NULL, cRubyPyObject);
|
92
|
-
|
93
|
-
Data_Get_Struct(rObj, PObj, self);
|
94
|
-
|
95
|
-
self->pObject = pObj;
|
96
|
-
|
97
|
-
return rObj;
|
98
|
-
}
|
99
|
-
|
100
|
-
/*
|
101
|
-
Returns the name of the Python object which this instance wraps.
|
102
|
-
|
103
|
-
If it cannot determine a reasonable name it just gives up.
|
104
|
-
*/
|
105
|
-
static
|
106
|
-
VALUE rpObjectectGetName(VALUE self)
|
107
|
-
{
|
108
|
-
//It only makes sense to query a python object if the interpreter is running.
|
109
|
-
if(Py_IsInitialized())
|
110
|
-
{
|
111
|
-
PyObject *pObject,*pName,*pRepr;
|
112
|
-
VALUE rName;
|
113
|
-
|
114
|
-
pObject = rpObjectGetPyObject(self);
|
115
|
-
|
116
|
-
|
117
|
-
pName = PyObject_GetAttrString(pObject,"__name__");
|
118
|
-
|
119
|
-
if(!pName)
|
120
|
-
{
|
121
|
-
PyErr_Clear();
|
122
|
-
|
123
|
-
pName = PyObject_GetAttrString(pObject,"__class__");
|
124
|
-
pRepr = PyObject_Repr(pName);
|
125
|
-
rName = ptorString(pRepr);
|
126
|
-
Py_XDECREF(pRepr);
|
127
|
-
|
128
|
-
return rb_str_concat(rb_str_new2("An instance of "), rName);
|
129
|
-
if(!pName)
|
130
|
-
{
|
131
|
-
PyErr_Clear();
|
132
|
-
|
133
|
-
pName = PyObject_Repr(pObject);
|
134
|
-
|
135
|
-
if(!pName)
|
136
|
-
{
|
137
|
-
PyErr_Clear();
|
138
|
-
return rb_str_new2("__Unnameable__");
|
139
|
-
}
|
140
|
-
}
|
141
|
-
}
|
142
|
-
|
143
|
-
rName = ptorString(pName);
|
144
|
-
|
145
|
-
Py_XDECREF(pName);
|
146
|
-
|
147
|
-
return rName;
|
148
|
-
}
|
149
|
-
|
150
|
-
return rb_str_new2("__FREED__");
|
151
|
-
|
152
|
-
}
|
153
|
-
|
154
|
-
//Test to see the RubyPyObj has the supplied symbol as an attribute
|
155
|
-
int rpHasSymbol(VALUE self, VALUE symbol)
|
156
|
-
{
|
157
|
-
PObj *cself;
|
158
|
-
VALUE rName;
|
159
|
-
|
160
|
-
Data_Get_Struct(self, PObj, cself);
|
161
|
-
rName = rb_funcall(symbol, rb_intern("to_s"), 0);
|
162
|
-
|
163
|
-
if(PyObject_HasAttrString(cself->pObject, STR2CSTR(rName))) return 1;
|
164
|
-
|
165
|
-
return 0;
|
166
|
-
}
|
167
|
-
|
168
|
-
/* Tests whether the wrapped object will respond to the given method*/
|
169
|
-
VALUE rpRespondsTo(VALUE self, VALUE mname)
|
170
|
-
{
|
171
|
-
if(rpHasSymbol(self, mname))
|
172
|
-
{
|
173
|
-
return Qtrue;
|
174
|
-
}
|
175
|
-
|
176
|
-
return Qfalse;
|
177
|
-
}
|
178
|
-
|
179
|
-
/*
|
180
|
-
A wrapper class for Python objects that allows them to manipulated from within ruby.
|
181
|
-
|
182
|
-
Important wrapper functionality is found in the RubyPyModule, RubyPyClass, and RubyPyFunction
|
183
|
-
classes which wrap Python objects of similar names.
|
184
|
-
|
185
|
-
*/
|
186
|
-
inline void Init_RubyPyObject()
|
187
|
-
{
|
188
|
-
cRubyPyObject = rb_define_class_under(mRubyPythonBridge,"RubyPyObject", cBlankObject);
|
189
|
-
rb_define_alloc_func(cRubyPyObject, rpObjectAlloc);
|
190
|
-
rb_define_method(cRubyPyObject,"free_pobj", rpObjectFreePobj, 0);
|
191
|
-
rb_define_method(cRubyPyObject,"__name", rpObjectectGetName, 0);
|
192
|
-
rb_define_method(cRubyPyObject,"respond_to?", rpRespondsTo, 1);
|
193
|
-
|
194
|
-
}
|