[Django] #30405: IndexError in _get_lines_from_file when module does not match file contents (via loader)

13 views
Skip to first unread message

Django

unread,
Apr 26, 2019, 12:49:23 AM4/26/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
-------------------------------------------+------------------------
Reporter: Daniel Hahler | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
{{{
self = <django.views.debug.ExceptionReporter object at 0x7f2a7908ac18>
filename = '…/project/.venv/lib/python3.7/site-packages/pdb.py'
lineno = 230
context_lines = 7
loader = <_frozen_importlib_external.SourceFileLoader object at
0x7f2a73609278>
module_name = 'pdb'

[23]
…/Vcs/django/django/core/handlers/exception.py(90)response_for_exception()
-> response = handle_uncaught_exception(request,
get_resolver(get_urlconf()), sys.exc_info())
[24]
…/Vcs/django/django/core/handlers/exception.py(125)handle_uncaught_exception()
-> return debug.technical_500_response(request, *exc_info)
[25] …/Vcs/django/django/views/debug.py(94)technical_500_response()
-> html = reporter.get_traceback_html()
[26] …/Vcs/django/django/views/debug.py(333)get_traceback_html()
-> c = Context(self.get_traceback_data(), use_l10n=False)
[27] …/Vcs/django/django/views/debug.py(264)get_traceback_data()
-> frames = self.get_traceback_frames()
[28] …/Vcs/django/django/views/debug.py(427)get_traceback_frames()
-> filename, lineno, 7, loader, module_name,

385 try:
386 context_line = source[lineno]
387 except:
388 __import__('pdb').set_trace()
389 -> post_context = source[lineno + 1:upper_bound]
390
391 return lower_bound, pre_context, context_line,
post_context
(Pdb++) source
['# this file is needed to hijack pdb without eggs', 'import os.path',
"pdb_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
'pdb.py')", 'with open(pdb_path) as f:', " exec(compile(f.read(),
pdb_path, 'exec'))"]
}}}

It uses the loader
(https://github.com/django/django/blob/47885278c669dd7a13a4c3ff7e58e1cbe88af385/django/views/debug.py#L351),
which picks up the `pth`, and then the contents does not match the
expected line number.

I think it should maybe always use the given filename?!

--
Ticket URL: <https://code.djangoproject.com/ticket/30405>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 26, 2019, 10:38:58 AM4/26/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
---------------------------------+--------------------------------------

Reporter: Daniel Hahler | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by Carlton Gibson):

Hey Daniel. Can you give some reproduce steps that are a little easier to
follow please? Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:1>

Django

unread,
Apr 26, 2019, 11:13:28 AM4/26/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
---------------------------------+--------------------------------------

Reporter: Daniel Hahler | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by Daniel Hahler):

It happened for me while using pdb, and pdb itself crashed
(https://github.com/antocuni/pdb/issues/198).

I am using pdb++/pdbpp, which replaces the original "pdb" module itself
(https://github.com/antocuni/pdb/blob/81f8ea9f09d9179aceefe9676e47254a8cd95d05/pdb.py#L140-L154).

So in general the issue might not be very common, but I wanted to report
it anyway after investigating, since I think that error handling should be
robust by itself.

I guess you could simulate it in a unittest by making
``loader.get_source(module_name)`` not match the source.

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:2>

Django

unread,
Apr 30, 2019, 4:23:45 AM4/30/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
---------------------------------+------------------------------------

Reporter: Daniel Hahler | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Florian Apolloner):

* stage: Unreviewed => Accepted


Comment:

I think I have seen similar things in the past when using Jinja, in this
case the source would be the HTML file which is generally shorter than the
generated py code. If you could supply a Unittest, that would be perfect.

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:3>

Django

unread,
Apr 30, 2019, 4:40:51 AM4/30/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
---------------------------------+------------------------------------

Reporter: Daniel Hahler | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Florian Apolloner):

* cc: Florian Apolloner (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:4>

Django

unread,
Aug 31, 2019, 9:01:58 AM8/31/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: (none) => Hasan Ramezani
* status: new => assigned


Comment:

@Florian, as far as I understand the solution would be something like
this. am I right?


{{{
diff --git a/django/views/debug.py b/django/views/debug.py
index 86da47ee20..8af1c2e1ca 100644
--- a/django/views/debug.py
+++ b/django/views/debug.py
@@ -349,7 +349,7 @@ class ExceptionReporter:
if hasattr(loader, 'get_source'):
try:
source = loader.get_source(module_name)
- except ImportError:
+ except Exception:
pass
if source is not None:
source = source.splitlines()

}}}

If you agree with me I can prepare a patch and include the test.

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:5>

Django

unread,
Oct 8, 2019, 6:04:57 PM10/8/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:6>

Django

unread,
Nov 12, 2019, 5:53:06 AM11/12/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Error reporting | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"e8de188c06823ed5c574778dc1e47f035d7d0bf9" e8de188c]:
{{{
#!CommitTicketReference repository=""
revision="e8de188c06823ed5c574778dc1e47f035d7d0bf9"
Refs #30405 -- Added ExceptionReporter._get_source().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:7>

Django

unread,
Nov 12, 2019, 5:53:08 AM11/12/19
to django-...@googlegroups.com
#30405: IndexError in _get_lines_from_file when module does not match file contents
(via loader)
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Error reporting | Version: 2.2
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"4b78546ef19b93c02c60806c065e4560ee4fbee3" 4b78546e]:
{{{
#!CommitTicketReference repository=""
revision="4b78546ef19b93c02c60806c065e4560ee4fbee3"
Fixed #30405 -- Fixed source code mismatch crash in ExceptionReporter.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30405#comment:8>

Reply all
Reply to author
Forward
0 new messages