473,800 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using marshal to manually "import" a python module

I had a situation recently that required I manually load python bytecode
from a .pyc file on disk.

So, for the most part, I took code from imputil.py which loads the .pyc
data via the marshal module and then exec's it into a newly created
module object (created by imp.new_module( )). The relevant pieces of code
are in imputil.py_suff ix_importer and imputil.Importe r._process_resu lt
(where it exec's the code into the module's __dict__)

The strange thing is, it worked fine locally on my two machines (32bit
running python 2.3.5 and 64bit running python 2.4.1), but when run by a
64bit machine on the network, it would fail every time in the following
manner:

My marshal/exec-based loader would load the module from the pyc
apparently without problems, but if I then pulled a specific function
attr out of the resulting module object, and that function called
another function defined within the same module, it would raise a
"TypeError: 'NoneType' object is not callable" exception when attempting
to call that second function. So the setup is:

module blah:
def A():
...

def B():
x = A()

compiled to bytecode: blah.pyc
then, in my program:

m = my_marshal_load er("blah.pyc")
f = getattr(m,"B")

x = f()

raises "TypeError: 'NoneType' object is not callable" inside that call
to f(), on the line x = A(), as though A is a None and not the function
object defined in the module.

I can't figure out why this would work locally, but not when the module
is loaded across a network. In fact, I have no idea what would ever
cause it not to see A as a function. I'm stumped, and this is over my
head as far as intimate knowledge of the direct loading of python
bytecode via marshal is concerned...so I'm not clear on the best way to
debug it. If anyone has an inkling of what might be going on, I'd love
to hear it.

Thanks in advance,
-Dave

--
Presenting:
mediocre nebula.
Nov 29 '07 #1
1 2726
The strange thing is, it worked fine locally on my two machines (32bit
running python 2.3.5 and 64bit running python 2.4.1), but when run by a
64bit machine on the network, it would fail every time in the following
manner:
It may not that much be an issue of 32-bit vs. 64-bit, but of Python 2.3
vs. Python 2.4. The marshal format changes across versions, and files
created in one version might not be understood correctly by another version.

Now, you don't give enough details on resolving this completely: what
specific microprocessors , and what specific operating systems are you
using? What is the specific code of the module that fails to load (e.g.
minimal example), and what is the resulting byte code?

Off-hand, I can't see why a 2.3-generated file shouldn't be loadable
by a 2.4 interpreter, but you say "it" fails without saying what "it"
is (i.e. running the 64-bit machine to generate a marshal file over
the net that is then read by the 32-bit machine, or vice versa).
I can't figure out why this would work locally, but not when the module
is loaded across a network. In fact, I have no idea what would ever
cause it not to see A as a function. I'm stumped, and this is over my
head as far as intimate knowledge of the direct loading of python
bytecode via marshal is concerned...so I'm not clear on the best way to
debug it.
I would trace through the loading, either in a debugger, or by adding
printf statements into marshal.

Regards,
Martin
Nov 30 '07 #2

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

Similar topics

2
1763
by: Torsten Mohr | last post by:
Hi, i tried to find the file and line in the C sources of python where the command "import" is implemented. Can anybody give me some hint on this? Thanks, Torsten.
2
3097
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the run time while parsing the XML file. I wonder what is the best solution for this problem? I also have some problem about the "import". How should I design my packages? Say, I have all code locates at c:\projects\sami, "c:\project" is in my...
3
5434
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions. They will only need to be run once per execution. Example (directory level): Sys1: A B C
2
3512
by: denny | last post by:
Hello, rookie here. I'm trying to import a module at runtime using variables to specify which module, and which functions to execute. for example: mStr = "sys" fStr = "exit" # load mod mod = __import__(mStr)
8
1534
by: John Salerno | last post by:
Hi all. Quick question (but aren't they all?) :) Do you think it's a good idea to use the 'from <name> import *' statement when using a GUI module? It seems on wxPython's site, they recommend using import wx nowadays, but I wonder if that advice is followed. Also, I'm still reading some Tkinter docs that seem to use 'from Tkinter import *' a lot. I understand the danger of doing this, but is it safer in these cases, given the more...
5
2191
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
1
2359
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the main module through: from support import *
14
2138
by: Paulo da Silva | last post by:
Hi! If I have two files .py such as m.py from c import * ... x=c() ... os.any_method ...
1
1432
by: smalltalk | last post by:
I have three files names t1.py,t2.py,t3.py in e:\test\dir1,of course dir2 is exsit the content of t1.py as follow: t1.py import os print 'this is t1.py' os.chdir('..\\dir2') the content of t2.py as follow: print "this is t2.py" the content of t3.py as follow:
0
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.