diff --git a/gen_wrap.py b/gen_wrap.py index 07818bf8ac94324cc63ac64a1cbf9fd508eda2b2..4f58acf0fe627c1e0208d7105c19d192ed463b20 100644 --- a/gen_wrap.py +++ b/gen_wrap.py @@ -329,6 +329,9 @@ def _deref_ctx(ctx_data, ctx_iptr): class _ISLObjectBase(object): + def __init__(self, _data): + self._setup(_data) + def _setup(self, data): assert not hasattr(self, "data") assert isinstance(data, ffi.CData) @@ -371,13 +374,6 @@ class _ISLObjectBase(object): return data -def _instantiate(cls, data): - result = _ISLObjectBase.__new__(_ISLObjectBase) - result.__class__ = cls - result._setup(data) - return result - - class _ManagedCString(object): def __init__(self, cdata): self.data = libc.strdup(cdata) @@ -879,7 +875,7 @@ def write_classes_to_wrapper(wrapper_f): if data == ffi.NULL: raise Error("failed to copy instance of {py_cls}") - return _instantiate({py_cls}, data) + return {py_cls}(_data=data) """ .format(cls=cls_name, py_cls=py_cls)) @@ -924,7 +920,7 @@ def gen_callback_wrapper(gen, cb, func_name): passed_args.append("_py_%s" % arg.name) pre_call( - "_py_{name} = _instantiate({py_cls}, {name})" + "_py_{name} = {py_cls}(_data={name})" .format( name=arg.name, py_cls=isl_class_to_py_class(arg.base_type))) @@ -1076,7 +1072,7 @@ def write_method_wrapper(gen, cls_name, meth): if _cdata_{name} == ffi.NULL: raise Error("isl_val_int_from_si failed") - {val_name} = _instantiate(Val, _cdata_{name}) + {val_name} = Val(_data=_cdata_{name}) else: raise IslTypeError("{name} is a %s and cannot " @@ -1164,7 +1160,7 @@ def write_method_wrapper(gen, cls_name, meth): if _retptr_{name} == ffi.NULL: _ret_{name} = None else: - _ret_{name} = _instantiate({py_cls}, _retptr_{name}) + _ret_{name} = {py_cls}(_data=_retptr_{name}) """ .format(name=arg.name, cls=arg.base_type, py_cls=py_cls)) @@ -1252,7 +1248,7 @@ def write_method_wrapper(gen, cls_name, meth): .format(meth.c_name)) py_ret_cls = isl_class_to_py_class(ret_cls) - ret_vals.insert(0, "_instantiate({}, _result)".format(py_ret_cls)) + ret_vals.insert(0, "{}(_data=_result)".format(py_ret_cls)) ret_descrs.insert(0, ":class:`%s`" % py_ret_cls) elif meth.return_base_type in ["const char", "char"] and meth.return_ptr == "*": diff --git a/islpy/__init__.py b/islpy/__init__.py index f3a9c2a2268f3cc7e462280059ea72b9ca2a82fb..a234399be3bbfc769d5367d04595c50edb07b273 100644 --- a/islpy/__init__.py +++ b/islpy/__init__.py @@ -130,10 +130,13 @@ EXPR_CLASSES = tuple(cls for cls in ALL_CLASSES def _add_functionality(): # {{{ Context - def context_init(self): - new_ctx = Context.alloc() - self._setup(new_ctx.data) - new_ctx._release() + def context_init(self, _data=None): + if _data is not None: + super(Context, self).__init__(_data) + else: + new_ctx = Context.alloc() + self._setup(new_ctx.data) + new_ctx._release() def context_getstate(self): if self.data == _DEFAULT_CONTEXT.data: @@ -155,18 +158,27 @@ def _add_functionality(): # {{{ generic initialization, pickling - def obj_init(self, s, context=None): + def obj_init(self, s=None, context=None, _data=None): """Construct a new object from :class:`str` s. :arg context: a :class:`islpy.Context` to use. If not supplied, use a global default context. """ - if context is None: - context = _DEFAULT_CONTEXT + if _data is not None: + if s is not None: + raise TypeError("may not pass _data and string at the same time") - new_me = self.read_from_str(context, s) - self._setup(new_me._release()) + _isl._ISLObjectBase.__init__(self, _data) + else: + if s is None: + raise TypeError("'s' argument not supplied") + + if context is None: + context = _DEFAULT_CONTEXT + + new_me = self.read_from_str(context, s) + self._setup(new_me._release()) def generic_getstate(self): ctx = self.get_ctx() @@ -454,20 +466,24 @@ def _add_functionality(): # {{{ Id - def id_new(cls, name, user=None, context=None): + def id_init(self, name=None, user=None, context=None, _data=None): + if _data is not None: + if name is not None: + raise TypeError("may not pass _data and name at the same time") + + _isl._ISLObjectBase.__init__(self, _data) + return + + if name is None: + raise TypeError("'name' argument not supplied") + if context is None: context = _DEFAULT_CONTEXT - result = cls.alloc(context, name, user) - result._made_from_python = True - return result - - def id_bogus_init(self, name, user=None, context=None): - assert self._made_from_python - del self._made_from_python + new_me = cls.alloc(context, name, user) + self._setup(new_me._release()) - Id.__new__ = staticmethod(id_new) - Id.__init__ = id_bogus_init + Id.__init__ = id_init #Id.user = property(Id.get_user) # FIXME: reenable Id.name = property(Id.get_name) @@ -737,23 +753,28 @@ def _add_functionality(): # {{{ Val - def val_new(cls, src, context=None): + def val_init(self, src=None, context=None, _data=None): + if _data is not None: + if src is not None: + raise TypeError("may not pass _data and src at the same time") + + _isl._ISLObjectBase.__init__(self, _data) + return + + if src is None: + raise TypeError("'src' argument not supplied") + if context is None: context = _DEFAULT_CONTEXT if isinstance(src, six.string_types): - result = cls.read_from_str(context, src) + new_me = Val.read_from_str(context, src) elif isinstance(src, six.integer_types): - result = cls.int_from_si(context, src) + new_me = Val.int_from_si(context, src) else: raise TypeError("'src' must be int or string") - result._made_from_python = True - return result - - def val_bogus_init(self, src, context=None): - assert self._made_from_python - del self._made_from_python + self._setup(new_me._release()) def val_rsub(self, other): return -self + other @@ -774,8 +795,7 @@ def _add_functionality(): else: return int(self.to_str()) - Val.__new__ = staticmethod(val_new) - Val.__init__ = val_bogus_init + Val.__init__ = val_init Val.__add__ = Val.add Val.__radd__ = Val.add Val.__sub__ = Val.sub