PY_NEW(Integer) and Integer.__new__(Integer)

46 views
Skip to first unread message

Vincent Delecroix

unread,
Apr 8, 2018, 3:41:17 AM4/8/18
to sage-devel
Dear all,

What is the status of Integer.__new__(Integer) vs PY_NEW(Integer). One
can find both versions in the source code

$ grep -R "PY_NEW(Integer)" | wc -l
152
$ grep -R "Integer.__new__(Integer)" | wc -l
94

But the doc of PY_NEW (ext/stdsage.pxd) says that we should avoid the
second one

Return ``t.__new__(t)``. This works even for types like
:class:`Integer` where we change ``tp_new`` at runtime (Cython
optimizations assume that ``tp_new`` doesn't change).

Should I change the 94 bad usage?

Vincent

PS: this happen to be a question from
https://trac.sagemath.org/ticket/24717

Jean-Pierre Flori

unread,
Apr 9, 2018, 4:27:38 AM4/9/18
to sage-devel

Erik Bray

unread,
Apr 9, 2018, 5:02:34 AM4/9/18
to sage-devel
On Mon, Apr 9, 2018 at 10:27 AM, Jean-Pierre Flori <jpf...@gmail.com> wrote:
> I thought PY_NEW was to be avoided (except for classes overriding tp_new).
> See https://trac.sagemath.org/ticket/17668 and
> https://github.com/cython/cython/wiki/FAQ#can-cython-create-objects-or-apply-operators-to-locally-created-objects-as-pure-c-code

Which is exactly what we're doing for Integer. In the ticket you cited:

"Change this everywhere except for Integer and RealDoubleElement
classes, which use a custom hooked tp_new that Cython isn't aware of."


> On Sunday, April 8, 2018 at 9:41:17 AM UTC+2, vdelecroix wrote:
>>
>> Dear all,
>>
>> What is the status of Integer.__new__(Integer) vs PY_NEW(Integer). One
>> can find both versions in the source code
>>
>> $ grep -R "PY_NEW(Integer)" | wc -l
>> 152
>> $ grep -R "Integer.__new__(Integer)" | wc -l
>> 94
>>
>> But the doc of PY_NEW (ext/stdsage.pxd) says that we should avoid the
>> second one
>>
>> Return ``t.__new__(t)``. This works even for types like
>> :class:`Integer` where we change ``tp_new`` at runtime (Cython
>> optimizations assume that ``tp_new`` doesn't change).
>>
>> Should I change the 94 bad usage?
>>
>> Vincent
>>
>> PS: this happen to be a question from
>> https://trac.sagemath.org/ticket/24717
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages