示例#1
0
import upywraptest

simple1 = upywraptest.Simple(0)
print(simple1())
simple1.Add(1)
print(simple1.Value())
print(simple1())
print(simple1)

simple2 = upywraptest.Simple(2)
print(simple2.Value())

simple1.Plus(simple2)
print(simple1.Value())

print(upywraptest.Simple(1).SimpleFunc(upywraptest.Simple(3)).Value())

simple1.val = 5
print(simple1.val)
try:
    simple1.val2
except AttributeError:
    print('AttributeError')

print(simple1 == simple2)
print(simple1 == simple1)
print(hash(simple1) == hash(simple2))
print(hash(simple1) == hash(simple1))
# Since we do not use MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE this is automatically false.
# However if we would use that, forwarding __eq__ to Simple::operator == directly isn't
# really conforming to Python since it would raise a TypeError trying to convert the rhs.
示例#2
0
import upywraptest

simple1 = upywraptest.Simple( 0 )
simple1.Add( 1 )
print( simple1.Value() )
print( simple1 )

simple2 = upywraptest.Simple( 2 )
print( simple2.Value() )

simple1.Plus( simple2 )
print( simple1.Value() )

print( upywraptest.Simple( 1 ).SimpleFunc( upywraptest.Simple( 3 ) ).Value() )

simple1.val = 5
print( simple1.val )
try:
  simple1.val2
except AttributeError:
  print( 'AttributeError' )

print( simple1 == simple2 )
print( simple1 == simple1 )
print( hash( simple1 ) == hash( simple2 ) )
print( hash( simple1 ) == hash( simple1 ) )

print( hasattr( simple1, 'Something' ) )
print( hasattr( simple1, 'val' ) )

print( upywraptest.Simple.x )
import gc
import upywraptest


def TestRefCount(getObject):
    simpleCollection = upywraptest.SimpleCollection()
    simpleCollection.Add(getObject())
    for i in range(20):
        if simpleCollection.Reference(0) == 1:
            print('collect did reclaim object', i)
            break
        gc.collect()
    else:
        print('collect did not reclaim object')


if __name__ == '__main__':
    TestRefCount(lambda: upywraptest.Simple(1))