Comments (17)
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.
+1
from django-rest-swagger.
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.
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.
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.
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.
Works! Thanks!
from django-rest-swagger.
Thanks for the snippet too. I am wondering if this will be patched into the code someday?
from django-rest-swagger.
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.
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.
I am using Django 1.7 and DRF 2.4.3. The DRF browser works fine.
from django-rest-swagger.
looks like #123 is biting you. Symptoms are identical to #129
from django-rest-swagger.
you are right. Did not see them. Thank you. Too bad this repository is not maintained.
from django-rest-swagger.
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.
sorry but still having the issue from installing by pip. Is it merged and updated to pypi ?
from django-rest-swagger.
Same here. Doesn't work with Django REST Framework 2.4.3 and Django 1.7.
from django-rest-swagger.
fixed in develop.
from django-rest-swagger.
Related Issues (20)
- _frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('django.test.signals') HOT 3
- Hide some classes from doc
- AttributeError at /swagger/ 'NoneType' object has no attribute 'title'
- Django JWT authentication does not load all the protected urls HOT 2
- 'AutoSchema' object has no attribute 'get_link' HOT 4
- swagger when use models.ForeignKey('self')
- Swagger not setting token in Authorization header HOT 2
- Can't add custom headers
- Wrong swagger grouping of root urls HOT 1
- Some URLs are not displaed in Swagger UI
- (django_version >= 3.0) template tag "staticfiles" removed HOT 9
- On reloading Swagger UI, it is loosing the authentication
- Restrict access to only admins
- TypeError: Expected a 'coreapi.Document' instance
- How to handle TemplateDoesNotExist HOT 4
- keyerror
- API's not visibale in Django Swagger landing page
- Server Error (500) Adding swagger doc to my api
- Django 3+ not support staticfiles pls support this feature HOT 2
- request: add option to remove the need to click "try it out" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-swagger.