Example #1
0
PyObject* PyBuiltInInput::__call__(vector<PyObject*>* args) {
    PyObject* x;
    PyStr* y;
    string input;
    char buffer[256];
    ostringstream msg;

    if (args->size() != 1) {
         msg << "TypeError: expected 1 arguments, got " << args->size();
        throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
    }
    
    x = (*args)[0];

    if (x->getType()->typeId() != PyStrType) {
        throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid argument to input(): Expected str, found " + x->getType()->toString());
    }

    y = (PyStr*) x;

    cout << y->toString();

    ostringstream s;

    cin.getline(buffer, 256);
    
    while (cin.gcount() == 255) {
        s << buffer;
        cin.getline(buffer, 256);
    }
    
    s << buffer;

    return new PyStr(s.str());
}
PyObject* TopoShapeFacePy::curveOnSurface(PyObject *args)
{
    PyObject* e;
    if (!PyArg_ParseTuple(args, "O!", &(TopoShapeEdgePy::Type), &e))
        return 0;

    try {
        TopoDS_Shape shape = static_cast<TopoShapeEdgePy*>(e)->getTopoShapePtr()->getShape();
        if (shape.IsNull()) {
            PyErr_SetString(PyExc_RuntimeError, "invalid shape");
            return 0;
        }

        TopoDS_Edge edge = TopoDS::Edge(shape);
        const TopoDS_Face& face = TopoDS::Face(getTopoShapePtr()->getShape());

        Standard_Real first, last;
        Handle(Geom2d_Curve) curve = BRep_Tool::CurveOnSurface(edge, face, first, last);
        std::unique_ptr<Part::Geom2dCurve> geo2d = getCurve2dFromGeom2d(curve);
        if (!geo2d)
            Py_Return;

        Py::Tuple tuple(3);
        tuple.setItem(0, Py::asObject(geo2d->getPyObject()));
        tuple.setItem(1, Py::Float(first));
        tuple.setItem(2, Py::Float(last));
        return Py::new_reference_to(tuple);
    }
    catch (Standard_Failure& e) {
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
        return 0;
    }
}
Example #3
0
PyObject* PyBuiltInIter::__call__(vector<PyObject*>* args) {
    vector<PyObject*>* iterArgs = new vector<PyObject*>();
    PyObject* x;
    ostringstream msg;
    
    if (args->size() != 1) {
        msg << "TypeError: expected 1 arguments, got " << args->size();
        throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());   
    }
    
    x = (*args)[0];
    
    PyObject* result = x->callMethod("__iter__",iterArgs);
    
    return result;
}
PyObject* GeometryCurvePy::intersectCS(PyObject *args)
{
    Handle_Geom_Curve curve = Handle_Geom_Curve::DownCast(getGeometryPtr()->handle());
    try {
        if (!curve.IsNull()) {
            PyObject *p;
            double prec = Precision::Confusion();
            if (!PyArg_ParseTuple(args, "O!|d", &(Part::GeometrySurfacePy::Type), &p, &prec))
                return 0;
            Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast(static_cast<GeometryPy*>(p)->getGeometryPtr()->handle());
            GeomAPI_IntCS intersector(curve, surf);
            if (!intersector.IsDone()) {
                PyErr_SetString(PyExc_Exception, "Intersection of curve and surface failed");
                return 0;
            }

            Py::List points;
            for (int i = 1; i <= intersector.NbPoints(); i++) {
                gp_Pnt p = intersector.Point(i);
                points.append(Py::Object(new PointPy(new GeomPoint(Base::Vector3d(p.X(), p.Y(), p.Z())))));
            }
            Py::List segments;
            for (int i = 1; i <= intersector.NbSegments(); i++) {
                Handle_Geom_Curve seg = intersector.Segment(i);
                segments.append(makeGeometryCurvePy(seg));
            }

            Py::Tuple tuple(2);
            tuple.setItem(0, points);
            tuple.setItem(1, segments);
            return Py::new_reference_to(tuple);
        }
    }
    catch (Standard_Failure) {
        Handle_Standard_Failure e = Standard_Failure::Caught();
        PyErr_SetString(PyExc_Exception, e->GetMessageString());
        return 0;
    }

    PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
    return 0;
}
Example #5
0
PyObject* PyNoneType::__call__(vector<PyObject*>* args) {

    vector<PyObject*>* emptyArgs = new vector<PyObject*>();
    PyObject * arg;
    string funName;

    switch (args-> size()) {
        case 0:
            throw new PyException(PYILLEGALOPERATIONEXCEPTION, "TypeError: cannot create 'NoneType' instances");
            break;
        case 1: 
            arg = (*args)[0];
            funName = "__"+this->toString()+"__";
            return arg->callMethod(funName,emptyArgs);
            break;
        default:
            throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Incorrect number of arguments for PyNone type");
            break;
    }
}
Example #6
0
PyObject* PyBuiltInFPrint::__call__(vector<PyObject*>* args) {
    //cerr << "In print() with " << args->size() << " args to print." <<  endl;
    string output = "";
    PyObject* x;
    PyObject* w;
    vector<PyObject*>* strargs = new vector<PyObject*>();
    ostringstream msg;

    if (args->size() != 1) {
        msg << "TypeError: expected 1 arguments, got " << args->size();
        throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());    }

    PyObject* arg = (*args)[0];
    output = arg->toString();

    if (arg->getType()->typeId()==PyStrType)
        output = processit(output);
    
    cout << output;

    return this;
}
Example #7
0
bool PyCachedObjectDecoder::Decode(PySubStream **in_ss)
{
    PySubStream *ss = *in_ss;    //consume
    *in_ss = NULL;

    PySafeDecRef( cache );
    PySafeDecRef( objectID );

    ss->DecodeData();
    if(ss->decoded() == NULL) {
		sLog.Error("PyCachedObjectDecoder","Unable to decode initial stream for PycachedObject");

        PyDecRef( ss );
        return false;
    }

    if(!ss->decoded()->IsObject()) {
		sLog.Error("PyCachedObjectDecoder","Cache substream does not contain an object: %s", ss->decoded()->TypeString());

        PyDecRef( ss );
        return false;
    }
    PyObject *po = (PyObject *) ss->decoded();
    //TODO: could check type string, dont care... (should be objectCaching.CachedObject)

    if(!po->arguments()->IsTuple()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's args is not a tuple: %s", po->arguments()->TypeString());

        PyDecRef( ss );
        return false;
    }
    PyTuple *args = (PyTuple *) po->arguments();

    if(args->items.size() != 7) {
		sLog.Error("PyCachedObjectDecoder","Cache object's args tuple has %lu elements instead of 7", args->items.size());

        PyDecRef( ss );
        return false;
    }

    if(!args->items[0]->IsTuple()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not a Tuple: %s", 0, args->items[0]->TypeString());

        PyDecRef( ss );
        return false;
    }
    //ignore unknown [1]
    /*if(!args->items[1]->IsInt()) {
        _log(CLIENT__ERROR, "Cache object's arg %d is not a None: %s", 1, args->items[1]->TypeString());

        PyDecRef( ss );
        return false;
    }*/
    if(!args->items[2]->IsInt()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not an Integer: %s", 2, args->items[2]->TypeString());
        PyDecRef( ss );
        return false;
    }

    if(!args->items[3]->IsInt()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not an Integer: %s", 3, args->items[3]->TypeString());
        PyDecRef( ss );
        return false;
    }

    if(!args->items[5]->IsInt()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not a : %s", 5, args->items[5]->TypeString());
        PyDecRef( ss );
        return false;
    }

    PyTuple *objVt = (PyTuple *) args->items[0];
    if(!objVt->items[0]->IsInt()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's version tuple %d is not an Integer: %s", 0, objVt->items[0]->TypeString());
        PyDecRef( ss );
        return false;
    }

    if(!objVt->items[1]->IsInt()) {
		sLog.Error("PyCachedObjectDecoder","Cache object's version tuple %d is not an Integer: %s", 1, objVt->items[1]->TypeString());
        PyDecRef( ss );
        return false;
    }

    PyInt *nodeidr = (PyInt *) args->items[2];
    PyInt *sharedr = (PyInt *) args->items[3];
    PyInt *compressedr = (PyInt *) args->items[5];
    PyInt *timer = (PyInt *) objVt->items[0];
    PyInt *versionr = (PyInt *) objVt->items[1];

    timestamp = timer->value();
    version = versionr->value();
    nodeID = nodeidr->value();
    shared = ( sharedr->value() != 0 );
    compressed = ( compressedr->value() != 0 );

    //content (do this as the last thing, since its the heavy lifting):
    if(args->items[4]->IsSubStream()) {
        cache = (PySubStream *) args->items[4];
        //take it
        args->items[4] = NULL;
    } else if(args->items[4]->IsBuffer()) {
        //this is a data buffer, likely compressed.
        PyBuffer* buf = args->items[4]->AsBuffer();

        PyIncRef( buf );
		cache = new PySubStream( buf );
    } else if(args->items[4]->IsString()) {
        //this is a data buffer, likely compressed, not sure why it comes through as a string...
        PyString* str = args->items[4]->AsString();

        cache = new PySubStream( new PyBuffer( *str ) );
    } else {
		sLog.Error("PyCachedObjectMgr", "Cache object's arg %d is not a substream or buffer: %s", 4, args->items[4]->TypeString());
        PyDecRef( ss );
        return false;
    }

    objectID = args->items[6]; PyIncRef(objectID);

    PyDecRef( ss );
    return true;
}
Example #8
0
bool PyAddress::Decode(PyRep *&in_object) {
    PyRep *base = in_object;
    in_object = NULL;

    if(!base->IsObject()) {
        codelog(NET__PACKET_ERROR, "Invalid element type, expected object");
        PyDecRef(base);
        return false;
    }

    PyObject *obj = (PyObject *) base;
    //do we care about the object type? should be "macho.MachoAddress"

    if(!obj->arguments()->IsTuple()) {
        codelog(NET__PACKET_ERROR, "Invalid argument type, expected tuple");
        PyDecRef(base);
        return false;
    }

    PyTuple *args = (PyTuple *) obj->arguments();
    if(args->items.size() < 3) {
        codelog(NET__PACKET_ERROR, "Not enough elements in address tuple: %lu", args->items.size());
        args->Dump(NET__PACKET_ERROR, "  ");
        PyDecRef(base);
        return false;
    }

    //decode the address type.
    if(!args->items[0]->IsInt()) {
        codelog(NET__PACKET_ERROR, "Wrong type on address type element (0)");
        args->items[0]->Dump(NET__PACKET_ERROR, "  ");
        PyDecRef(base);
        return false;
    }
    PyInt *typei = (PyInt *) args->items[0];
    switch(typei->value()) {
    case Any: {
        if(args->items.size() != 3) {
            codelog(NET__PACKET_ERROR, "Invalid number of elements in Any address tuple: %lu", args->items.size());
            PyDecRef(base);
            return false;
        }
        type = Any;

        if(!_DecodeService(args->items[1])
        || !_DecodeCallID(args->items[2])) {
            PyDecRef(base);
            return false;
        }

        break;
    }
    case Node: {
        if(args->items.size() != 4) {
            codelog(NET__PACKET_ERROR, "Invalid number of elements in Node address tuple: %lu", args->items.size());
            PyDecRef(base);
            return false;
        }
        type = Node;

        if(!_DecodeTypeID(args->items[1])
        || !_DecodeService(args->items[2])
        || !_DecodeCallID(args->items[3])) {
            PyDecRef(base);
            return false;
        }
        break;
    }
    case Client: {
        if(args->items.size() != 4) {
            codelog(NET__PACKET_ERROR, "Invalid number of elements in Client address tuple: %lu", args->items.size());
            PyDecRef(base);
            return false;
        }
        type = Client;

        if(!_DecodeTypeID(args->items[1])
        || !_DecodeCallID(args->items[2])
        || !_DecodeService(args->items[3])) {
            PyDecRef(base);
            return false;
        }

        break;
    }
    case Broadcast: {
        if(args->items.size() != 4) {
            codelog(NET__PACKET_ERROR, "Invalid number of elements in Broadcast address tuple: %lu", args->items.size());
            PyDecRef(base);
            return false;
        }
        type = Broadcast;

        if(!args->items[1]->IsString()) {
            codelog(NET__PACKET_ERROR, "Invalid type %s for brodcastID", args->items[1]->TypeString());
            PyDecRef(base);
            return false;
        } else if(!args->items[3]->IsString()) {
            codelog(NET__PACKET_ERROR, "Invalid type %s for idtype", args->items[3]->TypeString());
            PyDecRef(base);
            return false;
        }

        PyString *bid = (PyString *) args->items[1];
        PyString *idt = (PyString *) args->items[3];

        service = bid->content();
        bcast_idtype = idt->content();

        //items[2] is either a list or a tuple.
        /*
        //PyList *nclist = (PyList *) args->items[2];
        if(!nclist->items.empty()) {
            printf("Not decoding narrowcast list:");
            nclist->Dump(NET__PACKET_ERROR, "     ");
        }*/

        break;
    }
    default:
        codelog(NET__PACKET_ERROR, "Unknown address type: %c", typei->value() );
        PyDecRef(base);
        return false;
    }

    PyDecRef(base);
    return true;
}
Example #9
0
bool PyPacket::Decode(PyRep **in_packet)
{
    PyRep *packet = *in_packet;  //consume
    *in_packet = NULL;

    PySafeDecRef(payload);
    PySafeDecRef(named_payload);

    if(packet->IsChecksumedStream())
    {
        PyChecksumedStream* cs = packet->AsChecksumedStream();

        //TODO: check cs->checksum
        packet = cs->stream();
        PyIncRef( packet );

        PyDecRef( cs );
    }

    //Dragon nuance... it gets wrapped again
    if(packet->IsSubStream())
    {
        PySubStream* ss = packet->AsSubStream();

        ss->DecodeData();
        if(ss->decoded() == NULL)
        {
            codelog(NET__PACKET_ERROR, "failed: unable to decode initial packet substream.");
            PyDecRef(packet);
            return false;
        }

        packet = ss->decoded();
        PyIncRef( packet );

        PyDecRef( ss );
    }

    if(!packet->IsObject())
    {
        codelog(NET__PACKET_ERROR, "failed: packet body is not an 'Object': %s", packet->TypeString());
        PyDecRef(packet);
        return false;
    }

    PyObject *packeto = (PyObject *) packet;
    type_string = packeto->type()->content();

    if(!packeto->arguments()->IsTuple())
    {
        codelog(NET__PACKET_ERROR, "failed: packet body does not contain a tuple");
        PyDecRef(packet);
        return false;
    }

    PyTuple *tuple = (PyTuple *) packeto->arguments();

    if(tuple->items.size() != 7)
    {
        codelog(NET__PACKET_ERROR, "failed: packet body does not contain a tuple of length 7");
        PyDecRef(packet);

        return false;
    }

    if(!tuple->items[0]->IsInt())
    {
        codelog(NET__PACKET_ERROR, "failed: First main tuple element is not an integer");
        PyDecRef(packet);

        return false;
    }
    PyInt *typer = (PyInt *) tuple->items[0];
    switch(typer->value()) {
    case AUTHENTICATION_REQ:
    case AUTHENTICATION_RSP:
    case IDENTIFICATION_REQ:
    case IDENTIFICATION_RSP:
    case CALL_REQ:
    case CALL_RSP:
    case TRANSPORTCLOSED:
    case RESOLVE_REQ:
    case RESOLVE_RSP:
    case NOTIFICATION:
    case ERRORRESPONSE:
    case SESSIONCHANGENOTIFICATION:
    case SESSIONINITIALSTATENOTIFICATION:
    case PING_REQ:
    case PING_RSP:
        type = (MACHONETMSG_TYPE) typer->value();
        break;
    default:
        codelog(NET__PACKET_ERROR, "failed: Unknown message type %"PRIu64, typer->value());
        PyDecRef(packet);

        return false;
        break;
    }

    //source address
    if(!source.Decode(tuple->items[1]))
    {
        //error printed in decoder
        PyDecRef(packet);

        return false;
    }
    //dest address
    if(!dest.Decode(tuple->items[2]))
    {
        //error printed in decoder
        PyDecRef(packet);

        return false;
    }

    if(tuple->items[3]->IsInt())
    {
        PyInt *i = (PyInt *) tuple->items[3];
        userid = i->value();
    } else if(tuple->items[3]->IsNone()) {
        userid = 0;
    } else {
        codelog(NET__PACKET_ERROR, "failed: User ID has invalid type");
        PyDecRef(packet);
        return false;
    }

    //payload
    if(!(tuple->items[4]->IsBuffer() || tuple->items[4]->IsTuple())) {
        codelog(NET__PACKET_ERROR, "failed: Fifth main tuple element is not a tuple");
        PyDecRef(packet);
        return false;
    }
    payload = (PyTuple *) tuple->items[4];
    tuple->items[4] = NULL; //we keep this one


    //options dict
    if(tuple->items[5]->IsNone())
    {
        named_payload = NULL;
    }
    else if(tuple->items[5]->IsDict())
    {
        named_payload = (PyDict *) tuple->items[5];
        tuple->items[5] = NULL; //we keep this too.
    }
    else
    {
        codelog(NET__PACKET_ERROR, "failed: Sixth main tuple element is not a dict");
        PyDecRef(packet);
        return false;
    }

    PyDecRef(packet);
    return true;
}