That code is quite old. This comment tries to explain it:
```
/* Check that the use doesn't do something silly and unsafe like
   object.__new__(dict). To do this, we check that the
    most derived base that's not a heap type is this type. */
```
I think you may have to special-case this and arrange for B.__new__() to be
called, like it or not.

(If you want us to change the code, please file a bpo bug report. I know
that's no fun, but it's the way to get the right people involved.)

On Mon, Feb 1, 2021 at 3:27 AM Phil Thompson via Python-Dev <
python-dev@python.org> wrote:

> Hi,
>
> I'm trying to understand the purpose of the check in tp_new_wrapper() of
> typeobject.c that results in the "is not safe" exception.
>
> I have the following class hierarchy...
>
> B -> A -> object
>
> ...where B and A are implemented in C. Class A has an implementation of
> tp_new which does a few context-specific checks before calling
> PyBaseObject_Type.tp_new() directly to actually create the object. This
> works fine.
>
> However I want to allow class B to be used with a Python mixin. A's
> tp_new() then has to do something similar to super().__new__(). I have
> tried to implement this by locating the type object after A in B's MRO,
> getting it's '__new__' attribute and calling it (using PyObject_Call())
> with B passed as the only argument. However I then get the "is not safe"
> exception, specifically...
>
> TypeError: object.__new__(B) is not safe, use B.__new__()
>
> I take the same approach for __init__() and that works fine.
>
> If I comment out the check in tp_new_wrapper() then everything works
> fine.
>
> So, am I doing something unsafe? If so, what?
>
> Or, is the check at fault in not allowing the case of a C extension type
> with its own tp_new?
>
> Thanks,
> Phil
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/HRGDEMURCJ5DSNEPMQPQR3R7VVDFA4ZX/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/X5EDFSASK7RKYISS7MVMHHYWMRRUSNAM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to