Sha256: 335a07db7e9b801655ad9bc4b11343cbea5b70d7cb1130eeaf8516a6498dfa3f

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

#include "rubypython_bridge.h"

VALUE mRubyPythonBridge;
extern VALUE cRubyPyObject;
extern VALUE cRubyPyModule;
extern VALUE cRubyPyClass;

static VALUE func_with_module(VALUE self, VALUE args)
{
	int started_here=safe_start();
	VALUE module,func,return_val;
	if(RARRAY(args)->len<2) return Qfalse;
	module=rb_ary_shift(args);
	func=rb_ary_shift(args);
	return_val=rp_call_func_with_module_name(module,func,args);
	safe_stop(started_here);
	return return_val;
}

static VALUE rp_import_module(VALUE self,VALUE module)
{
	VALUE instance=rb_class_instance_new(1,&module,cRubyPyModule);
	return instance;
}



/*
* call-seq: import(modname)
* 
* imports a python file_module using the interpreter and returns a ruby wrapper

*/
static VALUE rp_import(VALUE self,VALUE mname)
{
	return rb_class_new_instance(1,&mname,cRubyPyModule);
}

static VALUE rp_python_block(VALUE self)
{
	rb_funcall(self,rb_intern("start"),0);
	rb_obj_instance_eval(0,NULL,self);
	rb_funcall(self,rb_intern("stop"),0);
	
}



/*
* call-seq: start()
*
* Starts the python interpreter
* 	RubyPythonBridge.start
*/
VALUE rp_start(VALUE self)
{
	

	if(Py_IsInitialized())
	{
		return Qfalse;
	}
	Py_Initialize();
	return Qtrue;
}

VALUE rp_stop(VALUE self)
{
	
	if(Py_IsInitialized())
	{
		Py_Finalize();
		return Qtrue;
	}
	return Qfalse;
	
}


/*
* Module containing an interface to the the python interpreter.
*
* Use RubyPython instead.
*/

void Init_rubypython_bridge()
{
	mRubyPythonBridge=rb_define_module("RubyPythonBridge");
	rb_define_module_function(mRubyPythonBridge,"func",func_with_module,-2);
	rb_define_module_function(mRubyPythonBridge,"import_module",rp_import_module,1); 
	rb_define_module_function(mRubyPythonBridge,"start",rp_start,0);
	rb_define_module_function(mRubyPythonBridge,"stop",rp_stop,0);
	rb_define_module_function(mRubyPythonBridge,"run",rp_python_block,0);
	rb_define_module_function(mRubyPythonBridge,"import",rp_import,1);
	Init_RubyPyObject();
	Init_RubyPyModule();
	Init_RubyPyClass();
	Init_RubyPyFunction();
	Init_RubyPyError();
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubypython-0.2.0 ext/rubypython_bridge/rubypython_bridge.c
rubypython-0.2.1 ext/rubypython_bridge/rubypython_bridge.c