Code Monkey home page Code Monkey logo

cxxpython's Introduction

cxxpython

A C++ Wrapper of the Python C Api

Getting Started

This is a example of the python docs example:

#define PY_SSIZE_T_CLEAN
#include <CxxPython.h>
#include <cstdlib> // system function
#include <iostream>

static CxxPyError* error = new CxxPyError { "spam.error", nullptr, nullptr };

static CxxPyObject *
spam_system(CxxPyObject *self, CxxPyObject *args)
{
    std::string command;
    int sts;

    if (!args->parseTuple("s", &command))
        return nullptr;
    sts = system(command);
    if (sts < 0)
        return error->raise("System command failed"); 
    return new CxxPyObject { sts };
}



static CxxPyMethodDef SpamMethods[] = {
    {"system",  spam_system, CXX_METH_VARARGS, "Execute a shell command."},
    { nullptr }        /* Sentinel */
};

static struct CxxPyModuleDef spammodule = {
    CxxPyModuleDef::HEAD_INIT,
    "spam",   /* name of module */
    "The example of CxxPython", /* module documentation, may be NULL */
    -1,       /* size of per-interpreter state of the module,
                 or -1 if the module keeps state in global variables. */
    SpamMethods
};

CxxPyMODINIT_FUNC
CxxPyInit_spam(void)
{
    return new CxxPyModule { spammodule };
}

int
main(int argc, char *argv[])
{
    wchar_t *program = CxxPyUtil::decodeLocale(argv[0], nullptr);
    if (program == nullptr) {
        std::cerr << "Fatal error: cannot decode argv[0]" << std::endl;
        exit(1);
    }

    CxxPython cxx_python { program };
  
    if (cxx_python.appendInitTab("spam", CxxPyInit_spam) == -1) {
        std::cerr << "Error: could not extend in-built modules table" << std::endl;
        exit(1);
    }

    /* Initialize the Python interpreter.  Required.
       If this step fails, it will be a fatal error. */
    cxx_python.init();

    /* Optionally import the module; alternatively,
       import can be deferred until the embedded script
       imports it. */
    CxxPyModule *pmodule = cxx_python.import("spam");
    if (!pmodule) {
        cxx_python.raise_error();
    }

    CxxPyMem::free(program);
    return 0;
}




cxxpython's People

Contributors

nightprog avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.