473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Question about extending the interperter

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

Jul 19 '05 #1
6 1261
"Eli" wrote:
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>

Jul 19 '05 #2
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

Jul 19 '05 #3
> 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:
def f() : .... pass
.... print f.func_name f

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

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 -

--
Learn from the mistakes of others.
You can't live long enough to make them all yourself.
--

Jul 19 '05 #4
"Eli" <el**@flashmail.com> wrote:
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)
import mymodule
mymodule.myfunc("hello")

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>

Jul 19 '05 #5
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

Jul 19 '05 #6
Eli
Thanks for your answer, I've tried the way Fredrik suggested which
pointed out to a solution.

cheers

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Guinness Mann | last post by:
Greetings, I think I saw an article yesterday in which you told someone not to make an Identity column a primary key. I seem to remember you saying something like "It undermines the entire...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
0
by: VJ | last post by:
How do I turn of the Deisgn Time VB interperter.. it is just annoying, by trying to add items to the Task List and some times hangs... VJ
14
by: petermichaux | last post by:
Hi, Hopefully the group doesn't mind an(other) inheritance question. Maybe the prototype inheritance style is starting to become a low dim light in my brain...probably not yet. ---- If I...
8
by: Robert | last post by:
Hi! So far I have been doing "inheritance" in Javascript like this: ****** function TestObject(value) { this.value = value; }
29
by: Knut Olsen-Solberg | last post by:
I try to change the text in a <p> using getElementById(). I wonder what properties exists, and which one to use here. (The following does not work.) Regards Knut ______________________ ...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
12
by: Bit byte | last post by:
I have an application written in C (actually PostgreSQL). The application appears to have been built using the Mingw set of tools (mingw compiler tools). I want to write an extension library...
4
by: Martitza | last post by:
Hi. I work for a small company (actually in process of forming) interested in embedding or extending python as part of our commercial non-open-source product. We have legal counsel, but are...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.