473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterating generator from C

Hi,
I am working on project that has embedded python interpreter to run
user-specified python procedures. Those procedures might return any
iterable object with set of result data -- basically everything for which
iter() returns valid object (list, tuple, dict, iterator etc)

It works ok, except generator under Python 2.4 with debugging enabled (see
http://sourceforge.net/tracker/index...0&atid=105470).

Is there any way to rewrite following program to handle returned generator
without hitting this bug?
Error handling is omitted for clarity.

#include <Python.h>

const char *py_source =
"def fn():\n"
" yield 1\n";

int main ()
{
Py_Initialize() ;

PyObject *globals = PyDict_New();

// insert function code into interpreter
PyObject *code = PyRun_String(py _source, Py_file_input, globals, NULL);
Py_DECREF(code) ;

// do call
code = Py_CompileStrin g("fn()", "<string>", Py_eval_input);
PyObject *gen = PyEval_EvalCode ((PyCodeObject *)code, globals, NULL);

// iterate result
PyObject *item;
while ((item = PyIter_Next(gen ))) {
printf("> %ld\n", PyInt_AsLong(it em));
Py_DECREF(item) ;
}

Py_DECREF(gen);
Py_DECREF(code) ;
Py_DECREF(globa ls);

Py_Finalize();
return 0;
}
Br,
--
Sven Suursoho
May 13 '06 #1
0 1266

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

Similar topics

24
3339
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation which speeds things up for larger lists but why was it necessary to add it instead of improving list...
8
2296
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None): self.children = self.children.append(left) self.children.append(right) self.data = data
5
2243
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
2
1313
by: Swap | last post by:
Hi, I want to build my own generalize Code Generator means replacing XML tags in template file which developer can create(or Customize) and codegenerator can replace that tag with table structure. as for example <field> code </field>
6
2688
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following test-driven development, writing tests and then coding to satisfy them, I'll start with some of the simple tests for a class.
0
293
by: sven.suursoho | last post by:
Does messing with signal handlers and longjmp affect Python interpreter? I'm trying to find solution for problem, described in http://groups.google.com/group/comp.lang.python/browse_thread/thread/98cbae94ca4beefb/9d4d96fd0dd9fbc3 and came up with test application. It works well but i'm not sure it is ok for long-running python interpreter? #include <Python.h> #include <signal.h>
9
2422
by: Rhamphoryncus | last post by:
The problems of this are well known, and a suggestion for making this easier was recently posted on python-dev. However, I believe this can be done just as well without a change to the language. What's more, most of the suggested methods (in my search results as well as the suggestion itself) do not scale well, which my approach would solve. My approach is to make a set of indexes to removed while iterating, then use a list...
18
1802
by: Drew | last post by:
All - I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list. My code is working just fine, but I'm wondering if this is the most "elegant" way to do this. Essentially, I'm searching the dict self.contacts for a key that matches the pattern entered by the user. If so, I print the value associated with that...
10
1668
by: Gerard flanagan | last post by:
Simon Mullis wrote: data = from itertools import groupby datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s: s)) print datadict
0
7880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8255
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8010
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.