If it really came down to it, Sec. 8.05(a) and Sec. 9.22 say:
Sec. 8.05. DURESS. (a) It is an affirmative defense to prosecution that the actor engaged in the proscribed conduct because he was compelled to do so by threat of imminent death or serious bodily injury to himself or another.
Sec. 9.22. NECESSITY. Conduct is justified if:
(1) the actor reasonably believes the conduct is immediately necessary to avoid imminent harm;
(2) the desirability and urgency of avoiding the harm clearly outweigh, according to ordinary standards of reasonableness, the harm sought to be prevented by the law proscribing the conduct; and
(3) a legislative purpose to exclude the justification claimed for the conduct does not otherwise plainly appear.
But then my legal experience consists of Matlock and Perry Mason.
In 2004, a gorilla escaped the Dallas zoo and was shot by police after it had injured several people. Unless you are Cass Sunstein or PETA, I'd think the general concensus is that the life of a human child outweighs whatever animal you put down (IIRC, PETA did make a fuss about it). I'd think a good dose of pepper spray may deter an attack, but this is such an abstract scenario - and if all you have is a gun, there you go.