/* A python-R interface*/
/*
* The authors for the original RPy code, as well as
* belopolsky for his contributed code, are listed here as authors;
* although the design is largely new, parts of this code is
* derived from their contributions.
*
* Laurent Gautier - 2008
*/
/*
***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Copyright (C) 2008-2009 Laurent Gautier
*
* Portions created by Alexander Belopolsky are
* Copyright (C) 2006 Alexander Belopolsky.
*
* Portions created by Gregory R. Warnes are
* Copyright (C) 2003-2008 Gregory Warnes.
*
* Portions created by Walter Moreira are
* Copyright (C) 2002-2003
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "Python.h"
#if Win32
#include <winsock2.h>
#endif
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include <Rinterface.h>
#include <R_ext/Complex.h>
#include <Rembedded.h>
#include <R_ext/eventloop.h>
/* FIXME: consider the use of parsing */
/* #include <R_ext/Parse.h> */
#include <R_ext/Rdynload.h>
#include <signal.h>
#include "rinterface.h"
#include "array.h"
#include "r_utils.h"
//#define RPY_VERBOSE
/* A tuple that holds options to initialize R */
static PyObject *initOptions;
/* Helper variables to quickly resolve SEXP types.
* The first variable gives the highest possible
* SEXP type.
* The second in an array of strings giving either
* the SEXP name (INTSXP, REALSXP, etc...), or a NULL
* if there is no such valid SEXP.
*/
static const int maxValidSexpType = 99;
static char **validSexpType;
static SEXP GetErrMessage_SEXP;
static PyObject *RPyExc_RuntimeError = NULL;
//FIXME: see the details of interruption
/* Indicates whether the R interpreter was interrupted by a SIGINT */
int interrupted = 0;
/* Abort the current R computation due to a SIGINT */
static void
interrupt_R(int signum)
{
interrupted = 1;
error("Interrupted");
}
/* Helper variable to store R's status
*/
const unsigned int const RPY_R_INITIALIZED = 0x01;
const unsigned int const RPY_R_BUSY = 0x02;
static unsigned int embeddedR_status = 0;
static PyObject *embeddedR_isInitialized;
inline void embeddedR_setlock(void) {
embeddedR_status = embeddedR_status | RPY_R_BUSY;
}
inline void embeddedR_freelock(void) {
embeddedR_status = embeddedR_status ^ RPY_R_BUSY;
}
/* The Python original SIGINT handler */
PyOS_sighandler_t python_sigint;
PyDoc_STRVAR(module_doc,
"Low-level functions to interface with R.\n\
One should mostly consider calling the functions defined here when\
writing a higher level interface between python and R.\
Check the documentation for the module this is bundled into if\
you only wish to have an off-the-shelf interface with R.\
\n\
");
static PySexpObject *globalEnv;
static PySexpObject *baseNameSpaceEnv;
static PySexpObject *emptyEnv;
static PySexpObject *na_string;
/* early definition of functions */
static PySexpObject* newPySexpObject(const SEXP sexp);
static SEXP newSEXP(PyObject *object, const int rType);
/* --- set output from the R console ---*/
static inline PyObject* EmbeddedR_setAnyCallback(PyObject *self,
PyObject *args,
PyObject **target)
{
PyObject *result = NULL;
PyObject *function;
if ( PyArg_ParseTuple(args, "O:console",
&function)) {
if (function != Py_None && !PyCallable_Check(function)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
Py_XDECREF(*target);
if (function == Py_None) {
*target = NULL;
} else {
Py_XINCREF(function);
*target = function;
}
Py_INCREF(Py_None);
result = Py_None;
} else {
PyErr_SetString(PyExc_TypeError, "The parameter should be a callable.");
}
return result;
}
static inline PyObject* EmbeddedR_getAnyCallback(PyObject *self,
PyObject *args,
PyObject *target)
{
PyObject *result = NULL;
if (PyArg_ParseTuple(args, "")) {
if (target == NULL)
result = NULL;
else
result = target;
Py_INCREF(result);
}
return result;
}
static PyObject* writeConsoleCallback = NULL;
static PyObject* EmbeddedR_setWriteConsole(PyObject *self,
PyObject *args)
{
return EmbeddedR_setAnyCallback(self, args, &writeConsoleCallback);
}
PyDoc_STRVAR(EmbeddedR_setWriteConsole_doc,
"Set how to handle output from the R console.\n\n"
":param f: callback function such as"
"None <- f(output), or None.\n");
static PyObject * EmbeddedR_getWriteConsole(PyObject *self,
PyObject *args)
{
return EmbeddedR_getAnyCallback(self, args, writeConsoleCallback);
}
PyDoc_STRVAR(EmbeddedR_getWriteConsole_doc,
"Retrieve current R console output handler (see setWriteConsole).");
static void
EmbeddedR_WriteConsole(const char *buf, int len)
{
PyOS_sighandler_t old_int;
PyObject *arglist;
PyObject *result;
/* It is necessary to restore the Python handler when using a Python
function for I/O. */
old_int = PyOS_getsig(SIGINT);
PyOS_setsig(SIGINT, python_sigint);
arglist = Py_BuildValue("(s)", buf);
if (! arglist) {
PyErr_NoMemory();
/* signal(SIGINT, old_int); */
//return NULL;
}
if (writeConsoleCallback == NULL) {
return;
}
result = PyEval_CallObject(writeConsoleCallback, arglist);
PyObject* pythonerror = PyErr_Occurred();
if (pythonerror != NULL) {
/* All R actions should be stopped since the Python callback failed,
and the Python exception raised up.*/
//FIXME: Print the exception in the meanwhile
PyErr_Print();
PyErr_Clear();
}
Py_DECREF(arglist);
/* signal(SIGINT, old_int); */
Py_XDECREF(result);
}
static PyObject* readConsoleCallback = NULL;
static PyObject* EmbeddedR_setReadConsole(PyObject *self,
PyObject *args)
{
return EmbeddedR_setAnyCallback(self, args, &readConsoleCallback);
}
PyDoc_STRVAR(EmbeddedR_setReadConsole_doc,
"Set the function handling input to the R console.\n\n"
":param f: a callback function such as "
"result <- f(prompt) \n");
static PyObject * EmbeddedR_getReadConsole(PyObject *self,
PyObject *args)
{
return EmbeddedR_getAnyCallback(self, args, readConsoleCallback);
}
PyDoc_STRVAR(EmbeddedR_getReadConsole_doc,
"Retrieve current R console output handler (see
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:rpy2-2.0.8.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
rpy2-2.0.8.tar.gz (65个子文件)
rpy2-2.0.8
PKG-INFO 536B
demos
radmin.py 23KB
example01.py 552B
MANIFEST 2KB
MPL_LICENSE 25KB
README 2KB
LGPL_LICENSE 26KB
GPL_LICENSE 18KB
AUTHORS 570B
doc
Makefile 2KB
source
rpy_classic.rst 3KB
conf.py 5KB
robjects_convert.rst 2KB
rinterface.rst 21KB
rlike.rst 3KB
faq.rst~ 52B
robjects.rst 13KB
changes.rst 85B
numpy.rst 2KB
faq.rst 330B
overview.rst 6KB
rpy2_logo.png 17KB
introduction.rst 8KB
index.rst 199B
setup.py 6KB
NEWS 12KB
rpy
rlike
tests
test_indexing.py 570B
__init__.py 484B
test_functional.py 1KB
test_container.py 7KB
__init__.py 0B
indexing.py 484B
container.py 8KB
functional.py 1012B
__init__.py 22B
robjects
numpy2ri.py 2KB
conversion.py 333B
tests
testNumpyConversions.py 4KB
testRObject.py 1KB
testRDataFrame.py 1KB
__init__.py 1KB
testRobjects.py 5KB
testRFunction.py 1KB
testRFormula.py 896B
testREnvironment.py 644B
testRVector.py 6KB
testRArray.py 1KB
__init__.py 16KB
tests_rpy_classic.py 2KB
rpy_classic.py 7KB
tests.py 679B
rinterface
array.h 143B
rinterface.h 998B
array.c 4KB
tests
test_SexpClosure.py 3KB
test_SexpVectorNumeric.py 2KB
test_EmbeddedR.py 6KB
test_SexpVector.py 10KB
__init__.py 1KB
test_Sexp.py 4KB
test_SexpEnvironment.py 3KB
rinterface.c 81KB
__init__.py 3KB
r_utils.h 108B
r_utils.c 778B
共 65 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 技术资料分享uCOS-II信号量集很好的技术资料.zip
- 技术资料分享ucOS-II入门教程(任哲)很好的技术资料.zip
- 技术资料分享UCOSII 2.90 ReleaseNotes很好的技术资料.zip
- 技术资料分享Ucos-II-中文注释版很好的技术资料.zip
- 技术资料分享uCGUI的性能与资源占用很好的技术资料.zip
- 技术资料分享uCGUI 简介很好的技术资料.zip
- 技术资料分享TJA1050很好的技术资料.zip
- 技术资料分享TF应用很好的技术资料.zip
- CourseDesign_Graph-数据结构课程设计
- AndroidStudio Demo-android studio计算器
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功