Fixed/implemented various QResources methods.
- data() is now exported and returns a Python read-only buffer object. - (un)registerResourceData now accept buffers as arguments, and respects zeroes in the middle of data. - Improved unit test.
This commit is contained in:
parent
702cd168e1
commit
13a6cba842
5 changed files with 3568 additions and 26 deletions
|
|
@ -491,25 +491,51 @@
|
||||||
</modify-function>
|
</modify-function>
|
||||||
</value-type>
|
</value-type>
|
||||||
<object-type name="QResource">
|
<object-type name="QResource">
|
||||||
<modify-function signature="registerResource(const uchar*, const QString&)" remove="all"/>
|
<modify-function signature="data()const">
|
||||||
<modify-function signature="unregisterResource(const uchar*, const QString&)" remove="all"/>
|
<inject-documentation>
|
||||||
<modify-function signature="data()const" remove="all"/>
|
Returns a read only buffer object pointing to the segment of data that this resource represents. If the resource is compressed the data returns is compressed and qUncompress() must be used to access the data. If the resource is a directory None is returned.
|
||||||
<!-- TODO: find a way for this mimic the PyQt4 behaviour, remove it for now.
|
</inject-documentation>
|
||||||
|
<modify-argument index="return">
|
||||||
|
<replace-type modified-type="PyObject" />
|
||||||
|
</modify-argument>
|
||||||
|
<inject-code>
|
||||||
|
const uchar* d = %CPPSELF.%FUNCTION_NAME();
|
||||||
|
if (d) {
|
||||||
|
%PYARG_0 = PyBuffer_FromMemory(const_cast<uchar*>(d), %CPPSELF.size());
|
||||||
|
} else {
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
%PYARG_0 = Py_None;
|
||||||
|
}
|
||||||
|
</inject-code>
|
||||||
|
</modify-function>
|
||||||
|
<template name="QResource_registerResource">
|
||||||
|
PyTypeObject* pyType = %PYARG_1->ob_type;
|
||||||
|
if (pyType->tp_as_buffer
|
||||||
|
&& pyType->tp_as_buffer->bf_getreadbuffer
|
||||||
|
&& pyType->tp_as_buffer->bf_getsegcount(%PYARG_1, 0) == 1) {
|
||||||
|
void* ptr;
|
||||||
|
pyType->tp_as_buffer->bf_getreadbuffer(%PYARG_1, 0, &ptr);
|
||||||
|
%PYARG_0 = %CONVERTTOPYTHON[bool](%CPPSELF.%FUNCTION_NAME(const_cast<const uchar*>((uchar*)ptr), %2));
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "The object must support buffer protocol with just one segment.");
|
||||||
|
}
|
||||||
|
</template>
|
||||||
|
|
||||||
<modify-function signature="unregisterResource(const uchar*, const QString&)" rename="unregisterResourceData">
|
<modify-function signature="unregisterResource(const uchar*, const QString&)" rename="unregisterResourceData">
|
||||||
<modify-argument index="1">
|
<modify-argument index="1">
|
||||||
<replace-type modified-type="const char*"/>
|
<replace-type modified-type="PyObject"/>
|
||||||
</modify-argument>
|
</modify-argument>
|
||||||
|
<inject-code>
|
||||||
|
<insert-template name="QResource_registerResource" />
|
||||||
|
</inject-code>
|
||||||
</modify-function>
|
</modify-function>
|
||||||
<modify-function signature="registerResource(const uchar*, const QString&)" rename="registerResourceData"/>
|
<modify-function signature="registerResource(const uchar*, const QString&)" rename="registerResourceData">
|
||||||
<modify-argument index="1">
|
<modify-argument index="1">
|
||||||
<replace-type modified-type="const char*"/>
|
<replace-type modified-type="PyObject"/>
|
||||||
</modify-argument>
|
|
||||||
</modify-function>
|
|
||||||
-->
|
|
||||||
<modify-function signature="data() const">
|
|
||||||
<modify-argument index="return">
|
|
||||||
<replace-type modified-type="str"/>
|
|
||||||
</modify-argument>
|
</modify-argument>
|
||||||
|
<inject-code>
|
||||||
|
<insert-template name="QResource_registerResource" />
|
||||||
|
</inject-code>
|
||||||
</modify-function>
|
</modify-function>
|
||||||
</object-type>
|
</object-type>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,38 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
|
from helper import adjust_filename
|
||||||
from PySide.QtCore import QFile, QIODevice
|
from PySide.QtCore import QFile, QIODevice
|
||||||
import resources_mc
|
import resources_mc
|
||||||
|
|
||||||
class ResourcesUsage(unittest.TestCase):
|
class ResourcesUsage(unittest.TestCase):
|
||||||
'''Test case for resources usage'''
|
'''Test case for resources usage'''
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
f = open(os.path.join(os.path.dirname(__file__), 'quoteEnUS.txt'))
|
|
||||||
self.text = f.read()
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.text = None
|
|
||||||
|
|
||||||
def testPhrase(self):
|
def testPhrase(self):
|
||||||
#Test loading of quote.txt resource
|
#Test loading of quote.txt resource
|
||||||
|
f = open(adjust_filename('quoteEnUS.txt', __file__))
|
||||||
|
orig = f.read()
|
||||||
|
f.close()
|
||||||
|
|
||||||
f = QFile(':/quote.txt')
|
f = QFile(':/quote.txt')
|
||||||
f.open(QIODevice.ReadOnly|QIODevice.Text)
|
f.open(QIODevice.ReadOnly|QIODevice.Text)
|
||||||
content = f.readAll()
|
copy = f.readAll()
|
||||||
f.close()
|
f.close()
|
||||||
self.assertEqual(self.text, content)
|
self.assertEqual(orig, copy)
|
||||||
|
|
||||||
|
def testImage(self):
|
||||||
|
#Test loading of sample.png resource
|
||||||
|
f = open(adjust_filename('sample.png', __file__))
|
||||||
|
orig = f.read()
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
f = QFile(':/sample.png')
|
||||||
|
f.open(QIODevice.ReadOnly)
|
||||||
|
copy = f.readAll()
|
||||||
|
f.close()
|
||||||
|
self.assertEqual(len(orig), len(copy))
|
||||||
|
self.assertEqual(orig, copy)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
<RCC>
|
<RCC>
|
||||||
<qresource prefix="/">
|
<qresource prefix="/">
|
||||||
<file alias="quote.txt">quoteEnUS.txt</file>
|
<file alias="quote.txt">quoteEnUS.txt</file>
|
||||||
|
<file alias="sample.png">sample.png</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
BIN
tests/QtCore/sample.png
Normal file
BIN
tests/QtCore/sample.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Loading…
Add table
Add a link
Reference in a new issue