<commit_before>#include "rp_module.h"

#include "rp_object.h"
#include "rp_function.h"

VALUE cRubyPyModule;

RUBY_EXTERN VALUE mRubyPythonBridge;
RUBY_EXTERN VALUE cRubyPyFunction;
RUBY_EXTERN VALUE cRubyPyClass;
RUBY_EXTERN VALUE cRubyPyObject;

static
VALUE rpModuleCallFunction(VALUE self, VALUE func_name, VALUE args)
{
	PyObject *pModule,*pFunc;
	VALUE rReturn;
	
	pModule = rpObjectGetPyObject(self);
	
	pFunc = rpGetFunctionWithModule(pModule, func_name);
	rReturn = rpCall(pFunc, args);
	Py_XDECREF(pFunc);
	
	return rReturn;
	
}

//:nodoc:
static
VALUE rpModuleInit(VALUE self, VALUE mname)
{
	PObj* cself;
	VALUE rDict;
	PyObject *pModuleDict;
	
	Data_Get_Struct(self, PObj, cself);
	cself->pObject = rpGetModule(mname);
	
	pModuleDict = PyModule_GetDict(cself->pObject);
	Py_XINCREF(pModuleDict);
	
	rDict = rpObjectFromPyObject
(pModuleDict);
	
	rb_iv_set(self,"@pdict", rDict);
	return self;
}

static
VALUE rpModuleSetAttr(VALUE self, VALUE args)
{
	VALUE rDict;
	PObj *pDict;
	VALUE mname = rb_ary_shift(args);
	VALUE name_string = rb_funcall(mname, rb_intern("to_s"), 0);
	
	rb_funcall(name_string, rb_intern("chop!"), 0);
	
	if(!rpHasSymbol(self, name_string))
	{		
		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);
	}
	
	if(NUM2INT(rb_funcall(args, rb_intern("size"), 0)) == 1)
	{
		args = rb_ary_entry(args, 0);
	}
	
	rDict = rb_iv_get(self,"@pdict");
	Data_Get_Struct(rDict, PObj, pDict);
	PyDict_SetItemString(pDict->pObject, STR2CSTR(name_string), rtopObject(args, 0));
	
	return Qtrue;
}

//:nodoc:
VALUE rpModuleDelegate(VALUE self, VALUE args)
{
	VALUE name, name_string, rDict, result;
	VALUE ret;
	PObj *pDict;
	PyObject *pCalled;
	
	if(rpSymbolIsSetter(args))
	{
		return rpModuleSetAttr(self, args);
	}
	
	// if(rpSymbolIsDoubleBang)
	// {
	// 	return rp_mod_attr_db(args);
	// }
	if(!rpHasSymbol(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 = ptorObjectKeep(pCalled);
	
	if(rb_obj_is_instance_of(result, cRubyPyFunction))
	{<commit_msg>Fixed small bug in module code.<commit_after>#include "rp_module.h"

#include "rp_object.h"
#include "rp_function.h"

VALUE cRubyPyModule;

RUBY_EXTERN VALUE mRubyPythonBridge;
RUBY_EXTERN VALUE cRubyPyFunction;
RUBY_EXTERN VALUE cRubyPyClass;
RUBY_EXTERN VALUE cRubyPyObject;

static
VALUE rpModuleCallFunction(VALUE self, VALUE func_name, VALUE args)
{
	PyObject *pModule,*pFunc;
	VALUE rReturn;
	
	pModule = rpObjectGetPyObject(self);
	
	pFunc = rpGetFunctionWithModule(pModule, func_name);
	rReturn = rpCall(pFunc, args);
	Py_XDECREF(pFunc);
	
	return rReturn;
	
}

//:nodoc:
static
VALUE rpModuleInit(VALUE self, VALUE mname)
{
	PObj* cself;
	VALUE rDict;
	PyObject *pModuleDict;
	
	Data_Get_Struct(self, PObj, cself);
	cself->pObject = rpGetModule(mname);
	
	pModuleDict = PyModule_GetDict(cself->pObject);
	Py_XINCREF(pModuleDict);
	
	rDict = rpObjectFromPyObject(pModuleDict);
	
	rb_iv_set(self,"@pdict", rDict);
	
	return self;
}

static
VALUE rpModuleSetAttr(VALUE self, VALUE args)
{
	VALUE rDict;
	PyObject* pDict;
	
	VALUE mname = rb_ary_shift(args);
	VALUE name_string = rb_funcall(mname, rb_intern("to_s"), 0);
	
	//The method name ends with "=" because it is a setter.
	//We must chop that off before we pass the string to python.
	rb_funcall(name_string, rb_intern("chop!"), 0);
	
	//The wrapped python object does not have method or attribute with the
	//request named. Check for it in the Ruby superclass.
	if(!rpHasSymbol(self, name_string))
	{		
		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);
	}
	
	if(NUM2INT(rb_funcall(args, rb_intern("size"), 0)) == 1)
	{
		args = rb_ary_entry(args, 0);
	}
	
	rDict = rb_iv_get(self,"@pdict");
	
	pDict = rpObjectGetPyObject(rDict);
	
	PyDict_SetItemString(pDict, STR2CSTR(name_string), rtopObject(args, 0));
	
	return Qtrue;
}

//:nodoc:
VALUE rpModuleDelegate(VALUE self, VALUE args)
{
	VALUE name, name_string, rDict, result;
	VALUE ret;
	PyObject *pCalled, *pDict;
	
	if(rpSymbolIsSetter(args))
	{
		return rpModuleSetAttr(self, args);
	}
	
	// if(rpSymbolIsDoubleBang)
	// {
	// 	return rp_mod_attr_db(args);
	// }
	if(!rpHasSymbol(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");
	
	pDict = rpObjectGetPyObject(rDict);
	
	pCalled = PyDict_GetItemString(pDict, STR2CSTR(name_string));
	Py_XINCREF(pCalled);
	
	result = ptorObjectKeep(pCalled);
	
	if(rb_obj_is_instance_of(result, cRubyPyFunction))
	{
{'fraction_non_alphanumeric': 0.12776412776412777, 'fraction_numerical': 0.004158004158004158, 'mean_word_length': 5.1678321678321675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4718334', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2131, 'n_words': 424}