Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warnings inside PyRun_SimpleString() display argv[1] #53033

Closed
Sebastian mannequin opened this issue May 22, 2010 · 8 comments
Closed

warnings inside PyRun_SimpleString() display argv[1] #53033

Sebastian mannequin opened this issue May 22, 2010 · 8 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@Sebastian
Copy link
Mannequin

Sebastian mannequin commented May 22, 2010

BPO 8787
Nosy @amauryfa, @vstinner, @benjaminp
Superseder
  • bpo-33375: warnings: get filename from frame.f_code.co_filename
  • Files
  • _warnings.c.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-22.23:03:09.499>
    created_at = <Date 2010-05-22.14:00:30.817>
    labels = ['type-bug']
    title = 'warnings inside PyRun_SimpleString() display argv[1]'
    updated_at = <Date 2019-10-22.23:03:21.011>
    user = 'https://bugs.python.org/Sebastian'

    bugs.python.org fields:

    activity = <Date 2019-10-22.23:03:21.011>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-22.23:03:09.499>
    closer = 'vstinner'
    components = ['None']
    creation = <Date 2010-05-22.14:00:30.817>
    creator = 'Sebastian'
    dependencies = []
    files = ['17461']
    hgrepos = []
    issue_num = 8787
    keywords = ['patch']
    message_count = 8.0
    messages = ['106306', '106307', '106417', '106419', '106442', '106783', '106784', '355171']
    nosy_count = 4.0
    nosy_names = ['amaury.forgeotdarc', 'vstinner', 'benjamin.peterson', 'Sebastian']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '33375'
    type = 'behavior'
    url = 'https://bugs.python.org/issue8787'
    versions = ['Python 2.6']

    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented May 22, 2010

    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]

    @Sebastian Sebastian mannequin added the type-bug An unexpected behavior, bug, or error label May 22, 2010
    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented May 22, 2010

    Could anyone please correct the title? Thx :)

    @amauryfa
    Copy link
    Member

    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.

    @amauryfa amauryfa changed the title PySys_Get warnings inside PyRun_SimpleString() display argv[1] May 25, 2010
    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented May 25, 2010

    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)
    [...]

    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented May 25, 2010

    attached a patch for this issue now.

    Now it first uses the name of the script,
    instead of __file__.

    @Sebastian
    Copy link
    Mannequin Author

    Sebastian mannequin commented May 30, 2010

    any news on this?

    @benjaminp
    Copy link
    Contributor

    First of all, your patch needs a test.

    @brettcannon brettcannon removed their assignment Sep 27, 2010
    @vstinner
    Copy link
    Member

    Fixed in bpo-33375.

    commit 11a8966
    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).
    

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants