8000 another try at fixinig the function cast issue · libvips/pyvips@2903bf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2903bf4

Browse files
committed
another try at fixinig the function cast issue
it seems cffi in api mode cannot cast function pointers ... try some hackery with cpp instead
1 parent a3698a4 commit 2903bf4

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

pyvips/decls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def cdefs(features):
5353
void* g_malloc (size_t size);
5454
void g_free (void* data);
5555
56+
// not what's in the headers, but see the comment in pyvips_build.py
57+
int vips_free (void* a, void* b);
58+
5659
void vips_leak_set (int leak);
5760
5861
char* vips_path_filename7 (const char* path);

pyvips/gvalue.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ def set(self, value):
188188
# can own
189189
memory = glib_lib.g_malloc(len(value))
190190
ffi.memmove(memory, value, len(value))
191-
free_fn = ffi.cast('VipsCallbackFn', glib_lib.g_free)
192-
193191
vips_lib.vips_value_set_blob(self.gvalue,
194-
free_fn, memory, len(value))
192+
vips_lib.vips_free, memory, len(value))
195193
else:
196194
raise Error('unsupported gtype for set {0}, fundamental {1}'.
197195
format(type_name(gtype), type_name(fundamental)))
@@ -269,7 +267,7 @@ def get(self):
269267
elif gtype == GValue.blob_type:
270268
psize = ffi.new('size_t *')
271269
array = vips_lib.vips_value_get_blob(self.gvalue, psize)
272-
buf = ffi.cast("char*", array)
270+
buf = ffi.cast('char*', array)
273271

274272
result = ffi.unpack(buf, psize[0])
275273
else:

pyvips/pyvips_build.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@
1010

1111
ffibuilder = FFI()
1212

13+
# this is very hacky ... cffi in API mode won't let us cast function pointers,
14+
# so we can't pass vips_free() as a vipsCallbackFn, which we need to be able to
15+
# do when we set a blob
16+
#
17+
# to fix this, we rename vips_free during the vips header load as
18+
# real_vips_free, then declare a fake type ourselves that decl.py then hooks up
19+
# to
20+
1321
ffibuilder.set_source("_libvips",
1422
r"""
23+
#define vips_free real_vips_free
1524
#include <vips/vips.h>
25+
#undef vips_free
26+
27+
extern VipsCallbackFn vips_free;
1628
""",
1729
**pkgconfig.parse('vips'))
1830

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@
2121
setup_deps = [
2222
'cffi>=1.0.0',
2323
'pytest-runner',
24-
'pkgconfig'
24+
'pkgconfig',
2525
]
2626

2727
install_deps = [
2828
'cffi>=1.0.0',
29-
'pkgconfig'
29+
'pkgconfig',
3030
]
3131

3232
test_deps = [
33+
'cffi>=1.0.0',
3334
'pytest',
3435
'pytest-catchlog',
35-
'pytest-flake8'
36+
'pytest-flake8',
3637
]
3738

3839
extras = {
3940
'test': test_deps,
40-
'doc': ['sphinx', 'sphinx_rtd_theme']
41+
'doc': ['sphinx', 'sphinx_rtd_theme'],
4142
}
4243

4344
import sys

0 commit comments

Comments
 (0)
0