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