Skip to content

Sys unraisablehook#3576

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:sys-unraisablehook
Apr 23, 2022
Merged

Sys unraisablehook#3576
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:sys-unraisablehook

Conversation

@youknowone
Copy link
Copy Markdown
Member

No description provided.

@Snowapril
Copy link
Copy Markdown
Contributor

Snowapril commented Mar 13, 2022

There are many test cases related to sys.unraisablehook with below comments.

  • # TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'unraisablehook'
  • # TODO: RUSTPYTHON, sys.unraisablehook

@youknowone youknowone marked this pull request as draft March 13, 2022 09:48
@youknowone youknowone force-pushed the sys-unraisablehook branch 3 times, most recently from 41b4d10 to 7c409b6 Compare March 21, 2022 17:18
@youknowone youknowone marked this pull request as ready for review March 21, 2022 17:51
@youknowone youknowone requested a review from coolreader18 March 21, 2022 17:51
@youknowone
Copy link
Copy Markdown
Member Author

@Snowapril could you review this PR?

Comment thread vm/src/stdlib/sys.rs Outdated
Comment thread vm/src/stdlib/sys.rs Outdated
Comment thread vm/src/stdlib/sys.rs Outdated
@Snowapril
Copy link
Copy Markdown
Contributor

Isn't the print for __module__ and __qualname__ from unraisable.exc_type necessary for this PR at now?
If it need to be implemented later, maybe it is good to add comment about it 😊

https://github.com/python/cpython/blob/main/Python/errors.c#L1336-L1377

assert(PyExceptionClass_Check(exc_type));
PyObject *modulename = PyObject_GetAttr(exc_type, &_Py_ID(__module__));
if (modulename == NULL || !PyUnicode_Check(modulename)) {
    Py_XDECREF(modulename);
    _PyErr_Clear(tstate);
    if (PyFile_WriteString("<unknown>", file) < 0) {
        return -1;
    }
}
else {
    if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
        !_PyUnicode_Equal(modulename, &_Py_ID(__main__))) {
        if (PyFile_WriteObject(modulename, file, Py_PRINT_RAW) < 0) {
            Py_DECREF(modulename);
            return -1;
        }
        Py_DECREF(modulename);
        if (PyFile_WriteString(".", file) < 0) {
            return -1;
        }
    }
    else {
        Py_DECREF(modulename);
    }
}
PyObject *qualname = PyType_GetQualName((PyTypeObject *)exc_type);
if (qualname == NULL || !PyUnicode_Check(qualname)) {
    Py_XDECREF(qualname);
    _PyErr_Clear(tstate);
    if (PyFile_WriteString("<unknown>", file) < 0) {
        return -1;
    }
}
else {
    if (PyFile_WriteObject(qualname, file, Py_PRINT_RAW) < 0) {
        Py_DECREF(qualname);
        return -1;
    }
    Py_DECREF(qualname);
}

@youknowone
Copy link
Copy Markdown
Member Author

thanks, I fixed it based on the code

Copy link
Copy Markdown
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on structseq and other nits

Comment thread derive/src/pystructseq.rs Outdated
Comment thread vm/src/vm.rs Outdated
Comment thread vm/src/stdlib/sys.rs
Comment thread vm/src/stdlib/sys.rs Outdated
}

#[pyfunction]
fn unraisablehook(unraisable: PyRef<UnraisableHookArgs>, vm: &VirtualMachine) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are supposed to be the same, (sys.unraisablehook is sys.__unraisablehook__) == True in CPython.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still sys.unraisablehook is not sys.__unraisablehook__, but I regard this result as bug of #[pymodule] macro for current version

Comment thread vm/src/stdlib/sys.rs Outdated
Comment thread vm/src/stdlib/sys.rs Outdated

#[pyfunction]
fn __unraisablehook__(
unraisable: PyRef<UnraisableHookArgs>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. for this (instead of the PyValue thing) you could write a custom TryFromObject for UnraisableHookArgs that just checks if x.isinstance(Self::static_type()) and then extracts the tuple from there.

@youknowone youknowone force-pushed the sys-unraisablehook branch 6 times, most recently from 70179eb to 6dbbfbd Compare April 14, 2022 15:00
@youknowone
Copy link
Copy Markdown
Member Author

@coolreader18 I splited macro work to #3613 and rebased this PR on it.

@youknowone youknowone force-pushed the sys-unraisablehook branch 5 times, most recently from a1f34c7 to 4337cd3 Compare April 18, 2022 21:19
Comment thread vm/src/stdlib/sys.rs
Ok(())
}

#[pyattr]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coolreader18 Now they will be the same object

@youknowone youknowone force-pushed the sys-unraisablehook branch 2 times, most recently from d2370a4 to bf533c8 Compare April 19, 2022 07:26
@youknowone youknowone merged commit c564a18 into RustPython:main Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants