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

Change PtDebugPrint to behave more like Py3 print. #649

Merged
merged 1 commit into from
May 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,37 +402,69 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtDebugAssert, args, "Params: cond, msg\nDebug o
PYTHON_RETURN_NONE;
}

PYTHON_GLOBAL_METHOD_DEFINITION_WKEY(PtDebugPrint, args, kwargs, "Params: *msgs, **kwargs\nPrints msgs to the Python log given the message's level")
PYTHON_GLOBAL_METHOD_DEFINITION_WKEY(PtDebugPrint, args, kwargs, "Params: *msgs, level, sep, end\n"
"Prints msgs to the Python log given the message's level, "
"optionally separated and terminated by the given strings")
{
uint32_t level = cyMisc::kErrorLevel;
ST::string sep = ST_LITERAL(" ");
ST::string end = ST_LITERAL("\n");

do {
// Grabbin' levelz
if (kwargs && PyDict_Check(kwargs)) {
PyObject* value = PyDict_GetItem(kwargs, PyString_FromString("level"));
PyObject* value = PyDict_GetItemString(kwargs, "level");
if (value) {
if (PyInt_Check(value))
level = PyInt_AsLong(value);
else
break;
}

value = PyDict_GetItemString(kwargs, "sep");
Copy link
Member

Choose a reason for hiding this comment

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

Bonus points: add end as well :)

print('Things: ', end='')  # Elide newline
for thing in things:
    print(thing, end=', ')
print()   # Finish the line

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, plStatusLog unconditionally treats each call to AddLine as at least one line. Fixing that is outside the scope of this PR, but I will add an end argument that allows terminating with an arbitrary string.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, but I'd rather not see end default to '\n' if we're going to end up writing two newlines (one from AddLine and one from the default end)... Maybe it would be better to just leave out the end parameter until it's needed (and then update the DebugPrint to use it properly at the same time)

Copy link
Member Author

Choose a reason for hiding this comment

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

plStatusLog only adds the trailing \n if one is not present. I decided to default end to \n on the off chance that some later work allows eliding newlines in plStatusLog 😄

if (value) {
if (PyString_CheckEx(value))
sep = PyString_AsStringEx(value);
else
break;
}

value = PyDict_GetItemString(kwargs, "end");
if (value) {
if (PyString_CheckEx(value))
end = PyString_AsStringEx(value);
else
break;
}
}

ST::string_stream ss;
for (size_t i = 0; i < PySequence_Fast_GET_SIZE(args); ++i) {
PyObject* theMsg = PySequence_Fast_GET_ITEM(args, i);
if (!PyString_CheckEx(theMsg))
theMsg = PyObject_Repr(theMsg);

if (theMsg)
cyMisc::DebugPrint(PyString_AsStringEx(theMsg), level);
if (PyString_CheckEx(theMsg))
Py_XINCREF(theMsg);
else
break;
theMsg = PyObject_Str(theMsg);

if (i != 0)
ss << sep;
if (theMsg) {
ss << PyString_AsStringEx(theMsg);
Py_DECREF(theMsg);
} else {
PyErr_Format(PyExc_RuntimeError, "Failed to `str()` argument index %n", i);
PYTHON_RETURN_ERROR;
}
}
ss << end;
cyMisc::DebugPrint(ss.to_string(), level);
PYTHON_RETURN_NONE;
} while (false);

// fell through to the type error case
PyErr_SetString(PyExc_TypeError, "PtDebugPrint expects a sequence of strings and an optional int");
PyErr_SetString(PyExc_TypeError, "PtDebugPrint expects a sequence of objects, "
"an integer explicitly keyed `level`, "
"an object explicitly keyed `sep`, "
"and an object explicitly keyed `end`");
PYTHON_RETURN_ERROR;
}

Expand Down