question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using contextlib with cython

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rainwoodmancommented, Aug 10, 2017

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…)

0reactions
jdemeyercommented, Aug 11, 2017

Allow me to point to a blogpost of mine which explains some of this: http://opendreamkit.org/2017/06/09/CythonSphinx/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found