Extended functions in embedded code

Laura Creighton lac at openend.se
Tue Oct 13 18:02:36 EDT 2015


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 <airween at gmail.com> 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



More information about the Python-list mailing list