Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

c++ - Replacing PyString_FromString method in python 3

TLDR; PyString_FromString doesn't work in Python3.5 so I need an alternative.

I am following an example for including python 3 within a C++ project from this python documentation: https://docs.python.org/3/extending/embedding.html

Everything is working fine but now I want to change the line that reads:

pValue = PyLong_FromLong(atoi(argv[i + 3]));

to the following (I also no longer use i, that was from a loop):

pValue = PyString_FromString("A string instead of a number");

It seems the PyString_FromString function is no longer an option Python3.5 and I get the following error when I compile the code with g++:

main.cpp:559:60: error: ‘PyString_FromString’ was not declared in this scope
         pValue = PyString_FromString("A string instead of a number");`

Any idea on how I get around this? No matter what I look for I can't seem to find a solution that doesn't throw an error. I need to pass a string to my Python file and I have a string to start with already.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PyUnicode_FromString()

if (!(pValue = PyUnicode_FromString("A string instead of a number")))
  return NULL;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...