473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

__doc__ in compiled script

Hello

I have a script starting with a docstring. After compiling it with
compile(), is there any way I could get the docstring? __doc__ on the
code object doesn't work.
I can't use __import__ because the script has top-level statements
that have to be executed only once (it's not supposed to be used as a module).

--- begin test.py ---
body = """
"This is a docstring"

x=0
some(code).toBeExecuted("later")
"""

co = compile(body,'<string>','exec')
print co.__doc__

# what can I use for "something" below: ?
assert something(co)=="This is a docstring"
--- end test.py ---
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 2 '06 #1
5 1307
Gabriel Genellina wrote:
Hello

I have a script starting with a docstring. After compiling it with
compile(), is there any way I could get the docstring? __doc__ on the
code object doesn't work.
I can't use __import__ because the script has top-level statements
that have to be executed only once (it's not supposed to be used as a
module).

--- begin test.py ---
body = """
"This is a docstring"

x=0
some(code).toBeExecuted("later")
"""

co = compile(body,'<string>','exec')
print co.__doc__

# what can I use for "something" below: ?
assert something(co)=="This is a docstring"
--- end test.py ---
>>co.co_consts[list(co.co_names).index("__doc__")]
'This is a docstring'

or probably just
>>co.co_consts[0]
'This is a docstring'

as I cannot imaginge the docstring to have a non-zero index.

Peter

Nov 2 '06 #2
At Thursday 2/11/2006 17:54, Peter Otten wrote:
I have a script starting with a docstring. After compiling it with
compile(), is there any way I could get the docstring? __doc__ on the
code object doesn't work.
I can't use __import__ because the script has top-level statements
that have to be executed only once (it's not supposed to be used as a
module).
>co.co_consts[list(co.co_names).index("__doc__")]
'This is a docstring'
Good! I'll buy this one :) thanks!
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 3 '06 #3
Gabriel Genellina wrote:
>>co.co_consts[list(co.co_names).index("__doc__")]
'This is a docstring'

Good! I'll buy this one :) thanks!
however, there's no 1:1 mapping between names and constants; if you want
code that works by accident, you might as well use co_consts[0].

</F>

Nov 3 '06 #4
Fredrik Lundh wrote:
Gabriel Genellina wrote:
>>>co.co_consts[list(co.co_names).index("__doc__")]
'This is a docstring'

Good! I'll buy this one :) thanks!

however, there's no 1:1 mapping between names and constants; if you want
code that works by accident, you might as well use co_consts[0].
Seems like I hid the magic constant in some voodoo code. There is an
advantage over co_consts[0], though: it balks when there is no docstring.

Peter
Nov 3 '06 #5
Gabriel Genellina wrote:
I have a script starting with a docstring. After compiling it with
compile(), is there any way I could get the docstring? __doc__ on the
code object doesn't work.

The lazy coder's approach:
>>compiler.parse("""
.... 'the docstring'
.... print 42
.... """).doc
'the docstring'

Peter
Nov 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trevor Taylor | last post by:
Hi, I thought it would be simple but couldn't figure it out, nor find it addressed already: I can do: def validatePassword(p): 'do something' print validatePassword.__doc__
5
by: LutherRevisited | last post by:
This may be a dumb question, but are there any practical advantages of compiling a python application to *.pyo, or *.pyc? I haven't noticed any difference in the performance of text *.py or a...
1
by: CB | last post by:
I have what must be a very common design issue in web programming, probably discussed 2000 times already, but I can never find any mention by searching. In particular I have a web page with a...
3
by: Connie Edwards | last post by:
The following page.aspx is a very simple sample. But when I try to load it, it just prints 'ABCDED' as against making it a link. So, I was wondering if this file will first need to be compiled before...
24
by: Mark | last post by:
Hi, I'm new to python and looking for a better idiom to use for the manner I have been organising my python scripts. I've googled all over the place about this but found absolutely nothing. I'm...
7
by: gtb | last post by:
Greetings, Don't know the daily limit for dumb questions so I will ask one or more. In a function I can use the statement n = sys._getframe().f_code.co_name to get the name of the current...
6
by: James T. Dennis | last post by:
Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says: This module also provides some data items to the user: TMP_MAX - maximum number...
10
by: HYRY | last post by:
I have the following questions, I am using Python 2.4.2 19167152 #1 11306608 #1 1. the address of a.append and list.append is different, can I get the address of...
6
by: SanPy | last post by:
The subject of this message might be a little cryptic, so here's an example of what I mean: def foo(): """doc string of foo""" print foo.__doc__ doc string of foo What I want to know is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.