1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
"""
Wraps C++ class instance/static attributes.
"""
from typehandlers.base import ForwardWrapperBase, ReverseWrapperBase
from typehandlers import codesink
import settings
import utils
class PyGetter(ForwardWrapperBase):
"""generates a getter, for use in a PyGetSetDef table"""
def generate(self, code_sink):
"""Generate the code of the getter to the given code sink"""
raise NotImplementedError
def generate_call(self):
"""(not actually called)"""
raise AssertionError
class PySetter(ReverseWrapperBase):
"""generates a setter, for use in a PyGetSetDef table"""
NO_GIL_LOCKING = True
def generate(self, code_sink):
"""Generate the code of the setter to the given code sink"""
raise NotImplementedError
def generate_python_call(self):
"""(not actually called)"""
raise AssertionError
class CppInstanceAttributeGetter(PyGetter):
'''
A getter for a C++ instance attribute.
'''
def __init__(self, value_type, class_, attribute_name, getter=None):
"""
:param value_type: a ReturnValue object handling the value type;
:param class_: the class (CppClass object)
:param attribute_name: name of attribute
:param getter: None, or name of a method of the class used to get the value
"""
super(CppInstanceAttributeGetter, self).__init__(
value_type, [], "return NULL;", "return NULL;", no_c_retval=True)
self.class_ = class_
self.attribute_name = attribute_name
self.getter = getter
self.c_function_name = "_wrap_%s__get_%s" % (self.class_.pystruct,
self.attribute_name)
if self.getter is None:
value_type.value = "self->obj->%s" % self.attribute_name
else:
value_type.value = "self->obj->%s()" % self.getter
def generate_call(self):
"virtual method implementation; do not call"
pass
def generate(self, code_sink):
"""
:param code_sink: a CodeSink instance that will receive the generated code
"""
tmp_sink = codesink.MemoryCodeSink()
self.generate_body(tmp_sink)
code_sink.writeln("static PyObject* %s(%s *self, void * PYBINDGEN_UNUSED(closure))"
% (self.c_function_name, self.class_.pystruct))
code_sink.writeln('{')
code_sink.indent()
tmp_sink.flush_to(code_sink)
code_sink.unindent()
code_sink.writeln('}')
class CppStaticAttributeGetter(PyGetter):
'''
A getter for a C++ class static attribute.
'''
def __init__(self, value_type, class_, attribute_name):
"""
:param value_type: a ReturnValue object handling the value type;
:param c_value_expression: C value expression
"""
super(CppStaticAttributeGetter, self).__init__(
value_type, [], "return NULL;", "return NULL;", no_c_retval=True)
self.class_ = class_
self.attribute_name = attribute_name
self.c_function_name = "_wrap_%s__get_%s" % (self.class_.pystruct,
self.attribute_name)
value_type.value = "%s::%s" % (self.class_.full_name, self.attribute_name)
def generate_call(self):
"virtual method implementation; do not call"
pass
def generate(self, code_sink):
"""
:param code_sink: a CodeSink instance that will receive the generated code
"""
tmp_sink = codesink.MemoryCodeSink()
self.generate_body(tmp_sink)
code_sink.writeln("static PyObject* %s(PyObject * PYBINDGEN_UNUSED(obj),"
" void * PYBINDGEN_UNUSED(closure))"
% self.c_function_name)
code_sink.writeln('{')
code_sink.indent()
tmp_sink.flush_to(code_sink)
code_sink.unindent()
code_sink.writeln('}')
class CppInstanceAttributeSetter(PySetter):
'''
A setter for a C++ instance attribute.
'''
def __init__(self, value_type, class_, attribute_name, setter=None):
"""
:param value_type: a ReturnValue object handling the value type;
:param class_: the class (CppClass object)
:param attribute_name: name of attribute
:param setter: None, or name of a method of the class used to set the value
"""
super(CppInstanceAttributeSetter, self).__init__(
value_type, [], "return -1;")
self.class_ = class_
self.attribute_name = attribute_name
self.setter = setter
self.c_function_name = "_wrap_%s__set_%s" % (self.class_.pystruct,
self.attribute_name)
def generate(self, code_sink):
"""
:param code_sink: a CodeSink instance that will receive the generated code
"""
self.declarations.declare_variable('PyObject*', 'py_retval')
self.before_call.write_code(
'py_retval = Py_BuildValue((char *) "(O)", value);')
self.before_call.add_cleanup_code('Py_DECREF(py_retval);')
if self.setter is not None:
## if we have a setter method, redirect the value to a temporary variable
if not self.return_value.REQUIRES_ASSIGNMENT_CONSTRUCTOR:
value_var = self.declarations.declare_variable(self.return_value.ctype, 'tmp_value')
else:
value_var = self.declarations.reserve_variable('tmp_value')
self.return_value.value = value_var
else:
## else the value is written directly to a C++ instance attribute
self.return_value.value = "self->obj->%s" % self.attribute_name
self.return_value.REQUIRES_ASSIGNMENT_CONSTRUCTOR = False
self.return_value.convert_python_to_c(self)
parse_tuple_params = ['py_retval']
params = self.parse_params.get_parameters()
assert params[0][0] == '"'
params[0] = '(char *) ' + params[0]
parse_tuple_params.extend(params)
self.before_call.write_error_check('!PyArg_ParseTuple(%s)' %
(', '.join(parse_tuple_params),))
if self.setter is not None:
## if we have a setter method, now is the time to call it
self.after_call.write_code("self->obj->%s(%s);" % (self.setter, value_var))
## cleanup and return
self.after_call.write_cleanup()
self.after_call.write_code('return 0;')
## now generate the function itself
code_sink.writeln("static int %s(%s *self, PyObject *value, void * PYBINDGEN_UNUSED(closure))"
% (self.c_function_name, self.class_.pystruct))
code_sink.writeln('{')
code_sink.indent()
self.declarations.get_code_sink().flush_to(code_sink)
code_sink.writeln()
self.before_call.sink.flush_to(code_sink)
self.after_call.sink.flush_to(code_sink)
code_sink.unindent()
code_sink.writeln('}')
class CppStaticAttributeSetter(PySetter):
'''
A setter for a C++ class static attribute.
'''
def __init__(self, value_type, class_, attribute_name):
"""
:param value_type: a ReturnValue object handling the value type;
:param class_: the class (CppClass object)
:param attribute_name: name of attribute
"""
super(CppStaticAttributeSetter, self).__init__(
value_type, [], "return -1;")
self.class_ = class_
self.attribute_name = attribute_name
self.c_function_name = "_wrap_%s__set_%s" % (self.class_.pystruct,
self.attribute_name)
value_type.value = "%s::%s" % (self.class_.full_name, self.attribute_name)
value_type.REQUIRES_ASSIGNMENT_CONSTRUCTOR = False
def generate(self, code_sink):
"""
:param code_sink: a CodeSink instance that will receive the generated code
"""
self.declarations.declare_variable('PyObject*', 'py_retval')
self.before_call.write_code(
'py_retval = Py_BuildValue((char *) "(O)", value);')
self.before_call.add_cleanup_code('Py_DECREF(py_retval);')
self.return_value.convert_python_to_c(self)
parse_tuple_params = ['py_retval']
params = self.parse_params.get_parameters()
assert params[0][0] == '"'
params[0] = '(char *) ' + params[0]
parse_tuple_params.extend(params)
self.before_call.write_error_check('!PyArg_ParseTuple(%s)' %
(', '.join(parse_tuple_params),))
## cleanup and return
self.after_call.write_cleanup()
self.after_call.write_code('return 0;')
## now generate the function itself
code_sink.writeln(("static int %s(%s * PYBINDGEN_UNUSED(dummy), "
"PyObject *value, void * PYBINDGEN_UNUSED(closure))")
% (self.c_function_name, self.class_.pystruct))
code_sink.writeln('{')
code_sink.indent()
self.declarations.get_code_sink().flush_to(code_sink)
code_sink.writeln()
self.before_call.sink.flush_to(code_sink)
self.after_call.sink.flush_to(code_sink)
code_sink.unindent()
code_sink.writeln('}')
class PyMetaclass(object):
"""
Class that generates a Python metaclass
"""
def __init__(self, name, parent_metaclass_expr, getsets=None):
"""
:param name: name of the metaclass (should normally end with Meta)
:param parent_metaclass_expr: C expression that should give a
pointer to the parent metaclass
(should have a C type of
PyTypeObject*)
:param getsets: name of a PyGetSetDef C array variable, or None
"""
assert getsets is None or isinstance(getsets, PyGetSetDef)
assert isinstance(name, basestring)
assert isinstance(parent_metaclass_expr, basestring)
self.name = name
prefix = settings.name_prefix.capitalize()
self.pytypestruct = "Py%s%s_Type" % (prefix, self.name)
self.parent_metaclass_expr = parent_metaclass_expr
self.getsets = getsets
def generate(self, code_sink, module):
"""
Generate the metaclass to code_sink and register it in the module.
"""
code_sink.writeln('''
PyTypeObject %(pytypestruct)s = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
(char *) "%(name)s", /* tp_name */
0, /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
%(getset)s, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0 /* tp_del */
};
''' % dict(pytypestruct=self.pytypestruct, name=self.name,
getset=(self.getsets and self.getsets.cname or '0')))
module.after_init.write_code("""
%(pytypestruct)s.tp_base = %(parent_metaclass)s;
/* Some fields need to be manually inheritted from the parent metaclass */
%(pytypestruct)s.tp_traverse = %(parent_metaclass)s->tp_traverse;
%(pytypestruct)s.tp_clear = %(parent_metaclass)s->tp_clear;
%(pytypestruct)s.tp_is_gc = %(parent_metaclass)s->tp_is_gc;
/* PyType tp_setattro is too restrictive */
%(pytypestruct)s.tp_setattro = PyObject_GenericSetAttr;
PyType_Ready(&%(pytypestruct)s);
""" % dict(pytypestruct=self.pytypestruct, parent_metaclass=self.parent_metaclass_expr))
class PyGetSetDef(object):
"""
Class that generates a PyGetSet table
"""
def __init__(self, cname):
"""
:param cname: C name of the getset table
"""
self.cname = cname
self.attributes = [] # (name, getter, setter)
def empty(self):
return len(self.attributes) == 0
def add_attribute(self, name, getter, setter):
"""
Add a new attribute
:param name: attribute name
:param getter: a PyGetter object, or None
:param setter: a PySetter object, or None
"""
assert getter is None or isinstance(getter, PyGetter)
assert setter is None or isinstance(setter, PySetter)
self.attributes.append((name, getter, setter))
def generate(self, code_sink):
"""
Generate the getset table, return the table C name or '0' if
the table is empty
"""
if not self.attributes:
return '0'
getsets = {} # attrname -> (getter, setter)
for name, getter, setter in self.attributes:
getter_name = 'NULL'
if getter is not None:
# getter.generate(code_sink)
try:
utils.call_with_error_handling(getter.generate, (code_sink,), {}, getter)
except utils.SkipWrapper:
pass
else:
getter_name = getter.c_function_name
setter_name = 'NULL'
if setter is not None:
#setter.generate(code_sink)
try:
utils.call_with_error_handling(setter.generate, (code_sink,), {}, setter)
except utils.SkipWrapper:
pass
else:
setter_name = setter.c_function_name
assert name not in getsets
getsets[name] = (getter_name, setter_name)
code_sink.writeln("static PyGetSetDef %s[] = {" % self.cname)
code_sink.indent()
for name, (getter_c_name, setter_c_name) in getsets.iteritems():
code_sink.writeln('{')
code_sink.indent()
code_sink.writeln('(char*) "%s", /* attribute name */' % name)
## getter
code_sink.writeln(
'(getter) %s, /* C function to get the attribute */'
% getter_c_name)
## setter
code_sink.writeln(
'(setter) %s, /* C function to set the attribute */'
% setter_c_name)
code_sink.writeln('NULL, /* optional doc string */')
code_sink.writeln('NULL /* optional additional data '
'for getter and setter */')
code_sink.unindent()
code_sink.writeln('},')
code_sink.writeln('{ NULL, NULL, NULL, NULL, NULL }')
code_sink.unindent()
code_sink.writeln('};')
return self.cname
|