Skip to content

Commit

Permalink
Change PtDebugPrint to behave more like Py3 print.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoikas committed May 6, 2020
1 parent d7f1889 commit b365b60
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,37 +402,71 @@ 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");
if (value) {
value = PyObject_Str(value);
if (!value)
break;
sep = PyString_AsStringEx(value);
Py_DECREF(value);
}

value = PyDict_GetItemString(kwargs, "end");
if (value) {
value = PyObject_Str(value);
if (!value)
break;
end = PyString_AsStringEx(value);
Py_DECREF(value);
}
}

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

0 comments on commit b365b60

Please sign in to comment.