mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-08 21:10:06 +01:00
Switch from Tuple to more general Sequence, and other refactoring.
This commit is contained in:
@@ -31,6 +31,7 @@ def run():
|
||||
# customizing the generated code and docstrings.
|
||||
c = module.find('wxPalette')
|
||||
|
||||
|
||||
c.find('GetPixel.red').pyInt = True
|
||||
c.find('GetPixel.green').pyInt = True
|
||||
c.find('GetPixel.blue').pyInt = True
|
||||
@@ -56,53 +57,47 @@ def run():
|
||||
|
||||
c.find('Create').ignore()
|
||||
c.addCppMethod('PyObject*', 'Create', '(PyObject* red, PyObject* green, PyObject* blue)',
|
||||
doc="Creates a palette from tuples of integers, one for each red, blue or green component.",
|
||||
doc="Creates a palette from 3 sequences of integers, one for each red, blue or green component.",
|
||||
body="""\
|
||||
wxPyThreadBlocker blocker;
|
||||
PyObject* rval;
|
||||
|
||||
char* errMsg = "Expected a tuple of integer objects";
|
||||
if (!PyTuple_Check(red)) {
|
||||
PyErr_SetString(PyExc_TypeError, errMsg);
|
||||
return NULL;
|
||||
}
|
||||
if (!PyTuple_Check(green)) {
|
||||
PyErr_SetString(PyExc_TypeError, errMsg);
|
||||
return NULL;
|
||||
}
|
||||
if (!PyTuple_Check(blue)) {
|
||||
char* errMsg = "Expected a sequence of integer objects";
|
||||
if (!PySequence_Check(red) || !PySequence_Check(green) || !PySequence_Check(blue)) {
|
||||
PyErr_SetString(PyExc_TypeError, errMsg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int count = PyTuple_Size(red);
|
||||
if (PyTuple_Size(green) != count || PyTuple_Size(blue) != count) {
|
||||
PyErr_SetString(PyExc_ValueError, "Tuple sizes must be equal");
|
||||
Py_ssize_t count = PySequence_Size(red);
|
||||
if (PySequence_Size(green) != count || PySequence_Size(blue) != count) {
|
||||
PyErr_SetString(PyExc_ValueError, "Sequence lengths must be equal");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned char* redArray = new unsigned char[count];
|
||||
unsigned char* greenArray = new unsigned char[count];
|
||||
unsigned char* blueArray = new unsigned char[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
PyObject* redItem = PyTuple_GET_ITEM(red, i);
|
||||
PyObject* greenItem = PyTuple_GET_ITEM(green, i);
|
||||
PyObject* blueItem = PyTuple_GET_ITEM(blue, i);
|
||||
|
||||
for (Py_ssize_t i = 0; i < count; i++) {
|
||||
PyObject* redItem = PySequence_ITEM(red, i);
|
||||
PyObject* greenItem = PySequence_ITEM(green, i);
|
||||
PyObject* blueItem = PySequence_ITEM(blue, i);
|
||||
if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) {
|
||||
delete[] redArray;
|
||||
delete[] greenArray;
|
||||
delete[] blueArray;
|
||||
PyErr_SetString(PyExc_TypeError, errMsg);
|
||||
return NULL;
|
||||
rval = NULL;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
long redLong = wxPyInt_AsLong(redItem);
|
||||
long greenLong = wxPyInt_AsLong(greenItem);
|
||||
long blueLong = wxPyInt_AsLong(blueItem);
|
||||
if (redLong < 0 || redLong > 256 || greenLong < 0 || greenLong > 256 || blueLong < 0 || blueLong > 256) {
|
||||
delete[] redArray;
|
||||
delete[] greenArray;
|
||||
delete[] blueArray;
|
||||
PyErr_SetString(PyExc_ValueError, "Tuple values must be > 0 and < 256");
|
||||
return NULL;
|
||||
Py_DECREF(redItem);
|
||||
Py_DECREF(greenItem);
|
||||
Py_DECREF(blueItem);
|
||||
if (redLong < 0 || redLong > 255 || greenLong < 0 || greenLong > 255 || blueLong < 0 || blueLong > 255) {
|
||||
PyErr_SetString(PyExc_ValueError, "Sequence values must be >= 0 and < 256");
|
||||
rval = NULL;
|
||||
goto exit;
|
||||
}
|
||||
redArray[i] = redLong;
|
||||
greenArray[i] = greenLong;
|
||||
@@ -110,10 +105,18 @@ def run():
|
||||
}
|
||||
|
||||
if (self->Create(count, redArray, greenArray, blueArray)) {
|
||||
Py_RETURN_TRUE;
|
||||
rval = Py_True;
|
||||
Py_INCREF(rval);
|
||||
} else {
|
||||
Py_RETURN_FALSE;
|
||||
rval = Py_False;
|
||||
Py_INCREF(rval);
|
||||
}
|
||||
|
||||
exit:
|
||||
delete[] redArray;
|
||||
delete[] greenArray;
|
||||
delete[] blueArray;
|
||||
return rval;
|
||||
""")
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import wx
|
||||
|
||||
class palette_Tests(wtc.WidgetTestCase):
|
||||
|
||||
def test_palette(self):
|
||||
def test_palette1(self):
|
||||
rgb = bytearray(range(256))
|
||||
p = wx.Palette(256, rgb, rgb, rgb)
|
||||
self.assertTrue(p.IsOk())
|
||||
@@ -16,7 +16,14 @@ class palette_Tests(wtc.WidgetTestCase):
|
||||
with self.assertRaises(IndexError):
|
||||
p.GetRGB(257)
|
||||
|
||||
def test_paletteCreate(self):
|
||||
|
||||
# def test_palette2(self):
|
||||
# rgb = list(range(256))
|
||||
# p = wx.Palette(len(rgb), rgb, rgb, rgb)
|
||||
# self.assertTrue(p.IsOk())
|
||||
|
||||
|
||||
def test_paletteCreate1(self):
|
||||
p = wx.Palette()
|
||||
with self.assertRaises(TypeError):
|
||||
p.Create(1, 2, 3)
|
||||
@@ -28,9 +35,14 @@ class palette_Tests(wtc.WidgetTestCase):
|
||||
p.Create((257, 2, 3), (1, 2, 3), (1, 2, 3))
|
||||
with self.assertRaises(ValueError):
|
||||
p.Create((1, 2, 3), (-1, 2, 3), (1, 2, 3))
|
||||
self.assertTrue(p.Create((1, 2, 3), (4, 5, 6), (7, 8, 9)))
|
||||
|
||||
|
||||
def test_paletteCreate2(self):
|
||||
p = wx.Palette()
|
||||
self.assertTrue(p.Create((1, 2, 3), [4, 5, 6], bytearray([7, 8, 9])))
|
||||
self.assertTrue(p.GetRGB(1) == (2, 5, 8))
|
||||
|
||||
|
||||
def test_nullPalette(self):
|
||||
wx.NullPalette
|
||||
|
||||
|
||||
Reference in New Issue
Block a user