diff --git a/kitty/graphics.c b/kitty/graphics.c index 0bd114623..14c3e652a 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -445,7 +445,7 @@ image_as_dict(Image *img) { } -#define W(x) static PyObject* py##x(GraphicsManager *self, PyObject *args) +#define W(x) static PyObject* py##x(GraphicsManager UNUSED *self, PyObject *args) #define PA(fmt, ...) if(!PyArg_ParseTuple(args, fmt, __VA_ARGS__)) return NULL; W(image_for_client_id) { @@ -456,10 +456,28 @@ W(image_for_client_id) { return image_as_dict(img); } +W(shm_open) { + char *name; + PA("s", &name); + int fd = shm_open(name, O_CREAT | O_RDWR, S_IROTH | S_IRUSR | S_IRGRP | S_IWUSR); + if (fd == -1) { PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); return NULL; } + return PyLong_FromLong(fd); +} + +W(shm_unlink) { + char *name; + PA("s", &name); + int ret = shm_unlink(name); + if (ret == -1) { PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); return NULL; } + Py_RETURN_NONE; +} + #define M(x, va) {#x, (PyCFunction)py##x, va, ""} static PyMethodDef methods[] = { M(image_for_client_id, METH_O), + M(shm_open, METH_VARARGS), + M(shm_unlink, METH_VARARGS), {NULL} /* Sentinel */ }; diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index bc8259433..49f54c01c 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -12,7 +12,7 @@ from kitty.fast_data_types import parse_bytes from . import BaseTest -def img_path(name): +def relpath(name): base = os.path.dirname(os.path.abspath(__file__)) return os.path.join(base, name) @@ -86,3 +86,11 @@ class TestGraphics(BaseTest): f.seek(0), f.truncate(), f.write(compressed_random_data), f.flush() sl(f.name, s=24, v=32, t='t', o='z', expecting_data=random_data) self.assertRaises(FileNotFoundError, f.close) # check that file was deleted + + # Test loading from POSIX SHM + name = '/kitty-test-shm' + fd = g.shm_open(name) + f = os.fdopen(fd, 'wb') + f.write(random_data), f.flush(), f.close() + sl(name, s=24, v=32, t='s', expecting_data=random_data) + self.assertRaises(FileNotFoundError, g.shm_unlink, name) # check that file was deleted