2
* $Id: io.c 393 2008-01-02 17:34:28Z warnes $
3
* Input/Output routines
6
/* ***** BEGIN LICENSE BLOCK *****
7
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
9
* The contents of this file are subject to the Mozilla Public License Version
10
* 1.1 (the "License"); you may not use this file except in compliance with
11
* the License. You may obtain a copy of the License at
12
* http://www.mozilla.org/MPL/
14
* Software distributed under the License is distributed on an "AS IS" basis,
15
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16
* for the specific language governing rights and limitations under the
19
* The Original Code is the RPy python module.
21
* The Initial Developer of the Original Code is Walter Moreira.
22
* Portions created by the Initial Developer are Copyright (C) 2002
23
* the Initial Developer. All Rights Reserved.
26
* Gregory R. Warnes <greg@warnes.net> (Maintainer)
28
* Alternatively, the contents of this file may be used under the terms of
29
* either the GNU General Public License Version 2 or later (the "GPL"), or
30
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31
* in which case the provisions of the GPL or the LGPL are applicable instead
32
* of those above. If you wish to allow use of your version of this file only
33
* under the terms of either the GPL or the LGPL, and not to allow others to
34
* use your version of this file under the terms of the MPL, indicate your
35
* decision by deleting the provisions above and replace them with the notice
36
* and other provisions required by the GPL or the LGPL. If you do not delete
37
* the provisions above, a recipient may use your version of this file under
38
* the terms of any one of the MPL, the GPL or the LGPL.
40
* ***** END LICENSE BLOCK ***** */
45
#define ENTER_PY { PyThreadState* tstate = NULL;\
46
if (_PyThreadState_Current == NULL) {\
47
tstate = PyThreadState_New(my_interp);\
48
PyEval_AcquireThread(tstate);\
51
#define LEAVE_PY if (tstate) {\
52
PyEval_ReleaseThread(tstate);}\
55
PyObject *rpy_output=NULL, *rpy_input=NULL, *rpy_showfiles=NULL;
57
/* Show the traceback of an exception which occurred in a I/O process,
58
except when the error is a KeyboardInterrupt, in which case abort
65
if ((err = PyErr_Occurred())) {
66
if (PyErr_GivenExceptionMatches(err, PyExc_KeyboardInterrupt)) {
70
PyErr_WriteUnraisable(err);
78
RPy_WriteConsole(char *buf, int len)
80
PyOS_sighandler_t old_int;
83
/* It is necessary to restore the Python handler when using a Python
85
old_int = PyOS_getsig(SIGINT);
86
PyOS_setsig(SIGINT, python_sigint);
89
dummy = PyObject_CallFunction(rpy_output, "s", buf);
93
signal(SIGINT, old_int);
99
RPy_ReadConsole(char *prompt,
105
RPy_ReadConsole(char *prompt,
111
PyObject *input_data;
112
PyOS_sighandler_t old_int;
117
old_int = PyOS_getsig(SIGINT);
118
PyOS_setsig(SIGINT, python_sigint);
121
input_data = PyObject_CallFunction(rpy_input, "si", prompt, len);
125
signal(SIGINT, old_int);
133
snprintf(buf, len, "%s", PyString_AsString(input_data));
134
Py_DECREF(input_data);
139
RPy_ShowFiles(int nfile, char **file, char **headers,
140
char *wtitle, int del, char *pager)
142
PyObject *pyfiles, *pyheaders, *result, *f, *h;
143
PyOS_sighandler_t old_int;
146
if (rpy_showfiles==NULL)
149
old_int = PyOS_getsig(SIGINT);
150
PyOS_setsig(SIGINT, python_sigint);
154
pyfiles = PyList_New(0);
155
pyheaders = PyList_New(0);
156
if (!(pyfiles && pyheaders)) {
160
for (i=0; i<nfile; i++) {
161
f = PyString_FromString(file[i]);
162
h = PyString_FromString(headers[i]);
163
PyList_Append(pyfiles, f);
164
PyList_Append(pyheaders, h);
169
result = PyObject_CallFunction(rpy_showfiles, "OOsi", pyfiles, pyheaders,
172
Py_DECREF(pyheaders);
174
signal(SIGINT, old_int);
188
wrap_set(PyObject **var, char *name, PyObject *args)
193
argformat = (char *)PyMem_Malloc((strlen(name)+3)*sizeof(char));
194
sprintf(argformat, "O:%s", name);
195
if (!PyArg_ParseTuple(args, argformat, &func))
205
set_output(PyObject *self, PyObject *args)
207
return wrap_set(&rpy_output, "set_rpy_output", args);
211
set_input(PyObject *self, PyObject *args)
213
return wrap_set(&rpy_input, "set_rpy_input", args);
217
set_showfiles(PyObject *self, PyObject *args)
219
return wrap_set(&rpy_showfiles, "set_rpy_showfiles", args);
223
wrap_get(PyObject *o)
234
get_output(PyObject *self, PyObject *args)
236
if (!PyArg_ParseTuple(args, ":get_rpy_output"))
239
return wrap_get(rpy_output);
243
get_input(PyObject *self, PyObject *args)
245
if (!PyArg_ParseTuple(args, ":get_rpy_input"))
248
return wrap_get(rpy_input);
252
get_showfiles(PyObject *self, PyObject *args)
254
if (!PyArg_ParseTuple(args, ":get_rpy_showfiles"))
257
return wrap_get(rpy_showfiles);
261
init_io_routines(void)
269
ptr_R_WriteConsole = RPy_WriteConsole;
270
ptr_R_ReadConsole = RPy_ReadConsole;
271
ptr_R_ShowFiles = RPy_ShowFiles;