__doc__ in compiled script

  • Thread starter Gabriel Genellina
  • Start date
G

Gabriel Genellina

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
 
P

Peter Otten

Gabriel said:
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
'This is a docstring'

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

Peter
 
G

Gabriel Genellina

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
 
P

Peter Otten

Fredrik said:
Gabriel said:
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
 
P

Peter Otten

Gabriel said:
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:
.... 'the docstring'
.... print 42
.... """).doc
'the docstring'

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top