# Collection of code snippets by Arne Vajhøj 
# (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
class ManagedData(object):
    def __init__(self):
        print('ManagedData memory allocated - GC will deallocate')

class NativeResource(object):
    def __init__(self):
        print('NativeResource allocated')
    def __enter__(self):
        return self
    def __exit__(self, *args):
        print('NativeResource deallocated')

class MyException(Exception):
    pass

def m3():
    o = ManagedData()
    print('**** Error ****')
    raise MyException()

def m2():
    with NativeResource() as o:
        m3()
        print('Normal return')

def m1():
    o = ManagedData()
    m2()
    print('Normal return')

try:
    m1()
    print('Normal return')
except MyException:
    print('GC will deallocate all memory')
