Class: RubyPythonBridge::RubyPyModule

Inherits:
RubyPyObject show all
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

Methods inherited from RubyPyObject

#__name, #free_pobj, #inspect, #respond_to?

Constructor Details

#initialize(mname) ⇒ Object

:nodoc:



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/rubypython_bridge/rp_object.c', line 330

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:



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/rubypython_bridge/rp_object.c', line 413

VALUE rp_mod_delegate(VALUE self,VALUE args)
{
	VALUE name,name_string,rDict,result;
	VALUE ret;
	PObj *pDict;
	PyObject *pCalled;
	if(rp_equal(args))
	{
		return rp_mod_attr_set(self,args);
	}
	// if(rp_double_bang)
	// {
	// 	return rp_mod_attr_db(args);
	// }
	if(!rp_has_attr(self,rb_ary_entry(args,0)))
	{		
		int argc;
		
		VALUE *argv;
		argc=RARRAY_LEN(args);
		argv=ALLOC_N(VALUE,argc);
		MEMCPY(argv,RARRAY_PTR(args),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;
	
}