473,387 Members | 1,621 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,387 software developers and data experts.

Import/Create module from buffer

Hi people,

I'm currently working on python embedding with C++. My goal is that
the C++ part handle files writing/reading so that the Python part only
works with buffers.

I succeeded in buffer exchanges. The problem is that some of the files
I read/write are python files so that, before embedding, I imported
them as modules when I needed them.

But now that the Python part only receive buffers, I can't do it
anymore. So I wonder :
- is it possible to import module from a buffer instead of files?
- or is it possible to create a module object from my buffer?

I looked on the Internet but didn't find anything about that. Does
anyone have an idea?

Thanks
Benjamin
Jun 27 '08 #1
5 2747
En Mon, 12 May 2008 05:49:22 -0300, Gruik <be**************@gmail.comescribió:
I'm currently working on python embedding with C++. My goal is that
the C++ part handle files writing/reading so that the Python part only
works with buffers.

I succeeded in buffer exchanges. The problem is that some of the files
I read/write are python files so that, before embedding, I imported
them as modules when I needed them.

But now that the Python part only receive buffers, I can't do it
anymore. So I wonder :
- is it possible to import module from a buffer instead of files?
- or is it possible to create a module object from my buffer?
Yes, first compile the buffer to get a code object, then use PyImport_ExecCodeModule. See how this function is used in import.c

--
Gabriel Genellina

Jun 27 '08 #2
On May 12, 12:31*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 12 May 2008 05:49:22 -0300, Gruik <benjamin.frem...@gmail.comescribió:
I'm currently working on python embedding with C++. My goal is that
the C++ part handle files writing/reading so that the Python part only
works with buffers.
I succeeded in buffer exchanges. The problem is that some of the files
I read/write are python files so that, before embedding, I imported
them as modules when I needed them.
But now that the Python part only receive buffers, I can't do it
anymore. So I wonder :
- is it possible to import module from a buffer instead of files?
- or is it possible to create a module object from my buffer?

Yes, first compile the buffer to get a code object, then use PyImport_ExecCodeModule. See how this function is used in import.c

--
Gabriel Genellina
Thanks for your quick answer !
I think I'll have no problem with that in C++ and I'm going to try it
right after this message.

But before that 1 question: what if I'm in Python ?
Following your solution, I did that in Python :

def load_buffer(buffer) :
compiled_buffer = compile(buffer, "module_name", "exec")
exec(compiled_buffer)

It works great except that I can't have a module object and that it is
as if I did "from module import *"
But I need the module object and not an "import *" behavior.
Any idea about the way to do that?

Benjamin
Jun 27 '08 #3
Gruik wrote:
But before that 1 question: what if I'm in Python ?
Following your solution, I did that in Python :

def load_buffer(buffer) :
compiled_buffer = compile(buffer, "module_name", "exec")
exec(compiled_buffer)

It works great except that I can't have a module object and that it is
as if I did "from module import *"
But I need the module object and not an "import *" behavior.
Any idea about the way to do that?
Something along the lines of:

import new
mymodule = new.module("mymodule")
exec <<<code>>in mymodule.__dict__

--irmen
Jun 27 '08 #4
On May 12, 1:48*pm, Irmen de Jong <irmen.NOS...@xs4all.nlwrote:
Gruik wrote:
But before that 1 question: what if I'm in Python ?
Following your solution, I did that in Python :
* * def load_buffer(buffer) :
* * * * compiled_buffer = compile(buffer, "module_name", "exec")
* * * * exec(compiled_buffer)
It works great except that I can't have a module object and that it is
as if I did "from module import *"
But I need the module object and not an "import *" behavior.
Any idea about the way to do that?

Something along the lines of:

import new
mymodule = new.module("mymodule")
exec <<<code>>in mymodule.__dict__

--irmen
Yeah it works !

exec(compiled_module, globals(), mymodule.__dict__)

Just to add mymodule to sys.modules and it's good!

Thanks again
Benjamin
Jun 27 '08 #5
En Mon, 12 May 2008 08:09:45 -0300, Gruik <be**************@gmail.comescribió:
On May 12, 12:31*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Mon, 12 May 2008 05:49:22 -0300, Gruik <benjamin.frem...@gmail.comescribió:
I'm currently working on python embedding with C++. My goal is that
the C++ part handle files writing/reading so that the Python part only
works with buffers.
I succeeded in buffer exchanges. The problem is that some of the files
I read/write are python files so that, before embedding, I imported
them as modules when I needed them.
But now that the Python part only receive buffers, I can't do it
anymore. So I wonder :
- is it possible to import module from a buffer instead of files?
- or is it possible to create a module object from my buffer?

Yes, first compile the buffer to get a code object, then use PyImport_ExecCodeModule. See how this function is used in import.c

Thanks for your quick answer !
I think I'll have no problem with that in C++ and I'm going to try it
right after this message.

But before that 1 question: what if I'm in Python ?
Following your solution, I did that in Python :

def load_buffer(buffer) :
compiled_buffer = compile(buffer, "module_name", "exec")
exec(compiled_buffer)

It works great except that I can't have a module object and that it is
as if I did "from module import *"
But I need the module object and not an "import *" behavior.
Any idea about the way to do that?
Almost - you have to create a new module and exec the code into its namespace:

pyimport new
pyfoo = new.module("foo", "This is the foo module")
pyexec "def f(): pass" in foo.__dict__
pyfoo
<module 'foo' (built-in)>
pyfoo.f
<function f at 0x00A3E230>
pydir(foo)
['__builtins__', '__doc__', '__name__', 'f']

(replace "def f(): pass" with the compiled buffer)

Two differences with a "normal" module:

- it has no __file__ attribute - you may want to add it, with the original filename, or leave it off to show that it's not loaded from a file.
- it does not exist in sys.modules, so other modules cannot import it. If you want to allow that: sys.modules['foo'] = foo (perhaps one should check previously that there is no module named "foo" before replacing it...)

--
Gabriel Genellina

Jun 27 '08 #6

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

Similar topics

5
by: simo | last post by:
I've written a pretty big wxPython script, and I thought I'd split the source into a few files. I'm going to have a main.py file which includes global defs, wxApp initialisation code,...
2
by: Fritz Bosch | last post by:
Hi experts Is is possible to import/manipulate a module such that I can supply its __dict__? I want to supply my own dict subclass object to be filled by the import, e.g. a class like: >>>...
4
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program:...
2
by: Torsten Mohr | last post by:
Hi, is there some description available to overwrite the import hook? By googling i found out so far that i need to overwrite __builtins__.__import__ with something else. Can i also do this...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
3
by: kwatch | last post by:
What is the condition of module name which is available in 'from .. import ..' statement ? ---------------------------------------- import os print os.path # <module 'posixpath'...
2
by: Fuzzyman | last post by:
A very odd error with Python 2.5 (both 2.5.1 and 2.5.2 from the official msi installers and running on Vista under Parallels on the Mac). 'import site' fails due to a string in sys.path that...
10
by: nisp | last post by:
Hi all ! I'm trying to capture stderr of an external module I use in my python program. I'm doing this by setting up a class in my module overwriting the stderr file object method write. The...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.