itom 2.0.0
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2013, Institut fuer Technische Optik (ITO), 00005 Universitaet Stuttgart, Germany 00006 00007 This file is part of itom. 00008 00009 itom is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Library General Public Licence as published by 00011 the Free Software Foundation; either version 2 of the Licence, or (at 00012 your option) any later version. 00013 00014 itom is distributed in the hope that it will be useful, but 00015 WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library 00017 General Public Licence for more details. 00018 00019 You should have received a copy of the GNU Library General Public License 00020 along with itom. If not, see <http://www.gnu.org/licenses/>. 00021 *********************************************************************** */ 00022 00023 #ifndef PYTHONMATLAB_H 00024 #define PYTHONMATLAB_H 00025 00026 #if ITOM_PYTHONMATLAB == 1 00027 /* * * 00028 * Copyright 2010 Joakim Mller 00029 * 00030 * This file is part of pymatlab. 00031 * 00032 * pymatlab is free software: you can redistribute it and/or modify 00033 * it under the terms of the GNU General Public License as published by 00034 * the Free Software Foundation, either version 3 of the License, or 00035 * (at your option) any later version. 00036 * 00037 * pymatlab is distributed in the hope that it will be useful, 00038 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00039 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00040 * GNU General Public License for more details. 00041 * 00042 * You should have received a copy of the GNU General Public License 00043 * along with pymatlab. If not, see <http://www.gnu.org/licenses/>. 00044 * * */ 00045 00046 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 00047 00048 // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3 00049 #if (defined _DEBUG) && (defined WIN32) 00050 #undef _DEBUG 00051 #if (defined linux) | (defined CMAKE) 00052 #include "Python.h" 00053 #include "numpy/arrayobject.h" 00054 #elif (defined __APPLE__) | (defined CMAKE) 00055 #include "Python.h" 00056 #include "numpy/arrayobject.h" 00057 #else 00058 #include "Python.h" 00059 #include "../Lib/site-packages/numpy/core/include/numpy/arrayobject.h" //for numpy arrays 00060 #endif 00061 #define _DEBUG 00062 #else 00063 #if (defined linux) 00064 #include "Python.h" 00065 #include "numpy/arrayobject.h" 00066 #elif (defined __APPLE__) 00067 #include "Python.h" 00068 #include "numpy/arrayobject.h" 00069 #else 00070 #include "Python.h" 00071 #include "../Lib/site-packages/numpy/core/include/numpy/arrayobject.h" //for numpy arrays 00072 #endif 00073 #endif 00074 00075 00076 #include <stdio.h> 00077 #include <engine.h> 00078 00079 #define PYMATLAB_VERSION "0.2" 00080 #define AUTHOR "Joakim Moller, Marc Gronle (Python 3)" 00081 #define PYTHON3 00082 00083 /* This wraps a command call to the MATLAB interpretor */ 00084 const char * function_wrap = "try\n\t%s;\ncatch err\n\tpymatlaberrstring = sprintf('Error: %%s with message: %%s\\n',err.identifier,err.message);\n\tfor i = 1:length(err.stack)\n\t\tpymatlaberrstring = sprintf('%%sError: in fuction %%s in file %%s line %%i\\n',pymatlaberrstring,err.stack(i,1).name,err.stack(i,1).file,err.stack(i,1).line);\n\tend\nend\nif exist('pymatlaberrstring','var')==0\n\tpymatlaberrstring='';\nend"; 00085 00086 int mxtonpy[17] = { 00087 NPY_USERDEF, /* mxUNKNOWN_CLASS (0) */ 00088 NPY_USERDEF, /* mxCELL_CLASS (1) */ 00089 NPY_USERDEF, /* mxSTRUCT_CLASS (2) */ 00090 NPY_BOOL, /* mxLOGICAL_CLASS (3) */ 00091 NPY_CHAR, /* mxCHAR_CLASS (4) */ 00092 NPY_USERDEF, /* mxVOID_CLASS (5) */ 00093 NPY_DOUBLE, /* mxDOUBLE_CLASS (6) */ 00094 NPY_FLOAT, /* mxSINGLE_CLASS (7) */ 00095 NPY_BYTE, /* mxINT8_CLASS (8) */ 00096 NPY_UBYTE, /* mxUINT8_CLASS (9) */ 00097 NPY_SHORT, /* mxINT16_CLASS (10) */ 00098 NPY_USHORT, /* mxUINT16_CLASS (11) */ 00099 NPY_INT, /* mxINT32_CLASS (12) */ 00100 NPY_UINT, /* mxUINT32_CLASS (13) */ 00101 NPY_LONG, /* mxINT64_CLASS (14) */ 00102 NPY_ULONG, /* mxUINT64_CLASS (15) */ 00103 NPY_USERDEF /* mxFUNCTION_CLASS (16) */ 00104 }; 00105 00106 mxClassID npytomx[23]={ mxLOGICAL_CLASS, /*NPY_BOOL (0)*/ 00107 mxINT8_CLASS, /*NPY_BYTE (1)*/ 00108 mxUINT8_CLASS, /*NPY_UBYTE (2)*/ 00109 mxINT16_CLASS, /*NPY_SHORT (3)*/ 00110 mxUINT16_CLASS, /*NPY_USHORT (4)*/ 00111 mxINT32_CLASS, /*NPY_INT (5)*/ 00112 mxUINT32_CLASS, /*NPY_UINT (6)*/ 00113 mxINT64_CLASS, /*NPY_LONG (7)*/ 00114 mxUINT64_CLASS, /*NPY_ULONG (8)*/ 00115 mxUNKNOWN_CLASS, /*NPY_LONGLONG (9)*/ 00116 mxUNKNOWN_CLASS, /*NPY_ULONGLONG (10)*/ 00117 mxSINGLE_CLASS, /*NPY_FLOAT (11)*/ 00118 mxDOUBLE_CLASS, /*NPY_DOUBLE (12)*/ 00119 mxUNKNOWN_CLASS, /*NPY_LONGDOUBLE (13)*/ 00120 mxSINGLE_CLASS, /*NPY_CFLOAT (14)*/ 00121 mxDOUBLE_CLASS, /*NPY_CDOUBLE (15)*/ 00122 mxUNKNOWN_CLASS, /*NPY_CLONGDOUBLE (16)*/ 00123 mxUNKNOWN_CLASS, /*NPY_OBJECT (17)*/ 00124 mxCHAR_CLASS, /*NPY_STRING (18)*/ 00125 mxCHAR_CLASS, /*NPY_UNICODE (19)*/ 00126 mxUNKNOWN_CLASS, /*NPY_VOID (20)*/ 00127 mxUNKNOWN_CLASS, /*NPY_DATETIME (21)*/ 00128 mxCHAR_CLASS, /*NPY_TIMEDELTA (22)*/ 00129 }; 00130 00131 typedef struct 00132 { 00133 PyObject_HEAD 00134 Engine *ep; 00135 } PyMatlabSessionObject; 00136 00137 static PyObject * PyMatlabSessionObject_new(PyTypeObject *type, PyObject *args, PyObject * kwds) 00138 { 00139 PyMatlabSessionObject *self; 00140 self = (PyMatlabSessionObject *) type->tp_alloc(type,0); 00141 if (self!=NULL) 00142 { 00143 self->ep = NULL; 00144 } 00145 return (PyObject *) self; 00146 } 00147 00148 static int PyMatlabSessionObject_init(PyMatlabSessionObject *self, PyObject *args, PyObject *kwds) 00149 { 00150 int status; 00151 char *startstr=NULL; 00152 if (!PyArg_ParseTuple(args,"|s",&startstr)) 00153 return EXIT_FAILURE; 00154 if (!(self->ep = engOpen(startstr))) { 00155 fprintf(stderr, "\nCan't start MATLAB engine\n"); 00156 return EXIT_FAILURE; 00157 } 00158 status = engOutputBuffer(self->ep,NULL,0); 00159 return 0; 00160 } 00161 00162 static void 00163 PyMatlabSessionObject_dealloc(PyMatlabSessionObject *self) 00164 { 00165 if(self->ep) 00166 { 00167 engClose(self->ep); 00168 } 00169 self->ob_base.ob_type->tp_free((PyObject*)self); 00170 } 00171 00172 static PyObject * PyMatlabSessionObject_run(PyMatlabSessionObject *self, PyObject *args) 00173 { 00174 char * stringarg; 00175 char * command; 00176 char * errmsg; 00177 int status; 00178 const mxArray * mxresult; 00179 PyObject * result; 00180 00181 if(self->ep == NULL) 00182 { 00183 PyErr_SetString(PyExc_RuntimeError, "Matlab engine has not been started"); 00184 return NULL; 00185 } 00186 00187 if (!PyArg_ParseTuple(args,"s",&stringarg)) 00188 return NULL; 00189 if (!(command = (char*)malloc(sizeof(char)*3000))) 00190 return NULL; 00191 sprintf(command,function_wrap,stringarg); 00192 if (engEvalString(self->ep,command)!=0) 00193 { 00194 return PyErr_Format(PyExc_RuntimeError, "Was not able to evaluate command: %s",command); 00195 } 00196 if ((mxresult = engGetVariable(self->ep,"pymatlaberrstring"))==NULL) 00197 { 00198 PyErr_SetString(PyExc_RuntimeError, "can't get internal variable: pymatlaberrstring"); 00199 return NULL; 00200 } 00201 if (strcmp( mxArrayToString(mxresult),"")!=0) 00202 { 00203 /*make sure 'pymatlaberrstring' is empty or not exist until next call*/ 00204 status = engEvalString(self->ep,"clear pymatlaberrstring"); 00205 return PyErr_Format(PyExc_RuntimeError,"Error from Matlab: %s end.", mxArrayToString(mxresult)); 00206 } 00207 free((void*)command); 00208 Py_RETURN_NONE; 00209 } 00210 00211 static PyObject * PyMatlabSessionObject_setValue(PyMatlabSessionObject *self, PyObject *args) 00212 { 00213 const char * name; 00214 //PyArrayObject * ndarray; 00215 PyArrayObject * cont_ndarray; 00216 PyObject *obj; 00217 mxArray * mxarray; 00218 void *mx,*nd; 00219 float *mxReal32, *mxImag32, *nd32; 00220 double *mxReal64, *mxImag64, *nd64; 00221 int i,j; 00222 mwSize dims[] = {1}; 00223 00224 if(self->ep == NULL) 00225 { 00226 PyErr_SetString(PyExc_RuntimeError, "Matlab engine has not been started"); 00227 return NULL; 00228 } 00229 00230 if (!PyArg_ParseTuple(args,"sO",&name,&obj)) 00231 { 00232 return NULL; 00233 } 00234 00235 if(PyLong_Check(obj)) 00236 { 00237 if (!(mxarray=mxCreateNumericArray(1, dims, mxINT64_CLASS , mxREAL))) 00238 { 00239 PyErr_SetString(PyExc_RuntimeError, "Couldn't create mxarray."); 00240 return NULL; 00241 } 00242 00243 ((long*)mxGetData(mxarray))[0] = PyLong_AsLong(obj); 00244 } 00245 else if(PyFloat_Check(obj)) 00246 { 00247 if (!(mxarray=mxCreateNumericArray(1, dims, mxDOUBLE_CLASS , mxREAL))) 00248 { 00249 PyErr_SetString(PyExc_RuntimeError, "Couldn't create mxarray."); 00250 return NULL; 00251 } 00252 00253 ((double*)mxGetData(mxarray))[0] = PyFloat_AsDouble(obj); 00254 } 00255 else if(PyComplex_Check(obj)) 00256 { 00257 if (!(mxarray=mxCreateNumericArray(1, dims, mxDOUBLE_CLASS , mxCOMPLEX))) 00258 { 00259 PyErr_SetString(PyExc_RuntimeError, "Couldn't create mxarray."); 00260 return NULL; 00261 } 00262 00263 ((double*)mxGetData(mxarray))[0] = PyComplex_RealAsDouble(obj); 00264 ((double*)mxGetImagData(mxarray))[0] = PyComplex_ImagAsDouble(obj); 00265 } 00266 else 00267 { 00268 #if (NPY_FEATURE_VERSION < NPY_1_7_API_VERSION) 00269 cont_ndarray = (PyArrayObject*)PyArray_FROM_OF(obj,NPY_F_CONTIGUOUS | NPY_ALIGNED | NPY_WRITEABLE); 00270 #else 00271 cont_ndarray = (PyArrayObject*)PyArray_FROM_OF(obj,NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE); 00272 #endif 00273 00274 if(cont_ndarray == NULL || (PyObject*)cont_ndarray == Py_NotImplemented) 00275 { 00276 PyErr_SetString(PyExc_RuntimeError,"object cannot be interpreted as an array"); 00277 return NULL; 00278 } 00279 /* 00280 allocating and zero initialise */ 00281 if(PyArray_TYPE(cont_ndarray) != NPY_CFLOAT && PyArray_TYPE(cont_ndarray) != NPY_CDOUBLE && PyArray_TYPE(cont_ndarray) != NPY_CLONGDOUBLE) //real values 00282 { 00283 if (!(mxarray=mxCreateNumericArray((mwSize)PyArray_NDIM(cont_ndarray), 00284 (mwSize*)PyArray_DIMS(cont_ndarray), 00285 npytomx[PyArray_TYPE(cont_ndarray)], 00286 mxREAL))) 00287 { 00288 return PyErr_Format(PyExc_RuntimeError, "Couldn't create mxarray: NPYTYPE:%i - mxtype:%i", PyArray_TYPE(cont_ndarray), npytomx[PyArray_TYPE(cont_ndarray)]); 00289 } 00290 00291 nd=PyArray_DATA(cont_ndarray); 00292 mx=mxGetData(mxarray); 00293 j=PyArray_SIZE(cont_ndarray); 00294 00295 memcpy(mx, nd, PyArray_NBYTES(cont_ndarray)); 00296 /*for (i=0;i<j;i++) 00297 mx[i]=nd[i];*/ 00298 00299 00300 } 00301 else //else complex 00302 { 00303 if (!(mxarray=mxCreateNumericArray((mwSize)PyArray_NDIM(cont_ndarray), 00304 (mwSize*)PyArray_DIMS(cont_ndarray), 00305 npytomx[PyArray_TYPE(cont_ndarray)], 00306 mxCOMPLEX))) 00307 { 00308 return PyErr_Format(PyExc_RuntimeError, "Couldn't create mxarray: NPYTYPE:%i - mxtype:%i", PyArray_TYPE(cont_ndarray), npytomx[PyArray_TYPE(cont_ndarray)]); 00309 } 00310 00311 00312 j=PyArray_SIZE(cont_ndarray); 00313 00314 switch(PyArray_TYPE(cont_ndarray)) 00315 { 00316 case NPY_CFLOAT: 00317 nd32=(float*)PyArray_DATA(cont_ndarray); 00318 mxReal32 = (float*)mxGetData(mxarray); 00319 mxImag32 = (float*)mxGetImagData(mxarray); 00320 for(i=0;i<j;i++) 00321 { 00322 mxReal32[i]=nd32[2*i]; 00323 mxImag32[i]=nd32[2*i+1]; 00324 } 00325 break; 00326 case NPY_CDOUBLE: 00327 nd64=(double*)PyArray_DATA(cont_ndarray); 00328 mxReal64 = (double*)mxGetData(mxarray); 00329 mxImag64 = (double*)mxGetImagData(mxarray); 00330 for(i=0;i<j;i++) 00331 { 00332 mxReal64[i]=nd64[2*i]; 00333 mxImag64[i]=nd64[2*i+1]; 00334 } 00335 break; 00336 case NPY_CLONGDOUBLE: 00337 PyErr_SetString(PyExc_RuntimeError,"matrix of type complex long double cannot be sent to Matlab"); 00338 return NULL; 00339 } 00340 00341 } 00342 } 00343 00344 if ((engPutVariable(self->ep,name,mxarray)!=0)) 00345 { 00346 PyErr_SetString(PyExc_RuntimeError,"Couldn't place string on workspace"); 00347 return NULL; 00348 } 00349 /* 00350 if (ndarray!=cont_ndarray) 00351 Py_DECREF(cont_ndarray); 00352 Py_DECREF(ndarray); 00353 */ 00354 00355 Py_RETURN_NONE; 00356 } 00357 00358 static PyObject * PyMatlabSessionObject_setString(PyMatlabSessionObject *self, PyObject *args) 00359 { 00360 const char * name, * command_string; 00361 mxArray * variable; 00362 00363 if(self->ep == NULL) 00364 { 00365 PyErr_SetString(PyExc_RuntimeError, "Matlab engine has not been started"); 00366 return NULL; 00367 } 00368 00369 if (!PyArg_ParseTuple(args,"ss",&name,&command_string)) 00370 return NULL; 00371 if (!(variable=mxCreateString(command_string))) 00372 { 00373 PyErr_SetString(PyExc_RuntimeError,"Couldn't create mxarray"); 00374 return NULL; 00375 } 00376 if ((engPutVariable(self->ep,name,variable)!=0)) 00377 { 00378 PyErr_SetString(PyExc_RuntimeError,"Couldn't place string on workspace"); 00379 return NULL; 00380 } 00381 mxDestroyArray(variable); 00382 Py_RETURN_NONE; 00383 } 00384 00385 static PyObject * PyMatlabSessionObject_getString(PyMatlabSessionObject *self, PyObject *args) 00386 { 00387 const char * name; 00388 mxArray * variable; 00389 PyObject *pyString; 00390 00391 if(self->ep == NULL) 00392 { 00393 PyErr_SetString(PyExc_RuntimeError, "Matlab engine has not been started"); 00394 return NULL; 00395 } 00396 00397 if (!PyArg_ParseTuple(args,"s",&name)) 00398 return NULL; 00399 00400 if (!(variable = engGetVariable(self->ep,name))) 00401 { 00402 return PyErr_Format(PyExc_AttributeError,"Couldn't find '%s' at MATLAB desktop",name); 00403 } 00404 00405 if(mxIsChar(variable)) 00406 { 00407 size_t sizebuf = mxGetN(variable); 00408 //char *string = new char[sizebuf+1]; 00409 char *string = (char*)malloc(sizeof(char)*(sizebuf+1)); 00410 mxGetString(variable, string, sizebuf+1); 00411 //pyString = PyUnicode_FromString(string); 00412 pyString = PyUnicode_DecodeLatin1(string, strlen(string), NULL); 00413 free ((void*)string); 00414 return pyString; 00415 } 00416 else 00417 { 00418 return PyErr_Format(PyExc_AttributeError,"Variable '%s' is no string.",name); 00419 } 00420 00421 00422 Py_RETURN_NONE; 00423 } 00424 00425 static PyObject * PyMatlabSessionObject_GetValue(PyMatlabSessionObject * self, PyObject *args) 00426 { 00427 const char * variable; 00428 mxArray * mx; 00429 PyObject *result; 00430 void *real, *imag; 00431 00432 if(self->ep == NULL) 00433 { 00434 PyErr_SetString(PyExc_RuntimeError, "Matlab engine has not been started"); 00435 return NULL; 00436 } 00437 00438 if (!PyArg_ParseTuple(args,"s",&variable)) 00439 { 00440 PyErr_SetString(PyExc_RuntimeError, "argument must be a string denoting the matlab variable name"); 00441 return NULL; 00442 } 00443 00444 if (!(mx = engGetVariable(self->ep,variable))) 00445 { 00446 return PyErr_Format(PyExc_AttributeError,"Couldn't find '%s' at MATLAB desktop",variable); 00447 } 00448 00449 //check whether only one element is returned: 00450 if(mxGetM(mx) == 1 && mxGetN(mx) == 1) 00451 { 00452 if(mxIsComplex(mx)) 00453 { 00454 real = mxGetData(mx); 00455 imag = mxGetImagData(mx); 00456 00457 if(real != NULL && imag != NULL && mxGetClassID(mx) == mxSINGLE_CLASS) 00458 { 00459 return PyComplex_FromDoubles( (double)((float*)real)[0] , (double)((float*)imag)[0]); 00460 } 00461 else if(real != NULL && imag != NULL && mxGetClassID(mx) == mxDOUBLE_CLASS) 00462 { 00463 return PyComplex_FromDoubles( (double)((double*)real)[0] , (double)((double*)imag)[0]); 00464 } 00465 } 00466 else 00467 { 00468 real = mxGetData(mx); 00469 00470 if(real != NULL) 00471 { 00472 switch(mxGetClassID(mx)) 00473 { 00474 case mxINT8_CLASS: 00475 case mxINT16_CLASS: 00476 case mxINT32_CLASS: 00477 case mxINT64_CLASS: 00478 return PyLong_FromLong((long)((long*)real)[0]); 00479 break; 00480 case mxUINT8_CLASS: 00481 case mxUINT16_CLASS: 00482 case mxUINT32_CLASS: 00483 case mxUINT64_CLASS: 00484 return PyLong_FromLong((unsigned long)((unsigned long*)real)[0]); 00485 break; 00486 case mxSINGLE_CLASS: 00487 case mxDOUBLE_CLASS: 00488 return PyFloat_FromDouble( (double)((double*)real)[0] ); 00489 break; 00490 default: 00491 PyErr_SetString(PyExc_RuntimeError,"unknown datatype of matlab matrix"); 00492 return NULL; 00493 } 00494 } 00495 } 00496 } 00497 else 00498 { 00499 /* This is how we could make it own data to avoid memory leak: (set OWN_DATA) 00500 * data = malloc(sizeof(double[n*m])); 00501 * memcpy((void * )data,(void *)mxGetPr(mx),sizeof(double[n*m])); 00502 */ 00503 #if (NPY_FEATURE_VERSION < NPY_1_7_API_VERSION) 00504 int flag = NPY_F_CONTIGUOUS; 00505 #else 00506 int flag = NPY_ARRAY_F_CONTIGUOUS; 00507 #endif 00508 00509 if (!(result=PyArray_New(&PyArray_Type,(int) mxGetNumberOfDimensions(mx), 00510 (npy_intp*) mxGetDimensions(mx), mxtonpy[mxGetClassID(mx)], 00511 NULL, mxGetData(mx), NULL, flag, NULL))) 00512 { 00513 PyErr_SetString(PyExc_AttributeError,"Couldn't convert to PyArray"); 00514 return NULL; 00515 } 00516 00517 return (PyObject*)PyArray_GETCONTIGUOUS((PyArrayObject*)result); //make array c-contiguous 00518 } 00519 00520 00521 00522 /* 00523 mxDestroyArray(mx); 00524 free((void*)data); 00525 */ 00526 //return result; 00527 } 00528 00529 static PyObject * PyMatlabSessionObject_close(PyMatlabSessionObject * self, PyObject *args) 00530 { 00531 if(self->ep) engClose(self->ep); 00532 self->ep = NULL; 00533 Py_RETURN_NONE; 00534 } 00535 00536 PyDoc_STRVAR(module_doc, "A wrapper for executing matlab commands."); 00537 00538 static PyMethodDef PyMatlabSessionObject_methods[] = 00539 { 00540 {"run", (PyCFunction)PyMatlabSessionObject_run, METH_VARARGS, "Launch a command in MATLAB."}, 00541 {"close", (PyCFunction)PyMatlabSessionObject_close, METH_VARARGS, "Close the open MATLAB session"}, 00542 {"setString", (PyCFunction)PyMatlabSessionObject_setString, METH_VARARGS, "Put a string on the workspace"}, 00543 {"getString", (PyCFunction)PyMatlabSessionObject_getString, METH_VARARGS, "Get a string-variable from the workspace"}, 00544 {"getValue", (PyCFunction)PyMatlabSessionObject_GetValue, METH_VARARGS, "Get a variable from the workspace and return a ndarray"}, 00545 {"setValue", (PyCFunction)PyMatlabSessionObject_setValue, METH_VARARGS, "Put a variable to the workspace"}, 00546 {NULL,NULL,0,NULL} 00547 }; 00548 00549 static PyMemberDef PyMatlabSessionObject_members[] = 00550 { 00551 { NULL } 00552 }; 00553 00554 00555 static PyTypeObject PyMatlabSessionObjectType = 00556 { 00557 PyVarObject_HEAD_INIT(NULL,0) 00558 "PyMatlabSessionObject", /*tp_name*/ 00559 sizeof(PyMatlabSessionObject), /*tp_basicsize*/ 00560 0, /*tp_itemsize*/ 00561 (destructor)PyMatlabSessionObject_dealloc, /*tp_dealloc*/ 00562 0, /*tp_print*/ 00563 0, /*tp_getattr*/ 00564 0, /*tp_setattr*/ 00565 0, /*tp_compare*/ 00566 0, /*tp_repr*/ 00567 0, /*tp_as_number*/ 00568 0, /*tp_as_sequence*/ 00569 0, /*tp_as_mapping*/ 00570 0, /*tp_hash*/ 00571 0, /*tp_call*/ 00572 0, /*tp_str*/ 00573 0, /*tp_getattro*/ 00574 0, /*tp_setattro*/ 00575 0, /*tp_as_buffer*/ 00576 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 00577 0, /*tp_doc*/ 00578 0, /*tp_traverse*/ 00579 0, /*tp_clear*/ 00580 0, /*tp_richcompare*/ 00581 0, /*tp_weaklistoffset*/ 00582 0, /*tp_iter*/ 00583 0, /*tp_iternext*/ 00584 PyMatlabSessionObject_methods, /*tp_methods*/ 00585 PyMatlabSessionObject_members, /*tp_members*/ 00586 0, /*tp_getset*/ 00587 0, /*tp_base*/ 00588 0, /*tp_dict*/ 00589 0, /*tp_descr_get*/ 00590 0, /*tp_descr_set*/ 00591 0, /*tp_dictoffset*/ 00592 (initproc)PyMatlabSessionObject_init, /*tp_init*/ 00593 0, /*PyType_GenericAlloc,*/ /*tp_alloc*/ 00594 PyMatlabSessionObject_new, /*tp_new*/ 00595 }; 00596 00597 //static PyMethodDef matlab_methods[] = 00598 //{ 00599 // { NULL } 00600 //}; 00601 00602 #ifdef PYTHON3 00603 static PyModuleDef PyMatlabSessionObject_Module = { 00604 PyModuleDef_HEAD_INIT, "matlab", NULL, -1, 00605 NULL, NULL, NULL, NULL 00606 }; 00607 #endif 00608 00609 00610 00611 #ifdef PYTHON3 00612 PyMODINIT_FUNC PyInit_matlab(void) 00613 #else 00614 PyMODINIT_FUNC init_matlab() 00615 #endif 00616 { 00617 PyObject *module; 00618 00619 #ifdef PYTHON3 00620 module = PyModule_Create(&PyMatlabSessionObject_Module); 00621 #else 00622 module = Py_InitModule3("MatlabSession", NULL, module_doc); 00623 #endif 00624 00625 if (module == NULL) 00626 { 00627 PyErr_SetString(PyExc_AttributeError,"fail in init: module is zero"); 00628 goto fail; 00629 } 00630 00631 import_array(); 00632 00633 if (PyType_Ready(&PyMatlabSessionObjectType) < 0) 00634 { 00635 //PyErr_SetString(PyExc_AttributeError,"fail in init: pyMatlabSessionObjectType is not ready"); 00636 goto fail; 00637 } 00638 Py_INCREF(&PyMatlabSessionObjectType); 00639 PyModule_AddObject(module, "MatlabSession", (PyObject*)&PyMatlabSessionObjectType); 00640 00641 PyModule_AddStringConstant(module, "__version__", PYMATLAB_VERSION); 00642 PyModule_AddStringConstant(module, "__author__", AUTHOR); 00643 00644 #ifdef PYTHON3 00645 return module; 00646 #endif 00647 00648 fail: 00649 #ifdef PYTHON3 00650 return NULL; 00651 #else 00652 return; 00653 #endif 00654 } 00655 00656 00657 #endif 00658 00659 #endif