By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Rationale for read-only property of co_code

P: n/a
Hello all,

I've got this question that has been nagging me for a few days now.
What are the reasons for us to have co_code as read-only? I've been
trying to get some info about it, but I kept hitting the wall.

Correct me if I'm wrong, but as far as I understand, co_code
represents the compiled bytecode that should be run when, for
instance, a function is called. Wouldn't it be beneficial for
programmers to be able to change the bytecode in runtime? I mean, one
can't, as far as I'm aware, change the bytecode by accident, so if the
programmer would wish to change a function at runtime, he could do so
at his own risk.

If there is a higher reason behind the read-only property of co_code,
I definitely fail to see it, and would like to know what it is. If
not, why aren't we allowed to write into it?

Thanks in advance,

Joćo Neves
Apr 2 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 2, 5:00*am, Joćo Neves <seve...@gmail.comwrote:
Hello all,

I've got this question that has been nagging me for a few days now.
What are the reasons for us to have co_code as read-only? I've been
trying to get some info about it, but I kept hitting the wall.

Correct me if I'm wrong, but as far as I understand, co_code
represents the compiled bytecode that should be run when, for
instance, a function is called. Wouldn't it be beneficial for
programmers to be able to change the bytecode in runtime? I mean, one
can't, as far as I'm aware, change the bytecode by accident, so if the
programmer would wish to change a function at runtime, he could do so
at his own risk.

If there is a higher reason behind the read-only property of co_code,
I definitely fail to see it, and would like to know what it is. If
not, why aren't we allowed to write into it?

Thanks in advance,

Joćo Neves
Are Python bytes codes Python byte codes? Do you foresee any machine-
dependent optimizations?
Apr 2 '08 #2

P: n/a
I'm not quite sure I understood your question, sorry.

And you won't. He's one of the resident trolls... Better killfile him
ASAP :)

Diez
Apr 2 '08 #3

P: n/a
On Apr 3, 4:43 am, Scott David Daniels <Scott.Dani...@Acm.Orgwrote:
Nope: If you change the code in-place, the whole stack's references
to where they were running would need to get updated to corresponding
locations in the new code. _That_ is a lot of work.
Ah, there it is. Now I get it, it makes perfect sense.
Looks like I'll have to stick to the usual mechanisms!
Thanks everyone!

---
Joćo Neves
Apr 3 '08 #4

P: n/a
JoĆ£o Neves wrote:
Let me give a very basic example. Say we have these two functions:
I suppose you mean
>>def inc(x): return x + 1
....
>>def dec(x): return x - 1
....
>>inc(1), dec(1)
(2, 0)

Examining the compiled bytecodes for these two functions:
>inc.func_code.co_code
'|\x00\x00d\x01\x00\x17}\x00\x00d\x00\x00S'
>dec.func_code.co_code
'|\x00\x00d\x01\x00\x18}\x00\x00d\x00\x00S'

Now suppose that I wanted to mess with inc, and have it behave like
dec.
>>inc.func_code = dec.func_code
inc(1), dec(1)
(0, 0)

There you are, and there wasn't even a slight chance that you combined the
byte code with an incompatible function signature ;)

Peter
Apr 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.