Python 2.6-3.3 compatibility (using a single codebase approach)

#2 Merged at e3112fe
  1. Mikhail Korobov

Hi Robert,

I added Python 3.x compatibility to line_profiler using a single codebase approach - line_profiler should work under Pythons 2.6, 2.7, 3.2 and 3.3.

Unfortunately there are no tests, but what I checked works, and the fixes are IMHO straightforward.

What I checked manually:

  • %lprun IPython magic under Python 2.7 and 3.3 with functions;
  • %lprun IPython magic under Python 2.7 with generators;
  • %lprun IPython magic under Python 2.7 and 3.3 with invalid -f argument

What do you think? I would be great to see Python 3.x-compatible line_profiler on pypi! :)

Comments (17)

  1. Robert Kern repo owner

    I think this is a perfect reason to drop Python 2.5. Thanks!

    We can also drop the exec workaround to for supporting PEP 342 generators.

  2. Mikhail Korobov author works for me now. -b flag doesn't work though, but this seems unrelated to Python 3.x support (it fails even without my changes). The exception is:

    Traceback (most recent call last):
      File "/Users/kmike/envs/pymorphy/bin/", line 233, in <module>
      File "/Users/kmike/envs/pymorphy/bin/", line 230, in main
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 81, in print_stats
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 93, in __init__
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 107, in init
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 136, in load_stats
        self.__class__, arg)
    TypeError: Cannot create or construct a <class pstats.Stats at 0x1041f1f58> object from '<__main__.ContextualProfile object at 0x1041da910>''

    It is raised when there are not functions decorated with @profile.

    If cProfile import is commented out, then ContextualProfile.__init__ fails because profile.Profile is an old-style class and thus super() doesn't work. After changing it to use Profile.__init__ exception is no longer raised. But it seems we don't need to fix this because cProfile is always imported under 2.x, and under 3.x super always work.

  3. Mikhail Korobov author

    I think the pull request is ready. It seems that more code may be dropped from Cython extension because of removal of 2.5 support, but I don't want to make this pull request more complex.

  4. Mikhail Korobov author

    I'm using it since March too, without issues in both 2.x and 3.x virtualenvs.

    I guess merging is hard mainly because line_profiler is very stable software, there are no unit tests for this PR, and the changes are non-trivial, esp. if one haven't done Python 3.x porting before, so it is hard to check if this PR is valid. Also, after merging Robert should commit himself to supporting Python 3 in line_profiler and check patches for Python 3.x compatibility. I don't know how to improve the situation - writing proper tests for line_profiler looks quite hard, and unfortunately I don't have time for that now.

    What I can do is to offer help with fixing compatibility issues after merging this PR - say, I'll submit a PR with a fix for any Python 2.x or 3.x compat issue in a period of 14 days after raising it for all issues raised until 2015 :)

  5. gregcouch

    Since in the "print message," can't be converted to "print(message, end=)" and retain Python 2 compatibility, should the subsequent leading \n's should be dropped?

  6. Mikhail Korobov author

    There are two "\n"s (for dump_file and text_file), and the second "\n" may be either between the message and "text_file" or between "dump_file" and "text_file", so if we just remove these "\n"s we'll lose one separator when both options are ON.

    Does strict backwards compatibility matter in this case? I found extra empty line nice :)

  7. Matthias Bussonnier

    I would love to have that in, and maybe even push a --pre on PyPi, I'm trying to default all my work on Python 3 now, and this is one of the only things missing that I can't install with pip3 yet.