Your problem is not the sharing, it's the "nogil". Read the error messages
above, they are quite clear about what's happening and why this cannot work
when the code does not own the GIL. One of the reasons why the GIL exists
is to protect the object reference counting, i.e. exactly the kind of
operations you do above.
Just remove the "nogil" declaration from the test() function. If you really
need to call it without holding the GIL, declare it as "with gil" instead,
so that it can acquire the GIL when being called. However, if you tell us a
bit more about your intention, we might be able to give better advice.
Stefan