Question about extending the interperter

E

Eli

Hi,
I've followed the Python docs about extending the Python interperter
and created an extension library.
I've added my functions like this:

static PyMethodDef pmylib_methods[] = {
{"foo", pmylib_foo, METH_VARARGS, "foo() doc string"},
....
}
static PyObject *pmylib_foo(PyObject *self, PyObject *args)
{
....
char *p;
if (!PyArg_ParseTuple(args, "s", &p))
....
}

And that's works fine.
The problem for me is that the pointer "p" in the last function points
to the arguments:
If a user caller foo("123") - p points to '123'.
What I need is to point it to the whole string received - 'foo
("123")'.

Is there a way I can do this?

Thanks in advance,
Eli
 
F

Fredrik Lundh

Eli said:
I've followed the Python docs about extending the Python interperter
and created an extension library.
I've added my functions like this:

static PyMethodDef pmylib_methods[] = {
{"foo", pmylib_foo, METH_VARARGS, "foo() doc string"},
...
}
static PyObject *pmylib_foo(PyObject *self, PyObject *args)
{
...
char *p;
if (!PyArg_ParseTuple(args, "s", &p))
...
}

And that's works fine.
The problem for me is that the pointer "p" in the last function points
to the arguments:
If a user caller foo("123") - p points to '123'.

foo("123") means "call the callable identifed by the expression 'foo' with
foo with the string '123'", so that's just what should happen.
What I need is to point it to the whole string received - 'foo
("123")'.

received by whom? if you call a function with an argument, the function
receives the argument. the expression used to locate the callable (in this
case, the function name) is not part of the call.
Is there a way I can do this?

no (at least not given how you've described your problem).

</F>
 
E

Eli

Thanks for the answer; I should better explain my problem.

My organization already has a DOS command line tool which I would like
to transffer to Python.
Every function this DOS command line too can execute has a definition
entry in an array:
{"function name", function address, other info... },
Which means if 'function name' was enetered by the user the command
line tool will call 'function address'. Note that this is a *big*
array.

Now I'm trying to pass the above enteries to Python's interperter
extension:
One way would be going over all of my enteries and add Python calls
like that:

static PyMethodDef pmylib_methods[] = {
{"function name 1", pmylib_1, METH_VARARGS, "..."},
{"function name 2", pmylib_2, METH_VARARGS, "..."},
etc
....
}
The only problem I need to preprocess the input first (initialize
strtok).
So a solution would be creating 'function 1' which preprocess the input
and calls the original function 1, than do so for any other function.
This works, but there are *lots* of such enteries and I'm trying a
general way of doing so.
If I would have the full line the user enetered I could make all of the
extensions to call a general function which will call the original
functions (it can do so because it has the function address as a
parameter).
If I could get the full line the user has entered that would be great,
if that's possible.

Hope I've explained it correctly...
cheers,
Eli
 
H

harold fellermann

The problem for me is that the pointer "p" in the last function points
to the arguments:
If a user caller foo("123") - p points to '123'.
What I need is to point it to the whole string received - 'foo
("123")'.

Is there a way I can do this?

no. at least not a simple one. you can obtain the function name by
func_name,
as the corresponding python code suggests:.... pass
....f

However, you still don't know, how the function has been invoked.
Consider:
f

of course, the situation can be much more sphisticated. f could be a
value
in a dict returned by some other function, and so on. Of course there
are
ways to inspect the source code (like pdb or exceptions do), but are you
sure this is the functionality to want?

Cheers,

- harold -
 
F

Fredrik Lundh

Eli said:
Thanks for the answer; I should better explain my problem.

that's always a good idea ;-)
So a solution would be creating 'function 1' which preprocess the input
and calls the original function 1, than do so for any other function.
This works, but there are *lots* of such enteries and I'm trying a
general way of doing so.

can you extract a list of all available commands? if so, you can
add a call dispatcher to your interface module, and use Python
code to generate wrappers for all your commands:

# File: mymodule.py

import _mymodule # import the C interface

class wrapper:
def __init__(self, func):
self.func = func
def __call__(self, *args):
# prepare args in a suitable way. e.g
args = " ".join(map(str, args))
return _mymodule.callafunction(self.func, args)

# get list of function names
FUNCTIONS = "myfunc", "yourfunc"

# register wrappers for all functions
g = globals()
for func in FUNCTIONS:
g[func] = wrapper(func)
DEBUG OUTPUT: myfunc(hello) -> 10
10

if the names are available in some internal structure, you can also
add a function that returns a list of function names, so you can do:

for func in _mymodule.getfunctionnames():
g[func] = wrapper(func)

</F>
 
E

Eli

I've tried that and it worked. I've used Python to generate wrapper and
it seems ok- I'm yet testing it, so far so good.

thanks,
Elie
 
E

Eli

Thanks for your answer, I've tried the way Fredrik suggested which
pointed out to a solution.

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top