1

I'm trying to convert one of my Python module using Cython. Cython successfully generates a .pyd file, but when I try to import it, it fails:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 6, in init example
    @abstractmethod
  File "C:\Program Files\Python3\lib\abc.py", line 23, in abstractmethod
    funcobj.__isabstractmethod__ = True
AttributeError: attribute '__isabstractmethod__' of 'staticmethod' objects is not writable

My module contains a method which is both abstract and static:

from abc import abstractmethod

class c:
    @abstractmethod
    @staticmethod
    def foo():
        raise NotImplementedError

According to https://docs.python.org/3/library/abc.html#abc.abstractmethod this should be possible. If I remove the @abstractmethod annotation, I can cythonize and import the module. It also works if i use @abstractstaticmethod, but that decorator is deprecated in abc.

Is this a Cython bug, or am I doing something wrong? I'm using Cython version 0.28.5 with Build Tools für Visual Studio 2017 on Windows 7.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Can you try running Cython with the `language_level=3` compiler argument (see [the documentation](https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives) for how to apply it)? Cython defaults to Python 2 behaviour by default some of the time. Otherwise it's a bug and should be reported to https://github.com/cython/cython/issues – DavidW Oct 10 '18 at 16:58
  • Thanks for the hint to the compiler argument. Through example code from [here](https://stackoverflow.com/questions/34603628/how-to-specify-python-3-source-in-cythons-setup-py) I found out that it was not on level 3. Still, after adding the `!python` `#cython: language_level=3` directive to example.py, the `__isabstractmethod__` error persists, thus I'm going to report it as a Cython issue. – Daniel Herding Oct 11 '18 at 07:37
  • The new Cython issue is here: https://github.com/cython/cython/issues/2651 – Daniel Herding Oct 11 '18 at 07:56
  • Please note that my above original contains a mistake. As I put the `@abstractmethod` decorator before `@staticmethod`, the example module wouldn't even load in plain Python, as described here: https://github.com/cython/cython/issues/2651 Putting `@staticmethod` first makes the example runnable in Python, as long as you don't cythonize it. – Daniel Herding Oct 11 '18 at 07:59

0 Answers0