Issue297

Title warnings module does not work after compilation
Priority bug Status chatting
Superseder Nosy List hean
Assigned To Keywords wrong_execution

Created on 2016-03-18.15:03:54 by hean, last changed by kayhayen.

Files
File name Uploaded Type Edit Remove
test.py hean, 2016-03-18.15:03:54 text/x-python
Messages
msg1874 (view) Author: kayhayen Date: 2016-03-20.13:02:02
There is this stupid code in CPython:

              if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) {
                  PyErr_SetString(PyExc_TypeError,
                                  "warnings.showwarning() must be set to a "
                                  "function or method");
                  Py_DECREF(show_fxn);
                  goto cleanup;
              }

              res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
                                                  filename, lineno_obj,
                                                  NULL);

So it will only allow to work with its own function types, not just any 
callable, which it unnecessary, but that's the way it is. For Python3, there is 
no such issue.

You will be able to get it to work as a workaround by using "eval" on a string, 
like this:

warnings.showwarning = eval("lambda *args, **kw: flow_showwarning(*args, **kw)")

I will consider if this could be done in some plug-in automatically.

Yours,
Kay
msg1873 (view) Author: marc.jofre Date: 2016-03-18.16:29:55
i am also interested in using warnings

Marc
msg1872 (view) Author: hean Date: 2016-03-18.15:03:54
I am trying to use the warnings module in my code. It works fine when I run the .py 
files, but after I compile it, it fails with the error:

TypeError: warnings.showwarning() must be set to a function or method



import warnings

def flow_showwarning(message, category, filename, lineno, _=None, line=None):
    print warnings.formatwarning(message, category, filename, lineno, line)

warnings.showwarning = flow_showwarning

warnings.warn("testing")


Expected output:
$ python test.py
test.py:8: UserWarning: testing
  warnings.warn("testing")

Actual output:
$ nuitka-run test.py 
Traceback (most recent call last):
  File "/media/sf_shared/measurement/components/test.py", line 8, in <module>
    warnings.warn("testing")
TypeError: warnings.showwarning() must be set to a function or method



I would appreciate it if someone could look into this.
Best,
Henrique
History
Date User Action Args
2016-03-20 13:02:02kayhayensetmessages: + msg1874
keyword: + wrong_execution
2016-03-18 16:29:55marc.jofresetstatus: unread -> chatting
messages: + msg1873
2016-03-18 15:03:54heancreate