badrinarayanan / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

ClosureBuilder fails with TypeError: can't use a string pattern on a bytes-like object #605

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago

- What steps will reproduce the problem?

1. Install the environment (see "What version of the product are you using? On 
what operating system?" below).
2. Follow tutorial "Using ClosureBuilder" at 
https://developers.google.com/closure/library/docs/closurebuilder up to step 
"Shrinking Code with ClosureBuilder and the Closure Compiler" (you may need 
fixes provided in issue 603 and issue 604 to get that far).
3. Execute "closure-library\closure\bin\build\closurebuilder.py 
--root=closure-library\ --root=myproject\ --namespace="myproject.start" 
--output_mode=compiled --compiler_jar=compiler.jar 
--output_file=myproject\start-compiled.js" from the windows command prompt 
(change "D:\Projekt\PhpStorm\ClosureTest" according to your installation)

- What is the expected output? What do you see instead?

I am expecting the following:

D:\Projekt\PhpStorm\ClosureTest>closure-library\closure\bin\build\closurebuilder
.py --root=closure-library\ --root=myproject\ --namespace="myproject.start" 
--output_mode=compiled --compiler_jar=compiler.jar 
--output_file=myproject\start-compiled.js
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Scanning paths...
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: 935 sources scanned.
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Building dependency tree..
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Compiling with the following command: java -client -jar compiler.jar 
--js closure-library\closure\goog\base.js 
--js closure-library\closure\goog\debug\error.js 
--js closure-library\closure\goog\dom\nodetype.js 
--js closure-library\closure\goog\string\string.js 
--js closure-library\closure\goog\asserts\asserts.js 
--js closure-library\closure\goog\array\array.js 
--js closure-library\closure\goog\math\math.js 
--js closure-library\closure\goog\math\coordinate.js 
--js closure-library\closure\goog\dom\classes.js 
--js closure-library\closure\goog\useragent\useragent.js 
--js closure-library\closure\goog\object\object.js 
--js closure-library\closure\goog\math\size.js 
--js closure-library\closure\goog\functions\functions.js 
--js closure-library\closure\goog\dom\browserfeature.js 
--js closure-library\closure\goog\dom\tagname.js 
--js closure-library\closure\goog\dom\dom.js 
--js myproject\start.js

D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: JavaScript compilation succeeded.

but I get

D:\Projekt\PhpStorm\ClosureTest>closure-library\closure\bin\build\closurebuilder
.py --root=closure-library\ --root=myproject\ --namespace="myproject.start" 
--output_mode=compiled --compiler_jar=compiler.jar 
--output_file=myproject\start-compiled.js
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Scanning paths...
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: 935 sources scanned.
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Building dependency tree..
Traceback (most recent call last):
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder.py", line 256, in <module>
    main()
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder.py", line 241, in main
    options.compiler_flags)
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\jscompiler.py", line 50, in Compile
    if not (distutils.version.LooseVersion(_GetJavaVersion()) >=
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\jscompiler.py", line 34, in _GetJavaVersion
    return _VERSION_REGEX.search(version_line).group(1)
TypeError: can't use a string pattern on a bytes-like object

After some research, I changed method _GetJavaVersionString in 
closure-library\closure\bin\build\jscompiler.py from

def _GetJavaVersionString():
  """Get the version string from the Java VM."""
  return subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT)

to

def _GetJavaVersionString():
  """Get the version string from the Java VM."""
  java_version = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT)
  # Decode if java_version does not come as str but bytes. This may happen on Windows systems using Python version 3+
  if type(java_version) is bytes:
    java_version = java_version.decode("utf-8")
  return java_version

See attached file. Now I get

D:\Projekt\PhpStorm\ClosureTest>closure-library\closure\bin\build\closurebuilder
.py --root=closure-library\ --root=myproject\ --namespace="myproject.start" 
--output_mode=compiled --compiler_jar=compiler.jar 
--output_file=myproject\start-compiled.js
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Scanning paths...
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: 938 sources scanned.
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Building dependency tree..
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: Compiling with the following command: java -client -jar compiler.jar 
--js closure-library\closure\goog\base.js 
--js closure-library\closure\goog\debug\error.js 
--js closure-library\closure\goog\dom\nodetype.js 
--js closure-library\closure\goog\string\string.js 
--js closure-library\closure\goog\asserts\asserts.js 
--js closure-library\closure\goog\array\array.js 
--js closure-library\closure\goog\math\math.js 
--js closure-library\closure\goog\math\coordinate.js 
--js closure-library\closure\goog\dom\classes.js 
--js closure-library\closure\goog\useragent\useragent.js 
--js closure-library\closure\goog\object\object.js 
--js closure-library\closure\goog\math\size.js 
--js closure-library\closure\goog\functions\functions.js 
--js closure-library\closure\goog\dom\browserfeature.js 
--js closure-library\closure\goog\dom\tagname.js 
--js closure-library\closure\goog\dom\dom.js 
--js myproject\start.js
D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder
.py: JavaScript compilation succeeded.
Traceback (most recent call last):
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder.py", line 262, in <module>
    main()
  File "D:\Projekt\PhpStorm\ClosureTest\closure-library\closure\bin\build\closurebuilder.py", line 254, in main
    out.write(compiled_source)
TypeError: must be str, not bytes

So I changed method main in closure-library\closure\bin\build\closurebuilder.py

from

...
    logging.info('JavaScript compilation succeeded.')
    out.write(compiled_source)
...

to

...
    logging.info('JavaScript compilation succeeded.')

    # Decode if compiled_source does not come as str but bytes. This may happen on Windows systems using Python version 3+
    if type(compiled_source) is bytes:
      compiled_source = compiled_source.decode("utf-8")

    out.write(compiled_source)
...

See attached file. Now it runs through! 

As a newbie to Closure, I'm not sure if that solution has no side-effects I 
will encounter later. But it worked so far, i.e. myproject.html still works 
correctly using start-compiled.js ...
Additionally, I'd propose to centralize changing encoding (as shown above) in a 
method in some base class, because I strongly assume that these two locations 
were not the only ones that do not work in an environment like mine. 
Alternatively, there may be a general way to get around the encoding problem 
(something like temporarily changing the system's default encoding, which seems 
to be Windows1252 in my case, instead of obviously expected UTF8).

- What version of the product are you using? On what operating system?

I am running Windows 7 on a 64bit MacBook Pro. I created a clone of the current 
Closure Library version (http://code.google.com/p/closure-library/) using git 
and installed newest Python for Windows (python-3.3.2.amd64.msi).

- Please provide any additional information below.

- Note: we cannot accept patches without the contributor license agreement
being signed. See http://code.google.com/p/closure-
library/wiki/Contributors for more info.

Original issue reported on code.google.com by m...@jochenscharr.de on 10 Nov 2013 at 11:26

Attachments:

GoogleCodeExporter commented 8 years ago
I am running Windows 7 on a 64bit Dell desktop and I also encountered the same 
problem as described above

Original comment by crose...@gmail.com on 20 Dec 2013 at 12:14

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I ran into the same issue on Windows 8.1 with Python 3.3. It would be great if 
Jochen's proposed fix could be applied.

Original comment by mbercht...@gmail.com on 2 Feb 2014 at 1:00

GoogleCodeExporter commented 8 years ago
This issue was moved to GitHub, 
https://github.com/google/closure-library/issues/28, would you (or someone) 
mind putting together a Pull Request for this? We can review your change there.

Original comment by joelt...@google.com on 20 Jul 2015 at 10:47