Thread: PythonC reading string from PyDict

  1. #1
    Webhead Spidey's Avatar
    Join Date
    Jul 2009

    PythonC reading string from PyDict


    I'm using python to set up a rendering engine. The settings are stored in the script file as dictionary objects.


    RenderInfo = {};
    RenderInfo['Title']  					= "Simple Python Board"; #window Title
    RenderInfo['ScreenWidth']                      =  640;
    I'm trying to read this in C++ like this

    PyObject *renderInfo,*renderStats;
    renderInfo = PyDict_GetItemString(pDict,"RenderInfo");
    renderStats = PyDict_GetItemString(renderInfo,"Title");
    char* title = PyBytes_AsString(renderStats);
    //Also Tried  PyBytesArray_AsString
    printf("%s\n",title);  // Prints NULL
    renderStats = PyDict_GetItemString(renderInfo,"ScreenWidth");
    int sw = PyLong_AsLong(renderStats);
    printf("%d\n",sw); // prints 640
    All other types work, but the string's fail and return NULL. This is the first time I'm using the PyBytes_AsString function since it replaced PyString_AsString in version 3.1.

    Does Anyone know why this is not working ?
    Spidey out!

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Well, still no luck. I've added a temporary hack to fix this issue but I still can't read a string. Any Ideas ?
    Spidey out!

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Henderson, NV
    I have a lame idea and given that I just woke from a nap, take it for what it is worth. First, if you have ever used Python to do XML-RPC based calls passing objects as anything more than very simple data types, you know that you have to 'stringify' the object. This same method could be used to stringify your dictionary which C++ could then parse. I originally wrote a Python-based XML-RPC server, then wrote a C++ client to interact with it and this is what I wound up doing (writing C++ to parse the stringified objects that were being returned). There has GOT to be a better method but this does work....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM