@@ -10,10 +10,7 @@ int check_main_thread(void) {
1010 return 0 ;
1111 }
1212
13- unsigned long current_thread_id ;
14- current_thread_id = (unsigned long )PyThread_get_thread_ident ();
15-
16- if (current_thread_id != g_main_thread_id ) {
13+ if ((unsigned long )PyThread_get_thread_ident () != g_main_thread_id ) {
1714 PyErr_Format (PyExc_RuntimeError ,
1815 "Rayforce runtime can not be called from other threads than "
1916 "the one where it was initialized from" );
@@ -23,58 +20,58 @@ int check_main_thread(void) {
2320 return 1 ;
2421}
2522
26- // Initialize runtime (FFI function)
23+ // GENERIC UTILS
2724PyObject * raypy_init_runtime (PyObject * self , PyObject * args ) {
2825 (void )self ;
29- (void )args ; // Suppress unused parameter warning
26+ (void )args ;
3027
3128 if (g_runtime != NULL ) {
32- // Runtime already initialized
33- Py_RETURN_NONE ;
29+ PyErr_SetString ( PyExc_RuntimeError , " Runtime already initialized" );
30+ return NULL ;
3431 }
3532
36- char * argv [] = {"raypy" , "-r" , "0" , NULL };
37- g_runtime = runtime_create (3 , argv );
38- if (g_runtime == NULL ) {
33+ char * argv [] = {"py" , "-r" , "0" , NULL };
34+ if (runtime_create (3 , argv ) == NULL ) {
3935 PyErr_SetString (PyExc_RuntimeError , "Failed to initialize Rayforce" );
4036 return NULL ;
4137 }
4238
43- if (g_main_thread_id == 0 )
44- g_main_thread_id = (unsigned long )PyThread_get_thread_ident ();
45-
39+ g_main_thread_id = (unsigned long )PyThread_get_thread_ident ();
4640 Py_RETURN_NONE ;
4741}
42+ PyObject * raypy_wrap_ray_object (obj_p ray_obj ) {
43+ if (ray_obj == NULL ) {
44+ PyErr_SetString (PyExc_RuntimeError , "Rayforce object can't be null" );
45+ return NULL ;
46+ }
47+
48+ RayObject * result = (RayObject * )RayObjectType .tp_alloc (& RayObjectType , 0 );
49+ if (result != NULL )
50+ result -> obj = ray_obj ;
51+ return (PyObject * )result ;
52+ }
53+ // --
54+
55+ static void RayObject_dealloc (RayObject * self ) {
56+ if (self -> obj != NULL && self -> obj != NULL_OBJ )
57+ drop_obj (self -> obj );
58+ Py_TYPE (self )-> tp_free ((PyObject * )self );
59+ }
4860
4961PyTypeObject RayObjectType = {
5062 PyVarObject_HEAD_INIT (NULL , 0 ).tp_name = "_rayforce_c.RayObject" ,
5163 .tp_basicsize = sizeof (RayObject ),
5264 .tp_itemsize = 0 ,
53- .tp_dealloc = NULL , // Will be set below
65+ .tp_dealloc = ( destructor ) RayObject_dealloc ,
5466 .tp_flags = Py_TPFLAGS_DEFAULT ,
5567 .tp_doc = "RayObject objects" ,
56- .tp_methods = NULL , // Will be set below
68+ .tp_methods = NULL ,
5769 .tp_new = PyType_GenericNew ,
5870};
5971
60- static void RayObject_dealloc (RayObject * self ) {
61- if (self -> obj != NULL && self -> obj != NULL_OBJ )
62- drop_obj (self -> obj );
63- Py_TYPE (self )-> tp_free ((PyObject * )self );
64- }
65-
6672PyMODINIT_FUNC PyInit__rayforce_c (void );
6773
68- static struct PyModuleDef rayforce_module = {
69- PyModuleDef_HEAD_INIT ,
70- .m_name = "_rayforce_c" ,
71- .m_doc = "Python C API bus to Rayforce" ,
72- .m_size = -1 ,
73- .m_methods = NULL , // Will be set in PyInit__rayforce_c
74- };
75-
76- static PyMethodDef module_methods [] = {
77- // Constructors
74+ static PyMethodDef rayforce_methods [] = {
7875 {"init_i16" , raypy_init_i16 , METH_VARARGS , "Create a new i16 object" },
7976 {"init_i32" , raypy_init_i32 , METH_VARARGS , "Create a new i32 object" },
8077 {"init_i64" , raypy_init_i64 , METH_VARARGS , "Create a new i64 object" },
@@ -99,8 +96,6 @@ static PyMethodDef module_methods[] = {
9996 "Create a new dictionary object" },
10097 {"init_vector" , raypy_init_vector , METH_VARARGS ,
10198 "Create a new vector object" },
102-
103- // Readers
10499 {"read_i16" , raypy_read_i16 , METH_VARARGS , "Read i16 value from object" },
105100 {"read_i32" , raypy_read_i32 , METH_VARARGS , "Read i32 value from object" },
106101 {"read_i64" , raypy_read_i64 , METH_VARARGS , "Read i64 value from object" },
@@ -117,40 +112,26 @@ static PyMethodDef module_methods[] = {
117112 {"read_timestamp" , raypy_read_timestamp , METH_VARARGS ,
118113 "Read timestamp value from object" },
119114 {"read_guid" , raypy_read_guid , METH_VARARGS , "Read GUID value from object" },
120-
121- // Table operations
122115 {"table_keys" , raypy_table_keys , METH_VARARGS , "Get table keys" },
123116 {"table_values" , raypy_table_values , METH_VARARGS , "Get table values" },
124117 {"repr_table" , raypy_repr_table , METH_VARARGS , "Format table" },
125-
126- // Dictionary operations
127118 {"dict_keys" , raypy_dict_keys , METH_VARARGS , "Get dictionary keys" },
128119 {"dict_values" , raypy_dict_values , METH_VARARGS , "Get dictionary values" },
129120 {"dict_get" , raypy_dict_get , METH_VARARGS , "Get value from dictionary" },
130-
131- // Vector operations
132121 {"at_idx" , raypy_at_idx , METH_VARARGS , "Get element at index" },
133122 {"insert_obj" , raypy_insert_obj , METH_VARARGS , "Insert object at index" },
134123 {"push_obj" , raypy_push_obj , METH_VARARGS ,
135124 "Push object to the end of iterable" },
136125 {"set_obj" , raypy_set_obj , METH_VARARGS , "Set object at index" },
137- {"fill_vector" , raypy_fill_vector , METH_VARARGS ,
138- "Fill vector from Python list (bulk operation)" },
139- {"fill_list" , raypy_fill_list , METH_VARARGS ,
140- "Fill list from Python list (bulk operation)" },
141-
142- // Misc operations
143126 {"get_obj_length" , raypy_get_obj_length , METH_VARARGS , "Get object length" },
144127 {"eval_str" , raypy_eval_str , METH_VARARGS , "Evaluate string expression" },
145128 {"get_error_obj" , raypy_get_error_obj , METH_VARARGS , "Get error object" },
146129 {"binary_set" , raypy_binary_set , METH_VARARGS ,
147130 "Set value to symbol or file" },
148- {"env_get_internal_function_by_name" ,
149- raypy_env_get_internal_function_by_name , METH_VARARGS ,
150- "Get internal function by name" },
151- {"env_get_internal_name_by_function" ,
152- raypy_env_get_internal_name_by_function , METH_VARARGS ,
153- "Get internal function name" },
131+ {"env_get_internal_fn_by_name" , raypy_env_get_internal_fn_by_name ,
132+ METH_VARARGS , "Get internal function by name" },
133+ {"env_get_internal_name_by_fn" , raypy_env_get_internal_name_by_fn ,
134+ METH_VARARGS , "Get internal function name" },
154135 {"eval_obj" , raypy_eval_obj , METH_VARARGS , "Evaluate object" },
155136 {"loadfn_from_file" , raypy_loadfn , METH_VARARGS ,
156137 "Load function from shared library" },
@@ -159,35 +140,30 @@ static PyMethodDef module_methods[] = {
159140 {"set_obj_attrs" , raypy_set_obj_attrs , METH_VARARGS ,
160141 "Set object attributes" },
161142 {"get_obj_type" , raypy_get_obj_type , METH_VARARGS , "Get object type" },
162-
163- // Database operations
164- {"select" , raypy_select , METH_VARARGS , "Perform SELECT query" },
165143 {"update" , raypy_update , METH_VARARGS , "Perform UPDATE query" },
166144 {"insert" , raypy_insert , METH_VARARGS , "Perform INSERT query" },
167145 {"upsert" , raypy_upsert , METH_VARARGS , "Perform UPSERT query" },
168-
169- // IO operations
170146 {"hopen" , raypy_hopen , METH_VARARGS , "Open file or socket handle" },
171147 {"hclose" , raypy_hclose , METH_VARARGS , "Close file or socket handle" },
172148 {"write" , raypy_write , METH_VARARGS , "Write data to file or socket" },
173-
174149 {"init_runtime" , raypy_init_runtime , METH_VARARGS ,
175150 "Initialize Rayforce runtime" },
176151
177152 {NULL , NULL , 0 , NULL }};
178153
154+ static struct PyModuleDef rayforce_module = {
155+ PyModuleDef_HEAD_INIT , .m_name = "_rayforce_c" ,
156+ .m_doc = "Python C API bus to Rayforce" , .m_size = -1 ,
157+ .m_methods = rayforce_methods };
158+
179159static RayObject * g_null_obj = NULL ;
180160
181161PyMODINIT_FUNC PyInit__rayforce_c (void ) {
182162 PyObject * m ;
183163
184- // Initialize RayObjectType
185- RayObjectType .tp_dealloc = (destructor )RayObject_dealloc ;
186-
187164 if (PyType_Ready (& RayObjectType ) < 0 )
188165 return NULL ;
189166
190- rayforce_module .m_methods = module_methods ;
191167 m = PyModule_Create (& rayforce_module );
192168 if (m == NULL )
193169 return NULL ;
0 commit comments