Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76631

Re: why i can't get the sourcecode with inspect module?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'lines,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'python': 0.11; 'def': 0.12; "'__doc__',": 0.16; '8bit%:26': 0.16; 'available;': 0.16; 'did,': 0.16; 'finney': 0.16; 'oserror': 0.16; 'preserve': 0.16; 'raised.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'meant': 0.20; '>>>': 0.22; 'code,': 0.22; 'input': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'source': 0.25; 'defined': 0.27; 'skip:" 20': 0.27; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'inspect': 0.31; 'really,': 0.31; 'writes:': 0.31; 'file': 0.32; '(most': 0.33; 'maybe': 0.34; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'more,': 0.35; 'but': 0.35; 'accessible': 0.36; 'subject:?': 0.36; 'ben': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'took': 0.61; 'name': 0.63; 'skip:\xe2 10': 0.65; 'reads': 0.68; '8bit%:46': 0.78; 'subject:get': 0.81; 'received:125': 0.84; 'vain': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: why i can't get the sourcecode with inspect module?
Date Wed, 20 Aug 2014 13:21:48 +1000
References <53F41076.8010508@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:ZUt/jjvOrv58U8WYos22X4/GfbE=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13187.1408504921.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408504921 news.xs4all.nl 2846 [2001:888:2000:d::a6]:52519
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76631

Show key headers only | View raw


luofeiyu <elearn2014@gmail.com> writes:

> >>> import inspect
> >>> def changer(x,y):
> ...     return(x+y)
> ...

At this point, you have defined a function. It is accessible via the
‘changer’ name, and the code is available.

But the source code is not available; Python reads standard input but
doesn't preserve it.

> >>> dir()
> ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__
>  'changer', 'inspect']

I don't know what this is meant to demonstrate.

Maybe ‘dir(changer.__code__)’ would be instructive.

> >>> inspect.getsource(changer)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "D:\Python34\lib\inspect.py", line 830, in getsource
>     lines, lnum = getsourcelines(object)
>   File "D:\Python34\lib\inspect.py", line 819, in getsourcelines
>     lines, lnum = findsource(object)
>   File "D:\Python34\lib\inspect.py", line 667, in findsource
>     raise OSError('could not get source code')
> OSError: could not get source code
> >>>

Exactly. The ‘inspect.getsource’ function gets the source code, if it's
available. The source code doesn't exist any more, so it's not
available; an OSError is raised.

-- 
 \     “You say I took the name in vain / I don't even know the name / |
  `\    But if I did, well, really, what's it to you?” —Leonard Cohen, |
_o__)                                                     _Hallelujah_ |
Ben Finney

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: why i can't get the sourcecode with inspect module? Ben Finney <ben+python@benfinney.id.au> - 2014-08-20 13:21 +1000

csiph-web