0

When i run this code

./call try1 getByteLength '127 89 89'

it only accepts 127 as its argument and ignore the rest of it. [getByteLength] is just name of python function that i am calling from C.

I have use this code from python official page:

How can i do this ?

#include <Python.h>

int main(int argc, char *argv[]) {
Py_Initialize();
    PyRun_SimpleString("import sys");
        PyRun_SimpleString("import binascii");
        PyRun_SimpleString("sys.path.append(\".\")");
        PyObject *pName, *pModule, *pDict, *pFunc;
        PyObject *pArgs, *pValue;
        int i;

        if (argc < 3) {
            fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
            return 1;
        }

        //Py_Initialize();
        pName = PyString_FromString(argv[1]);
        /* Error checking of pName left out */

        pModule = PyImport_Import(pName);
        Py_DECREF(pName);

        if (pModule != NULL) {
            pFunc = PyObject_GetAttrString(pModule, argv[2]);
            /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
                pArgs = PyTuple_New(argc - 3);
                for (i = 0; i < argc - 3; ++i) {
                    pValue = PyInt_FromLong((argv[i + 3]));
                    if (!pValue) {
                       Py_DECREF(pArgs);
                       Py_DECREF(pModule);
                       fprintf(stderr, "Cannot convert argument\n");
                       return 1;
                    }
                    /* pValue reference stolen here: */
                    PyTuple_SetItem(pArgs, i, pValue);
            }
                pValue = PyObject_CallObject(pFunc, pArgs);
                Py_DECREF(pArgs);
                if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
                }
                else {
                    Py_DECREF(pFunc);
                    Py_DECREF(pModule);
                    PyErr_Print();
                    fprintf(stderr,"Call failed\n");
                    return 1;
                }
        }
        else {
            if (PyErr_Occurred())
                    PyErr_Print();
                    fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
            Py_XDECREF(pFunc);
            Py_DECREF(pModule);
    }
    else {
            PyErr_Print();
            fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
            return 1;
    }
        Py_Finalize();
        return 0;
}

2 Answers 2

2

When you use '127 89 89', that means the C code gets a single argument (a string with three numbers).

Then you call PyInt_FromLong((argv[i + 3])) which takes the address of the third argument and casts that to int (the address, it will ignore the content of the argument). This is basically a broken random number generator and won't do what you expect or need.

You should use PyInt_FromString() instead and use the pend pointer to move from one number in the string to the next. Note that you will probably have to skip the white space in the string yourself.

That said, I suggest to use ./call try1 getByteLength 127 89 89 (so you get each number in an individual argument) and then use PyInt_FromString() to parse each.

Sign up to request clarification or add additional context in comments.

5 Comments

"This is basically a random number generator." IBTD. The number you get is arbitrary, but far from usable as a RNG, failing most of the requirements.
can you please give me a example?
@user2738965: I'm not sure what kind of example you would need. Maybe you should run the code above in a debugger and see what it does?
I got it. I used PyString instead of PyInt.
You may want to post the solution here because someone else might run into a similar problem and wonder how you fixed it.
0

Instead of PyInt, I used PyString. It doesn't need any conversion.

pValue = PyString_FromString(argv[i + 3]);

For other methods, please refer python documentation page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.