Skip to content

Commit

Permalink
Merge ecb4f03 into 5bede68
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Sep 26, 2015
2 parents 5bede68 + ecb4f03 commit 585952d
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 167 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: python
python:
- "2.7"
- "3.4"
- "pypy"
before_install:
# We need GraphViz
- "sudo apt-get install graphviz 2>&1 | tail -n 2"
Expand All @@ -13,4 +14,4 @@ install:
script:
- nosetests --with-coverage --cover-package=pygraphviz
after_success:
- coveralls
- coveralls
9 changes: 7 additions & 2 deletions pygraphviz/agraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ def clear(self):
directed = self.directed
gv.agclose(self.handle)
self.handle = gv.agraphnew(name, strict, directed)
self.graph_attr.handle = self.handle
self.node_attr.handle = self.handle
self.edge_attr.handle = self.handle

def close(self):
# may be useful to clean up graphviz data
Expand Down Expand Up @@ -1201,7 +1204,7 @@ def read(self, path):
self.handle = gv.agread(fh, None)
except ValueError:
raise DotError

except IOError:
print("IO error reading file")

Expand All @@ -1221,7 +1224,9 @@ def write(self, path=None):
gv.agwrite(self.handle, fh)
except IOError:
print("IO error writing file")

finally:
if hasattr(fh, 'close') and not hasattr(path, 'write'):
fh.close()

def string_nop(self):
"""Return a string (unicode) representation of graph in dot format."""
Expand Down
17 changes: 15 additions & 2 deletions pygraphviz/graphviz.i
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@ extern PyTypeObject PyIOBase_Type;
%}

%typemap(in) FILE* (int fd, PyObject *mode_obj, PyObject *mode_byte_obj, char *mode) {
%#if PY_VERSION_HEX >= 0x03000000
%#if PY_VERSION_HEX >= 0x03000000 || defined(PYPY_VERSION)
%#if !defined(PYPY_VERSION)
if (!PyObject_IsInstance($input, (PyObject *)&PyIOBase_Type)) {
PyErr_SetString(PyExc_TypeError, "not a file handle");
return NULL;
}
// work around to get hold of FILE*
fd = PyObject_AsFileDescriptor($input);
%#else
fd = PyObject_AsFileDescriptor($input);
if (fd < 0) {
PyErr_SetString(PyExc_TypeError, "not a file handle");
return NULL;
}
%#endif
mode_obj = PyObject_GetAttrString($input, "mode");
mode_byte_obj = PyUnicode_AsUTF8String(mode_obj);
%#if !defined(PYPY_VERSION)
mode_byte_obj = PyUnicode_AsUTF8String(mode_obj);
%#else
mode_byte_obj = mode_obj;
Py_INCREF(mode_byte_obj);
%#endif
mode = PyBytes_AsString(mode_byte_obj);
$1 = fdopen(fd, mode);
Py_XDECREF(mode_obj);
Expand Down
Loading

0 comments on commit 585952d

Please sign in to comment.