Skip to content

[MRG] Working towards python3 compatible codebase #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

manu-chroma
Copy link
Member

@manu-chroma manu-chroma commented Jun 25, 2017

Builds on top of #412
Branched out when I used from __future__ import unicode_literals in all cwltool codebase to enforce unicode strings.

Updates

Possible solutions for this problem:

__author__ = "Aleksandr Slepchenkov"
__email__ = "[email protected]"
@manu-chroma manu-chroma changed the title [WIP] Working towards python3 compatible codebase [MRG] Working towards python3 compatible codebase Jun 30, 2017
@manu-chroma
Copy link
Member Author

manu-chroma commented Jun 30, 2017

@mr-c @anton-khodak: this PR is almost ready to merge now. I will do minor annotation improvements by tomorrow. Please review.

branch is on my fork, but edits from maintainers are allowed :)

@@ -15,6 +18,11 @@
from .stdfsaccess import StdFsAccess
from .utils import aslist

# if six.PY3:
# AvroSchemaFromJSONData = avro.schema.SchemaFromJSONData
# else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented out python3 lines until futher resolution of avro issue.

@@ -1 +1,3 @@
# -*- coding: utf-8 -*-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Should be a local config option, yes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this when I included unicode_literals import. I can delete this if we do not plan to use any unicode chars in our source code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep it out for now

@manu-chroma
Copy link
Member Author

manu-chroma commented Jul 4, 2017

Issues created for avro-python3: https://issues.apache.org/jira/browse/AVRO-2046

PR to make avro-python2 compatible with Python 3: apache/avro#234

Update: I've updated the description of the issue on JIRA to better express our interests.

@manu-chroma
Copy link
Member Author

manu-chroma commented Jul 6, 2017

Update
So in order to invoke 2_to_3 automated convertor on avro-python2 during install we have to modify it's setup.py file. There is no way to do it from cwltool's setup.py file.

But the changes we need to make to avro-python2 setup.py are very minimal for it convert all the source files to Python 3. Here's the link to the setup.py and below is the diff:

 try:
-  from setuptools import setup
+  from setuptools import setup, find_packages
 except ImportError:
   from distutils.core import setup
 from sys import version_info
@@ -27,10 +27,10 @@ if version_info[:2] <= (2, 5):
 
 setup(
   name = 'avro',
-  version = '@AVRO_VERSION@',
-  packages = ['avro',],
-  package_dir = {'avro': 'src/avro'},
-  scripts = ["./scripts/avro"],
+  version = '1.0',
+  package_dir = find_packages(),
+  use_2to3=True,
 
   #include_package_data=True,
   package_data={'avro': ['LICENSE', 'NOTICE']},

2to3 might introduce regressions. But I've tested it with schema_salad and cwltool and it passes all unit tests in Python 3.

cc: @mr-c @anton-khodak

@mr-c
Copy link
Member

mr-c commented Jul 7, 2017

@manu-chroma
Copy link
Member Author

manu-chroma commented Jul 8, 2017

@mr-c This looks like a great alternative. I've tested it locally and it works.

Just one problem here, it fails to convert this particular line https://github.com/apache/avro/blob/master/lang/py/src/avro/schema.py#L778. So I had to change it in avro source before it worked for me.

It passed both schema_salad and cwltool unit tests locally.

Possible solutions:

  • We can try changing this particular line in the pip installed avro source on user's machine but this is not the best approach.
  • Possbily make a PR to avro. This is pretty minimal change so maybe they might accept.

@manu-chroma
Copy link
Member Author

@manu-chroma manu-chroma force-pushed the unicode_literal_usage branch from 4ad297c to 79a3ec6 Compare July 8, 2017 17:50
@manu-chroma manu-chroma force-pushed the unicode_literal_usage branch from 79a3ec6 to aec5bb5 Compare July 8, 2017 17:54
@mr-c
Copy link
Member

mr-c commented Jul 9, 2017

@manu-chroma 👍 to a PR against their repo. Shall I merge what you've done so far?

@manu-chroma
Copy link
Member Author

@mr-c Yes please 😄

@mr-c mr-c merged commit f4fcc36 into common-workflow-language:master Jul 9, 2017
@mr-c
Copy link
Member

mr-c commented Jul 9, 2017

Woohoo! Getting closer!

@manu-chroma
Copy link
Member Author

manu-chroma commented Jul 10, 2017

👍 to a PR against their repo.

I'm trying to make a PR which they would convincingly accept. The problem is this code when run in python3:

from past import autotranslate

autotranslate(['avro'])
import avro.schema

outputs

Traceback (most recent call last):
  File "late.py", line 4, in <module>
    import avro.schema
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "/home/manu/Desktop/test/venv3/lib/python3.5/site-packages/past/translation/__init__.py", line 402, in load_module
    code = compile(source, self.pathname, 'exec')
  File "/home/manu/Desktop/test/venv3/lib/python3.5/site-packages/avro/schema.py", line 782
    % (json_string, e)), None, sys.exc_info()[2]
                       ^
SyntaxError: invalid syntax

The way autotranslate works is it converts code into py2+3 compatible code.

raise SchemaParseException('Error parsing JSON: %s, error = %s'
                               % (json_string, e)), None, sys.exc_info()[2]

to make this line work in cross-compatible way, we need to use six lib. If I re-write this line to work with both py2+3 without using any external library, it suppresses a useful exception generated by sys.exc_info()[2] keyword.

Still trying to figure out how to change it in such a way that gets accepted upstream.

@mr-c
Copy link
Member

mr-c commented Jul 10, 2017

@manu-chroma I'd implement it as separate py2 and py3 paths as documented in http://python-future.org/compatible_idioms.html#raising-exceptions with an if sys.version_info[0] < 3: to switch between them

@manu-chroma
Copy link
Member Author

Yes, I had the same idea @mr-c but I was doubtful they would accept such a patch. Though it's definitely worth a shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants