Using contextlib with cython
See original GitHub issueI am interested in using contextlib
with a cdef
class in cython in order to nicely wrap some OpenGL code. I had asked this question on stackoverflow. While the workaround I had received worked (using __enter__
and __exit__
),
Here is the test code for vbo.pyx
that was in that answer for reference:
import contextlib
cdef class Vbo:
def __init__(self):
pass
@contextlib.contextmanager
def bind(self):
self.do_something()
try:
yield
finally:
print("Finally")
def do_something(self):
print("something")
This code works for me if I remove the cdef
but this should not be necessary. If I use the sample code as is, I get an error message like this when I use that cdef
class from python:
Traceback (most recent call last):
File "main.py", line 2, in <module>
import vbo
File "vbo.pyx", line 21, in init vbo (vbo.c:15766)
@contextlib.contextmanager
File "C:\Python27\lib\contextlib.py", line 82, in contextmanager
@wraps(func)
File "C:\Python27\lib\functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'method_descriptor' object has no attribute '__module__'
It appears that this behavior might be a possible regression as this works apparently in cython 0.25.1 and python 3.6.1. I am using cython 0.25.2 and python 2.7.12.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Using contextlib with cython - python - Stack Overflow
Having tried a simplified version of your code it appears to work for me as is (Cython 0.25.1, Python 3.6.1): import contextlib cdef...
Read more >C API: Making a context manager - narkive
documentation about how to make a context manager using the C API. The code I am working to produce is the following (its...
Read more >using context manager for C malloc/free - Google Groups
I use this context manager in multiple files. Because of the `def` functions it appears like I can not place it in a...
Read more >contextlib — Utilities for with-statement contexts ... - Python Docs
contextmanager () uses ContextDecorator so the context managers it creates can be used as decorators as well as in with statements. When used...
Read more >cython - Jupyter Notebooks Gallery
In [2]:. %%cython # cython: language_level = 3 from libcpp.vector cimport vector cdef ... 472 self.build_extension(ext) 473 474 @contextlib.contextmanager ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, I bumped into this issue and realized the documentation of the ‘binding’ directive may need some additional explanation by adding an example (such as this case). Reading it twice I still have little clue what ‘like len’ and ‘binding to an instance’ really means. Thanks! (actually perhaps all of the directives need more documentation…)
Allow me to point to a blogpost of mine which explains some of this: http://opendreamkit.org/2017/06/09/CythonSphinx/