[Python-bugs-list] [ python-Bugs-771097 ] frozen programs fail due to implicit import of "warnings"

SourceForge.net noreply@sourceforge.net
Mon, 14 Jul 2003 17:42:04 -0700


Bugs item #771097, was opened at 2003-07-15 03:32
Message generated for change (Comment added) made by mhammond
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=771097&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Anthony Tuininga (atuining)
Assigned to: Nobody/Anonymous (nobody)
Summary: frozen programs fail due to implicit import of "warnings"

Initial Comment:
With Python 2.3b2, pythonrun.c now imports the
"warnings" module at startup, which causes problems
with freezing programs like py2exe, cx_Freeze and Installer

The main reason there is a problem is because this
takes place during Py_Initialize() so there is no way
of overriding the import so that it can find a copy of
the warnings module in anything but the filesystem. Is
there any way of getting around this?

What's worse is that warnings.py imports a raft of
other modules (14 others which I can list if necesary)
and so the workaround of simply copying the needed
files is unwieldy.

This is somewhat related to bug 683658.

----------------------------------------------------------------------

>Comment By: Mark Hammond (mhammond)
Date: 2003-07-15 10:42

Message:
Logged In: YES 
user_id=14198

I'm attaching a patch that goes some way to solving this. 
Note that all this is really a hack for bug 683658.

The new patch does the following:
* Still attempt to get the warnings module at startup, but
into a static variable.  Clear the Python error if we fail.

* New function, PyModule_GetWarningsModule() - this attempts
to use the module loaded at init time.  If this fails, it
attempts to look up the module in sys.__modules__.  If this
fails, it gives up (as it still can't do anything to acquire
the import lock)

* External functions (eg PyErr_Warn) use this function.

The end result of this is that even if the import for
warnings fails at startup, the C code can still use the
warning module, *iff* the warnings module has previously
been imported.

Freeze etc could then take the approach of bootstrapping
their import mechanism (which they must do now), then
manually importing the warnings module.

Not ideal, but better than what exists now :)

----------------------------------------------------------------------

Comment By: Anthony Tuininga (atuining)
Date: 2003-07-15 04:02

Message:
Logged In: YES 
user_id=619560

In current CVS (revision 2.193 of
dist/src/Python/pythonrun.c) at line 193 there is this line

        PyModule_WarningsModule =
PyImport_ImportModule("warnings");

There is no check afterward for any errors. Couldn't a
PyErr_Clear() be done so that further statements don't fail
because the import of warnings failed?


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=771097&group_id=5470