1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
#ifdef __CPLUSPLUS__
extern "C" {
#endif
#pragma warning(disable: 4275)
#pragma warning(disable: 4101)
#include "Python.h"
#include "compile.h"
#include "frameobject.h"
#include <complex>
#include <math.h>
#include <string>
#include "scxx/object.h"
#include "scxx/list.h"
#include "scxx/tuple.h"
#include "scxx/dict.h"
#include <iostream>
#include <stdio.h>
#include "numpy/arrayobject.h"
// global None value for use in functions.
namespace py {
object None = object(Py_None);
}
char* find_type(PyObject* py_obj)
{
if(py_obj == NULL) return "C NULL value";
if(PyCallable_Check(py_obj)) return "callable";
if(PyString_Check(py_obj)) return "string";
if(PyInt_Check(py_obj)) return "int";
if(PyFloat_Check(py_obj)) return "float";
if(PyDict_Check(py_obj)) return "dict";
if(PyList_Check(py_obj)) return "list";
if(PyTuple_Check(py_obj)) return "tuple";
if(PyFile_Check(py_obj)) return "file";
if(PyModule_Check(py_obj)) return "module";
//should probably do more intergation (and thinking) on these.
if(PyCallable_Check(py_obj) && PyInstance_Check(py_obj)) return "callable";
if(PyInstance_Check(py_obj)) return "instance";
if(PyCallable_Check(py_obj)) return "callable";
return "unkown type";
}
void throw_error(PyObject* exc, const char* msg)
{
//printf("setting python error: %s\n",msg);
PyErr_SetString(exc, msg);
//printf("throwing error\n");
throw 1;
}
void handle_bad_type(PyObject* py_obj, const char* good_type, const char* var_name)
{
char msg[500];
sprintf(msg,"received '%s' type instead of '%s' for variable '%s'",
find_type(py_obj),good_type,var_name);
throw_error(PyExc_TypeError,msg);
}
void handle_conversion_error(PyObject* py_obj, const char* good_type, const char* var_name)
{
char msg[500];
sprintf(msg,"Conversion Error:, received '%s' type instead of '%s' for variable '%s'",
find_type(py_obj),good_type,var_name);
throw_error(PyExc_TypeError,msg);
}
class int_handler
{
public:
int convert_to_int(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyInt_Check(py_obj))
handle_conversion_error(py_obj,"int", name);
return (int) PyInt_AsLong(py_obj);
}
int py_to_int(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyInt_Check(py_obj))
handle_bad_type(py_obj,"int", name);
return (int) PyInt_AsLong(py_obj);
}
};
int_handler x__int_handler = int_handler();
#define convert_to_int(py_obj,name) \
x__int_handler.convert_to_int(py_obj,name)
#define py_to_int(py_obj,name) \
x__int_handler.py_to_int(py_obj,name)
PyObject* int_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class float_handler
{
public:
double convert_to_float(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyFloat_Check(py_obj))
handle_conversion_error(py_obj,"float", name);
return PyFloat_AsDouble(py_obj);
}
double py_to_float(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyFloat_Check(py_obj))
handle_bad_type(py_obj,"float", name);
return PyFloat_AsDouble(py_obj);
}
};
float_handler x__float_handler = float_handler();
#define convert_to_float(py_obj,name) \
x__float_handler.convert_to_float(py_obj,name)
#define py_to_float(py_obj,name) \
x__float_handler.py_to_float(py_obj,name)
PyObject* float_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class complex_handler
{
public:
std::complex<double> convert_to_complex(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyComplex_Check(py_obj))
handle_conversion_error(py_obj,"complex", name);
return std::complex<double>(PyComplex_RealAsDouble(py_obj),PyComplex_ImagAsDouble(py_obj));
}
std::complex<double> py_to_complex(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyComplex_Check(py_obj))
handle_bad_type(py_obj,"complex", name);
return std::complex<double>(PyComplex_RealAsDouble(py_obj),PyComplex_ImagAsDouble(py_obj));
}
};
complex_handler x__complex_handler = complex_handler();
#define convert_to_complex(py_obj,name) \
x__complex_handler.convert_to_complex(py_obj,name)
#define py_to_complex(py_obj,name) \
x__complex_handler.py_to_complex(py_obj,name)
PyObject* complex_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class unicode_handler
{
public:
Py_UNICODE* convert_to_unicode(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
Py_XINCREF(py_obj);
if (!py_obj || !PyUnicode_Check(py_obj))
handle_conversion_error(py_obj,"unicode", name);
return PyUnicode_AS_UNICODE(py_obj);
}
Py_UNICODE* py_to_unicode(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyUnicode_Check(py_obj))
handle_bad_type(py_obj,"unicode", name);
Py_XINCREF(py_obj);
return PyUnicode_AS_UNICODE(py_obj);
}
};
unicode_handler x__unicode_handler = unicode_handler();
#define convert_to_unicode(py_obj,name) \
x__unicode_handler.convert_to_unicode(py_obj,name)
#define py_to_unicode(py_obj,name) \
x__unicode_handler.py_to_unicode(py_obj,name)
PyObject* unicode_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class string_handler
{
public:
std::string convert_to_string(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
Py_XINCREF(py_obj);
if (!py_obj || !PyString_Check(py_obj))
handle_conversion_error(py_obj,"string", name);
return std::string(PyString_AsString(py_obj));
}
std::string py_to_string(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyString_Check(py_obj))
handle_bad_type(py_obj,"string", name);
Py_XINCREF(py_obj);
return std::string(PyString_AsString(py_obj));
}
};
string_handler x__string_handler = string_handler();
#define convert_to_string(py_obj,name) \
x__string_handler.convert_to_string(py_obj,name)
#define py_to_string(py_obj,name) \
x__string_handler.py_to_string(py_obj,name)
PyObject* string_to_py(std::string s)
{
return PyString_FromString(s.c_str());
}
class list_handler
{
public:
py::list convert_to_list(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyList_Check(py_obj))
handle_conversion_error(py_obj,"list", name);
return py::list(py_obj);
}
py::list py_to_list(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyList_Check(py_obj))
handle_bad_type(py_obj,"list", name);
return py::list(py_obj);
}
};
list_handler x__list_handler = list_handler();
#define convert_to_list(py_obj,name) \
x__list_handler.convert_to_list(py_obj,name)
#define py_to_list(py_obj,name) \
x__list_handler.py_to_list(py_obj,name)
PyObject* list_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class dict_handler
{
public:
py::dict convert_to_dict(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyDict_Check(py_obj))
handle_conversion_error(py_obj,"dict", name);
return py::dict(py_obj);
}
py::dict py_to_dict(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyDict_Check(py_obj))
handle_bad_type(py_obj,"dict", name);
return py::dict(py_obj);
}
};
dict_handler x__dict_handler = dict_handler();
#define convert_to_dict(py_obj,name) \
x__dict_handler.convert_to_dict(py_obj,name)
#define py_to_dict(py_obj,name) \
x__dict_handler.py_to_dict(py_obj,name)
PyObject* dict_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class tuple_handler
{
public:
py::tuple convert_to_tuple(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyTuple_Check(py_obj))
handle_conversion_error(py_obj,"tuple", name);
return py::tuple(py_obj);
}
py::tuple py_to_tuple(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyTuple_Check(py_obj))
handle_bad_type(py_obj,"tuple", name);
return py::tuple(py_obj);
}
};
tuple_handler x__tuple_handler = tuple_handler();
#define convert_to_tuple(py_obj,name) \
x__tuple_handler.convert_to_tuple(py_obj,name)
#define py_to_tuple(py_obj,name) \
x__tuple_handler.py_to_tuple(py_obj,name)
PyObject* tuple_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class file_handler
{
public:
FILE* convert_to_file(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
Py_XINCREF(py_obj);
if (!py_obj || !PyFile_Check(py_obj))
handle_conversion_error(py_obj,"file", name);
return PyFile_AsFile(py_obj);
}
FILE* py_to_file(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyFile_Check(py_obj))
handle_bad_type(py_obj,"file", name);
Py_XINCREF(py_obj);
return PyFile_AsFile(py_obj);
}
};
file_handler x__file_handler = file_handler();
#define convert_to_file(py_obj,name) \
x__file_handler.convert_to_file(py_obj,name)
#define py_to_file(py_obj,name) \
x__file_handler.py_to_file(py_obj,name)
PyObject* file_to_py(FILE* file, char* name, char* mode)
{
PyObject* py_obj = NULL;
//extern int fclose(FILE *);
return (PyObject*) PyFile_FromFile(file, name, mode, fclose);
}
class instance_handler
{
public:
py::object convert_to_instance(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !PyInstance_Check(py_obj))
handle_conversion_error(py_obj,"instance", name);
return py::object(py_obj);
}
py::object py_to_instance(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyInstance_Check(py_obj))
handle_bad_type(py_obj,"instance", name);
return py::object(py_obj);
}
};
instance_handler x__instance_handler = instance_handler();
#define convert_to_instance(py_obj,name) \
x__instance_handler.convert_to_instance(py_obj,name)
#define py_to_instance(py_obj,name) \
x__instance_handler.py_to_instance(py_obj,name)
PyObject* instance_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class numpy_size_handler
{
public:
void conversion_numpy_check_size(PyArrayObject* arr_obj, int Ndims,
const char* name)
{
if (arr_obj->nd != Ndims)
{
char msg[500];
sprintf(msg,"Conversion Error: received '%d' dimensional array instead of '%d' dimensional array for variable '%s'",
arr_obj->nd,Ndims,name);
throw_error(PyExc_TypeError,msg);
}
}
void numpy_check_size(PyArrayObject* arr_obj, int Ndims, const char* name)
{
if (arr_obj->nd != Ndims)
{
char msg[500];
sprintf(msg,"received '%d' dimensional array instead of '%d' dimensional array for variable '%s'",
arr_obj->nd,Ndims,name);
throw_error(PyExc_TypeError,msg);
}
}
};
numpy_size_handler x__numpy_size_handler = numpy_size_handler();
#define conversion_numpy_check_size x__numpy_size_handler.conversion_numpy_check_size
#define numpy_check_size x__numpy_size_handler.numpy_check_size
class numpy_type_handler
{
public:
void conversion_numpy_check_type(PyArrayObject* arr_obj, int numeric_type,
const char* name)
{
// Make sure input has correct numeric type.
int arr_type = arr_obj->descr->type_num;
if (PyTypeNum_ISEXTENDED(numeric_type))
{
char msg[80];
sprintf(msg, "Conversion Error: extended types not supported for variable '%s'",
name);
throw_error(PyExc_TypeError, msg);
}
if (!PyArray_EquivTypenums(arr_type, numeric_type))
{
char* type_names[23] = {"bool", "byte", "ubyte","short", "ushort",
"int", "uint", "long", "ulong", "longlong", "ulonglong",
"float", "double", "longdouble", "cfloat", "cdouble",
"clongdouble", "object", "string", "unicode", "void", "ntype",
"unknown"};
char msg[500];
sprintf(msg,"Conversion Error: received '%s' typed array instead of '%s' typed array for variable '%s'",
type_names[arr_type],type_names[numeric_type],name);
throw_error(PyExc_TypeError,msg);
}
}
void numpy_check_type(PyArrayObject* arr_obj, int numeric_type, const char* name)
{
// Make sure input has correct numeric type.
int arr_type = arr_obj->descr->type_num;
if (PyTypeNum_ISEXTENDED(numeric_type))
{
char msg[80];
sprintf(msg, "Conversion Error: extended types not supported for variable '%s'",
name);
throw_error(PyExc_TypeError, msg);
}
if (!PyArray_EquivTypenums(arr_type, numeric_type))
{
char* type_names[23] = {"bool", "byte", "ubyte","short", "ushort",
"int", "uint", "long", "ulong", "longlong", "ulonglong",
"float", "double", "longdouble", "cfloat", "cdouble",
"clongdouble", "object", "string", "unicode", "void", "ntype",
"unknown"};
char msg[500];
sprintf(msg,"received '%s' typed array instead of '%s' typed array for variable '%s'",
type_names[arr_type],type_names[numeric_type],name);
throw_error(PyExc_TypeError,msg);
}
}
};
numpy_type_handler x__numpy_type_handler = numpy_type_handler();
#define conversion_numpy_check_type x__numpy_type_handler.conversion_numpy_check_type
#define numpy_check_type x__numpy_type_handler.numpy_check_type
class numpy_handler
{
public:
PyArrayObject* convert_to_numpy(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
Py_XINCREF(py_obj);
if (!py_obj || !PyArray_Check(py_obj))
handle_conversion_error(py_obj,"numpy", name);
return (PyArrayObject*) py_obj;
}
PyArrayObject* py_to_numpy(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !PyArray_Check(py_obj))
handle_bad_type(py_obj,"numpy", name);
Py_XINCREF(py_obj);
return (PyArrayObject*) py_obj;
}
};
numpy_handler x__numpy_handler = numpy_handler();
#define convert_to_numpy(py_obj,name) \
x__numpy_handler.convert_to_numpy(py_obj,name)
#define py_to_numpy(py_obj,name) \
x__numpy_handler.py_to_numpy(py_obj,name)
PyObject* numpy_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
class catchall_handler
{
public:
py::object convert_to_catchall(PyObject* py_obj, const char* name)
{
// Incref occurs even if conversion fails so that
// the decref in cleanup_code has a matching incref.
if (!py_obj || !(py_obj))
handle_conversion_error(py_obj,"catchall", name);
return py::object(py_obj);
}
py::object py_to_catchall(PyObject* py_obj, const char* name)
{
// !! Pretty sure INCREF should only be called on success since
// !! py_to_xxx is used by the user -- not the code generator.
if (!py_obj || !(py_obj))
handle_bad_type(py_obj,"catchall", name);
return py::object(py_obj);
}
};
catchall_handler x__catchall_handler = catchall_handler();
#define convert_to_catchall(py_obj,name) \
x__catchall_handler.convert_to_catchall(py_obj,name)
#define py_to_catchall(py_obj,name) \
x__catchall_handler.py_to_catchall(py_obj,name)
PyObject* catchall_to_py(PyObject* obj)
{
return (PyObject*) obj;
}
static PyObject* Ramp(PyObject*self, PyObject* args, PyObject* kywds)
{
py::object return_val;
int exception_occured = 0;
PyObject *py_local_dict = NULL;
static char *kwlist[] = {"result","start","end","local_dict", NULL};
PyObject *py_result, *py_start, *py_end;
int result_used, start_used, end_used;
py_result = py_start = py_end = NULL;
result_used= start_used= end_used = 0;
if(!PyArg_ParseTupleAndKeywords(args,kywds,"OOO|O:Ramp",kwlist,&py_result, &py_start, &py_end, &py_local_dict))
return NULL;
try
{
py_result = py_result;
PyArrayObject* result_array = convert_to_numpy(py_result,"result");
conversion_numpy_check_type(result_array,PyArray_DOUBLE,"result");
#define RESULT1(i) (*((double*)(result_array->data + (i)*Sresult[0])))
#define RESULT2(i,j) (*((double*)(result_array->data + (i)*Sresult[0] + (j)*Sresult[1])))
#define RESULT3(i,j,k) (*((double*)(result_array->data + (i)*Sresult[0] + (j)*Sresult[1] + (k)*Sresult[2])))
#define RESULT4(i,j,k,l) (*((double*)(result_array->data + (i)*Sresult[0] + (j)*Sresult[1] + (k)*Sresult[2] + (l)*Sresult[3])))
intp* Nresult = result_array->dimensions;
intp* Sresult = result_array->strides;
int Dresult = result_array->nd;
double* result = (double*) result_array->data;
result_used = 1;
py_start = py_start;
double start = convert_to_float(py_start,"start");
start_used = 1;
py_end = py_end;
double end = convert_to_float(py_end,"end");
end_used = 1;
/*<function call here>*/
const int size = Nresult[0];
const double step = (end-start)/(size-1);
double val = start;
for (int i = 0; i < size; i++)
{
result[i] = val;
val += step;
}
if(py_local_dict)
{
py::dict local_dict = py::dict(py_local_dict);
}
}
catch(...)
{
return_val = py::object();
exception_occured = 1;
}
/*cleanup code*/
if(result_used)
{
Py_XDECREF(py_result);
#undef RESULT1
#undef RESULT2
#undef RESULT3
#undef RESULT4
}
if(!(PyObject*)return_val && !exception_occured)
{
return_val = Py_None;
}
return return_val.disown();
}
static PyMethodDef compiled_methods[] =
{
{"Ramp",(PyCFunction)Ramp , METH_VARARGS|METH_KEYWORDS},
{NULL, NULL} /* Sentinel */
};
PyMODINIT_FUNC initramp_ext(void)
{
Py_Initialize();
import_array();
PyImport_ImportModule("numpy");
(void) Py_InitModule("ramp_ext", compiled_methods);
}
#ifdef __CPLUSCPLUS__
}
#endif
|