Skip to content

Commit

Permalink
python: Update wrapping code to use the cpython shim
Browse files Browse the repository at this point in the history
Shuffled a few things around as well.

This works for trying stuff out:
from wrappers import c_ray
  • Loading branch information
vkoskiv committed Dec 6, 2023
1 parent 8d4e3e2 commit ef121e5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 65 deletions.
6 changes: 3 additions & 3 deletions lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OBJS_driver=$(patsubst %.c, $(OBJDIR_driver)/%.o, $(SRCS_driver))

lib: $(BIN_lib)

pylib: wrappers/cray.so
pylib: wrappers/cray_wrap.so

$(OBJDIR_driver)/%.o: %.c $(OBJDIR_driver)
@mkdir -p '$(@D)'
Expand All @@ -32,6 +32,6 @@ $(LIB): $(OBJS_lib) $(OBJDIR_lib)
$(BIN_lib): $(LIB) $(OBJS_driver) $(OBJDIR_driver)
@echo "LD $@"
@$(CC) $(CFLAGS) $(OBJS_driver) $(LIB) -o $@ $(LDFLAGS)
wrappers/cray.so: $(LIB) wrappers/cray.c
wrappers/cray_wrap.so: $(LIB) wrappers/cray_wrap.c
@echo "Building Python module"
@$(CC) -shared $(CFLAGS) -fPIC `pkg-config --cflags python3` wrappers/cray.c $(LIB) -o $@
@$(CC) -shared $(CFLAGS) -fPIC `pkg-config --cflags python3` wrappers/cray_wrap.c $(LIB) -o $@
48 changes: 17 additions & 31 deletions wrappers/cr_renderer.py → wrappers/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
from contextlib import contextmanager
from enum import IntEnum

c_ray = ct.CDLL("lib/libc-ray.so")

c_ray.cr_get_version.restype = ct.c_char_p
def cr_get_version():
return c_ray.cr_get_version().decode()

c_ray.cr_get_git_hash.restype = ct.c_char_p
def cr_get_git_hash():
return c_ray.cr_get_git_hash().decode()
from . import cray_wrap as _lib

class cr_renderer(ct.Structure):
pass
Expand All @@ -31,13 +23,6 @@ class cr_bitmap(ct.Structure):
("height", ct.c_size_t),
]

c_ray.cr_renderer_render.restype = ct.POINTER(cr_bitmap)
c_ray.cr_renderer_render.argtypes = [ct.POINTER(cr_renderer)]
c_ray.cr_bitmap_free.argtypes = [ct.POINTER(cr_bitmap)]

c_ray.cr_new_renderer.restype = ct.POINTER(cr_renderer)
c_ray.cr_destroy_renderer.argtypes = [ct.POINTER(cr_renderer)]

class _cr_rparam(IntEnum):
# int
threads = 0
Expand All @@ -59,25 +44,17 @@ class _cr_rparam(IntEnum):
output_filetype = 15
node_list = 16

c_ray.cr_renderer_set_num_pref.restype = ct.c_bool
c_ray.cr_renderer_set_num_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int, ct.c_uint64]
def _r_set_num(ptr, param, value):
return c_ray.cr_renderer_set_num_pref(ptr, param, value)
return _lib.renderer_set_num_pref(ptr, param, value)

c_ray.cr_renderer_get_num_pref.restype = ct.c_uint64
c_ray.cr_renderer_get_num_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int]
def _r_get_num(ptr, param):
return c_ray.cr_renderer_get_num_pref(ptr, param)
return _lib.renderer_get_num_pref(ptr, param)

c_ray.cr_renderer_set_str_pref.restype = ct.c_bool
c_ray.cr_renderer_set_str_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int, ct.c_char_p]
def _r_set_str(ptr, param, value):
return c_ray.cr_renderer_set_str_pref(ptr, param, value.encode())
return _lib.renderer_set_str_pref(ptr, param, value)

c_ray.cr_renderer_get_str_pref.restype = ct.c_char_p
c_ray.cr_renderer_get_str_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int]
def _r_get_str(ptr, param):
return c_ray.cr_renderer_get_str_pref(ptr, param).decode()
return _lib.renderer_get_str_pref(ptr, param)

class _pref:
def __init__(self, r_ptr):
Expand Down Expand Up @@ -185,14 +162,23 @@ def _set_node_list(self, value):
_r_set_str(self.r_ptr, _cr_rparam.node_list, value)
node_list = property(_get_node_list, _set_node_list, None, "")

class _version:
def _get_semantic(self):
return _lib.get_version()
semantic = property(_get_semantic, None, None, "")
def _get_hash(self):
return _lib.get_git_hash()
githash = property(_get_hash, None, None, "")

version = _version()

class renderer:
def __init__(self):
self.obj_ptr = c_ray.cr_new_renderer()
self.obj_ptr = _lib.new_renderer()
self.prefs = _pref(self.obj_ptr)

def close(self):
c_ray.cr_destroy_renderer(self.obj_ptr)

del(self.obj_ptr)

@classmethod
def from_param(cls, param):
Expand Down
3 changes: 0 additions & 3 deletions wrappers/cray.py

This file was deleted.

6 changes: 3 additions & 3 deletions wrappers/cray.c → wrappers/cray_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ static PyMethodDef cray_methods[] = {
{ NULL, NULL, 0, NULL }
};

static struct PyModuleDef cray = {
static struct PyModuleDef cray_wrap = {
PyModuleDef_HEAD_INIT,
"cray",
NULL,
-1,
cray_methods
};

PyMODINIT_FUNC PyInit_cray(void) {
return PyModule_Create(&cray);
PyMODINIT_FUNC PyInit_cray_wrap(void) {
return PyModule_Create(&cray_wrap);
}
11 changes: 0 additions & 11 deletions wrappers/meta.py

This file was deleted.

14 changes: 0 additions & 14 deletions wrappers/test.py

This file was deleted.

0 comments on commit ef121e5

Please sign in to comment.