Changeset 29fc843 in git for Singular/pyobject.cc


Ignore:
Timestamp:
Aug 1, 2012, 11:31:30 PM (11 years ago)
Author:
Alexander Dreyer <alexander.dreyer@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'ad2543eab51733612ba7d118afc77edca719600e')
Children:
4e59df46c64bb033f20a2ce6b6f131f03b06e6eb
Parents:
a04a05cbf86469aad8cb230b8c9418b1b96c2925
git-author:
Alexander Dreyer <alexander.dreyer@itwm.fraunhofer.de>2012-08-01 23:31:30+02:00
git-committer:
Alexander Dreyer <alexander.dreyer@itwm.fraunhofer.de>2012-08-01 23:32:11+02:00
Message:
new: convert pyobject <-> intvec
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/pyobject.cc

    ra04a05 r29fc843  
    2121
    2222#include <kernel/febase.h>
     23#include <kernel/intvec.h>
    2324
    2425#include "subexpr.h"
     
    298299class PythonCastStatic:
    299300  public PythonObject {
     301  typedef PythonCastStatic self;
    300302public:
    301303
     
    309311  ptr_type get(ptr_type value)       { return value; }
    310312  ptr_type get(long value)           { return PyInt_FromLong(value); }
     313  ptr_type get(int value)            { return PyInt_FromLong((long)value); }
    311314  ptr_type get(const char* value)    { return PyString_FromString(value); }
    312315  ptr_type get(char* value) { return get(const_cast<const char*>(value)); }
     316  ptr_type get(intvec* value);       // inlined below
    313317  ptr_type get(lists value);         // inlined after PythonObjectDynamic
    314318};
    315319
    316 
     320template <class CastType>
     321inline PythonObject::ptr_type
     322PythonCastStatic<CastType>::get(intvec* value)
     323{
     324  ptr_type pylist(PyList_New(0));
     325  for (int idx = 0; idx < value->length(); ++idx)
     326    PyList_Append(pylist, self::get((*value)[idx]));
     327
     328  return pylist;
     329}
    317330
    318331/** @class PythonCastDynamic
     
    337350    case STRING_CMD: return PythonCastStatic<const char*>(value);
    338351    case LIST_CMD:   return PythonCastStatic<lists>(value);
     352    case INTVEC_CMD: return PythonCastStatic<intvec*>(value);
    339353    }
    340354
     
    521535      res->data = (void*) omStrDup("pyobject");
    522536      res->rtyp = STRING_CMD; 
    523       return FALSE; 
     537      return FALSE;
    524538  }
    525539
     
    575589BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
    576590{
    577   typedef PythonCastStatic<PythonObject::sequence_tag> seq_type;
    578 
    579591  switch(op)                    // built-in return types first
    580592  {
     
    586598      return FALSE;
    587599    }
    588   }
    589 
     600
     601    case INTVEC_CMD:
     602      PythonObject obj = PythonCastStatic<>(args->Data());
     603      unsigned long len = obj.size();
     604
     605      intvec* vec = new intvec(len);
     606      for(unsigned long idx = 0; idx != len; ++idx) {
     607        long value = PyInt_AsLong(obj[idx]);
     608        (*vec)[idx] = static_cast<int>(value);
     609
     610        if ((value == -1) &&  PyErr_Occurred()) {
     611          value = 0;
     612          PyErr_Clear();
     613        }
     614        if (value != long((*vec)[idx])) {
     615          delete vec;
     616          Werror("'pyobject` cannot be converted to intvec");
     617          return TRUE;
     618        }
     619      }
     620      res->data = (void *)vec;
     621      res->rtyp = op;
     622      return FALSE;
     623  }
    590624  typedef PythonCastStatic<PythonObject::sequence_tag> seq_type;
    591625  if (! PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res))
Note: See TracChangeset for help on using the changeset viewer.