safer direct creation of a new instance avoiding the __init__ method
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7772 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
b59c0b0da2
commit
c28010f762
2 changed files with 76 additions and 57 deletions
|
|
@ -47,7 +47,8 @@
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/* Flags for new pointer objects */
|
/* Flags for new pointer objects */
|
||||||
#define SWIG_POINTER_NEW SWIG_POINTER_OWN << 1
|
#define SWIG_POINTER_NOSHADOW SWIG_POINTER_OWN << 1
|
||||||
|
#define SWIG_POINTER_NEW SWIG_POINTER_NOSHADOW | SWIG_POINTER_OWN
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
@ -473,7 +474,6 @@ PySwigPacked_type(void) {
|
||||||
#endif
|
#endif
|
||||||
return &pyswigpacked_type;
|
return &pyswigpacked_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
SWIGRUNTIME PyObject *
|
SWIGRUNTIME PyObject *
|
||||||
PySwigPacked_New(void *ptr, size_t size, swig_type_info *ty)
|
PySwigPacked_New(void *ptr, size_t size, swig_type_info *ty)
|
||||||
{
|
{
|
||||||
|
|
@ -510,6 +510,14 @@ PySwigPacked_UnpackData(PyObject *obj, void *ptr, size_t size)
|
||||||
* pointers/data manipulation
|
* pointers/data manipulation
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SWIGRUNTIME
|
||||||
|
PyObject *
|
||||||
|
SWIG_This()
|
||||||
|
{
|
||||||
|
static PyObject *_this = SWIG_STATIC_POINTER(_this) PyString_FromString("this");
|
||||||
|
return _this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Convert a pointer value */
|
/* Convert a pointer value */
|
||||||
SWIGRUNTIME int
|
SWIGRUNTIME int
|
||||||
|
|
@ -520,11 +528,6 @@ SWIG_Python_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags)
|
||||||
PyObject *pyobj = 0;
|
PyObject *pyobj = 0;
|
||||||
void *vptr = 0;
|
void *vptr = 0;
|
||||||
PySwigObject *sobj = 0;
|
PySwigObject *sobj = 0;
|
||||||
static PyObject *SWIG_this =
|
|
||||||
#if !defined(__cplusplus)
|
|
||||||
0; if (!SWIG_this) SWIG_this =
|
|
||||||
#endif
|
|
||||||
PyString_FromString("this");
|
|
||||||
|
|
||||||
if (!obj) return SWIG_ERROR;
|
if (!obj) return SWIG_ERROR;
|
||||||
if (obj == Py_None) {
|
if (obj == Py_None) {
|
||||||
|
|
@ -534,7 +537,7 @@ SWIG_Python_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags)
|
||||||
|
|
||||||
if (!(PySwigObject_Check(obj))) {
|
if (!(PySwigObject_Check(obj))) {
|
||||||
pyobj = obj;
|
pyobj = obj;
|
||||||
obj = PyObject_GetAttr(obj,SWIG_this);
|
obj = PyObject_GetAttr(obj, SWIG_This());
|
||||||
if (!obj) goto type_error;
|
if (!obj) goto type_error;
|
||||||
if (!PySwigObject_Check(obj)) {
|
if (!PySwigObject_Check(obj)) {
|
||||||
Py_DECREF(obj);
|
Py_DECREF(obj);
|
||||||
|
|
@ -606,10 +609,46 @@ SWIG_Python_ConvertPacked(PyObject *obj, void *ptr, size_t sz, swig_type_info *t
|
||||||
* Create a new pointer object
|
* Create a new pointer object
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
SWIGRUNTIMEINLINE void *
|
||||||
|
SWIG_NewClientData(PyObject* obj)
|
||||||
|
{
|
||||||
|
if (PyClass_Check(obj)) {
|
||||||
|
Py_INCREF(obj);
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
|
PyObject *args = PyTuple_New(1);
|
||||||
|
PyTuple_SetItem(args, 0, obj);
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create a new instance object whitout calling __init__ */
|
||||||
|
|
||||||
|
SWIGRUNTIME PyObject*
|
||||||
|
SWIG_NewInstance(PyObject * obj)
|
||||||
|
{
|
||||||
|
PyObject *inst = 0;
|
||||||
|
if (PyTuple_Check(obj)) {
|
||||||
|
static PyObject* fnew = SWIG_STATIC_POINTER(fnew) PyObject_GetAttrString((PyObject*)&PyBaseObject_Type, "__new__");
|
||||||
|
inst = PyObject_Call(fnew, obj, NULL);
|
||||||
|
} else if (PyClass_Check(obj)) {
|
||||||
|
inst = PyInstance_NewRaw(obj, NULL);
|
||||||
|
} else {
|
||||||
|
PyObject* args = PyTuple_New(1);
|
||||||
|
PyTuple_SetItem(args,0,obj);
|
||||||
|
inst = SWIG_NewInstance(args);
|
||||||
|
Py_DECREF(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
SWIGRUNTIME PyObject *
|
SWIGRUNTIME PyObject *
|
||||||
SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
|
SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
|
||||||
PyObject *robj = 0;
|
PyObject *robj = 0;
|
||||||
int own = (flags & SWIG_POINTER_OWN) || (flags & SWIG_POINTER_NEW);
|
int own = (flags & SWIG_POINTER_OWN);
|
||||||
|
int shadow = !(flags & SWIG_POINTER_NOSHADOW);
|
||||||
if (!type) {
|
if (!type) {
|
||||||
if (!PyErr_Occurred()) {
|
if (!PyErr_Occurred()) {
|
||||||
PyErr_SetString(PyExc_TypeError, "Swig: null type passed to NewPointerObj");
|
PyErr_SetString(PyExc_TypeError, "Swig: null type passed to NewPointerObj");
|
||||||
|
|
@ -622,15 +661,11 @@ SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
|
||||||
}
|
}
|
||||||
robj = PySwigObject_New((void *) ptr, type, own);
|
robj = PySwigObject_New((void *) ptr, type, own);
|
||||||
if (!robj || (robj == Py_None)) return robj;
|
if (!robj || (robj == Py_None)) return robj;
|
||||||
/* direct new call doesn't create a shadow */
|
if (shadow && type->clientdata) {
|
||||||
if (!(flags & SWIG_POINTER_NEW) && type->clientdata) {
|
PyObject *inst = SWIG_NewInstance((PyObject *)type->clientdata);
|
||||||
PyObject *inst;
|
|
||||||
PyObject *args = PyDict_New();
|
|
||||||
PyDict_SetItemString(args, "_swig_this", robj);
|
|
||||||
Py_DECREF(robj);
|
|
||||||
inst = PyEval_CallObjectWithKeywords((PyObject *) type->clientdata, NULL, args);
|
|
||||||
Py_DECREF(args);
|
|
||||||
if (inst) {
|
if (inst) {
|
||||||
|
PyObject_SetAttr(inst, SWIG_This(), robj);
|
||||||
|
Py_DECREF(robj);
|
||||||
robj = inst;
|
robj = inst;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -533,19 +533,14 @@ public:
|
||||||
* to use keyword args or not.
|
* to use keyword args or not.
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
|
|
||||||
String *funcCallHelper(String *name, int kw, const char* self = 0) {
|
String *funcCallHelper(String *name, int kw) {
|
||||||
String *str;
|
String *str;
|
||||||
|
|
||||||
str = NewString("");
|
str = NewString("");
|
||||||
if (apply) {
|
if (apply) {
|
||||||
Printv(str, "apply(", module, ".", name, ", args", (kw ? ", kwargs" : ""), ")", NIL);
|
Printv(str, "apply(", module, ".", name, ", args", (kw ? ", kwargs" : ""), ")", NIL);
|
||||||
} else {
|
} else {
|
||||||
if (self) {
|
Printv(str, module, ".", name, "(*args", (kw ? ", **kwargs" : ""), ")", NIL);
|
||||||
Printv(str, module, ".", name, "(", self, ", *args", (kw ? ", **kwargs" : ""), ")", NIL);
|
|
||||||
} else {
|
|
||||||
Printv(str, module, ".", name, "(*args", (kw ? ", **kwargs" : ""), ")", NIL);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
@ -1177,7 +1172,7 @@ public:
|
||||||
int destructor = (!Cmp(nodeType, "destructor"));
|
int destructor = (!Cmp(nodeType, "destructor"));
|
||||||
String *storage = Getattr(n,"storage");
|
String *storage = Getattr(n,"storage");
|
||||||
int isVirtual = (Cmp(storage,"virtual") == 0);
|
int isVirtual = (Cmp(storage,"virtual") == 0);
|
||||||
|
int handled_as_constructor = constructor || Getattr(n,"handled_as_constructor");
|
||||||
if (Getattr(n,"sym:overloaded")) {
|
if (Getattr(n,"sym:overloaded")) {
|
||||||
overname = Getattr(n,"sym:overname");
|
overname = Getattr(n,"sym:overname");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1268,7 +1263,7 @@ public:
|
||||||
|
|
||||||
/* Generate code for argument marshalling */
|
/* Generate code for argument marshalling */
|
||||||
|
|
||||||
Printf(kwargs,"{ ");
|
Printf(kwargs,"{");
|
||||||
for (i = 0, p=l; i < num_arguments; i++) {
|
for (i = 0, p=l; i < num_arguments; i++) {
|
||||||
|
|
||||||
while (checkAttribute(p,"tmap:in:numinputs","0")) {
|
while (checkAttribute(p,"tmap:in:numinputs","0")) {
|
||||||
|
|
@ -1469,7 +1464,7 @@ public:
|
||||||
Replaceall(tm,"$source", "result");
|
Replaceall(tm,"$source", "result");
|
||||||
Replaceall(tm,"$target", "resultobj");
|
Replaceall(tm,"$target", "resultobj");
|
||||||
Replaceall(tm,"$result", "resultobj");
|
Replaceall(tm,"$result", "resultobj");
|
||||||
if (constructor || Getattr(n,"handled_as_constructor")) {
|
if (handled_as_constructor) {
|
||||||
Replaceall(tm,"$owner","SWIG_POINTER_NEW");
|
Replaceall(tm,"$owner","SWIG_POINTER_NEW");
|
||||||
} else {
|
} else {
|
||||||
if (GetFlag(n,"feature:new")) {
|
if (GetFlag(n,"feature:new")) {
|
||||||
|
|
@ -2106,39 +2101,25 @@ public:
|
||||||
SwigType_remember(ct);
|
SwigType_remember(ct);
|
||||||
if (CPlusPlus) {
|
if (CPlusPlus) {
|
||||||
Printv(f_wrappers,
|
Printv(f_wrappers,
|
||||||
"SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *, PyObject *args) {\n",
|
"SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *, PyObject *args) {\n", NIL);
|
||||||
tab4, "PyObject *obj;\n",
|
|
||||||
tab4, "if (!PyArg_ParseTuple(args,(char*)\"O\", &obj)) return NULL;\n",
|
|
||||||
tab4, "SWIG_TypeClientData(SWIGTYPE", SwigType_manglestr(ct),", obj);\n",
|
|
||||||
tab4, "Py_INCREF(obj);\n",
|
|
||||||
tab4, "return Py_BuildValue((char *)\"\");\n",
|
|
||||||
"}\n",NIL);
|
|
||||||
} else {
|
} else {
|
||||||
Printv(f_wrappers,
|
Printv(f_wrappers,
|
||||||
"SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *self SWIGUNUSED, PyObject *args) {\n",
|
"SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *self SWIGUNUSED, PyObject *args) {\n", NIL);
|
||||||
tab4, "PyObject *obj;\n",
|
|
||||||
tab4, "if (!PyArg_ParseTuple(args,(char*)\"O\", &obj)) return NULL;\n",
|
|
||||||
tab4, "SWIG_TypeClientData(SWIGTYPE", SwigType_manglestr(ct),", obj);\n",
|
|
||||||
tab4, "Py_INCREF(obj);\n",
|
|
||||||
tab4, "return Py_BuildValue((char *)\"\");\n",
|
|
||||||
"}\n",NIL);
|
|
||||||
}
|
}
|
||||||
|
Printv(f_wrappers,
|
||||||
|
tab4, "PyObject *obj;\n",
|
||||||
|
tab4, "if (!PyArg_ParseTuple(args,(char*)\"O\", &obj)) return NULL;\n",
|
||||||
|
tab4, "SWIG_TypeClientData(SWIGTYPE", SwigType_manglestr(ct),", SWIG_NewClientData(obj));\n",
|
||||||
|
tab4, "Py_INCREF(Py_None);\n",
|
||||||
|
tab4, "return Py_None;\n",
|
||||||
|
"}\n",NIL);
|
||||||
String *cname = NewStringf("%s_swigregister", class_name);
|
String *cname = NewStringf("%s_swigregister", class_name);
|
||||||
add_method(cname, cname, 0);
|
add_method(cname, cname, 0);
|
||||||
Delete(cname);
|
Delete(cname);
|
||||||
Delete(ct);
|
Delete(ct);
|
||||||
}
|
}
|
||||||
if (!have_constructor) {
|
if (!have_constructor) {
|
||||||
String *rclassname = Swig_class_name(getCurrentClass());
|
Printv(f_shadow_file, tab4,"def __init__(self): raise AttributeError, \"No constructor defined\"\n", NIL);
|
||||||
Printv(f_shadow_file, tab4,"def __init__(self, **kwargs):\n",NIL);
|
|
||||||
Printv(f_shadow_file, tab8, "try: this = kwargs[\"_swig_this\"]\n", NIL);
|
|
||||||
Printv(f_shadow_file, tab8, "except: this = None\n", NIL);
|
|
||||||
Printv(f_shadow_file, tab8, "if this == None: raise RuntimeError, \"No constructor defined\"\n", NIL);
|
|
||||||
if (!modern) {
|
|
||||||
Printv(f_shadow_file, tab8, "_swig_setattr(self, ", rclassname, ", 'this', this )\n", NIL);
|
|
||||||
} else {
|
|
||||||
Printv(f_shadow_file, tab8, "self.this = this\n", NIL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!have_repr) {
|
if (!have_repr) {
|
||||||
|
|
@ -2147,7 +2128,10 @@ public:
|
||||||
if (new_repr) {
|
if (new_repr) {
|
||||||
Printv(f_shadow_file,
|
Printv(f_shadow_file,
|
||||||
tab4, "def __repr__(self):\n",
|
tab4, "def __repr__(self):\n",
|
||||||
tab8, "return \"<%s.%s; proxy of ", CPlusPlus ? "C++ " : "C ", rname," instance at 0x%x>\" % (self.__class__.__module__, self.__class__.__name__, self.this,)\n",
|
tab8, "try: \n",
|
||||||
|
tab8, tab4, "sthis = \"at 0x%x\" %( self.this, ) \n",
|
||||||
|
tab8, "except: sthis = \"\" \n",
|
||||||
|
tab8, "return \"<%s.%s; proxy of ", CPlusPlus ? "C++ " : "C ", rname," instance %s>\" % (self.__class__.__module__, self.__class__.__name__, sthis,)\n",
|
||||||
NIL);
|
NIL);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -2382,15 +2366,14 @@ public:
|
||||||
NIL);
|
NIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Printv(f_shadow, tab4, "def __init__(self, *args, **kwargs):\n", NIL);
|
Printv(f_shadow, tab4, "def __init__(self, *args",
|
||||||
|
(allow_kwargs ? ", **kwargs" : ""),"):\n", NIL);
|
||||||
if ( have_docstring(n) )
|
if ( have_docstring(n) )
|
||||||
Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
|
Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
|
||||||
if ( have_pythonprepend(n) )
|
if ( have_pythonprepend(n) )
|
||||||
Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
|
Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
|
||||||
Printv(f_shadow, pass_self, NIL);
|
Printv(f_shadow, pass_self, NIL);
|
||||||
Printv(f_shadow, tab8, "try: this = kwargs[\"_swig_this\"]\n", NIL);
|
Printv(f_shadow, tab8, "this = ", funcCallHelper(Swig_name_construct(symname), allow_kwargs),"\n", NIL);
|
||||||
Printv(f_shadow, tab8, "except: this = None\n", NIL);
|
|
||||||
Printv(f_shadow, tab8, "if this == None: this = ", funcCallHelper(Swig_name_construct(symname), allow_kwargs),"\n", NIL);
|
|
||||||
if (!modern) {
|
if (!modern) {
|
||||||
Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', this )\n", NIL);
|
Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', this )\n", NIL);
|
||||||
Printv(f_shadow,
|
Printv(f_shadow,
|
||||||
|
|
@ -2474,7 +2457,8 @@ public:
|
||||||
Printv(f_shadow, tab8, "except: pass\n", NIL);
|
Printv(f_shadow, tab8, "except: pass\n", NIL);
|
||||||
#else
|
#else
|
||||||
Printv(f_shadow, tab8, "try:\n", NIL);
|
Printv(f_shadow, tab8, "try:\n", NIL);
|
||||||
Printv(f_shadow, tab8, tab4, "if self.this.own(): destroy(self.this)\n", NIL);
|
Printv(f_shadow, tab8, tab4, "this = self.this\n", NIL);
|
||||||
|
Printv(f_shadow, tab8, tab4, "if this.own(): destroy(this)\n", NIL);
|
||||||
Printv(f_shadow, tab8, "except: pass\n", NIL);
|
Printv(f_shadow, tab8, "except: pass\n", NIL);
|
||||||
#endif
|
#endif
|
||||||
if ( have_pythonappend(n) )
|
if ( have_pythonappend(n) )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue