Python Forum
C-API for Python 3 - Get String from Object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C-API for Python 3 - Get String from Object
#1
I try to read a string from a python object in the C-API but seem to be incapable to find the documentation on how to do that properly in Python 3. The problem could be Unicode or any other issue I am not aware of. I had a solution for Python 2.

So what is wrong with the following code? It fails in the last step with out==0.

---------------------------------
#include <stdio.h>
#include <Python.h>

const char* pystart = "t = \"test\"";

int main()
{
    printf("Test Programm for Python 3.8\n");

    Py_Initialize();

    PyObject* pModule = PyImport_AddModule("__main__"); //create main module
    if (!pModule)
    {
        printf("Could not create main module in Python.");
        return 0;
    }

    if (PyRun_SimpleString(pystart) == -1)
    {
        printf("Could not run pystart.");
        return 0;
    }

    PyObject* t = PyObject_GetAttrString(pModule, "t");
    if (!t)
    {
        printf("Could not get t.");
        return 0;
    }
    char* out = PyBytes_AsString(t);
    if (!out)
    {
        printf("Could not get the string.");
        return 0;
    }
    printf(out);

    return 1;
}
Reply
#2
With your code, the object t will not be a bytes object but a str object, that's why PyBytes_AsString fails and return NULL. You could replace pystart with
pystart = "t = b\"test\"";
Reply
#3
(Jun-22-2020, 04:12 PM)Gribouillis Wrote: With your code, the object t will not be a bytes object but a str object, that's why PyBytes_AsString fails and return NULL. You could replace pystart with
pystart = "t = b\"test\"";

That does indeed work, I tried it. Unfortunately, I cannot use this in the program I am developing. I need to get the content of a Python string via C.

I found the solution now. You have to actively convert with PyUnicode_AsEncodedString before applying PyBytes_AsString. I finally found that in the docs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python update binary object (override delivered Object properties) pierre38 4 1,745 May-19-2022, 07:52 AM
Last Post: pierre38
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,177 May-02-2021, 03:45 PM
Last Post: Anldra12
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 3,613 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,588 May-08-2020, 04:26 AM
Last Post: bowlofred
  converting string object inside a list into an intiger bwdu 4 2,590 Mar-31-2020, 10:36 AM
Last Post: buran
  type of object as a string Skaperen 1 2,273 Dec-02-2019, 08:01 AM
Last Post: midarq
  getting a full path string from a pathlib.PurePath object Skaperen 14 141,185 Mar-24-2018, 03:55 AM
Last Post: Skaperen
  How to convert a Status object to String? Vucko 6 7,228 Jan-14-2018, 08:33 PM
Last Post: buran
  Convert String to Datetime Object tkj80 2 33,767 Apr-20-2017, 08:45 AM
Last Post: volcano63
  want to know the kind of object whether its a python or json object johnkennykumar 5 59,293 Jan-21-2017, 08:47 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020