473,385 Members | 1,766 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,385 software developers and data experts.

Question about bytecode and yield

Hello,

1) I am trying to understand how yield works in a bytecode way. How
exactly does the YIELD_VALUE instruction work?
Does the function 'change' to return a generator at compile time
(which, from my understanding, not much happens at that stage in
python) or at the YIELD_VALUE bytecode?

Any pointer to documentation about this would be appriciated, I have
tried, but havn't really found anything.

2) This is the reason I am asking #1. Is it possible to, at runtime,
change the bytecode of a function so that to turn it into a generator
from a normal, average, function? Would just putting in a YIELD_VALUE
(in a sane way, obvioussly) work?

3) yield does some saving of the current frame, and so forth. Does
this happen, burried underneath, or would it be possible to do
something like that (saving the frame, local variables, etc... and
resuming it later) on my own, even as a C module.

Once again, any documentation (in CVS, or something like that) about
this would be appriciated.

Thank You,
Daniel Brodie
Jul 18 '05 #1
1 1479
[Daniel Brodie]
1) I am trying to understand how yield works in a bytecode way. How
exactly does the YIELD_VALUE instruction work?
Does the function 'change' to return a generator at compile time
(which, from my understanding, not much happens at that stage in
python) or at the YIELD_VALUE bytecode?
At compile time. The code object's co_flags member has to have the
CO_GENERATOR flag set in order for generator setup to occur correctly.
That is, the runtime must know that a function is a generator before
it can invoke it correctly.
Any pointer to documentation about this would be appriciated, I have
tried, but havn't really found anything.
PEP 255 documents the semantic of generators, The implementation in
CPython is, of course, wholly defined by the source code. See
ceval.c. The implementation of generators is pretty simple (but
because Python always had heap-allocated "stack frames").
2) This is the reason I am asking #1. Is it possible to, at runtime,
change the bytecode of a function so that to turn it into a generator
from a normal, average, function?
No.
Would just putting in a YIELD_VALUE (in a sane way, obvioussly) work?
No.
3) yield does some saving of the current frame, and so forth.
The primary thing it does is *refrain* from decrementing the refcount
on the frame object. That's what keeps the locals and "instruction
pointer" etc alive for resumption.
Does this happen, burried underneath, or would it be possible to do
something like that (saving the frame, local variables, etc... and
resuming it later) on my own, even as a C module.


Anything crossing the C-Python boundary requires entirely different
machinery. See

http;//www.stackless.com/

for fancier stuff.
Jul 18 '05 #2

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

Similar topics

6
by: Naresh Agarwal | last post by:
Hi How does JIT compilers work? Does they first compile the *entire* bytecode into machine code and then starts execution? Or they first compile a method, execute that and then compile another...
6
by: Benjamin Scherrey | last post by:
I'm curious as to how difficult it would be to take a string that contains compiled bytecode, load it into memory, give it a function name then execute that function. I'm thinking of a database...
12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
8
by: Jerald | last post by:
Hi. I'd like to know when python started working with bytecode. It seems natural that in the first python implementations code was really interpreted : executed directly. As a result, in the...
19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
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...
33
by: Maurice LING | last post by:
Hi, I've been using Python for about 2 years now, for my honours project and now my postgrad project. I must say that I am loving it more and more now. From my knowledge, Python bytecodes are...
2
by: IntraRELY | last post by:
I know this isnt really a VB question per say, but is what I am developing in VB.NET and Excel is the only place that has provided direction. I wanted to ask the public if perhaps you could lend me...
4
by: Fabiano Sidler | last post by:
Hi folks! I'm looking for a way to compile python source to bytecode instead of code-objects. Is there a possibility to do that? The reason is: I want to store pure bytecode with no additional...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.