As far as I understand the code, this change seems OK to me: if the beamed element is a rest, there is no stem, and computing its stem length makes no sense. I would even move the addition right before line 330, skipping the stem length computation regardless of _direction value.