use of time.avg() in a function

Dear all,

I try to get the time between two interrupts from a push on a button with the code :

import time, pyb

testClock = time.clock()

def test(line) :
   print( testClock.avg() )
   testClock.tick()
   return

testClock.tick()
pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
   time.sleep(10)

In openmvIDE, i have the error :
Uncaught exception in ExtInt interrupt handler line 12
MemoryError:

Any help could be pleased, Thanks,

You can’t call any functions that allocate memory in an interrupt handler. Try something like this:

import time, pyb

def test(arg):    
    global ticks_passed, last_tick, extint, switch
    switch = True
    extint.disable()
    ticks_passed = time.ticks() - last_tick
    last_tick = time.ticks()
    
switch = False
last_tick = time.ticks()
ticks_passed = 0
extint = pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
    if (switch):
        switch = False
        print(ticks_passed)
        extint.enable()