Skip to content

Commit

Permalink
Fix Importer.get_data
Browse files Browse the repository at this point in the history
  • Loading branch information
Orochimarufan committed Apr 5, 2016
1 parent b449eae commit 3134043
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/PythonQtImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)


PyObject *
PythonQtImporter_get_data(PyObject* /*obj*/, PyObject*args)
PythonQtImporter_get_data(PyObject* /*obj*/, PyObject* args)
{
char *path;

if (!PyArg_ParseTuple(args, "s:PythonQtImporter.get_data"), &path)
if (!PyArg_ParseTuple(args, "s:PythonQtImporter.get_data", &path))
return NULL;

if (PythonQt::importInterface()->exists(path))
Expand All @@ -353,7 +353,12 @@ PythonQtImporter_get_data(PyObject* /*obj*/, PyObject*args)
return PyBytes_FromStringAndSize(data.constData(), data.size());
}

PyErr_Format(PyExc_FileNotFoundError, "Resource not found: %s", path);
PyObject *err_args = Py_BuildValue("iss", ENOENT, strerror(ENOENT), path);
#ifdef PY3K
PyErr_SetObject(PyExc_FileNotFoundError, err_args);
#else
PyErr_SetObject(PyExc_IOError, err_args);
#endif
return NULL;
}

Expand Down
23 changes: 20 additions & 3 deletions tests/PythonQtTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,28 @@ void PythonQtTestApi::testVariables()
QVERIFY(v4==QVariant());
}

static QByteArray testSource = "PQ_test = True\n";

void PythonQtTestApi::testImporter()
{
PythonQt::self()->setImporter(_helper);
PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
PyRun_SimpleString("import bla\n");
QVERIFY(PythonQt::self()->importModule("bla").getVariable("PQ_test") == QVariant::fromValue(true));
}

void PythonQtTestApi::testImporterData()
{
PythonQt::self()->setImporter(_helper);
PythonQt::self()->overwriteSysPath(QStringList() << "/");
PythonQtObjectPtr loader = PythonQt::self()->lookupObject(PythonQt::self()->importModule("bla"), "__loader__");
QVERIFY(loader);
QByteArray content = loader.call("get_data", QVariantList() << "test").toByteArray();
QVERIFY(content == testSource);
PyObject *res = PyObject_CallMethod(loader.object(), "get_data", "s", "NOEXIST");
QVERIFY(!res);
QVERIFY(PyErr_ExceptionMatches(PyExc_IOError));
PyErr_Clear();
}

void PythonQtTestApi::testQtNamespace()
Expand Down Expand Up @@ -585,20 +602,20 @@ void PythonQtTestApi::testQColorDecorators()

QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
{
QByteArray b;
QByteArray b(testSource);
return b;
}

QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
{
QByteArray b;
QByteArray b(testSource);
ok = true;
return b;
}

bool PythonQtTestApiHelper::exists(const QString& filename)
{
return true;
return !filename.endsWith(".pyc") && !filename.endsWith(".pyo") && !filename.contains("NOEXIST");
}

QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
Expand Down
1 change: 1 addition & 0 deletions tests/PythonQtTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private Q_SLOTS:
void testVariables();
void testRedirect();
void testImporter();
void testImporterData();
void testQColorDecorators();
void testQtNamespace();
void testConnects();
Expand Down

0 comments on commit 3134043

Please sign in to comment.