How to get Python code to work with C++ App? - c++

I have the following python 3 file:
import base64
import xxx
str = xxx.GetString()
str2 = base64.b64encode(str.encode())
str3 = str2.decode()
print str3
xxx is a module exported by some C++ code. This script does not work because calling Py_InitModule on this script returns NULL. The weird thing is if I create a stub xxx.py in the same directory
def GetString() :
return "test"
and run the original script under python.exe, it works and outputs the base64 string. My question is why doesn't it like the return value of xxx.GetString? In the C++ code, it returns a string object. I hope I have explained my question well enough... this is a strange error.

I know everybody says this...but:
Boost has an awesome library for exposing classes to python and getting data to and fro. If you're having problems, and looking for alternatives is an option I'd highly recommend the boost python library of the C interface. I've used them both, boost wins hands down imo.

Py_InitModule() is for initializing extension modules written in C, which is not what you are looking for here. If you want to import a module from C, there is a wealth of functions available in the C API: http://docs.python.org/c-api/import.html
But if your aim is really to run a script rather than import a module, you could also use one of the PyRun_XXX() functions described here: http://docs.python.org/c-api/veryhigh.html

Er... You have to investigate why Py_InitModule returns NULL. Posting the Python code using that module won't help.

Related

ANTLR 4 Python Documentation

I am very new to antlr 4 and my target language is PYTHON2.
I am not able to understand CommonTokenStream in python and how I can access tokens in antlr 4.
What I require is to access tokens present in Hidden Channel ?
Please someone point me to some proper documentation where I can understand how to access tokens and manipulate them in python.
I am sorry if the question is vague, I am new here.
The ANTLR book is one.
https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference
In the chapter 12 "Wielding Lexical Black Magic", it has "Accessing hidden channel" section. Use the TokenStreamRewriter to rewrite the token.
You need to mentally convert Java code in the book to Python code. The runtime libraries have subtle differences but they are virtually the same.
It is not the only way. You can override lexer's emit() function (which I usually do). Then you have total control over the token routing.
If you are on python 3 it is all wonderfully done cooked and ready
https://github.com/jszheng/py3antlr4book
For Some Python start hints try
https://github.com/antlr/antlr4/blob/master/doc/python-target.md
If you are using Anaconda3 try egrep of class def import comments(#) of all *.py
Anaconda3\Lib\site-packages\antlr4_python3_runtime-4.7.1-py3.6.egg\antlr4
Or even write a ANTLR script to create the python docs and share with me and the world
Also at run time this helps to see what methods and properties are in say a CTX object
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
print("-------------------------------------------")
dump(ctx)
print("===========================================")

python-sip: How to access a DLL from both Python and C++

I have a C++ GUI, it load a DLL when running. I use SIP to import the DLL in python. I need to embed the python part in the GUI, and some data are needed to exchange between python and C++.
For example, in the C++ GUI, I can enter command from a panel, such as "drawSomething()", it will call corresponding function in python, and the result will be shown in the GUI.
Can I use SIP to
extract a C++ object from python object (just like the way boost.python does), or is there a better way to share data between python and c++ seamlessly?
thanks.
It turns out that I do not need to do anything complicated...
In my case, there is no difference to call functions in DLL from C++ or from python code embedded in C++.
I am totally over-thinked.
Please take a look at this Library
http://www.swig.org/Doc1.3/Python.html

how to get value from spinctrl and send it to .exe file in python script

I am new to python and wxpython, I am making a automated tool using python and for user interface wxpython and i use shell script.shell script can be called from the python. but now I am facing problem with the spinctrl value. whenever that spinctrl value changes it have to send that value into one txt.exe file which is written in BASH .{ if we run txt.exe file in command line it will ask for number then it will accept that value whenever we press enter}. but i am not able to understand that how to send value from spinctrl to txt.exe whenever i press "ok" button in GUI. please share your thoughts and knowledge.
Thank you
To call an executable in Python, you need to look at Python's subprocess module: http://docs.python.org/2/library/subprocess.html
If your exe doesn't accept arguments, then it won't work. If you created the exe yourself using something like PyInstaller or py2Exe, then you need to make it so your app can accept arguments. The simplest way is using sys.argv, but there are also the optparse and argparse libraries in Python as well.
I do not get a good feeling about your concept. It is hard to say, but I suspect you would benefit from talking with someone experienced about what you are trying to achieve and how it could best be done.
However, to get you started down your current path, I think you should take a look at wxExecute ( http://docs.wxwidgets.org/2.8/wx_processfunctions.html#wxexecute ). This will allow you to run your txt.exe utility from inside your GUI.
wxExecute is a wxWidgets utility. If you are working in python then there will be a more direct way to do this using a python utility - some-one else will have to advise on that. I suggest you edit your question for clarity so a python expert can help you.

python script to create another python script (or python executable)

As part of my code, I want to write a python method which when called, creates an executable file. (Another script that I can execute with python's interpreter is also fine).
This final script is almost fixed, except for a few input objects that only my method knows about, and which are necessary for the final executable to work (a dictionary for example). How can I link these objects to the final executable?
This sounds like a work for some templating engine. Jinja for example.
You can also use pickle to serialize/deserialize (save/load) the data which the other executables would use.

How to override Py_GetPrefix(), Py_GetPath()?

I'm trying to embed the Python interpreter and need to customize the way the Python standard library is loaded. Our library will be loaded from the same directory as the executable, not from prefix/lib/.
We have been successful in making this work by manually modifying sys.path after calling Py_Initialize(), however, this generates a warning because Py_Initialize is looking for site.py in ./lib/, and it's not present until after Py_Initialize has been called and we have updated sys.path.
The Python c-api docs hint that it's possible to override Py_GetPrefix() and Py_GetPath(), but give no indication of how. Does anyone know how I would go about overriding them?
You could set Py_NoSiteFlag = 1, call PyInitialize and import site.py yourself as needed.
Have you considered using putenv to adjust PYTHONPATH before calling Py_Initialize?
I see it was asked long ago, but I've just hit the same problem. Py_NoSiteFlag will help with the site module, but generally it's better to rewrite Modules/getpath.c; Python docs officially recommend this for “[a]n application that requires total control.” Python does import some modules during initialization (the one that hit me was encodings), so, unless you don't want them or have embedded them too, the module search path has to be ready before you call Py_Initialize().
From what I understand Py_GetPath merely returns module search path; Py_GetProgramFullPath is self-describing; and Py_GetPrefix and Py_GetExecPrefix are not used by anyone, except some mysterious “ILU”.
The following functions can be called before calling Py_Initialize():
Py_SetProgramName()
Py_SetPythonHome()
Py_SetPath()
All of these affect the way Python finds modules. I recommend reading the documentation on these functions and playing around with them.