[Python-Dev] Py_CLEAR and assigning values

"Martin v. Löwis" martin at v.loewis.de
Wed Aug 6 17:55:07 CEST 2008


Guido van Rossum wrote:
> I don't see a way that __del__ could be invoked and access the NULL
> between the Py_CLEAR() call and the Py_INCREF() call in the second
> version.

Consider this:

  class Bad:
    def __del__(self):
      obj.some_method()

  obj.set_foo(Bad())
  obj.set_foo(None)

Now, the second set_foo does a Py_CLEAR on foo, which invokes
Bad.__del__, which invokes a method on obj, which crashes as foo
is NULL - it has not yet been assigned Py_None as expected.

Regards,
Martin


More information about the Python-Dev mailing list