﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29543	CPointerBase.__del__() crashes with ImportError	Evandro Myller	Mushtaq Ali	"I think something is wrong when GeoDjango tries to cleanup a pointer before the program exits.

I noticed this while writing some tests; everything goes just fine if all tests pass, but I'm getting this whenever pytest exits with a failure:

{{{
============================================== FAILURES ==============================================
_________________________________________TestManagerGetQueryset.test_address ___________________________________________
self = <unit.models.test_address.TestManagerGetQueryset object at 0x7f740aee0828>, address = <Address: ...>

    def test_address(self, address):
>       raise
E       RuntimeError: No active exception to reraise

tests/unit/models/test_address.py:14: RuntimeError
======================================================================================================== 1 failed, 1 error in 3.82 seconds ========================================================================================================
Exception ignored in: <bound method CPointerBase.__del__ of <Point object at 0x7f740afb2b38>>
Traceback (most recent call last):
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py"", line 36, in __del__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 155, in __call__
  File ""/usr/local/lib/python3.6/site-packages/django/utils/functional.py"", line 36, in __get__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <bound method CPointerBase.__del__ of <Polygon object at 0x7f740afb2808>>
Traceback (most recent call last):
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py"", line 36, in __del__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 155, in __call__
  File ""/usr/local/lib/python3.6/site-packages/django/utils/functional.py"", line 36, in __get__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <bound method CPointerBase.__del__ of <Polygon object at 0x7f740afb2cd0>>
Traceback (most recent call last):
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py"", line 36, in __del__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 155, in __call__
  File ""/usr/local/lib/python3.6/site-packages/django/utils/functional.py"", line 36, in __get__
  File ""/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py"", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down
}}}

This is how I'm able to patch it:

{{{#!python
import pytest
from django.contrib.gis.ptr import CPointerBase


@pytest.fixture(autouse=True, scope=""session"")
def bad_del_fix():
    """"""
    Fix a bad __del__ happening at GeoDjango
    """"""
    original_del = CPointerBase.__del__

    def patched_del(self):
        try:
            original_del(self)
        except ImportError:
            pass

    CPointerBase.__del__ = patched_del
}}}

I'm not really sure if the fix is just add `ImportError` to the exception list it expects for at https://github.com/django/django/blob/2.0/django/contrib/gis/ptr.py#L37."	Bug	closed	GIS	2.0	Normal	fixed			Accepted	1	0	1	0	0	0
