EXCEPTION_ACCESS_VIOLATION Blender 2.8 Python
Do not use operators from Python, where possible. You will probably need to remake your code to just operate on bpy.types.Armature.pose.bones.

The problem I think comes from the fact that are you changing things like bone parenting from the pose mode context, while Blender expects that to be done in edit mode. But it is a bad idea to switch modes and do this with operators as they are slower than just assigning data with Python directly.