diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c index ba933ee76..fd6c003d9 100644 --- a/contrib/py-bindings/gfmodule.c +++ b/contrib/py-bindings/gfmodule.c @@ -3,6 +3,7 @@ // #include +#include #include "pygf.h" /* utilities */ @@ -107,6 +108,8 @@ languageCode(PGFModule *self, PyObject *args) Lang *lang; if (!PyArg_ParseTuple(args, "O", &lang)) return NULL; + if (!checkType(lang, &LangType)) + return NULL; char* scode = gf_languageCode(self, lang); if (scode) { PyObject* result = PyString_FromString(scode); @@ -149,6 +152,8 @@ printName(PGFModule *self, PyObject *args) CId* id; if (!PyArg_ParseTuple(args, "OO", &lang, &id)) return NULL; + if (!checkType(lang,&LangType)) return NULL; + if (!checkType(id,&CIdType)) return NULL; char *pname = gf_showPrintName(self, lang, id); PyObject* result = PyString_FromString(pname); free(pname); @@ -199,13 +204,19 @@ static PGFModule* readPGF(PyObject *self, PyObject *args) { char *path; + struct stat info; PGFModule *pgf; if (!PyArg_ParseTuple(args, "s", &path)) return NULL; - pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL); - if (!pgf) return NULL; - gf_readPGF(pgf, path); - return pgf; + if (stat(path, &info) == 0) { + pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL); + if (!pgf) return NULL; + gf_readPGF(pgf, path); + return pgf; + } else { + PyErr_Format(PyExc_IOError, "No such file: %s", path); + return NULL; + } } //Todo: repr diff --git a/contrib/py-bindings/test.py b/contrib/py-bindings/test.py index 46164b126..716aba6c3 100644 --- a/contrib/py-bindings/test.py +++ b/contrib/py-bindings/test.py @@ -30,13 +30,17 @@ def rmprefix(obj): # return m and s[m.end(0):] class TestPgfInfo(unittest.TestCase): - def pgf(self): - return gf.read_pgf(self.path) + def pgf(self, path=None): + path = path or self.path + return gf.read_pgf(path) def setUp(self): self.path = 'Query.pgf' def test_readPgf(self): pgf = self.pgf() self.assertNotEqual(pgf,None) + def test_readNonExistent(self): + nopath = 'x' + self.path + self.assertRaises(IOError, self.pgf, nopath) def test_startcat(self): pgf = self.pgf() cat = pgf.startcat()