On Wed, Dec 21, 2011 at 6:24 PM, Anthon van der Neut <[email protected]> wrote:
> I had the same 'problem' as Daniel Kluev reported on 2010-06-28 on the
> mailing list, namely that I switched to using the .format string method
> some time ago instead of using the % operator. ( I considered having the
> '%' key on my keyboard emit an electric shock, as I kept falling back to
> using the % operator for 2-3 weeks).
>
> I did not see any code that this had been implemented in the since then,
> and also 'format' is still in BUILTINS.TODO list.
>
> However pypy is now at 2.7.1 (was at 2.5 at the time of the email
> exchange, and did not have .format). So I pulled newformat.py and
> test_newformat.py from there, simplified the first into a file
> stringformat.py so that it no longer has external dependencies and the
> second so it uses unittest and tested the whole thing under CPython 2.7.
>
> Then I dug into the pyjs code and found pyjslib.py and added a format
> operator on string:
>
> String.prototype.format = function() {
>     var args =
> $p['tuple']($pyjs_array_slice.call(arguments,0,arguments.length-1));
>
>     var kw = arguments.length >= 1 ? arguments[arguments.length-1] :
> arguments[arguments.length];
>     if (typeof kw != 'object' || kw.__name__ != 'dict' || typeof
> kw.$pyjs_is_kwarg == 'undefined') {
>         if (typeof kw != 'undefined') args.__array.push(kw);
>         kw = arguments[arguments.length+1];
>     } else {
>         delete kw['$pyjs_is_kwarg'];
>     }
>     if (typeof kw == 'undefined') {
>         kw = $p['__empty_dict']();
>     }
>     return $p['string_format'](this, args, kw);
> }
> String.prototype.format.__args__ = ['args', ['kw']];
>
> I copied this code from pyjs generated code  for a python function that
> has the same signature as .format.

 ahh good man!

> Most test now work (more than 95%).

 superb.

> One of the things I have to look
> into has probably to do with the newformat.py code relying on
> multiplication of strings to make the fill of a field specified with a
> specific width.

 oh!  ok, then that's also a bug, which is itself best raised with a
unit test that demonstrates the problem, rather than "fixing"
newformat.py

> Even without those last few tests I think this is useful. So my
> questions are:
> - if anyone is interested please let me know how to best provide you
> with the stuff ( check in to git?, diff? )

 yes, if you understand git and are happy to use git-format-patch
that's the absolute best way, because it preserves the commit
messages.

 if you're happy with that, please do alter changelog and add yourself
to the copyright file (otherwise i have to do it *sigh*).

> - is the above insertion of .format in the pyjslib.py the correct way to
> do this?

 yyep.

> - where should the stringformat.py file best go (it is currently next to
> pyjslib.py)

 hmmm i would say somewhere in pyjslib.py ... hrmmm... ok, yep,
pyjslib.py for now but put a comment at the start and end saying "this
code came from pypy from this point onwards...."

> - any pointers on how I should do the testing of stringformat.py in a
> pyjamas like way?

 add it to LibTest.

> - should the CPython 2.7 unittest(2( code be checked in somewhere, or do
> I keep that for myself?

 if you can munge it into LibTest happy format that would be great.
even better would be munging LibTest slightly so that it supports the
test file "as-is".

 great work, anthon.

 l.

Reply via email to