This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Py_GetVersion() is broken when using mqueue and a long patch name
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6, Python 3.5, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, martin.panter, python-dev
Priority: normal Keywords: patch

Created on 2014-04-19 22:17 by eric.snow, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
parse-version.patch martin.panter, 2016-05-13 04:49 review
Messages (6)
msg216883 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-04-19 22:17
Py_GetVersion() (in Python/getversion.c) builds the version string returned by sys.version:

  PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
            PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());

In turn, Py_GetBuildInfo() (in Modules/getbuildinfo.c) returns the "build" portion of Python's version string.  When available, the tag returned by "hg id -t" constitutes part of that build string.

The problem is that when using mqueue the result of "hg id -t" can be pretty long.  For example:

  issue21226-fix-PyImport_ExecCodeModuleObject.diff qbase qtip tip

That's 74 characters for just part of a build string that may be well over 100 characters.  However, Py_GetVersion() truncates it to 80 characters.

The consequence is that this value of sys.version causes platform._sys_version() to fail since the version string does not match the expected pattern.

So either Py_GetVersion() should relax (-1) or Py_GetBuildInfo() should restrict the length of the resulting build string (+1).  Would it work to truncate just the hgid part so that the whole string returned by Py_GetBuildInfo() is no more than length 80?  E.g.

-    PyOS_snprintf(buildinfo, sizeof(buildinfo),
-                  "%s%s%s, %.20s, %.9s", hgid, sep, revision, DATE, TIME);
+    PyOS_snprintf(buildinfo, 80,
+                  "%.43s%.1s%.13s, %.20s, %.9s", hgid, sep, revision, DATE, TIME);
msg238905 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-03-22 14:36
Any comments on the proposal to restrict the length of the returned string from Py_GetBuildInfo() to 80 characters?
msg265443 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-13 04:49
Currently Py_GetBuildInfo() just returns a long untruncated string. Perhaps it would be safer to leave that as it is, and just make the parsing in the “platform” module more tolerant. What do you think?

I never really cared about the details in Py_GetBuildInfo(), but it would be nice if the test suite would run :)
msg265444 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-13 04:56
>>> Py_GetBuildInfo = pythonapi.Py_GetBuildInfo
>>> Py_GetBuildInfo.restype = c_char_p
>>> Py_GetBuildInfo()  # Not truncated
'qbase qtip subprocess-stderr_redirect_with_no_stdout_redirect-2.diff tip:0b641285389d+, May 13 2016, 02:10:26'

Demo of my problem with the test suite:

$ ./python -m test.regrtest
Traceback (most recent call last):
  [. . .]
  File "/media/disk/home/proj/python/cpython/Lib/test/", line 1423, in check_impl_detail
    return guards.get(platform.python_implementation().lower(), default)
  File "/media/disk/home/proj/python/cpython/Lib/", line 1451, in python_implementation
    return _sys_version()[0]
  File "/media/disk/home/proj/python/cpython/Lib/", line 1416, in _sys_version
ValueError: failed to parse CPython sys.version: '2.7.11+ (qbase qtip subprocess-stderr_redirect_with_no_stdout_redirect-2.diff tip:0b64128) \n[GCC 5.3.0]'
msg267827 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-08 09:50
New changeset b86e259271b3 by Martin Panter in branch '2.7':
Issue #21313: Tolerate truncated buildinfo in sys.version

New changeset 4deec876db0d by Martin Panter in branch '3.5':
Issue #21313: Tolerate truncated buildinfo in sys.version

New changeset aec5a3fc4890 by Martin Panter in branch 'default':
Issue #21313: Merge version parsing from 3.5
msg267842 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-08 12:16
Closing now that platform._sys_version() can tolerate the truncated version info.
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65512
2016-06-08 12:16:20martin.pantersetstatus: open -> closed
resolution: fixed
messages: + msg267842

stage: patch review -> resolved
2016-06-08 09:50:50python-devsetnosy: + python-dev
messages: + msg267827
2016-05-15 11:28:07BreamoreBoysetnosy: - BreamoreBoy
2016-05-13 04:56:35martin.pantersetmessages: + msg265444
2016-05-13 04:49:52martin.pantersetfiles: + parse-version.patch
versions: + Python 3.6, - Python 3.4
nosy: + martin.panter

messages: + msg265443

keywords: + patch
2015-03-22 14:36:28BreamoreBoysetnosy: + BreamoreBoy
messages: + msg238905
2014-04-19 22:17:18eric.snowcreate