Changeset 188de86 in git for Singular/pyobject.cc
- Timestamp:
- Aug 2, 2012, 4:09:13 PM (11 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'c987db42cd2ec943b97ac5746c99892ceddf909c')
- Children:
- 4f9652847609cefd5ff3b91d14f34611592ab699
- Parents:
- 0419aba8292e7109329604d8ad03b70f8ef6542344a1c2199e33375a631289eaab7a81954c8383e2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/pyobject.cc
r0419ab r188de86 16 16 #include <kernel/mod2.h> 17 17 #include <misc/auxiliary.h> 18 #include "newstruct.h" 18 19 19 20 #include <omalloc/omalloc.h> 20 21 21 22 #include <kernel/febase.h> 22 // #include <kernel/longrat.h>23 #include <kernel/intvec.h> 23 24 24 25 #include "subexpr.h" … … 110 111 111 112 PythonObject(): m_ptr(Py_None) { } 112 PythonObject(ptr_type ptr): m_ptr(ptr) {if (!ptr) handle_exception();} 113 PythonObject(ptr_type ptr): m_ptr(ptr) { 114 if (!ptr && handle_exception()) m_ptr = Py_None; 115 } 113 116 114 117 ptr_type check_context(ptr_type ptr) const { … … 129 132 return *this; 130 133 131 Werror("unary op %d not implemented for pyobject", op); 132 return self(); 134 return self(NULL); 133 135 } 134 136 … … 149 151 case '.': case COLONCOLON: case ATTRIB_CMD: return attr(arg); 150 152 } 151 Werror("binary op %d not implemented for pyobject!", op); 152 153 return self(); 153 return self(NULL); 154 154 } 155 155 … … 159 159 switch(op) 160 160 { 161 case ATTRIB_CMD: PyObject_SetAttr(*this, arg1, arg2); return self();162 }163 164 Werror("ternary op %d not implemented for pyobject!", op);165 return self( );161 case ATTRIB_CMD: 162 if(PyObject_SetAttr(*this, arg1, arg2) == -1) handle_exception(); 163 return self(); 164 } 165 return self(NULL); 166 166 } 167 167 … … 186 186 BOOLEAN assign_to(leftv result) 187 187 { 188 return (m_ptr == Py_None? none_to(result): python_to(result));188 return (m_ptr? (m_ptr == Py_None? none_to(result): python_to(result)): TRUE); 189 189 } 190 190 … … 229 229 } 230 230 231 void handle_exception() { 231 BOOLEAN handle_exception() const { 232 233 if(!PyErr_Occurred()) return FALSE; 232 234 233 235 PyObject *pType, *pMessage, *pTraceback; … … 242 244 243 245 PyErr_Clear(); 246 return TRUE; 244 247 } 245 248 … … 268 271 BOOLEAN none_to(leftv result) const 269 272 { 273 Py_XDECREF(m_ptr); 270 274 result->data = NULL; 271 275 result->rtyp = NONE; … … 276 280 { 277 281 result->data = m_ptr; 282 Py_XINCREF(m_ptr); 278 283 result->rtyp = PythonInterpreter::id(); 279 284 return !m_ptr; … … 295 300 class PythonCastStatic: 296 301 public PythonObject { 302 typedef PythonCastStatic self; 297 303 public: 298 304 … … 306 312 ptr_type get(ptr_type value) { return value; } 307 313 ptr_type get(long value) { return PyInt_FromLong(value); } 314 ptr_type get(int value) { return PyInt_FromLong((long)value); } 308 315 ptr_type get(const char* value) { return PyString_FromString(value); } 309 316 ptr_type get(char* value) { return get(const_cast<const char*>(value)); } 317 ptr_type get(intvec* value); // inlined below 310 318 ptr_type get(lists value); // inlined after PythonObjectDynamic 311 319 }; 312 320 313 321 template <class CastType> 322 inline PythonObject::ptr_type 323 PythonCastStatic<CastType>::get(intvec* value) 324 { 325 ptr_type pylist(PyList_New(0)); 326 for (int idx = 0; idx < value->length(); ++idx) 327 PyList_Append(pylist, self::get((*value)[idx])); 328 329 return pylist; 330 } 314 331 315 332 /** @class PythonCastDynamic … … 334 351 case STRING_CMD: return PythonCastStatic<const char*>(value); 335 352 case LIST_CMD: return PythonCastStatic<lists>(value); 336 // case UNKNOWN: return PythonCastStatic<const char*>((void*)value->Name()); 337 } 338 339 Werror("type # %d incompatible with pyobject", typeId); 353 case INTVEC_CMD: return PythonCastStatic<intvec*>(value); 354 } 355 356 sleftv tmp; 357 BOOLEAN newstruct_equal(int, leftv, leftv); // declaring overloaded '=' 358 if (!newstruct_equal(PythonInterpreter::id(), &tmp, value)) 359 return PythonCastStatic<>(&tmp); 360 361 if (typeId > MAX_TOK) // custom types 362 { 363 blackbox *bbx = getBlackboxStuff(typeId); 364 assume(bbx != NULL); 365 if (! bbx->blackbox_Op1(PythonInterpreter::id(), &tmp, value)) 366 return PythonCastStatic<>(&tmp); 367 } 368 369 Werror("type '%s` incompatible with 'pyobject`", iiTwoOps(typeId)); 340 370 return PythonObject(); 341 371 } … … 347 377 { 348 378 ptr_type pylist(PyList_New(0)); 349 for ( size_t i = 0; i <= value->nr; ++i)379 for (int i = 0; i <= value->nr; ++i) 350 380 PyList_Append(pylist, PythonCastDynamic((value->m) + i)); 351 381 … … 416 446 PyRun_SimpleString(reinterpret_cast<const char*>(arg->Data())); 417 447 sync_contexts(); 448 449 Py_XINCREF(Py_None); 418 450 return PythonCastStatic<>(Py_None).assign_to(result); 419 451 } … … 448 480 sync_contexts(); 449 481 482 Py_XINCREF(Py_None); 450 483 return PythonCastStatic<>(Py_None).assign_to(result); 451 484 } … … 454 487 void* pyobject_Init(blackbox*) 455 488 { 489 Py_XINCREF(Py_None); 456 490 return Py_None; 457 491 } … … 495 529 long value = PyInt_AsLong(PythonCastStatic<>(head)); 496 530 if( (value == -1) && PyErr_Occurred() ) { 497 Werror(" pyobjectcannot be converted to integer");531 Werror("'pyobject` cannot be converted to integer"); 498 532 PyErr_Clear(); 499 533 return TRUE; … … 506 540 res->data = (void*) omStrDup("pyobject"); 507 541 res->rtyp = STRING_CMD; 508 return FALSE; 509 510 case LIST_CMD: 542 return FALSE; 543 } 544 545 if (!PythonCastStatic<>(head)(op).assign_to(res)) 511 546 return FALSE; 512 547 513 }514 return PythonCastStatic<>(head)(op).assign_to(res);548 BOOLEAN newstruct_Op1(int, leftv, leftv); // forward declaration 549 return newstruct_Op1(op, res, head); 515 550 } 516 551 … … 532 567 return lhs.attr(get_attrib_name(arg2)).assign_to(res); 533 568 } 569 534 570 PythonCastDynamic rhs(arg2); 535 return lhs(op, rhs).assign_to(res); 571 if (!lhs(op, rhs).assign_to(res)) 572 return FALSE; 573 574 BOOLEAN newstruct_Op2(int, leftv, leftv, leftv); // forward declaration 575 return newstruct_Op2(op, res, arg1, arg2); 576 536 577 } 537 578 … … 543 584 PythonCastDynamic rhs2(arg3); 544 585 545 return lhs(op, rhs1, rhs2).assign_to(res); 586 if (!lhs(op, rhs1, rhs2).assign_to(res)) 587 return FALSE; 588 589 return blackboxDefaultOp3(op, res, arg1, arg2, arg3); 546 590 } 547 591 … … 550 594 BOOLEAN pyobject_OpM(int op, leftv res, leftv args) 551 595 { 552 typedef PythonCastStatic<PythonObject::sequence_tag> seq_type;553 554 596 switch(op) // built-in return types first 555 597 { … … 561 603 return FALSE; 562 604 } 563 } 564 605 606 case INTVEC_CMD: 607 PythonObject obj = PythonCastStatic<>(args->Data()); 608 unsigned long len = obj.size(); 609 610 intvec* vec = new intvec(len); 611 for(unsigned long idx = 0; idx != len; ++idx) { 612 long value = PyInt_AsLong(obj[idx]); 613 (*vec)[idx] = static_cast<int>(value); 614 615 if ((value == -1) && PyErr_Occurred()) { 616 value = 0; 617 PyErr_Clear(); 618 } 619 if (value != long((*vec)[idx])) { 620 delete vec; 621 Werror("'pyobject` cannot be converted to intvec"); 622 return TRUE; 623 } 624 } 625 res->data = (void *)vec; 626 res->rtyp = op; 627 return FALSE; 628 } 565 629 typedef PythonCastStatic<PythonObject::sequence_tag> seq_type; 566 return PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res); 630 if (! PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res)) 631 return FALSE; 632 633 BOOLEAN newstruct_OpM(int, leftv, leftv); // forward declaration 634 return newstruct_OpM(op, res, args); 567 635 } 568 636 … … 637 705 b->blackbox_Op3 = pyobject_Op3; 638 706 b->blackbox_OpM = pyobject_OpM; 707 b->data = omAlloc0(newstruct_desc_size()); 639 708 640 709 PythonInterpreter::init(setBlackboxStuff(b,"pyobject"));
Note: See TracChangeset
for help on using the changeset viewer.