This copy is usually gratuitous.  No need to copy weight if it was created in this function -- only if it was originally input as an ImageI by the user.  I would recommend using a new variable name for the thing you plan to return, and remove the copy here.  

Maybe first check `if isinstance(weight.view(), galsim.ImageViewI) and not badpix: return weight`  Then go ahead and create the object to be returned and you know you're allowed to modify it.