diff options
author | Josh Haberman <jhaberman@gmail.com> | 2015-08-28 16:34:48 -0700 |
---|---|---|
committer | Josh Haberman <jhaberman@gmail.com> | 2015-08-28 16:34:48 -0700 |
commit | fc80fad9d543922f5cfaed06e39bc93f07f6baa6 (patch) | |
tree | e26127f30b8b51b052f7bc70f9dd05c5b7050aca /python | |
parent | 26f0c678f6c2179b2e07141b8bc6cde62b93a129 (diff) | |
download | protobuf-fc80fad9d543922f5cfaed06e39bc93f07f6baa6.tar.gz protobuf-fc80fad9d543922f5cfaed06e39bc93f07f6baa6.tar.bz2 protobuf-fc80fad9d543922f5cfaed06e39bc93f07f6baa6.zip |
Fix for https://github.com/google/protobuf/issues/758
Change-Id: I590b34b96c84a3ba6e094a0bd86f153147ade3d3
Diffstat (limited to 'python')
-rw-r--r-- | python/google/protobuf/pyext/message.cc | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index aa3ab97a..ed07de16 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -1776,22 +1776,25 @@ class PythonFieldValuePrinter : public TextFormat::FieldValuePrinter { // Python floats to ensure consistency. string PrintFloat(float value) const { return PrintDouble(value); } string PrintDouble(double value) const { - // Same as float.__str__() - char* buf = PyOS_double_to_string( - value, -#if PY_MAJOR_VERSION < 3 - 'g', PyFloat_STR_PRECISION, // Output is rounded to 12 digits. -#else - 'r', 0, -#endif - Py_DTSF_ADD_DOT_0, // Trailing .0 is always printed. - NULL); - if (!buf) { + // This implementation is not highly optimized (it allocates two temporary + // Python objects) but it is simple and portable. If this is shown to be a + // performance bottleneck, we can optimize it, but the results will likely + // be more complicated to accommodate the differing behavior of double + // formatting between Python 2 and Python 3. + // + // (Though a valid question is: do we really want to make out output + // dependent on the Python version?) + ScopedPyObjectPtr py_value(PyFloat_FromDouble(value)); + if (!py_value.get()) { return string(); } - string result(buf); - PyMem_Free(buf); - return result; + + ScopedPyObjectPtr py_str(PyObject_Str(py_value.get())); + if (!py_str.get()) { + return string(); + } + + return string(PyString_AsString(py_str.get())); } }; |