Test for image loading from shared memory

This commit is contained in:
Kovid Goyal 2017-09-28 16:11:28 +05:30
parent dfca991173
commit 03dd6e0490
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 2 deletions

View File

@ -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 */
};

View File

@ -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