Int8 calibration in python 3

Based on the example here https://devblogs.nvidia.com/int8-inference-autonomous-vehicles-tensorrt/, I am trying to implement Int8 calibration in Python. Everything works fine except for the first line in write_calibration_cache fails with

TypeError: int() argument must be a string, a bytes-like object or a number, not 'PyCapsule'

I believe that this could be valid python 2 code, so I’d like to know what’s the corresponding Python 3 code? Printing ptr and size yields

<capsule object NULL at 0x7f23e57228a0> 333

I’ve managed to solve the issue using ctypes:

ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_char_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]

def write_calibration_cache(self, ptr, size):
        value = ctypes.pythonapi.PyCapsule_GetPointer(ptr, None)
        with open(self.path, 'wb') as f:
            f.write(value)
        return None