[Python-Dev] Strange crash in import

Jack Jansen Jack.Jansen@oratrix.com
Tue, 17 Dec 2002 22:43:23 +0100


I'm getting a strange crash in import, and it turns out that there's a  
refcount 0 object on sys.path. Has any of the code for zip imports been  
checked in yet? Otherwise, does anyone have a clue what could be going  
on (i.e. what silly mistake did I make:-)?

The situation I'm in (the stack trace is below) is that I'm in the  
tp_init routine of an object when a macos-error occurs. I then call  
PyMac_Error to format the error. PyMac_Error then needs to import a  
pure Python module (which will give it the pretty huge list of MacOS  
error codes and the corresponding strings). This crashes, because the  
second entry on sys.path is a refcount 0 object:

#1  0x003fcf10 in find_module (realname=0x46f2cc "", path=0x13b310,  
buf=0xbfffeac0 "macresource.pyc", buflen=1025, p_fp=0xbfffeed0) at  
../Python/import.c:987
987                     if (PyUnicode_Check(v)) {
(gdb) p _PyObject_Dump(path)
object  : ['', <refcnt 0 at 0x13e548>,  
'/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat- 
darwin',  
'/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/lib- 
tk',  
'/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/lib- 
dynload',  
'/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site- 
packages', '/Library/Frameworks/Python.framework/Versions/2.3/Mac/Lib',  
'/Library/Frameworks/Python.framework/Versions/2.3/Mac/Lib/lib- 
scriptpackages']

If I manually import the "macresource" module beforehand everything  
works as expected, and sys.path[1] isn't a refcount zero object.

Here's the full stack trace:
(gdb) where
#0  PyType_IsSubtype (a=0x0, b=0x457d44) at ../Objects/typeobject.c:704
#1  0x003fcf10 in find_module (realname=0x46f2cc "", path=0x13b310,  
buf=0xbfffeac0 "macresource.pyc", buflen=1025, p_fp=0xbfffeed0) at  
../Python/import.c:987
#2  0x003fe520 in import_submodule (mod=0x4522b8, subname=0xbfffefc0  
"macresource", fullname=0xbfffefc0 "macresource") at  
../Python/import.c:1998
#3  0x003fe034 in load_next (mod=0x4522b8, altmod=0x4522b8,  
p_name=0xbfffefc0, buf=0xbfffefc0 "macresource", p_buflen=0xbffff3d0)  
at ../Python/import.c:1863
#4  0x003ffef0 in import_module_ex (name=0x0, globals=0x457d44,  
locals=0x1b6, fromlist=0x13b390) at ../Python/import.c:1714
#5  0x003ff0d4 in PyImport_ImportModuleEx (name=0x145b9c "macresource",  
globals=0x148780, locals=0x148780, fromlist=0x13b390) at  
../Python/import.c:1755
#6  0x003ce2fc in builtin___import__ (self=0x0, args=0x457d44) at  
../Python/bltinmodule.c:40
#7  0x00370b98 in PyObject_CallFunction (callable=0x12efd0, format=0x0)  
at ../Objects/abstract.c:1675
#8  0x003feb40 in PyImport_Import (module_name=0x145b88) at  
../Python/import.c:2160
#9  0x003fefb8 in PyImport_ImportModule (name=0x0) at  
../Python/import.c:1686
#10 0x004108a0 in PyMac_StrError (err=-43) at  
../Python/mactoolboxglue.c:107
#11 0x00410e00 in PyMac_Error (err=-43) at  
../Python/mactoolboxglue.c:169
#12 0x00265af4 in myPyMac_GetFSRef (v=0x18c760, fsr=0x13e4f8) at  
/Users/jack/src/python/Mac/Modules/file/_Filemodule.c:2400
#13 0x00263ef8 in FSRef_tp_init (self=0x13e4f0, args=0x457d44,  
kwds=0x1b6) at  
/Users/jack/src/python/Mac/Modules/file/_Filemodule.c:1243
#14 0x003b407c in type_call (type=0x268454, args=0x14fed0, kwds=0x0) at  
../Objects/typeobject.c:438
#15 0x00374028 in PyObject_Call (func=0x0, arg=0x457d44, kw=0x1b6) at  
../Objects/abstract.c:1675
#16 0x003da754 in do_call (func=0x45e88c, pp_stack=0xbffff8fc, na=1,  
nk=4443236) at ../Python/ceval.c:3398
#17 0x003da53c in call_function (pp_stack=0xbffff8fc, oparg=4554052) at  
../Python/ceval.c:3268
#18 0x003d8044 in eval_frame (f=0xa37c0) at ../Python/ceval.c:2009
#19 0x003d9354 in PyEval_EvalCodeEx (co=0x141660, globals=0x457d44,  
locals=0x3fcf10, args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0,  
defcount=0, closure=0x0) at ../Python/ceval.c:2554
#20 0x003dbfe0 in PyEval_EvalCode (co=0x0, globals=0x457d44,  
locals=0x1b6) at ../Python/ceval.c:478
#21 0x0040b788 in run_node (n=0x0, filename=0x457d44 "",  
globals=0x148780, locals=0x148780, flags=0x0) at  
../Python/pythonrun.c:1089
#22 0x0040ac88 in PyRun_InteractiveOneFlags (fp=0xa390c,  
filename=0x141660 "", flags=0xbffffc50) at ../Python/pythonrun.c:593
#23 0x0040aa80 in PyRun_InteractiveLoopFlags (fp=0x44aaac,  
filename=0x137140 "", flags=0x14a4c0) at ../Python/pythonrun.c:522
#24 0x0040c604 in PyRun_AnyFileExFlags (fp=0xa0000cd4,  
filename=0x444fdc "<stdin>", closeit=669964, flags=0xbffffc50) at  
../Python/pythonrun.c:485
#25 0x00416db0 in Py_Main (argc=-1610609452, argv=0x0) at  
../Modules/main.c:442
#26 0x00001b5c in _start (argc=673326145, argv=0x0, envp=0x44653c) at  
/SourceCache/Csu/Csu-45/crt.c:267
#27 0x000019dc in start ()

--
- Jack Jansen        <Jack.Jansen@oratrix.com>         
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma  
Goldman -