python3 slimit压缩js时候出现问题 slice() int()的错误

slimit broken on Python 3. #64

 Open
doerwalter opened this  issue on 13 Jan 2014 · 9 comments



4 participants

@doerwalter @lelit @Alexey-T @delijati
@doerwalter
doerwalter commented  on 13 Jan 2014

I'm trying to use slimit on Python 3 and get the following exception:

Python 3.3.3 (default, Nov 19 2013, 12:08:16)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import slimit
>>> slimit.minify("/**\n * Copyright (c) 2007-2012 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com\n * Dual licensed under MIT and GPL.\n * @author Ariel Flesler\n * @version 1.4.3.1\n */\n;(function($){var h=$.scrollTo=function(a,b,c){$(window).scrollTo(a,b,c)};h.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1,limit:true};h.window=function(a){return $(window)._scrollable()};$.fn._scrollable=function(){return this.map(function(){var a=this,isWin=!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!isWin)return a;var b=(a.contentWindow||a).document||a.ownerDocument||a;return/webkit/i.test(navigator.userAgent)||b.compatMode=='BackCompat'?b.body:b.documentElement})};$.fn.scrollTo=function(e,f,g){if(typeof f=='object'){g=f;f=0}if(typeof g=='function')g={onAfter:g};if(e=='max')e=9e9;g=$.extend({},h.defaults,g);f=f||g.duration;g.queue=g.queue&&g.axis.length>1;if(g.queue)f/=2;g.offset=both(g.offset);g.over=both(g.over);return this._scrollable().each(function(){if(e==null)return;var d=this,$elem=$(d),targ=e,toff,attr={},win=$elem.is('html,body');switch(typeof targ){case'number':case'string':if(/^([+-]=)?\\d+(\\.\\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=$(targ,this);if(!targ.length)return;case'object':if(targ.is||targ.style)toff=(targ=$(targ)).offset()}$.each(g.axis.split(''),function(i,a){var b=a=='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,old=d[key],max=h.max(d,a);if(toff){attr[key]=toff[pos]+(win?0:old-$elem.offset()[pos]);if(g.margin){attr[key]-=parseInt(targ.css('margin'+b))||0;attr[key]-=parseInt(targ.css('border'+b+'Width'))||0}attr[key]+=g.offset[pos]||0;if(g.over[pos])attr[key]+=targ[a=='x'?'width':'height']()*g.over[pos]}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)=='%'?parseFloat(c)/100*max:c}if(g.limit&&/^\\d+$/.test(attr[key]))attr[key]=attr[key]<=0?0:Math.min(attr[key],max);if(!i&&g.queue){if(old!=attr[key])animate(g.onAfterFirst);delete attr[key]}});animate(g.onAfter);function animate(a){$elem.animate(attr,f,g.easing,a&&function(){a.call(this,e,g)})}}).end()};h.max=function(a,b){var c=b=='x'?'Width':'Height',scroll='scroll'+c;if(!$(a).is('html,body'))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,html=a.ownerDocument.documentElement,body=a.ownerDocument.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/slimit/minifier.py", line 38, in minify
    tree = parser.parse(text)
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/slimit/parser.py", line 93, in parse
    return self.parser.parse(text, lexer=self.lexer, debug=debug)
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/ply/yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/ply/yacc.py", line 971, in parseopt_notrack
    p.callable(pslice)
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/slimit/parser.py", line 1101, in p_case_block
    p[0] = p[2:-1]
  File "/Users/walter/pyvenvs/cms/lib/python3.3/site-packages/ply/yacc.py", line 198, in __getitem__
    if n >= 0: return self.slice[n].value
TypeError: unorderable types: slice() >= int()

The problem seems to be that in Python 3 slices are handled via __getitem__ not via __getslice__.

@lelit
lelit commented  on 9 Feb 2014

I think this error is fixed by current (not yet released) ply.

Using that, and with a bunch of little fixes of mine all tests are passing, under Python 2.7 and Python 3.3 at least.

@Alexey-T
Alexey-T commented  on 22 Feb 2014

Hello.Where can I download PY3k ready Slimit version? Is some URL avail?

@lelit
  lelit commented  on 22 Feb 2014
@Alexey-T
Alexey-T commented  on 22 Feb 2014

I see err with Py3.2 (embedded, can't use 3.3). I need 3.3 or...?

Python 3.2.5 (default, May 15 2013, 23:06:03) [MSC v.1500 32 bit (Intel)]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\S\Py\syn_js_minifier\__init__.py", line 8, in <module>
import slimit
  File "D:\S\Py\syn_js_minifier\slimit\__init__.py", line 27, in <module>
from slimit.minifier import minify
  File "D:\S\Py\syn_js_minifier\slimit\minifier.py", line 31, in <module>
from slimit import mangler
  File "D:\S\Py\syn_js_minifier\slimit\mangler.py", line 27, in <module>
from slimit.scope import SymbolTable
  File "D:\S\Py\syn_js_minifier\slimit\scope.py", line 34, in <module>
from slimit.lexer import Lexer
  File "D:\S\Py\syn_js_minifier\slimit\lexer.py", line 29, in <module>
from slimit.unicode_chars import (
  File "D:\S\Py\syn_js_minifier\slimit\unicode_chars.py", line 33
u'[\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6'
                                     ^
SyntaxError: invalid syntax
@lelit
  lelit commented  on 22 Feb 2014
@doerwalter
doerwalter commented  on 27 Mar 2014

It seems that with ply 3.4 and slimit 0.8.1 the slice handling is the only remaining problem on Python 3.3. I can monkeypath around that problem like this:

from ply import yacc
import slimit

def __getitem__(self,n):
    if isinstance(n, slice):
        return self.__getslice__(n.start, n.stop)
    if n >= 0: return self.slice[n].value
    else: return self.stack[n].value

yacc.YaccProduction.__getitem__ = __getitem__

s = "function x(){switch (x) { case 17: break; case 23: break; }} "

print(slimit.minify(s))
@delijati
delijati commented  on 19 Nov 2014

I also tried the ply version from github with some more errors. Maynbe you can fork ply with your current fix and relase it under a another name ... ugly i know

SyntaxError: Unexpected token (RBRACE, '}') at 1:1730813 between LexToken(ID,'disabled',1,1730804) and LexToken(RPAREN,')',1,1730814)
@lelit
lelit commented  on 20 Nov 2014

Do you mean that you tried mine fork of ply, and that gave you the reported error?

@delijati
delijati commented  on 20 Nov 2014

No sorry for the confusion,i tried the 3.5 vanilla version.

@jleclanche jleclanche referenced this issue from a commit in jleclanche/MediaCrush  on 31 Jan
@jleclanche Disable javascript minification on Python 3 (py3)  2afeb1f

下面是ply的简介

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值