Re: Extended functions in embedded code

2015-10-15 Thread Ervin Hegedüs
Hi,

I've read many docs and examples, then I made a usable test version. If
anybody interested about this (and for the mailing list archives), then it
could be found here:

https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/

Hope this helps, and many thanks for all help.

Cheers,

a.

ps: after I've done, I realized, that will not good for me :). Nevermind,
this was a funny work.



On Tue, Oct 13, 2015 at 1:36 PM, Ervin Hegedüs  wrote:

> Hello there,
>
> I'm interesting for the embeding of Python code - the examples and docs
> are very helpfully. The main code, which embeds the Python interpreter, had
> written in C. There are several functions, what I have to use in embedded
> (Python) code, so I must to write them as Python extension.
>
> That's no problem - I found a solution for that, I don't need to made (and
> I don't _want_) a separated .so file (a new Python module): the extension
> in same C source, where the embedded code exists, like this:
>
> #include 
> #include 
>
> /* python mymodule */
> static PyObject*
> mymodule_usleep(PyObject *self, PyObject *args)
> {
> ...
> }
>
> ...
> static PyMethodDef mymodule_methods[] = {
> {"usleep",mymodule_usleep,METH_VARARGS,
> mymodule_usleep_doc},
> {NULL, NULL}
> };
>
> PyMODINIT_FUNC
> initmymodule(void)
> {
> (void) Py_InitModule("mymodule", mymodule_methods);
> }
>
> /* python mymodule */
>
>
> /* python embedding */
> void foo() {
> Py_Initialize();
> initmymodule();
> 
> Py_Finalize();
> }
>
> /* python embedding */
>
>
> Then I have a Python file, which the code above calls:
>
> import mymodule
>
> def bar(d)
> do_something()
> mymodule.usleep(1)
> return something
>
>
> Just my "2 cents" question: is there any way to make the extension without
> "import" keyword? Or is there a way to leave that?
>
>
> Thanks,
>
>
> a.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Ervin Hegedüs
On Wed, Oct 14, 2015 at 12:02:36AM +0200, Laura Creighton wrote:
> In a message of Tue, 13 Oct 2015 22:28:54 +0200, Ervin Hegedüs writes:
> >Hi Chris,
> >
> >what I misses: currently I'm using Python 2.7.
> >
> >On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote:
[...]

> >
> >PyModule_AddFunction was introduced in Python 3.5. Most of stable
> >Linux distribution has Python 3.4
> > 
> >> instead of the current module initialization. You import the name
> >> 'builtins', stuff some extra stuff into it, and then go on your merry
> >> way. It should be reasonably easy.
> >
> >Is there any other solution to add functions to builtins?
> >
> 
> You can stuff things into the __dict__ of __builtin__ if you like.
> It's highly frowned upon.
> But see discussion attatched to:
> http://code.activestate.com/recipes/577888-see-what-the-builtins-are/

As I understand this, it shows the Python's builtins (or
__builtins__) capabilities.

As Chris wrote, the soultion would be, that I'm loading the
__builtins__ module in C (through API), and add/extend its
__dict__ with my funtions.

The link above doesn't help me in this :).

Thanks,

a.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Chris Angelico
On Wed, Oct 14, 2015 at 7:28 AM, Ervin Hegedüs  wrote:
> Hi Chris,
>
> what I misses: currently I'm using Python 2.7.

Oh, sorry. In that case, you'll be importing "__builtin__" rather than
"builtins", but the same technique works.

> On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote:
>> It'd look broadly like this:
>>
>> /* initialize the interpreter, yada yada */
>> PyObject *builtins = PyImport_ImportModule("builtins");
>> PyModule_AddFunctions(builtins, mymodule_methods);
>
> PyModule_AddFunction was introduced in Python 3.5. Most of stable
> Linux distribution has Python 3.4

It's been years since I actually did this, so I cheated and just
flipped through the docs :) But there'll be a way to create a Python
function from a C function, and then you can simply stuff that into
the module's dictionary.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Laura Creighton
In a message of Tue, 13 Oct 2015 22:28:54 +0200, Ervin Hegedüs writes:
>Hi Chris,
>
>what I misses: currently I'm using Python 2.7.
>
>On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote:
>> On Wed, Oct 14, 2015 at 2:29 AM, Ervin Hegedüs  wrote:
>> >>
>> >> Sounds to me like the easiest way would be to inject into the
>> >> builtins. You should be able to import the builtins module from your C
>> >> code, and then stuff some extra attributes into it; they'll be
>> >> automatically available to the script, same as the "normal" built-in
>> >> names like int, super, and ValueError.
>> >
>> > well, sounds good - this solution would be right for me. Could
>> > you show me a good example and/or documentation about this? I've
>> > looked up, but "python extend built-in module" is may be too
>> > simple expression :).
>> 
>> It'd look broadly like this:
>> 
>> /* initialize the interpreter, yada yada */
>> PyObject *builtins = PyImport_ImportModule("builtins");
>> PyModule_AddFunctions(builtins, mymodule_methods);
>
>PyModule_AddFunction was introduced in Python 3.5. Most of stable
>Linux distribution has Python 3.4
> 
>> instead of the current module initialization. You import the name
>> 'builtins', stuff some extra stuff into it, and then go on your merry
>> way. It should be reasonably easy.
>
>Is there any other solution to add functions to builtins?
>
>
>Thanks,
>
>a.

You can stuff things into the __dict__ of __builtin__ if you like.
It's highly frowned upon.
But see discussion attatched to:
http://code.activestate.com/recipes/577888-see-what-the-builtins-are/

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Emile van Sebille

On 10/13/2015 1:32 PM, Ervin Hegedüs wrote:

Hi,

On Tue, Oct 13, 2015 at 08:55:42AM -0700, Emile van Sebille wrote:

On 10/13/2015 8:29 AM, Ervin Hegedüs wrote:

Hi Chris,

On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote:



Sounds to me like the easiest way would be to inject into the
builtins. You should be able to import the builtins module from your C
code, and then stuff some extra attributes into it; they'll be
automatically available to the script, same as the "normal" built-in
names like int, super, and ValueError.


well, sounds good - this solution would be right for me. Could
you show me a good example and/or documentation about this? I've
looked up, but "python extend built-in module" is may be too
simple expression :).


Maybe the site module helps you. See
https://docs.python.org/3/library/site.html


no, I think this module is totally different, what I need.



or perhaps 
http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions


Emile



--
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Ervin Hegedüs
Hi,

On Tue, Oct 13, 2015 at 08:55:42AM -0700, Emile van Sebille wrote:
> On 10/13/2015 8:29 AM, Ervin Hegedüs wrote:
> >Hi Chris,
> >
> >On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote:
> 
> >>Sounds to me like the easiest way would be to inject into the
> >>builtins. You should be able to import the builtins module from your C
> >>code, and then stuff some extra attributes into it; they'll be
> >>automatically available to the script, same as the "normal" built-in
> >>names like int, super, and ValueError.
> >
> >well, sounds good - this solution would be right for me. Could
> >you show me a good example and/or documentation about this? I've
> >looked up, but "python extend built-in module" is may be too
> >simple expression :).
> 
> Maybe the site module helps you. See
> https://docs.python.org/3/library/site.html

no, I think this module is totally different, what I need.


thanks,

a.


-- 
I � UTF-8
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Ervin Hegedüs
Hi Chris,

what I misses: currently I'm using Python 2.7.

On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote:
> On Wed, Oct 14, 2015 at 2:29 AM, Ervin Hegedüs  wrote:
> >>
> >> Sounds to me like the easiest way would be to inject into the
> >> builtins. You should be able to import the builtins module from your C
> >> code, and then stuff some extra attributes into it; they'll be
> >> automatically available to the script, same as the "normal" built-in
> >> names like int, super, and ValueError.
> >
> > well, sounds good - this solution would be right for me. Could
> > you show me a good example and/or documentation about this? I've
> > looked up, but "python extend built-in module" is may be too
> > simple expression :).
> 
> It'd look broadly like this:
> 
> /* initialize the interpreter, yada yada */
> PyObject *builtins = PyImport_ImportModule("builtins");
> PyModule_AddFunctions(builtins, mymodule_methods);

PyModule_AddFunction was introduced in Python 3.5. Most of stable
Linux distribution has Python 3.4
 
> instead of the current module initialization. You import the name
> 'builtins', stuff some extra stuff into it, and then go on your merry
> way. It should be reasonably easy.

Is there any other solution to add functions to builtins?


Thanks,

a.
 

-- 
I � UTF-8
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Emile van Sebille

On 10/13/2015 8:29 AM, Ervin Hegedüs wrote:

Hi Chris,

On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote:



Sounds to me like the easiest way would be to inject into the
builtins. You should be able to import the builtins module from your C
code, and then stuff some extra attributes into it; they'll be
automatically available to the script, same as the "normal" built-in
names like int, super, and ValueError.


well, sounds good - this solution would be right for me. Could
you show me a good example and/or documentation about this? I've
looked up, but "python extend built-in module" is may be too
simple expression :).


Maybe the site module helps you. See 
https://docs.python.org/3/library/site.html


Emile



--
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Chris Angelico
On Wed, Oct 14, 2015 at 2:29 AM, Ervin Hegedüs  wrote:
>>
>> Sounds to me like the easiest way would be to inject into the
>> builtins. You should be able to import the builtins module from your C
>> code, and then stuff some extra attributes into it; they'll be
>> automatically available to the script, same as the "normal" built-in
>> names like int, super, and ValueError.
>
> well, sounds good - this solution would be right for me. Could
> you show me a good example and/or documentation about this? I've
> looked up, but "python extend built-in module" is may be too
> simple expression :).

It'd look broadly like this:

/* initialize the interpreter, yada yada */
PyObject *builtins = PyImport_ImportModule("builtins");
PyModule_AddFunctions(builtins, mymodule_methods);

instead of the current module initialization. You import the name
'builtins', stuff some extra stuff into it, and then go on your merry
way. It should be reasonably easy.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Ervin Hegedüs
Hi Chris,

On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote:
> On Wed, Oct 14, 2015 at 1:59 AM, Ervin Hegedüs  wrote:
> > no, I have filesystem. I help to contribute a software, which had
> > written in C. The configuration schema is very simple, there are
> > several keywords, but not all required function could be
> > configure with them. Python would be a good choice, but the
> > users aren't programmers in most cases. I just don't want to
> > start the documentation, that
> >
> > "Make a Python script like this:
> >
> > import mymodul
> >
> > "
> >
> > and nobody knows, why is it require, because there isn't any
> > module with this name.
> 
> Sounds to me like the easiest way would be to inject into the
> builtins. You should be able to import the builtins module from your C
> code, and then stuff some extra attributes into it; they'll be
> automatically available to the script, same as the "normal" built-in
> names like int, super, and ValueError.

well, sounds good - this solution would be right for me. Could
you show me a good example and/or documentation about this? I've
looked up, but "python extend built-in module" is may be too
simple expression :).


a.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Chris Angelico
On Wed, Oct 14, 2015 at 1:59 AM, Ervin Hegedüs  wrote:
> no, I have filesystem. I help to contribute a software, which had
> written in C. The configuration schema is very simple, there are
> several keywords, but not all required function could be
> configure with them. Python would be a good choice, but the
> users aren't programmers in most cases. I just don't want to
> start the documentation, that
>
> "Make a Python script like this:
>
> import mymodul
>
> "
>
> and nobody knows, why is it require, because there isn't any
> module with this name.

Sounds to me like the easiest way would be to inject into the
builtins. You should be able to import the builtins module from your C
code, and then stuff some extra attributes into it; they'll be
automatically available to the script, same as the "normal" built-in
names like int, super, and ValueError.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Ervin Hegedüs
Hi,


On Tue, Oct 13, 2015 at 02:51:21PM +0200, Laura Creighton wrote:
> Are you looking for this:?
> https://docs.python.org/3.5/library/runpy.html

I think I'm not - I'm afraid, the runpy modul wasn't developed
for me, for this reason.

> or maybe this:?
> https://docs.python.org/3.5/library/importlib.html#importlib.import_module

I think this is just an "alias" of the standard import.

> Or is your real problem 'I don't have a filesystem'?

no, I have filesystem. I help to contribute a software, which had
written in C. The configuration schema is very simple, there are
several keywords, but not all required function could be
configure with them. Python would be a good choice, but the
users aren't programmers in most cases. I just don't want to
start the documentation, that

"Make a Python script like this:

import mymodul

"

and nobody knows, why is it require, because there isn't any
module with this name.


As I wrote, that's just my 2-cents question, not a critical issue
- it could be better to know, is it possible or not. That's all :)
 


a.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extended functions in embedded code

2015-10-13 Thread Laura Creighton
Are you looking for this:?
https://docs.python.org/3.5/library/runpy.html
or maybe this:?
https://docs.python.org/3.5/library/importlib.html#importlib.import_module
Or is your real problem 'I don't have a filesystem'?

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list