Ticket #232 (closed enhancement: fixed)

Opened 3 years ago

Last modified 3 years ago

result type of calling an extension type is unknown

Reported by: scoder Owned by: scoder
Priority: major Milestone: 0.12
Component: Optimization Keywords:
Cc:

Description

For an extension type MyExt, calling MyExt() can only result in an instance of type MyExt. However, Cython does not assign the type to the result and uses the generic Python object type instead, so that things like MyExt().some_c_func() fail.

Change History

Changed 3 years ago by scoder

  • component changed from Code Generation to Type Analysis

Changed 3 years ago by robertwb

Are we saying we won't ever provide new support?

Changed 3 years ago by scoder

  • priority changed from minor to major
  • type changed from defect to enhancement
  • component changed from Type Analysis to Optimization

The discussion on cython-dev showed that there are valid use cases for returning objects of a different type from a future __new__ special method. Therefore, this ticket turns into a request for optimising the most common case that calling a known type returns an instance of exactly the called type or at least a subtype.

Changed 3 years ago by cwitty

I'm copying this here from cython-dev:

How about this? In the hypothetical future version of Cython that allows overriding __new__, say that the overrides have to be declared in the .pxd file, with

   cdef TYPE __new__(self, foo, bar)

(where TYPE defaults to the class being declared, if omitted). So when generating code, if the class doesn't override __new__, you know that calling the class returns an instance; if the class does override __new__, then the return type is explicitly given. This could be a parent class, for my use case where I want to return an instance of a sibling class; or it could be "object", if anybody ever has a use case for having __new__ return an arbitrary object.

This idea means we can go ahead and optimize the common case as initially requested, without worrying about compatibility headaches with future Cython versions.

Changed 3 years ago by scoder

Changed 3 years ago by scoder

Supporting __new__() in extension types is ticket #238.

Changed 3 years ago by scoder

  • owner changed from somebody to scoder
  • status changed from new to assigned
  • milestone changed from wishlist to 0.12

Changed 3 years ago by scoder

  • status changed from assigned to closed
  • resolution set to fixed

Cython now handles extension type tracking like Pyrex. See ticket #238 for ___new__() support.

Note: See TracTickets for help on using tickets.