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

controlling input behavior of embedded python with PyOS_ReadlineFunctionPointer

Hello all,

I am not sure this is the correct list to ask as my problem is really
related to python low level implementation, but as I do not have found a
python.dev list, here it is :-)

We are curently developing python bindings to make a MPI parallel
application (Finite element solver) script driven. I have written a main
interpreter, able to execute scripts or interractive commands,in // ,
based on the following techniques:

*file execution: all python interpreter processes run the file using
PyRun_SimpleFile (file must be available to all processes)

*file execution using broadcast: process 0 read input file and store it in
a file, broadcast it to all processes and the script is run using
PyRun_SimpleString (file must be available to process 0 only)

*interractive execution: All processes listen to user input using a
PyRun_InteractiveLoop. Process 0 read stdin and broadcast the line to all
process, the other process listen to this broadcast. This is implemented
using the (very usefull in this case :-) ) PyOS_ReadlineFunctionPointer ,
reassigning it to an input function which wrap the default
PyOS_ReadlineFunctionPointer (to be able to reuse nice readline
functionalities) and add the broadcasting mechanism...

All of this work perfectly, but I had to modify the python sources,
Parser/myreadline.c to be precise... Indeed, the behavior of PyOS_Readline
is to use the user-modifiable PyOS_ReadlineFunctionPointer as input
mechanism, except when input is non-interractive in which case it fall
back to the non-user-modifiable PyOS_StdioReadline...As my processes are
non-interractive except for proc 0 (launched in background), proc 0
broadcast but the other ones do not listen. I thus had to remove this
test, so that the user-modifiable function is called in all cases.

/*Python code snipped from Parser/myreadline.c*/

char *
PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) {
char *rv;

if (PyOS_ReadlineFunctionPointer == NULL) {
#ifdef __VMS
PyOS_ReadlineFunctionPointer = vms__StdioReadline;
#else
PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
#endif
}
}
Py_BEGIN_ALLOW_THREADS

/* This is needed to handle the unlikely case that the
* interpreter is in interactive mode *and* stdin/out are not * a
tty. This can happen, for example if python is run like * this:
python -i < test1.py
*/
/* my modif: comment out the use of PyOS_StdioReadline for
non-interractive input...*/
/*if (!isatty (fileno (sys_stdin)) || !isatty (fileno(sys_stdout)))
rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
else*/
rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin,
sys_stdout,
prompt);
Py_END_ALLOW_THREADS
return rv;
}
}
/*end of snipped code*/

This is a small modif, but annoying because we would prefer to use the
plain python distribution, and anyway I wonder if the current behavior of
python is desirable, because basically it removes part of the
functionality of PyOS_ReadlineFunctionPointer: flexibility dissapear for
non-interractive input.
In addition, this seems not so robust, it seems to me that it is not
correct for vms, where PyOS_StdioReadline will be used in non-interractive
case while vms__StdioReadline will be used for interractive one....maybe
this is the intended behavior, but then the function naming is strange :-)

So basically, shouldn't it be the resposability of the code which change
PyOS_ReadlineFunctionPointer to check if the function given is adapted to
interractiveness of input? this seems the more flexible approach, and
probably involve only minor modif of the readline module.
In addition, wouldn't it be nice to initialize PyOS_ReadlineFunctionPointer
to a default value (suitable reading function) at declaration, instead of
defining it to NULL and let PyOS_Readline do the initialization when
needed?
This way, user code can get back a meaningfull reading function
storing the old value of PyOS_ReadlineFunctionPointer, use it to write an
extended input function, and reasign it to PyOS_ReadlineFunctionPointer...
This seems to me like the most flexible way to add user-tunable input
method...
So, to all experienced python developers, having used this
PyOS_ReadlineFunctionPointer
function or implemented this code, what do you think of these possible slight
modifications? Or is there a better way to use it than my current method?

Sorry for the long (and possibly strangely written, english is not my
native language :-) ) post,

Best reagards,

Greg.

Jul 18 '05 #1
0 1362

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

Similar topics

0
by: martin z | last post by:
I'm having a lot of trouble figuring out the details of controlling Python compiling. I can compile and embed just fine, but I want some finer granularity on what gets compiled into Python. I can...
1
by: ron | last post by:
Hi, I'm still new at Python and have been away from programming for a number of years in general. I apologized in advance if this has been discussed extensively already. Is the input()...
12
by: Thomas Lotze | last post by:
Hi, I'm trying to figure out what is the most pythonic way to interact with a generator. The task I'm trying to accomplish is writing a PDF tokenizer, and I want to implement it as a Python...
0
by: Al Christians | last post by:
I'm starting to test a python application that creates an Excel workbook, then fills in values for some cells and formulas for other cells. The formulas involve circular references, which will...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.