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