handle malloc() failure in encoding setup code

--HG--
extra : transplant_source : %F3%9A%B7r%F7%DB%C7%88%2B%D2%A4%9C%5E%0FT%B8_XS%D2
minor code cleanup

--HG--
extra : transplant_source : M%D5%2C%C2%15%9E%3C%BC%3C%89%25n%95%DF%5C%B8%B5%C5%B8%D9
make encoding setup code safer by not expecting sys.getdefaultencoding() to safely return a bytes object

--HG--
extra : transplant_source : cZx%3E%03l%AC%EDK%E3%C2%F2%2C%9BV%E5%EC%5D%B7%C8
free sys module reference earlier in encoding setup code

--HG--
extra : transplant_source : %CA%BB%21%89%8CQ%A0F%3EN%E5%7Cs%01%5D%060A%C8%AF