forked from GitHub/gf-core
Checking args passed to gf functions.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include "pygf.h"
|
#include "pygf.h"
|
||||||
|
|
||||||
/* utilities */
|
/* utilities */
|
||||||
@@ -107,6 +108,8 @@ languageCode(PGFModule *self, PyObject *args)
|
|||||||
Lang *lang;
|
Lang *lang;
|
||||||
if (!PyArg_ParseTuple(args, "O", &lang))
|
if (!PyArg_ParseTuple(args, "O", &lang))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
if (!checkType(lang, &LangType))
|
||||||
|
return NULL;
|
||||||
char* scode = gf_languageCode(self, lang);
|
char* scode = gf_languageCode(self, lang);
|
||||||
if (scode) {
|
if (scode) {
|
||||||
PyObject* result = PyString_FromString(scode);
|
PyObject* result = PyString_FromString(scode);
|
||||||
@@ -149,6 +152,8 @@ printName(PGFModule *self, PyObject *args)
|
|||||||
CId* id;
|
CId* id;
|
||||||
if (!PyArg_ParseTuple(args, "OO", &lang, &id))
|
if (!PyArg_ParseTuple(args, "OO", &lang, &id))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
if (!checkType(lang,&LangType)) return NULL;
|
||||||
|
if (!checkType(id,&CIdType)) return NULL;
|
||||||
char *pname = gf_showPrintName(self, lang, id);
|
char *pname = gf_showPrintName(self, lang, id);
|
||||||
PyObject* result = PyString_FromString(pname);
|
PyObject* result = PyString_FromString(pname);
|
||||||
free(pname);
|
free(pname);
|
||||||
@@ -199,13 +204,19 @@ static PGFModule*
|
|||||||
readPGF(PyObject *self, PyObject *args)
|
readPGF(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *path;
|
char *path;
|
||||||
|
struct stat info;
|
||||||
PGFModule *pgf;
|
PGFModule *pgf;
|
||||||
if (!PyArg_ParseTuple(args, "s", &path))
|
if (!PyArg_ParseTuple(args, "s", &path))
|
||||||
return NULL;
|
return NULL;
|
||||||
pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL);
|
if (stat(path, &info) == 0) {
|
||||||
if (!pgf) return NULL;
|
pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL);
|
||||||
gf_readPGF(pgf, path);
|
if (!pgf) return NULL;
|
||||||
return pgf;
|
gf_readPGF(pgf, path);
|
||||||
|
return pgf;
|
||||||
|
} else {
|
||||||
|
PyErr_Format(PyExc_IOError, "No such file: %s", path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Todo: repr
|
//Todo: repr
|
||||||
|
|||||||
@@ -30,13 +30,17 @@ def rmprefix(obj):
|
|||||||
# return m and s[m.end(0):]
|
# return m and s[m.end(0):]
|
||||||
|
|
||||||
class TestPgfInfo(unittest.TestCase):
|
class TestPgfInfo(unittest.TestCase):
|
||||||
def pgf(self):
|
def pgf(self, path=None):
|
||||||
return gf.read_pgf(self.path)
|
path = path or self.path
|
||||||
|
return gf.read_pgf(path)
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.path = 'Query.pgf'
|
self.path = 'Query.pgf'
|
||||||
def test_readPgf(self):
|
def test_readPgf(self):
|
||||||
pgf = self.pgf()
|
pgf = self.pgf()
|
||||||
self.assertNotEqual(pgf,None)
|
self.assertNotEqual(pgf,None)
|
||||||
|
def test_readNonExistent(self):
|
||||||
|
nopath = 'x' + self.path
|
||||||
|
self.assertRaises(IOError, self.pgf, nopath)
|
||||||
def test_startcat(self):
|
def test_startcat(self):
|
||||||
pgf = self.pgf()
|
pgf = self.pgf()
|
||||||
cat = pgf.startcat()
|
cat = pgf.startcat()
|
||||||
|
|||||||
Reference in New Issue
Block a user