Skip to content

Commit 75f6421

Browse files
cvdubdakra
authored andcommitted
Ignore Python objects with no source location
Some objects are created dynamically rather than defined explicitly in a file. Calling "findsource" on these objects raises an OSError. Because these objects don't exist in an actual file, a user can't jump to them, so it makes sense to exclude them from Djangonaut's results.
1 parent 3e15470 commit 75f6421

File tree

1 file changed

+109
-18
lines changed

1 file changed

+109
-18
lines changed

djangonaut.el

Lines changed: 109 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ from django.core.management import get_commands
9292
for command_name, module_name in get_commands().items():
9393
module = import_module(module_name + '.management.commands.' + command_name)
9494
command = module.Command
95-
result[command_name] = [getsourcefile(command), findsource(command)[1]]
95+
try:
96+
source = findsource(command)
97+
except OSError:
98+
pass
99+
else:
100+
result[command_name] = [getsourcefile(command), source[1]]
101+
96102
97103
" "Python source code to get command definitions.")
98104

@@ -185,15 +191,25 @@ except ImportError:
185191
for site in all_sites:
186192
for admin_instance in site._registry.values():
187193
admin_class = admin_instance.__class__
188-
result[str(admin_instance)] = [getsourcefile(admin_class), findsource(admin_class)[1]]
194+
try:
195+
source = findsource(admin_class)
196+
except OSError:
197+
pass
198+
else:
199+
result[str(admin_instance)] = [getsourcefile(admin_class), source[1]]
189200
190201
" "Python source code to get admin classes.")
191202

192203
(defvar djangonaut-get-models-code "
193204
from inspect import findsource, getsourcefile
194205
195206
for model in apps.get_models():
196-
result[model._meta.app_label + '.' + model.__name__] = [getsourcefile(model), findsource(model)[1]]
207+
try:
208+
source = findsource(model)
209+
except OSError:
210+
pass
211+
else:
212+
result[model._meta.app_label + '.' + model.__name__] = [getsourcefile(model), source[1]]
197213
198214
" "Python source code to get models.")
199215

@@ -206,7 +222,12 @@ from django.db.models import Manager
206222
for obj in get_objects():
207223
if isclass(obj) and issubclass(obj, Manager):
208224
name = getmodule(obj).__name__ + '.' + obj.__name__
209-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
225+
try:
226+
source = findsource(obj)
227+
except OSError:
228+
pass
229+
else:
230+
result[name] = [getsourcefile(obj), source[1]]
210231
211232
" "Python source code to get model managers.")
212233

@@ -221,7 +242,12 @@ loader.load_disk()
221242
for (label, module_name), migration in sorted(loader.disk_migrations.items()):
222243
name = label + '.' + module_name
223244
Migration = migration.__class__
224-
result[name] = [getsourcefile(Migration), findsource(Migration)[1]]
245+
try:
246+
source = findsource(Migration)
247+
except OSError:
248+
pass
249+
else:
250+
result[name] = [getsourcefile(Migration), source[1]]
225251
226252
" "Python source code to get migrations.")
227253

@@ -234,7 +260,12 @@ from django.db.models import Func
234260
for obj in get_objects():
235261
if isclass(obj) and issubclass(obj, Func):
236262
name = getmodule(obj).__name__ + '.' + obj.__name__
237-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
263+
try:
264+
source = findsource(obj)
265+
except OSError:
266+
pass
267+
else:
268+
result[name] = [getsourcefile(obj), source[1]]
238269
239270
" "Python source code to get sql functions.")
240271

@@ -253,7 +284,12 @@ for obj in get_objects():
253284
if receiver is None:
254285
continue
255286
name = getmodule(receiver).__name__ + '.' + receiver.__name__
256-
result[name] = [getsourcefile(receiver), findsource(receiver)[1]]
287+
try:
288+
source = findsource(receiver)
289+
except OSError:
290+
pass
291+
else:
292+
result[name] = [getsourcefile(receiver), source[1]]
257293
258294
" "Python source code to get signal receivers.")
259295

@@ -269,7 +305,12 @@ import_module(settings.ROOT_URLCONF)
269305
for obj in get_objects():
270306
if isclass(obj) and issubclass(obj, Serializer):
271307
name = getmodule(obj).__name__ + '.' + obj.__name__
272-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
308+
try:
309+
source = findsource(obj)
310+
except OSError:
311+
pass
312+
else:
313+
result[name] = [getsourcefile(obj), source[1]]
273314
274315
" "Python source code to get drf serializers.")
275316

@@ -285,7 +326,12 @@ import_module(settings.ROOT_URLCONF)
285326
for obj in get_objects():
286327
if isclass(obj) and issubclass(obj, BasePermission):
287328
name = getmodule(obj).__name__ + '.' + obj.__name__
288-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
329+
try:
330+
source = findsource(obj)
331+
except OSError:
332+
pass
333+
else:
334+
result[name] = [getsourcefile(obj), source[1]]
289335
290336
" "Python source code to get drf permissions.")
291337

@@ -333,20 +379,35 @@ def collect_views(resolver):
333379
# DRF as_view result.
334380
view = view.cls
335381
name = getmodule(view).__name__ + '.' + view.__name__
336-
result[name] = [getsourcefile(view), findsource(view)[1]]
382+
try:
383+
source = findsource(view)
384+
except OSError:
385+
pass
386+
else:
387+
result[name] = [getsourcefile(view), source[1]]
337388
for attrname in dir(view):
338389
view_attr = getattr(view, attrname)
339390
if getattr(view_attr, 'bind_to_methods', None):
340391
# DRF ViewSet method view.
341-
result[name + '.' + attrname] = [getsourcefile(view_attr), findsource(view_attr)[1]]
392+
try:
393+
source = findsource(view_attr)
394+
except OSError:
395+
pass
396+
else:
397+
result[name + '.' + attrname] = [getsourcefile(view_attr), source[1]]
342398
continue
343399
else:
344400
view = unwrap(view)
345401
if ismethod(view):
346402
name = getmodule(view).__name__ + '.' + view.__self__.__class__.__name__ + '.' + view.__name__
347403
else:
348404
name = getmodule(view).__name__ + '.' + view.__name__
349-
result[name] = [getsourcefile(view), findsource(view)[1]]
405+
try:
406+
source = findsource(view)
407+
except OSError:
408+
pass
409+
else:
410+
result[name] = [getsourcefile(view), source[1]]
350411
351412
collect_views(get_resolver(get_urlconf()))
352413
@@ -359,7 +420,12 @@ from django.utils.module_loading import import_string
359420
360421
for name in getattr(settings, 'MIDDLEWARE', None) or settings.MIDDLEWARE_CLASSES:
361422
middleware = import_string(name)
362-
result[name] = [getsourcefile(middleware), findsource(middleware)[1]]
423+
try:
424+
source = findsource(middleware)
425+
except OSError:
426+
pass
427+
else:
428+
result[name] = [getsourcefile(middleware), source[1]]
363429
364430
" "Python source code to get middlewares.")
365431

@@ -408,7 +474,12 @@ import_module(settings.ROOT_URLCONF)
408474
for obj in get_objects():
409475
if isclass(obj) and issubclass(obj, (BaseForm, BaseFormSet)):
410476
name = getmodule(obj).__name__ + '.' + obj.__name__
411-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
477+
try:
478+
source = findsource(obj)
479+
except OSError:
480+
pass
481+
else:
482+
result[name] = [getsourcefile(obj), source[1]]
412483
413484
" "Python source code to get forms.")
414485

@@ -424,7 +495,12 @@ import_module(settings.ROOT_URLCONF)
424495
for obj in get_objects():
425496
if isclass(obj) and issubclass(obj, Widget):
426497
name = getmodule(obj).__name__ + '.' + obj.__name__
427-
result[name] = [getsourcefile(obj), findsource(obj)[1]]
498+
try:
499+
source = findsource(obj)
500+
except OSError:
501+
pass
502+
else:
503+
result[name] = [getsourcefile(obj), source[1]]
428504
429505
" "Python source code to get widgets.")
430506

@@ -497,13 +573,23 @@ for library_name, library in libraries.items():
497573
for tag_name, tag in library.tags.items():
498574
tag = unwrap(tag)
499575
try:
500-
result[library_name + '.' + tag_name] = [getsourcefile(tag), findsource(tag)[1]]
576+
try:
577+
source = findsource(tag)
578+
except OSError:
579+
pass
580+
else:
581+
result[library_name + '.' + tag_name] = [getsourcefile(tag), source[1]]
501582
except TypeError:
502583
# This is Django 1.8 and we met functools.partial result. We take class defined
503584
# in the decorator from bound keyword arguments. This class has a method with a
504585
# closure where we can find decorated function.
505586
tag = tag.keywords['node_class'].render.__closure__[-1].cell_contents
506-
result[library_name + '.' + tag_name] = [getsourcefile(tag), findsource(tag)[1]]
587+
try:
588+
source = findsource(tag)
589+
except OSError:
590+
pass
591+
else:
592+
result[library_name + '.' + tag_name] = [getsourcefile(tag), source[1]]
507593
508594
" "Python source code to get template tags.")
509595

@@ -542,7 +628,12 @@ except ImportError:
542628
for library_name, library in libraries.items():
543629
for filter_name, filter in library.filters.items():
544630
filter = unwrap(filter)
545-
result[library_name + '.' + filter_name] = [getsourcefile(filter), findsource(filter)[1]]
631+
try:
632+
source = findsource(filter)
633+
except OSError:
634+
pass
635+
else:
636+
result[library_name + '.' + filter_name] = [getsourcefile(filter), source[1]]
546637
547638
" "Python source code to get template filters.")
548639

0 commit comments

Comments
 (0)