Commit 4a61be54 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Provide fallback case in graph_draw() function when open_memstream is not available

parent 26cce6a0
...@@ -44,6 +44,7 @@ import time ...@@ -44,6 +44,7 @@ import time
import warnings import warnings
import ctypes import ctypes
import ctypes.util import ctypes.util
import tempfile
from .. import _degree, _prop, PropertyMap, _check_prop_vector,\ from .. import _degree, _prop, PropertyMap, _check_prop_vector,\
_check_prop_scalar, _check_prop_writable, group_vector_property,\ _check_prop_scalar, _check_prop_writable, group_vector_property,\
ungroup_vector_property, GraphView ungroup_vector_property, GraphView
...@@ -66,7 +67,8 @@ except ImportError: ...@@ -66,7 +67,8 @@ except ImportError:
try: try:
libname = ctypes.util.find_library("c") libname = ctypes.util.find_library("c")
libc = ctypes.CDLL(libname) libc = ctypes.CDLL(libname)
libc.open_memstream.restype = ctypes.POINTER(ctypes.c_char) if hasattr(libc, "open_memstream"):
libc.open_memstream.restype = ctypes.POINTER(ctypes.c_char)
except OSError: except OSError:
pass pass
...@@ -498,14 +500,21 @@ def graph_draw(g, pos=None, size=(15, 15), pin=False, layout=None, maxiter=None, ...@@ -498,14 +500,21 @@ def graph_draw(g, pos=None, size=(15, 15), pin=False, layout=None, maxiter=None,
if return_string: if return_string:
if output_format == "auto": if output_format == "auto":
output_format = "png" output_format = "png"
buf = ctypes.c_char_p() if hasattr(libc, "open_memstream"):
buf_len = ctypes.c_size_t() buf = ctypes.c_char_p()
fstream = libc.open_memstream(ctypes.byref(buf), buf_len = ctypes.c_size_t()
ctypes.byref(buf_len)) fstream = libc.open_memstream(ctypes.byref(buf),
libgv.gvRender(gvc, gvg, output_format, fstream) ctypes.byref(buf_len))
libc.fclose(fstream) libgv.gvRender(gvc, gvg, output_format, fstream)
data = copy.copy(ctypes.string_at(buf, buf_len.value)) libc.fclose(fstream)
libc.free(buf) data = copy.copy(ctypes.string_at(buf, buf_len.value))
libc.free(buf)
else:
# write to temporary file, if open_memstream is not available
output = tempfile.mkstemp()[1]
libgv.gvRenderFilename(gvc, gvg, output_format, output)
data = open(output).read()
os.remove(output)
else: else:
if output_format == "auto": if output_format == "auto":
if output == "": if output == "":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment