pystache icon indicating copy to clipboard operation
pystache copied to clipboard

Python / Jython 2.2 support for v0.5.0

Open clach04 opened this issue 13 years ago • 11 comments

I've made some quick changes for Python 2.2 support (tested with http://code.google.com/p/movable-python/downloads/detail?name=movpy-2.0.0-py2.2.3.zip ) I've not yet tried Jython 2.2.

Test suite is now runnable but there are some failures in the test. Py2.2 does NOT include datetime, decimal, or simplejson so these are additional requirements for 2.2 users.

clach04 avatar Apr 05 '12 20:04 clach04

Also see issue #98 for comments regarding nose and running unit tests.

cjerdonek avatar Apr 07 '12 01:04 cjerdonek

It occurs to me that we can get around the absence of simplejson (at least for the spec tests) by storing or generating a version of the spec tests in native Python code (e.g. by using Python's repr() on the deserialized YAML/JSON).

cjerdonek avatar Apr 08 '12 21:04 cjerdonek

Its funny you mentioned repr(), I have this in a few of my projects where I deal with self generated json (i.e. it isn't coming from an untrusted user):

"""json support
TODO consider:
  * http://pypi.python.org/pypi/omnijson
  * http://opensource.xhaus.com/projects/show/jyson
"""
try:
    # Python 2.6+
    import json
except ImportError:
    try:
        # from http://code.google.com/p/simplejson
        import simplejson as json
    except ImportError:
        json = None
......
if json is None:
    warnings.warn('Using naive json handlers')

    def dump_json(x, indent=None):
        """dumb not safe!
        Works for the purposes of this specific script as quotes never
        appear in data set.

        Parameter indent ignored"""
        # could use pprint for the purposes of this specific script
        return repr(x).replace("'", '"')

    def load_json(x):
        """dumb not safe! Works for the purposes of this specific script"""
        x = x.replace('\r', '')
        try:
            result = eval(x)
        except Exception, e:
            logging.error('parsing json string data: %r', x)
            print ''
            print '** ERROR parsing json string data'
            print ''
            raise Exception(e)
        return result
else:
    dump_json = json.dumps
    load_json = json.loads

I've not tried this with pystache yet though.

Backported datetime/decimal work fine though.

I found an old article that talks about Nose py2.2 support so I'll try that out when I get chance but manually running the tests has worked out so far.

clach04 avatar Apr 09 '12 17:04 clach04

With regard to unit tests, my current thinking is to move away from requiring nose. This is primarily because Distribute's test seems to provide better support for running 2to3 automatically when testing with Python 3, which I feel is important going forward:

python setup.py test

Using Distribute's test required me to write a manual test collector to collect the doctests from all packages. I used this approach in conjunction with the load_tests protocol introduced in Python 2.7.

To support Python 2.2, it probably wouldn't be too hard to write a modified test collector that works with Python 2.2 and grabs all unit tests and doctests. That would let us unit test in Python 2.2 more easily going forward and without all the manual updates. By the way, I'd like eventually to be able to run the tests across all versions of Python with a single command. I've made issue #107 for this. It would be too much work to support all versions of Python without this capability.

cjerdonek avatar Apr 09 '12 18:04 cjerdonek

I just spent a few hours attempting to backport Nose to Py 2.2 see https://github.com/clach04/nose/tree/python_2.2_support it seems to work for CPython 2.2 but not Jython (2.2). I'm not likely to pursue Nose further.

Basically Nose support under Jython 2.2 is a no go, I'm not sure if test (module) discovery is an option under Jython 2.2 :-(

The nice thing about the way you've setup the test suite is that it uses unittest and then any test discovery mechanisms work if available, with manual invocation as a backup option (the changes to the unittests in this pull request make sure vanilla unittest work). Tox looks interesting.

clach04 avatar Apr 10 '12 03:04 clach04

Thanks for putting effort into seeing if nose would work. I wouldn't have had a problem with using nose for older versions and Distribute's test for newer versions. By the way, it looks like Distribute only works for Python 2.3 or later.

Test discovery and collection isn't impossible to implement on our own. Pretty much all you need is unittest and a programmatic way to import at runtime (e.g. Python's __import__()). I actually did something like this for WebKit when I was working on their Python scripts a couple years ago. I just found the relevant section of code here. The license is BSD.

In the meantime, a poor man's option (which we'd have to maintain by hand) would be to have a single test module, say, with a line that looks like from test_module import * for each unit test module in our code base. And then we could run unittest from that module because that module would have all the unittest.TestCase classes in it (as long as the class names are unique).

cjerdonek avatar Apr 10 '12 04:04 cjerdonek

For future reference, here is the simpler, original form of the manual test-collection code I referenced above (just ~100 lines). Also for future reference, unittest was new as of Python 2.1.

cjerdonek avatar Apr 10 '12 04:04 cjerdonek

FYI, I got tox working in the development branch and pushed it. Tests pass for Python 2.4 to 3.2 with one command. Tox's documentation says they support Jython by default.

cjerdonek avatar Apr 11 '12 05:04 cjerdonek

I'm more convinced not to use nose after seeing all the open issues they have. I just ran into this one myself.

cjerdonek avatar Apr 14 '12 16:04 cjerdonek

FYI, I changed the test harness in the development branch so that nose is no longer needed or used. Running tox now tests Python 2.4 through 3.2. Instructions are in the README. I wonder if this will get you any farther in including 2.2 into the test harness.

cjerdonek avatar Apr 23 '12 15:04 cjerdonek

Jython has come on a long way since 2012, with 2.7 compatibility being fairly good, it's worth seeing if pystache still needs this patch.

stuaxo avatar Dec 11 '17 23:12 stuaxo