classification
Title: warnings inside PyRun_SimpleString() display argv[1]
Type: behavior Stage: resolved
Components: None Versions: Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: warnings: get filename from frame.f_code.co_filename
View: 33375
Assigned To: Nosy List: Sebastian, amaury.forgeotdarc, benjamin.peterson, vstinner
Priority: normal Keywords: patch

Created on 2010-05-22 14:00 by Sebastian, last changed 2019-10-22 23:03 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
_warnings.c.patch Sebastian, 2010-05-25 15:36
Messages (8)
msg106306 - (view) Author: Sebastian (Sebastian) Date: 2010-05-22 14:00
Hi all,

I found a bug in the exception handler. When I
start the app without any arguments I get an
output I expect:

__main__:2:DeprecationWarning: Deprecated function.

When I run the app with arguments, the arguments
are printed somehow in the exception output:

-test=HALLO:1:DeprecationWarning: Deprecated function

Can anyone please confirm?

Bye, Seb



[code]
#include "Python/Python.h"

static PyObject *testfunc(PyObject *self, PyObject *args, PyObject *keywords)
{
	PyErr_Warn(PyExc_DeprecationWarning, "Deprecated function.");
	Py_RETURN_NONE;
}

static PyMethodDef testmod[] =
{		
	{"testfunc", (PyCFunction)testfunc, METH_NOARGS, "Prints out a text to stdout."},
	{NULL}
};

int main (int argc, char **argv)
{
	Py_Initialize();
	
	PySys_SetArgv(argc, argv);
	
	PyObject *mod = Py_InitModule4("testmod", testmod, "", NULL, PYTHON_API_VERSION);
	if(mod == NULL) return -1;
	
	PyRun_SimpleString(	"import testmod\n"
											"testmod.testfunc()");
	Py_Finalize();
	return 0;
}
[/code]
msg106307 - (view) Author: Sebastian (Sebastian) Date: 2010-05-22 14:02
Could anyone please correct the title? Thx :)
msg106417 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-05-25 08:04
Yes, the warnings module tries to display the file name. Inside PyRun_SimpleString(), globals()['__name__'] == '__main__', and the warnings module supposes that argv[1] is the name of the script.

I wonder whether __file__ would be more accurate: it is filled when running a script, but not when running a string. And sys.argv would not be used any more.
msg106419 - (view) Author: Sebastian (Sebastian) Date: 2010-05-25 08:33
Oh, damn. I really forgot the argv filename thing. Nevermind :)

But back to topic. __file__ might be not the best solution for that. What does Python when embedded, and __file__ is not set? That can happen when the source of your code is not a file (multiline textbox, ...)

I would simply follow the way how the traceback solves this. Just print out the filename passed to:

Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
[...]
msg106442 - (view) Author: Sebastian (Sebastian) Date: 2010-05-25 15:36
attached a patch for this issue now.

Now it first uses the name of the script,
instead of __file__.
msg106783 - (view) Author: Sebastian (Sebastian) Date: 2010-05-30 23:26
any news on this?
msg106784 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-05-31 00:42
First of all, your patch needs a test.
msg355171 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-22 23:03
Fixed in bpo-33375.

commit 11a896652ee98aa44e59ed25237f9efb56635dcf
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Fri Jun 8 21:28:37 2018 +0200

    bpo-33375: Get filename for warnings from frame.f_code.co_filename (GH-6622)
    
    More consistent with how other parts of Python find the filename (e.g. tracebacks and pdb).
History
Date User Action Args
2019-10-22 23:03:21vstinnersetsuperseder: warnings: get filename from frame.f_code.co_filename
resolution: fixed -> duplicate
2019-10-22 23:03:09vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg355171

resolution: fixed
stage: resolved
2012-11-17 17:47:11brett.cannonsetnosy: - brett.cannon
2010-09-27 20:59:58brett.cannonsetassignee: brett.cannon ->
2010-05-31 00:42:54benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg106784
2010-05-30 23:26:51Sebastiansetmessages: + msg106783
2010-05-25 15:36:09Sebastiansetfiles: + _warnings.c.patch
keywords: + patch
messages: + msg106442
2010-05-25 08:33:43Sebastiansetmessages: + msg106419
2010-05-25 08:04:30amaury.forgeotdarcsettitle: PySys_Get -> warnings inside PyRun_SimpleString() display argv[1]
nosy: + amaury.forgeotdarc, brett.cannon

messages: + msg106417

assignee: brett.cannon
2010-05-22 14:02:47Sebastiansetmessages: + msg106307
2010-05-22 14:00:30Sebastiancreate