How to test if Cython property is generator?











up vote
2
down vote

favorite












In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:



%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1


The call looks like



SomeCls().x
# prints <generator at 0x102f61ee8>


I am having trouble testing if that property is a generator:



import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False

import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False


How can I determine whether a property of a Cython class is a generator?










share|improve this question






















  • Avoid answering questions in comments.
    – user1717828
    Nov 21 at 20:56










  • Testing against types.GeneratorType works just fine when not using cython...
    – Eric
    Nov 21 at 21:00










  • Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
    – hpaulj
    Nov 21 at 21:15










  • This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
    – DavidW
    Nov 21 at 22:37















up vote
2
down vote

favorite












In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:



%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1


The call looks like



SomeCls().x
# prints <generator at 0x102f61ee8>


I am having trouble testing if that property is a generator:



import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False

import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False


How can I determine whether a property of a Cython class is a generator?










share|improve this question






















  • Avoid answering questions in comments.
    – user1717828
    Nov 21 at 20:56










  • Testing against types.GeneratorType works just fine when not using cython...
    – Eric
    Nov 21 at 21:00










  • Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
    – hpaulj
    Nov 21 at 21:15










  • This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
    – DavidW
    Nov 21 at 22:37













up vote
2
down vote

favorite









up vote
2
down vote

favorite











In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:



%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1


The call looks like



SomeCls().x
# prints <generator at 0x102f61ee8>


I am having trouble testing if that property is a generator:



import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False

import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False


How can I determine whether a property of a Cython class is a generator?










share|improve this question













In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:



%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1


The call looks like



SomeCls().x
# prints <generator at 0x102f61ee8>


I am having trouble testing if that property is a generator:



import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False

import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False


How can I determine whether a property of a Cython class is a generator?







python types generator cython isinstance






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 20:55









user1717828

3,23151637




3,23151637












  • Avoid answering questions in comments.
    – user1717828
    Nov 21 at 20:56










  • Testing against types.GeneratorType works just fine when not using cython...
    – Eric
    Nov 21 at 21:00










  • Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
    – hpaulj
    Nov 21 at 21:15










  • This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
    – DavidW
    Nov 21 at 22:37


















  • Avoid answering questions in comments.
    – user1717828
    Nov 21 at 20:56










  • Testing against types.GeneratorType works just fine when not using cython...
    – Eric
    Nov 21 at 21:00










  • Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
    – hpaulj
    Nov 21 at 21:15










  • This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
    – DavidW
    Nov 21 at 22:37
















Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56




Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56












Testing against types.GeneratorType works just fine when not using cython...
– Eric
Nov 21 at 21:00




Testing against types.GeneratorType works just fine when not using cython...
– Eric
Nov 21 at 21:00












Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15




Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15












This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
– DavidW
Nov 21 at 22:37




This doesn't fix the problem, but Cython now supports the standard @property syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
– DavidW
Nov 21 at 22:37












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Why doesn't the usual way work?



First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...) and isinstance(..., types.GeneratorType) - the inspect-module just calls isinstance(..., types.GeneratorType).



On the other hand, types.GeneratorType is defined as



def _g():
yield 1
GeneratorType = type(_g())


CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance will boil down to comparing the C-object types.



However, Cython returns a __pyx_CoroutineObject for generators (just check the cythonized code to see)



typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;


which has nothing to do with PyGenObject as far as isinstanceis concerned - it doesn't really care whether generator is in the name of the type (but for us humans it can be really puzzling, because type(obj) says "generator").



So you will have to roll out your own version of isgenerator, which takes also Cython-"generators" into account. There are many ways, for example



%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)


and now:



import inspect   
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)

isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False





share|improve this answer





















  • This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
    – user1717828
    Nov 27 at 18:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420354%2fhow-to-test-if-cython-property-is-generator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Why doesn't the usual way work?



First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...) and isinstance(..., types.GeneratorType) - the inspect-module just calls isinstance(..., types.GeneratorType).



On the other hand, types.GeneratorType is defined as



def _g():
yield 1
GeneratorType = type(_g())


CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance will boil down to comparing the C-object types.



However, Cython returns a __pyx_CoroutineObject for generators (just check the cythonized code to see)



typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;


which has nothing to do with PyGenObject as far as isinstanceis concerned - it doesn't really care whether generator is in the name of the type (but for us humans it can be really puzzling, because type(obj) says "generator").



So you will have to roll out your own version of isgenerator, which takes also Cython-"generators" into account. There are many ways, for example



%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)


and now:



import inspect   
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)

isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False





share|improve this answer





















  • This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
    – user1717828
    Nov 27 at 18:45















up vote
2
down vote



accepted










Why doesn't the usual way work?



First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...) and isinstance(..., types.GeneratorType) - the inspect-module just calls isinstance(..., types.GeneratorType).



On the other hand, types.GeneratorType is defined as



def _g():
yield 1
GeneratorType = type(_g())


CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance will boil down to comparing the C-object types.



However, Cython returns a __pyx_CoroutineObject for generators (just check the cythonized code to see)



typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;


which has nothing to do with PyGenObject as far as isinstanceis concerned - it doesn't really care whether generator is in the name of the type (but for us humans it can be really puzzling, because type(obj) says "generator").



So you will have to roll out your own version of isgenerator, which takes also Cython-"generators" into account. There are many ways, for example



%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)


and now:



import inspect   
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)

isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False





share|improve this answer





















  • This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
    – user1717828
    Nov 27 at 18:45













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Why doesn't the usual way work?



First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...) and isinstance(..., types.GeneratorType) - the inspect-module just calls isinstance(..., types.GeneratorType).



On the other hand, types.GeneratorType is defined as



def _g():
yield 1
GeneratorType = type(_g())


CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance will boil down to comparing the C-object types.



However, Cython returns a __pyx_CoroutineObject for generators (just check the cythonized code to see)



typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;


which has nothing to do with PyGenObject as far as isinstanceis concerned - it doesn't really care whether generator is in the name of the type (but for us humans it can be really puzzling, because type(obj) says "generator").



So you will have to roll out your own version of isgenerator, which takes also Cython-"generators" into account. There are many ways, for example



%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)


and now:



import inspect   
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)

isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False





share|improve this answer












Why doesn't the usual way work?



First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...) and isinstance(..., types.GeneratorType) - the inspect-module just calls isinstance(..., types.GeneratorType).



On the other hand, types.GeneratorType is defined as



def _g():
yield 1
GeneratorType = type(_g())


CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance will boil down to comparing the C-object types.



However, Cython returns a __pyx_CoroutineObject for generators (just check the cythonized code to see)



typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;


which has nothing to do with PyGenObject as far as isinstanceis concerned - it doesn't really care whether generator is in the name of the type (but for us humans it can be really puzzling, because type(obj) says "generator").



So you will have to roll out your own version of isgenerator, which takes also Cython-"generators" into account. There are many ways, for example



%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)


and now:



import inspect   
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)

isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 6:44









ead

12k22455




12k22455












  • This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
    – user1717828
    Nov 27 at 18:45


















  • This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
    – user1717828
    Nov 27 at 18:45
















This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45




This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set SpacyGeneratorType = type(doc.subtree) and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420354%2fhow-to-test-if-cython-property-is-generator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Fiat S.p.A.

Type 'String' is not a subtype of type 'int' of 'index'