Reputation: 45542
At my work there is a Python 2.5 application in use that was compiled with py2exe and then distributed to users. Due to migration to a new database, I have been tasked with updating the application. Unfortunately, we no longer have the original source code. (Although we do have the source of other projects that were derived from that code base.)
Fortunately, I do have access to the compiled .pyc
files that are located in library.zip
. Having located the .pyc
file, I imported it, perused its documentation, and tested its behavior. Combined with the fact that I have a later version of the source code I believe I have a good handle on what's in the .pyc
file.
So now I have removed the old .pyc
file from library.zip
and added back in my new version. And it mostly works.
If provided with correct input, the old program successfully calls the new code in library.zip
and behaves just how it used to. However, the old code will not catch any of the new code's exceptions. I have opened both modules in the interpreter and compared the exceptions and they seem identical (same class name, even same error strings).
So my question is, where do I go from here? My suspicion is that even though I cannot see any difference between the exceptions, something about them must be different. How do I further test this? What else might I be missing?
Upvotes: 3
Views: 255
Reputation: 50190
The old and new exceptions have the same name, but they seem to be distinct objects living in different modules. At least, under that scenario you'll get exactly the behavior you describe. Solution: Try importing the exceptions from each of the remaining .pyc
files. When you find them, import them into your replacement code and raise those instead of your replacements.
Demonstration of the problem: The function call raises module2.myException
, but the caller expects module1.myException
. I can't say for sure that this is your problem, but if it's not, it ought to be :-)
---- module1.pyc --- (Old: no source code)
class myException(Exception):
pass
---- module2.py --- (New: Your replacement for the old module2.py)
class myException(Exception):
pass
def myfunction():
raise myException()
---- mainmodule.pyc --- (Old: no source code)
from oldmodule1 import myException
import module2
try:
module2.myfunction()
except myException:
print "Caught it!"
Upvotes: 2