Giter VIP home page Giter VIP logo

Comments (17)

hashstat avatar hashstat commented on May 18, 2024

The problem is that func_code and func_closure were renamed code and closure in Python 3. Below is a patch that allows the introspection to work with both Python 2.x and 3.x.

--- old/introspectors.py
+++ new/introspectors.py
@@ -287,14 +287,20 @@
             yield ViewSetMethodIntrospector(self, methods[method], method)

     def _resolve_methods(self):
-        if not hasattr(self.pattern.callback, 'func_code') or \
-                not hasattr(self.pattern.callback, 'func_closure') or \
-                not hasattr(self.pattern.callback.func_code, 'co_freevars') or \
-                'actions' not in self.pattern.callback.func_code.co_freevars:
+        callback = self.pattern.callback
+        try:
+            try:
+                closure = callback.func_closure
+            except AttributeError:
+                closure = callback.__closure__
+            try:
+                code = callback.func_code
+            except AttributeError:
+                code = callback.__code__
+            freevars = code.co_freevars
+        except AttributeError:
             raise RuntimeError('Unable to use callback invalid closure/function specified.')
-
-        idx = self.pattern.callback.func_code.co_freevars.index('actions')
-        return self.pattern.callback.func_closure[idx].cell_contents
+        return closure[freevars.index('actions')].cell_contents


 class ViewSetMethodIntrospector(BaseMethodIntrospector):

The 2to3 tool automatically handles renaming the attributes, rendering the inner try-except blocks unnecessary. The code could become:

    def _resolve_methods(self):
        callback = self.pattern.callback
        try:
            closure = callback.func_closure
            freevars = callback.func_code.co_freevars
        except AttributeError:
            raise RuntimeError('Unable to use callback invalid closure/function specified.')
        return closure[freevars.index('actions')].cell_contents

from django-rest-swagger.

linuxlewis avatar linuxlewis commented on May 18, 2024

+1

from django-rest-swagger.

jeffself avatar jeffself commented on May 18, 2024

I've updated the _resolve_methods method in my introspectors.py to look like this:

def _resolve_methods(self):
    callback = self.pattern.callback
    try:
        closure = callback.func_closure
        freevars = callback.func_code.co_freevars
    except AttributeError:
        raise RuntimeError('Unable to use callback invalid closure/function specified.')
    return closure[freevars.index('actions')].cell_contents
    idx = self.pattern.callback.__code__.co_freevars.index('actions')
    return self.pattern.callback.__closure__[idx].cell_contents

However, I'm still getting a 'Unable to use callback invalid closure/function specified.' error. I'm running Python 3.4.1. Any thoughts?

from django-rest-swagger.

jason-kane avatar jason-kane commented on May 18, 2024

Did github mangle your code somehow? The snippet doesn't make very much sense to me.

I'm using (with Python 3.4):

    def _resolve_methods(self):
        callback = self.pattern.callback
        try:
            try:
                closure = callback.func_closure
            except AttributeError:
                closure = callback.__closure__
            try:
                code = callback.func_code
            except AttributeError:
                code = callback.__code__
            freevars = code.co_freevars
        except AttributeError:
            raise RuntimeError('Unable to use callback invalid closure/function specified.')

        return closure[freevars.index('actions')].cell_contents

from django-rest-swagger.

jeffself avatar jeffself commented on May 18, 2024

Yeah, not sure what happened there. Here's what I've got:

def _resolve_methods(self):
    callback = self.pattern.callback
    try:
        closure = callback.func_closure
        freevars = callback.func_code.co_freevars
    except AttributeError:
        raise RuntimeError('Unable to use callback invalid closure/function specified.')
    return closure[freevars.index('actions')].cell_contents

Still getting same error message.

from django-rest-swagger.

jason-kane avatar jason-kane commented on May 18, 2024

As far as I know that is supposed to work but I don't trust the 2to3 black magic. Try the long winded explicit version I posted above.

from django-rest-swagger.

jeffself avatar jeffself commented on May 18, 2024

Works! Thanks!

from django-rest-swagger.

johansja avatar johansja commented on May 18, 2024

Thanks for the snippet too. I am wondering if this will be patched into the code someday?

from django-rest-swagger.

nielsole avatar nielsole commented on May 18, 2024

Hey, this worked to let the error message go away, but instead I now get a Value error:
tuple.index(x): x not in tuple
from this line
return closure[freevars.index('actions')].cell_contents
freevars only contains: ('view_func',)
I used the code from @jason-kane .
Did anyone else experience this problem?

from django-rest-swagger.

jason-kane avatar jason-kane commented on May 18, 2024

Haven't seen that; it looks like there must be something missing in the function it is trying to introspect. 'actions' looks like it comes from DRF itself not the swagger addition. Does the DRF browser work?

from django-rest-swagger.

nielsole avatar nielsole commented on May 18, 2024

I am using Django 1.7 and DRF 2.4.3. The DRF browser works fine.

from django-rest-swagger.

jason-kane avatar jason-kane commented on May 18, 2024

looks like #123 is biting you. Symptoms are identical to #129

from django-rest-swagger.

nielsole avatar nielsole commented on May 18, 2024

you are right. Did not see them. Thank you. Too bad this repository is not maintained.

from django-rest-swagger.

jason-kane avatar jason-kane commented on May 18, 2024

I added what looks like a suitable fix to pull request #85 ; I haven't actually tried it against DRF 2.4.3 yet.

from django-rest-swagger.

FlogFr avatar FlogFr commented on May 18, 2024

sorry but still having the issue from installing by pip. Is it merged and updated to pypi ?

from django-rest-swagger.

allardhoeve avatar allardhoeve commented on May 18, 2024

Same here. Doesn't work with Django REST Framework 2.4.3 and Django 1.7.

from django-rest-swagger.

ariovistus avatar ariovistus commented on May 18, 2024

fixed in develop.

from django-rest-swagger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.