swig/Examples/test-suite/python/swigobject.py
Marcelo Matus 0881d3c861 Add simple PySwigObject to handle the C/C++ instance pointers,
instead of using PyCObject or plain strings.

The new PySwigObject is even safer than PyCObject, and
more friendly than plain strings:

now you can do

  print a.this
  <Swig Object at _00691608_p_A>

  print str(a.this)
  _00691608_p_A

  print long(a.this)
  135686400

  print "%s 0x%x" % (a.this, a.this)
  _00691608_p_A 0x8166900


the last one is very useful when debugging the C/C++ side, since
is the pointer value you will usually get from the debugger.

Also, if you have some old code that uses the string representation
"_00691608_p_A", you can use it now again by calling str(ptr), or
maybe nothing special by just calling PyString_AsString(..).

This change is mainly for nostalgic swig users that miss the
string representation, but also allows to say again

  if a.this == b.this:
    return "a is b"

and well, since the change were really simple, maybe in the future
we will be able to do

    next = a.this + 1

or add native python iteration over native C/C++ arrays, ie, no
need to create/copy new tuples when returning and array or vector.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6759 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-11-19 20:40:20 +00:00

69 lines
2.3 KiB
Python

# This file was created automatically by SWIG.
# Don't modify this file, modify the SWIG interface instead.
# This file is compatible with both classic and new-style classes.
import _swigobject
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
if (name == "this"):
if isinstance(value, class_type):
self.__dict__[name] = value.this
if hasattr(value,"thisown"): self.__dict__["thisown"] = value.thisown
del value.thisown
return
method = class_type.__swig_setmethods__.get(name,None)
if method: return method(self,value)
if (not static) or hasattr(self,name) or (name == "thisown"):
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self,class_type,name,value):
return _swig_setattr_nondynamic(self,class_type,name,value,0)
def _swig_getattr(self,class_type,name):
method = class_type.__swig_getmethods__.get(name,None)
if method: return method(self)
raise AttributeError,name
import types
try:
_object = types.ObjectType
_newclass = 1
except AttributeError:
class _object : pass
_newclass = 0
del types
class A(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, A, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, A, name)
def __repr__(self):
return "<%s.%s; proxy of C++ A instance at %s>" % (self.__class__.__module__, self.__class__.__name__, self.this,)
__swig_setmethods__["name"] = _swigobject.A_name_set
__swig_getmethods__["name"] = _swigobject.A_name_get
if _newclass:name = property(_swigobject.A_name_get, _swigobject.A_name_set)
def __init__(self, *args):
_swig_setattr(self, A, 'this', _swigobject.new_A(*args))
_swig_setattr(self, A, 'thisown', 1)
def __del__(self, destroy=_swigobject.delete_A):
try:
if self.thisown: destroy(self)
except: pass
class APtr(A):
def __init__(self, this):
_swig_setattr(self, A, 'this', this)
if not hasattr(self,"thisown"): _swig_setattr(self, A, 'thisown', 0)
_swig_setattr(self, A,self.__class__,A)
_swigobject.A_swigregister(APtr)
pointer_str = _swigobject.pointer_str
a_ptr = _swigobject.a_ptr