add support for accessing 'self.thisown'

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7956 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-09 01:12:26 +00:00
commit 2a172ffc8a
3 changed files with 58 additions and 12 deletions

View file

@ -490,7 +490,7 @@ Version 1.3.28 (unreleased).
- Finally, no more ClassPtr shadow classes. You will see
only a clean Class shadow class in the .py file.
- No more thisown attribute either, the PySwigObject now
- No more 'real' thisown attribute either, the PySwigObject now
carries the ownership info.
You can also do something like
@ -499,12 +499,31 @@ Version 1.3.28 (unreleased).
>>> True
self.this.disown()
self.this.own(0)
print self.this.own()
>>> False
self.this.acquire()
self.this.own(1)
print self.this.own()
>>> True
>>> True
Still the old way,
print self.thisown
>>> True
self.thisown = 0
print self.thisown
>>> False
self.thisown = 1
print self.thisown
>>> True
is supported, and python dispatch the proper method
calls as needed.
- Support for iterators in STL/STD containers, for example, if you have
@ -605,20 +624,25 @@ Version 1.3.28 (unreleased).
*** POTENTIAL INCOMPATIBILITY ***
There is no more 'thisown' attribute. If you use it, you
need to change as follows:
There is no more 'thisown' attribute. If you use it, python
will translate the following code as follows:
if (self.thisown): ==> if (self.this.own()):
self.thisown = 1 ==> self.this.acquire()
self.thisown = 0 ==> self.this.disown()
self.thisown = 1 ==> self.this.own(1)
self.thisown = 0 ==> self.this.own(0)
if you really really like the old 'thisown' attribute, you
can submit a patch to treat it as a property, and dispatch
the new method calls. Of course, before you try, it needs
to work with old and new python versions, and with classic
and modern python classes.
Still, maybe in some strange case the translation is not
100% secure,so if you have a problem, please report it
and/or use the new 'self.this.own()' accessor.
*** POTENTIAL INCOMPATIBILITY ***
There is no more ClassPtr classes, so, if in the past you
needed to resort in some kind of trick to use, or overcome
their presence, it is no longer needed, but the extra code
you added could break.
10/30/2005: mkoeppe
[Guile] Make declared and defined linkage of SWIG_init consistent.

View file

@ -1,9 +1,25 @@
from disown import *
a = A()
tmp = a.thisown
a.thisown = 0
if a.thisown:
raise RuntimeError
a.thisown = 1
if (not a.thisown):
raise RuntimeError
a.thisown = tmp
if (a.thisown != tmp):
raise RuntimeError
b = B()
b.acquire(a)
if a.this.own():
if a.thisown:
raise RuntimeError

View file

@ -224,6 +224,7 @@ public:
} else if (strcmp(argv[i],"-classic") == 0) {
classic = 1;
apply = 1;
modern = 0;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-cppcast") == 0) {
cppcast = 1;
@ -439,6 +440,7 @@ public:
// Python-2.2 object hack
Printv(f_shadow,
"def _swig_setattr_nondynamic(self,class_type,name,value,static=1):\n",
tab4, "if (name == \"thisown\"): return self.this.own(value)\n",
tab4, "if (name == \"this\"):\n",
tab4, tab4, "if type(value).__name__ == 'PySwigObject':\n",
tab4, tab8, "self.__dict__[name] = value\n",
@ -463,6 +465,7 @@ public:
Printv(f_shadow,
"def _swig_getattr(self,class_type,name):\n",
tab4, "if (name == \"thisown\"): return self.this.own()\n",
tab4, "method = class_type.__swig_getmethods__.get(name,None)\n",
tab4, "if method: return method(self)\n",
tab4, "raise AttributeError,name\n\n",
@ -489,6 +492,7 @@ public:
#ifdef USE_THISOWN
tab4, tab4, "if hasattr(self,name) or (name in (\"this\", \"thisown\")):\n",
#else
tab4, tab4, "if (name == \"thisown\"): return self.this.own(value)\n",
tab4, tab4, "if hasattr(self,name) or (name == \"this\"):\n",
#endif
tab4, tab4, tab4, "set(self,name,value)\n",
@ -2229,6 +2233,8 @@ public:
tab4, "__getattr__ = lambda self, name: _swig_getattr(self, ", class_name, ", name)\n",
NIL);
} else {
Printv(f_shadow, tab4, "if _newclass: thisown = property(lambda x: x.this.own(), ",
"lambda x, v: x.this.own(v), doc='The membership flag')\n", NIL);
/* Add static attribute */
if (GetFlag(n,"feature:python:nondynamic")) {
Printv(f_shadow_file,