~ubuntu-branches/ubuntu/oneiric/rpy/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/io2070.c

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2009-04-26 13:38:24 UTC
  • mfrom: (6.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090426133824-d5qxhxhnw8762p4p
Tags: 1.0.3-7
* Rebuilt under R 2.9.0

* debian/control: Upgraded (Build-)Depends: to new R version

* debian/control: Upgraded Standards-Version: 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: io.c 393 2008-01-02 17:34:28Z warnes $ 
3
 
 * Input/Output routines
4
 
 */
5
 
 
6
 
/* ***** BEGIN LICENSE BLOCK *****
7
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8
 
 *
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/
13
 
 *
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
17
 
 * License.
18
 
 *
19
 
 * The Original Code is the RPy python module.
20
 
 *
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.
24
 
 *
25
 
 * Contributor(s):
26
 
 *    Gregory R. Warnes <greg@warnes.net> (Maintainer)
27
 
 *
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.
39
 
 *
40
 
 * ***** END LICENSE BLOCK ***** */
41
 
 
42
 
 
43
 
#include "RPy.h"
44
 
 
45
 
#define ENTER_PY { PyThreadState* tstate = NULL;\
46
 
    if (_PyThreadState_Current == NULL) {\
47
 
       tstate = PyThreadState_New(my_interp);\
48
 
       PyEval_AcquireThread(tstate);\
49
 
    }
50
 
 
51
 
#define LEAVE_PY  if (tstate) {\
52
 
       PyEval_ReleaseThread(tstate);}\
53
 
    } 
54
 
 
55
 
PyObject *rpy_output=NULL, *rpy_input=NULL, *rpy_showfiles=NULL;
56
 
 
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
59
 
   the R interpreter */
60
 
void
61
 
RPy_ShowException()
62
 
{
63
 
  PyObject *err;
64
 
 
65
 
  if ((err = PyErr_Occurred())) {
66
 
    if (PyErr_GivenExceptionMatches(err, PyExc_KeyboardInterrupt)) {
67
 
      interrupt_R(0);
68
 
    }
69
 
    else {
70
 
      PyErr_WriteUnraisable(err);
71
 
      PyErr_Clear();
72
 
    }
73
 
  }
74
 
}
75
 
 
76
 
 
77
 
void
78
 
RPy_WriteConsole(char *buf, int len)
79
 
{
80
 
  PyOS_sighandler_t old_int;
81
 
  PyObject *dummy;
82
 
 
83
 
  /* It is necessary to restore the Python handler when using a Python
84
 
     function for I/O. */
85
 
  old_int = PyOS_getsig(SIGINT);
86
 
  PyOS_setsig(SIGINT, python_sigint);
87
 
  if (rpy_output) {
88
 
    ENTER_PY
89
 
    dummy = PyObject_CallFunction(rpy_output, "s", buf);
90
 
    Py_XDECREF(dummy);
91
 
    LEAVE_PY
92
 
  }
93
 
  signal(SIGINT, old_int);
94
 
  RPy_ShowException();
95
 
}
96
 
 
97
 
#ifdef _WIN32
98
 
int
99
 
RPy_ReadConsole(char *prompt, 
100
 
                char *buf, 
101
 
                int len, 
102
 
                int addtohistory)
103
 
#else
104
 
int
105
 
RPy_ReadConsole(char *prompt, 
106
 
                unsigned char *buf, 
107
 
                int len, 
108
 
                int addtohistory)
109
 
#endif
110
 
{
111
 
  PyObject *input_data;
112
 
  PyOS_sighandler_t old_int;
113
 
 
114
 
  if (!rpy_input)
115
 
    return 0;
116
 
 
117
 
  old_int = PyOS_getsig(SIGINT);
118
 
  PyOS_setsig(SIGINT, python_sigint);
119
 
  ENTER_PY
120
 
  start_events();
121
 
  input_data = PyObject_CallFunction(rpy_input, "si", prompt, len);
122
 
  stop_events();
123
 
  LEAVE_PY
124
 
 
125
 
  signal(SIGINT, old_int);
126
 
 
127
 
  RPy_ShowException();
128
 
 
129
 
  if (!input_data) {
130
 
    PyErr_Clear();
131
 
    return 0;
132
 
  }
133
 
  snprintf(buf, len, "%s", PyString_AsString(input_data));
134
 
  Py_DECREF(input_data);
135
 
  return 1;
136
 
}
137
 
 
138
 
int
139
 
RPy_ShowFiles(int nfile, char **file, char **headers, 
140
 
              char *wtitle, int del, char *pager)
141
 
{
142
 
  PyObject *pyfiles, *pyheaders, *result, *f, *h;
143
 
  PyOS_sighandler_t old_int;
144
 
  int i;
145
 
 
146
 
  if (rpy_showfiles==NULL)
147
 
    return 0;
148
 
 
149
 
  old_int = PyOS_getsig(SIGINT);
150
 
  PyOS_setsig(SIGINT, python_sigint);
151
 
 
152
 
  ENTER_PY
153
 
 
154
 
  pyfiles = PyList_New(0);
155
 
  pyheaders = PyList_New(0);
156
 
  if (!(pyfiles && pyheaders)) {
157
 
    return 0;
158
 
  }
159
 
 
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);
165
 
    Py_DECREF(f);
166
 
    Py_DECREF(h);
167
 
  }
168
 
 
169
 
  result = PyObject_CallFunction(rpy_showfiles, "OOsi", pyfiles, pyheaders,
170
 
                                 wtitle, del);
171
 
  Py_DECREF(pyfiles);
172
 
  Py_DECREF(pyheaders);
173
 
 
174
 
  signal(SIGINT, old_int);
175
 
  RPy_ShowException();
176
 
 
177
 
  LEAVE_PY
178
 
 
179
 
  if (!result) {
180
 
    PyErr_Clear();
181
 
    return 0;
182
 
  }
183
 
  Py_DECREF(result);
184
 
  return 1;
185
 
}
186
 
 
187
 
PyObject *
188
 
wrap_set(PyObject **var, char *name, PyObject *args)
189
 
{
190
 
  char *argformat;
191
 
  PyObject *func;
192
 
 
193
 
  argformat = (char *)PyMem_Malloc((strlen(name)+3)*sizeof(char));
194
 
  sprintf(argformat, "O:%s", name);
195
 
  if (!PyArg_ParseTuple(args, argformat, &func))
196
 
    return NULL;
197
 
 
198
 
  Py_INCREF(func);
199
 
  *var = func;
200
 
  Py_INCREF(Py_None);
201
 
  return Py_None;
202
 
}
203
 
 
204
 
PyObject *
205
 
set_output(PyObject *self, PyObject *args)
206
 
{
207
 
  return wrap_set(&rpy_output, "set_rpy_output", args);
208
 
}
209
 
 
210
 
PyObject *
211
 
set_input(PyObject *self, PyObject *args)
212
 
{
213
 
  return wrap_set(&rpy_input, "set_rpy_input", args);
214
 
}
215
 
 
216
 
PyObject *
217
 
set_showfiles(PyObject *self, PyObject *args)
218
 
{
219
 
  return wrap_set(&rpy_showfiles, "set_rpy_showfiles", args);
220
 
}
221
 
 
222
 
PyObject *
223
 
wrap_get(PyObject *o)
224
 
{
225
 
  if (o) {
226
 
    return o;
227
 
  } else {
228
 
    Py_INCREF(Py_None);
229
 
    return Py_None;
230
 
  }
231
 
}
232
 
 
233
 
PyObject *
234
 
get_output(PyObject *self, PyObject *args)
235
 
{
236
 
  if (!PyArg_ParseTuple(args, ":get_rpy_output"))
237
 
    return NULL;
238
 
 
239
 
  return wrap_get(rpy_output);
240
 
}
241
 
 
242
 
PyObject *
243
 
get_input(PyObject *self, PyObject *args)
244
 
{
245
 
  if (!PyArg_ParseTuple(args, ":get_rpy_input"))
246
 
    return NULL;
247
 
 
248
 
  return wrap_get(rpy_input);
249
 
}
250
 
 
251
 
PyObject *
252
 
get_showfiles(PyObject *self, PyObject *args)
253
 
{
254
 
  if (!PyArg_ParseTuple(args, ":get_rpy_showfiles"))
255
 
    return NULL;
256
 
 
257
 
  return wrap_get(rpy_showfiles);
258
 
}
259
 
 
260
 
void
261
 
init_io_routines(void)
262
 
#ifdef _WIN32
263
 
264
 
  return; 
265
 
}
266
 
#else
267
 
{
268
 
  R_Outputfile = NULL;
269
 
  ptr_R_WriteConsole = RPy_WriteConsole;
270
 
  ptr_R_ReadConsole = RPy_ReadConsole;
271
 
  ptr_R_ShowFiles = RPy_ShowFiles;
272
 
}
273
 
#endif