Skip to content

Commit 9825810

Browse files
committed
Add _stc, fix wxMemoryBuffer bindings
1 parent 61f86b4 commit 9825810

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

etg/unfinished/_stc.py renamed to etg/_stc.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
# sources and/or additional dependencies when building this extension module.
3535
ETGFILES = ['etg/%s.py' % NAME] + tools.getEtgFiles(INCLUDES)
3636
DEPENDS = tools.getNonEtgFiles(INCLUDES)
37-
OTHERDEPS = [ ]
37+
OTHERDEPS = [ 'etg/cffi/_stc.py',
38+
]
3839

3940

4041
#---------------------------------------------------------------------------
@@ -49,7 +50,8 @@ def run():
4950
# Tweak the parsed meta objects in the module object as needed for
5051
# customizing the generated code and docstrings.
5152

52-
module.addHeaderCode('#include <wxpy_api.h>')
53+
# XXX(amauryfa): only for SIP?
54+
# module.addHeaderCode('#include <wxpy_api.h>')
5355
module.addImport('_core')
5456
module.addPyCode('import wx', order=10)
5557
module.addInclude(INCLUDES)
@@ -90,34 +92,9 @@ def run():
9092
ht2.find('row').out = True
9193
ht2.find('col').out = True
9294

93-
94-
# Replace the *Pointer methods with ones that return a memoryview object instead.
95-
c.find('GetCharacterPointer').ignore()
96-
c.addCppMethod('PyObject*', 'GetCharacterPointer', '()',
97-
doc="""\
98-
Compact the document buffer and return a read-only memoryview
99-
object of the characters in the document.""",
100-
body="""
101-
const char* ptr = self->GetCharacterPointer();
102-
Py_ssize_t len = self->GetLength();
103-
PyObject* rv;
104-
wxPyBLOCK_THREADS( rv = wxPyMakeBuffer((void*)ptr, len, true) );
105-
return rv;
106-
""")
107-
108-
c.find('GetRangePointer').ignore()
109-
c.addCppMethod('PyObject*', 'GetRangePointer', '(int position, int rangeLength)',
110-
doc="""\
111-
Return a read-only pointer to a range of characters in the
112-
document. May move the gap so that the range is contiguous,
113-
but will only move up to rangeLength bytes.""",
114-
body="""
115-
const char* ptr = self->GetRangePointer(position, rangeLength);
116-
Py_ssize_t len = rangeLength;
117-
PyObject* rv;
118-
wxPyBLOCK_THREADS( rv = wxPyMakeBuffer((void*)ptr, len, true) );
119-
return rv;
120-
""")
95+
96+
# TODO(amauryfa): GetCharacterPointer
97+
# TODO(amauryfa): GetRangePointer
12198

12299
# TODO: Add the UTF8 PyMethods from classic (see _stc_utf8_methods.py)
123100

etg/cffi/_stc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def run(module):
2+
3+
module.addPyCode('''\
4+
from ._stc import *
5+
from . import _stc
6+
''', order=0)

etg/cffi/wxpybuffer.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run():
1616
typedef struct wxPyBuffer
1717
{
1818
size_t m_len;
19-
char *m_ptr;
19+
char *m_ptr; // owned, allocated with malloc()
2020
} wxPyBuffer;
2121
""")
2222
module.addHeaderCode(tools.textfile_open('src/cffi/wxpybuffer.h').read())
@@ -53,12 +53,10 @@ def run():
5353
return False
5454
"""))
5555

56-
# TODO: Uncomment these when re-adding _stc or stream
57-
'''
5856
module.addItem(etgtools.MappedTypeDef_cffi(
5957
name='wxMemoryBuffer', cType='wxPyBuffer *',
6058

61-
py2c="""
59+
py2c="""\
6260
memview = memoryview(py_obj)
6361
chardata = ffi.new('char[]', memview.itemsize)
6462
for i in range(memview.itemsize):
@@ -70,12 +68,18 @@ def run():
7068
return (cdata, (cdata, chardata))
7169
""",
7270

73-
c2cpp="return new wxCharBuffer(cdata);",
71+
c2cpp="""\
72+
wxMemoryBuffer* buffer = new wxMemoryBuffer(cdata->m_len);
73+
memcpy(buffer->GetData(), cdata->m_ptr, buffer->GetDataLen());
74+
return buffer;
75+
""",
7476

7577
cpp2c="""\
76-
char *cdata = (char*)malloc(cpp_obj->length() + 1);
77-
memcpy(cdata, (char*)cpp_obj->GetData(), cpp_obj->GetDataLen());
78-
return cdata;
78+
wxPyBuffer* buffer = (wxPyBuffer*)malloc(sizeof(wxPyBuffer));
79+
buffer->m_len = cpp_obj->GetDataLen();
80+
buffer->m_ptr = (char*)malloc(buffer->m_len);
81+
memcpy(buffer->m_ptr, cpp_obj->GetData(), cpp_obj->GetDataLen());
82+
return buffer;
7983
""",
8084
c2py="""
8185
ret = bytearr
@@ -107,7 +111,6 @@ def run():
107111
return ret
108112
""",
109113
instanceCheck="return isinstance(py_obj, str)"))
110-
'''
111114

112115
#-----------------------------------------------------------------
113116
tools.doCommonTweaks(module)

etg/sip/_stc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import etgtools
2+
3+
def run(module):
4+
c = module.find('wxStyledTextCtrl')
5+
6+
# Replace the *Pointer methods with ones that return a memoryview object instead.
7+
c.find('GetCharacterPointer').ignore()
8+
c.addCppMethod('PyObject*', 'GetCharacterPointer', '()',
9+
doc="""\
10+
Compact the document buffer and return a read-only memoryview
11+
object of the characters in the document.""",
12+
body="""
13+
const char* ptr = self->GetCharacterPointer();
14+
Py_ssize_t len = self->GetLength();
15+
PyObject* rv;
16+
wxPyBLOCK_THREADS( rv = wxPyMakeBuffer((void*)ptr, len, true) );
17+
return rv;
18+
""")
19+
20+
c.find('GetRangePointer').ignore()
21+
c.addCppMethod('PyObject*', 'GetRangePointer', '(int position, int rangeLength)',
22+
doc="""\
23+
Return a read-only pointer to a range of characters in the
24+
document. May move the gap so that the range is contiguous,
25+
but will only move up to rangeLength bytes.""",
26+
body="""
27+
const char* ptr = self->GetRangePointer(position, rangeLength);
28+
Py_ssize_t len = rangeLength;
29+
PyObject* rv;
30+
wxPyBLOCK_THREADS( rv = wxPyMakeBuffer((void*)ptr, len, true) );
31+
return rv;
32+
""")
33+

0 commit comments

Comments
 (0)