[Pythonmac-SIG] problem with PyMac_PreferenceOptions?
Magladry, Stephen
stephenm@humongous.com
Wed, 13 Nov 2002 19:09:13 -0800
I am needing to set up a application that uses the Python Core but has no
reliance on
Python preferences. The existing code is mostly set up to handle this. I
have run into
one problem, though.
The problem is with PyMac_PreferenceOptions. Early on in the function in
gets the "Popt"
with a resource ID of PYTHONOPTIONSOVERRIDE_ID using Get1Resource. The
UseRefFile sets the
rez file to the global value PyMac_AppRefNum in that function.
Problem is PyMac_PreferenceOptions get called two different times, both
times from
init_common. init_common is called both from PyMac_InitApplication and
PyMac_Initialize.
PyMac_Initialize is also called by PyMac_InitApplication. Here is the call
stack:
PyMac_InitApplication
init_common
PyMac_PreferenceOptions
PyMac_Initialize
init_common
PyMac_PreferenceOptions
The first time through init_common, but after the call to
PyMac_PreferenceOptions, if
USE_MAC_SHARED_LIBRARY is defined, there is a call to PyMac_AddLibResources.
It does a
FSpOpenResFile to add the library's resources and does not restore current
resource file,
(and probably should not).
At the top of init_common, there is the code PyMac_AppRefNum = CurResFile().
So the
first time through init_common, PyMac_PreferenceOptions gets the "Popt"
resource
from the application. The second time through, CurResFile() now points to
the resource
of the library file. So in PyMac_PreferenceOptions, the Get1Resource
fails when trying to get PYTHONOPTIONSOVERRIDE_ID. Since it fails, The
Python preferences
gets read. I do not want that.
To fix the problem, I have initialized PyMac_AppRefNum to -1. The first time
through
init_common, I have changed the code as follows:
--begin old code-----
PyMac_AppRefNum = CurResFile();
-- end old code -----
--begin new code-----
if (-1 == PyMac_AppRefNum)
{
PyMac_AppRefNum = CurResFile();
}
-- end new code -----
Does this seem clean?