Commit bfa737ac authored by Xavier Thompson's avatar Xavier Thompson

Revert previous method generation attempt

parent 10388ed9
......@@ -57,6 +57,7 @@ from .StringEncoding import EncodedString
from .Pythran import has_np_pythran
def cypclass_iter(scope):
"""
Recursively iterate over nested cypclasses
......@@ -85,6 +86,7 @@ def generate_cypclass_typeobj_declarations(env, code, definition):
"""
Generate pre-declarations of the PyTypeObject for each cypclass
"""
for entry in cypclass_iter(env):
if definition or entry.defined_in_pxd:
# Todo: determine whether the __new__ called in the constructor
......@@ -93,85 +95,7 @@ def generate_cypclass_typeobj_declarations(env, code, definition):
# (cf generate_cyp_class_wrapper_definition)
code.putln("static PyTypeObject %s;" % (entry.type.typeobj_cname))
cyp_special_methods = (
"<alloc>",
"<new>",
"<constructor>"
)
def generate_cypclass_py_wrappers(env, code):
for cyp_entry, scope in cypclass_iter_scopes(env):
for name, entry in scope.entries.items():
if (entry.is_cfunction):
if name in cyp_special_methods:
continue # for now skip those
generate_cypclass_method_py_wrappers(entry, cyp_entry, code)
def generate_cypclass_method_py_wrappers(method_entry, cyp_entry, code):
py_wrapper_cname = "%s%s_%s" % (Naming.pywrap_prefix, cyp_entry.cname, method_entry.cname)
nb_alt = len(method_entry.all_alternatives())
max_args = max(map(lambda e : len(e.type.args), method_entry.all_alternatives()))
# header
self_arg_cname = "%s_self" % Naming.arg_prefix
self_arg_decl = "CyPyObject *%s" % self_arg_cname
code.put("PyObject *%s(%s" % (py_wrapper_cname, self_arg_decl))
for i in range(0, max_args):
code.put(", PyObject *%s%d" % (Naming.arg_prefix, i))
code.putln(")")
# body
code.putln("{")
code.enter_cfunc_scope()
this_var_cname = "%s_this" % Naming.var_prefix
this_var_decl = "%s *%s" % (cyp_entry.cname, this_var_cname)
code.putln("%s = reinterpret_cast<%s *>(%s->nogil_cyobject);" % (this_var_decl, cyp_entry.cname, self_arg_cname))
# - the simple case: only 1 signature
if (nb_alt == 1):
# cast arguments
c_arg_names = []
for i, arg in enumerate(method_entry.type.args):
c_var_name = "%s%s" % (Naming.var_prefix, arg.name)
c_arg_names.append(c_var_name)
c_var_decl = arg.type.declaration_code(c_var_name)
source_code = "%s%d" % (Naming.arg_prefix, i) # same as in the header
cast_code = arg.type.from_py_call_code(
source_code,
c_var_name,
method_entry.pos,
code
)
code.putln("%s;" % c_var_decl)
code.putln(cast_code)
# declare result, mangle with existing argument to avoid collision
c_return_type = method_entry.type.return_type
if not c_return_type.is_void:
c_result_cname = "%s_%s_result" % (Naming.var_prefix, c_arg_names[0] if c_arg_names else "")
c_result_decl = method_entry.type.return_type.declaration_code(c_result_cname)
code.putln("%s;" % c_result_decl)
# store result
c_call_code_begin = "%s = %s->%s" % (c_result_cname, this_var_cname, method_entry.cname)
else:
# no result to be stored
c_call_code_begin = "%s->%s" % (this_var_cname, method_entry.cname)
# call c method
code.put("%s(" % c_call_code_begin)
for i, c_arg in enumerate(c_arg_names, 1):
code.put("%s%s" % (c_arg, ", " if i < len(c_arg_names) else ""))
code.putln(");")
# else:
code.putln("return %s; // for now" % self_arg_cname)
code.putln("}")
code.exit_cfunc_scope()
#
......@@ -669,8 +593,9 @@ def generate_cyp_class_wrapper_definition(type, wrapper_entry, constructor_entry
# initialise PyObject fields
if (is_new_return_type):
code.putln("if(self) {")
code.putln("self->ob_cypyobject->ob_refcnt = 0;")
code.putln("self->ob_cypyobject->ob_type = &%s;" % type.typeobj_cname)
code.putln("self->ob_refcnt = 0;")
# code.putln("self->ob_type = NULL;")
code.putln("self->ob_type = &%s;" % type.typeobj_cname)
code.putln("}")
if init_entry:
......
......@@ -402,11 +402,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
self.generate_filename_table(globalstate['filename_table'])
self.generate_declarations_for_modules(env, modules, globalstate)
code.putln("")
code.putln("/* Python wrapper for cypclass methods */")
CypclassWrapper.generate_cypclass_py_wrappers(env, code)
h_code.write('\n')
for utilcode in env.utility_code_list[:]:
......
......@@ -56,19 +56,12 @@
int trywlock();
};
class CyObject;
struct CyPyObject : public PyObject {
CyObject * nogil_cyobject;
};
class CyObject {
class CyObject : public PyObject {
private:
CyObject_ATOMIC_REFCOUNT_TYPE nogil_ob_refcnt;
//pthread_rwlock_t ob_lock;
RecursiveUpgradeableRWLock ob_lock;
public:
CyPyObject * ob_cypyobject;
CyObject(): nogil_ob_refcnt(1) {}
virtual ~CyObject() {}
void CyObject_INCREF();
......@@ -176,14 +169,13 @@
* are likely (certain even?) to be followed by a Cy_DECREF; stealing the
* reference would mean that Cy_DECREF should not be called after this.
*/
static inline PyObject* __Pyx_PyObject_FromCyObject(CyObject * cy) {
CyPyObject * ob = cy->ob_cypyobject;
static inline PyObject* __Pyx_PyObject_FromCyObject(CyObject * ob) {
// artificial atomic increment the first time Python gets a reference
if (ob->ob_refcnt == 0)
cy->CyObject_INCREF();
ob->CyObject_INCREF();
// return a new Python reference
Py_INCREF(ob);
return ob;
Py_INCREF((PyObject *)ob);
return (PyObject *)ob;
}
/* Cast argument to CyObject* type. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment