Class: RubyPythonBridge::RubyPyModule
- Inherits:
-
RubyPyObject
- Object
- RubyPyObject
- RubyPythonBridge::RubyPyModule
- Defined in:
- lib/rubypython/wrapper_extensions.rb,
ext/rubypython_bridge/rp_object.c
Overview
A wrapper class for Python Modules.
Methods calls are delegated to the equivalent Python methods/functions. Attribute references return either the equivalent attribute converted to a native Ruby type, or wrapped reference to a Python object. RubyPyModule instances should be created through the use of RubyPython.import.
Instance Method Summary collapse
-
#initialize(mname) ⇒ Object
constructor
:nodoc:.
-
#method_missing(args) ⇒ Object
:nodoc:.
Methods inherited from RubyPyObject
#__name, #free_pobj, #inspect, #respond_to?
Constructor Details
#initialize(mname) ⇒ Object
:nodoc:
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'ext/rubypython_bridge/rp_object.c', line 233
VALUE rp_mod_init(VALUE self, VALUE mname)
{
PObj* cself;
Data_Get_Struct(self,PObj,cself);
cself->pObject=rp_get_module(mname);
VALUE rDict;
PyObject *pModuleDict;
pModuleDict=PyModule_GetDict(cself->pObject);
Py_XINCREF(pModuleDict);
rDict=rp_obj_from_pyobject(pModuleDict);
rb_iv_set(self,"@pdict",rDict);
return self;
}
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(args) ⇒ Object
:nodoc:
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'ext/rubypython_bridge/rp_object.c', line 272
VALUE rp_mod_delegate(VALUE self,VALUE args)
{
VALUE name,name_string,rDict,result;
VALUE ret;
PObj *pDict;
PyObject *pCalled;
if(!rp_has_attr(self,rb_ary_entry(args,0)))
{
int argc;
VALUE *argv;
argc=RARRAY(args)->len;
argv=ALLOC_N(VALUE,argc);
MEMCPY(argv,RARRAY(args)->ptr,VALUE,argc);
return rb_call_super(argc,argv);
}
name=rb_ary_shift(args);
name_string=rb_funcall(name,rb_intern("to_s"),0);
rDict=rb_iv_get(self,"@pdict");
Data_Get_Struct(rDict,PObj,pDict);
pCalled=PyDict_GetItemString(pDict->pObject,STR2CSTR(name_string));
Py_XINCREF(pCalled);
result=ptor_obj_no_destruct(pCalled);
if(rb_obj_is_instance_of(result,cRubyPyFunction))
{
ret=rp_call_func(pCalled,args);
return ret;
}
else if(rb_obj_is_instance_of(result,cRubyPyClass)&&(rb_funcall(args,rb_intern("empty?"),0)==Qfalse)&&PyCallable_Check(pCalled))
{
ret=rp_call_func(pCalled,args);
return ret;
}
return result;
}
|