[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2020-11-29 Thread Alexey Volkov


Alexey Volkov  added the comment:

This is also an issue even for non-interactive scenarios:

When doing `python -c ''` inspect.getsource does not work and there 
are no stack traces.

Perhaps this case will be easier to fix?

--
nosy: +Ark-kun

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2018-12-11 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Do we really need to say that getsource(object) can only get the object's 
source if it is accessible from the object?  Getsource also fails if a module 
is loaded from a .pyc with not corresponding .py available.

The problem is not the call being in __main__.  When I put the three lines 
(with the 3rd wrapped with print()) in an IDLE editor and run, and re-inspect, 
I get

 RESTART: F:\Python\a\tem3.py 
class A:
pass

>>> inspect.getsource(A)
'class A:\npass\n'

Ditto if I run > py -i -m a.tem3

If I continue in IDLE's Shell

>>> class B: pass

>>> inspect.getsource(B)
Traceback (most recent call last):
  File "", line 1, in 
inspect.getsource(B)
  File "F:\dev\37\lib\inspect.py", line 973, in getsource
lines, lnum = getsourcelines(object)
  File "F:\dev\37\lib\inspect.py", line 955, in getsourcelines
lines, lnum = findsource(object)
  File "F:\dev\37\lib\inspect.py", line 812, in findsource
raise OSError('could not find class definition')
OSError: could not find class definition

If I enter the three lines above in a fress python or IDLEs shell, I get the 
TypeError above.

IDLE does store interactive inputs into linecache, so that tracebacks contain 
the offending line (unlike interactive python). But it does so on a statement 
by statement basis, so that each entry is treated as a separate file.  In a 
traceback for an exception in a multiline statement, the line number is 
relative to the statement.

>>> def f():
# line2 of f
1/0


>>> f()
Traceback (most recent call last):
  File "", line 1, in 
f()
  File "", line 3, in f
1/0
ZeroDivisionError: division by zero

Interactive python displays '' as the file for all entries.  IDLE 
numbers them, so previous statements remained cached.  I consider enhanced 
interactive tracebacks to be an important feature.

But I don't see how to attach individual pseudofile names to classes and 
functions so that getsource could find their source lines.

--
nosy: +terry.reedy
versions: +Python 3.7, Python 3.8 -Python 2.7, Python 3.4, Python 3.5, Python 
3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2018-10-29 Thread Ivan Pozdeev


Ivan Pozdeev  added the comment:

See 
https://bugs.python.org/issue33826?@ok_message=msg%20328824%20created%0Aissue%2033826%20message_count%2C%20messages%20edited%20ok&@template=item#msg319692
 how IPython stores source from interactive input and why it's not appropriate 
for vanilla REPL IMO.

--
nosy: +Ivan.Pozdeev

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2017-07-05 Thread R. David Murray

R. David Murray added the comment:

Probably.  Figure out a protocol to inject them into linecache, perhaps.  But 
I'm not sure such a thing would be accepted.  If you can figure out a way to 
make it work at least theoretically, it would probably be best to talk about it 
on python-ideas first.

In the meantime it would be nice to improve the error message, which is what we 
should use this issue for.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2017-07-05 Thread Vitor Pereira

Vitor Pereira added the comment:

So, what would be the right approach here? Store the interactive session's 
input text in memory?

--
nosy: +vmsp

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2016-05-18 Thread Steven Barker

Steven Barker added the comment:

The problem being discussed here just came up on Stack Overflow today: 
http://stackoverflow.com/questions/37288135/inspect-module-for-python/

The cause of the incorrect error message is pretty clear. The relevant code 
from `inspect.getfile` should do something better when the object has a 
`__module__` attribute, but the module named (when looked up in `sys.modules`) 
does not have a `__file__` attribute. Currently it says the module is a builtin 
class, which is total nonsense.

A very basic fix would be to have an extra error case:

if isclass(object):
if hasattr(object, '__module__'):
object = sys.modules.get(object.__module__)
if hasattr(object, '__file__'):
return object.__file__
raise TypeError()  # need a relevant message here!!!
raise TypeError('{!r} is a built-in class'.format(object))

It might be easier to make a meaningful message if the code after the first 
`if` didn't overwrite `object` with the module.

But, really, it would be nice to figure out a better fix, which would make the 
relevant inspect functions actually work for classes defined interactively in 
the `__main__` module.

--
nosy: +Steven.Barker

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2015-10-12 Thread nikitakit

nikitakit added the comment:

I just ran into this issue trying to introspect an IPython session, in which 
case the __main__ module doesn't have a file associated with it.

But it turns out that methods defined in a class do have source code associated 
with them, so it's possible to add a workaround for the common case where a 
class actually has methods.

Code: https://gist.github.com/nikitakit/642cb96febdf2f812d0b

--
nosy: +nikitakit

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2015-06-24 Thread Zorceta

Zorceta added the comment:

 When provided object is not from a file

should be

'When `inspect` can't find the source file of provided object'.

My mistake.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue12920
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2015-06-23 Thread Zorceta

Changes by Zorceta zorc...@gmail.com:


--
nosy:  -docs@python

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue12920
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2015-06-23 Thread Zorceta

Changes by Zorceta zorc...@gmail.com:


--
components: +IDLE, Interpreter Core -Documentation

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue12920
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12920] inspect.getsource only works for objects loaded from files, not interactive session

2015-06-23 Thread Zorceta

Zorceta added the comment:

When provided object is not from a file, like input in interactive shell, 
`inspect` internals will check for it in `linecache`, which official Python 
shell and IDLE won't put interactive shell input into, yet. This can be simply 
solved.

Whether interactive shell input can be put into `linecache` may be a problem, 
but it'll make life easier, as interactive shell saves time from edit-save-run 
'loop'.

btw, I changed the title, since I don't think, what original author thought 
need to be documented, is absolutely right.

--
title: Document that inspect.getsource only works for objects loaded from 
files, not interactive session - inspect.getsource only works for objects 
loaded from files, not interactive session

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue12920
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com