Skip to content
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

Convert tool_shed response to unicode #1703

Closed
wants to merge 1 commit into from

Conversation

mvdbeek
Copy link
Member

@mvdbeek mvdbeek commented Feb 9, 2016

Addresses issue #1702, where strings returned from the toolshed are causing problems in sqlalchemy.
With this PR unpacked json values will be converted to unicode and downstream code that checks instance type is now using isinstance(var, text_types) instead of str or basestring (which is not available in python3).

@nsoranzo
Copy link
Member

nsoranzo commented Feb 9, 2016

Unit tests are failing because the 4 parameters are None by default.

@mvdbeek mvdbeek changed the title force tool_shed, name, description and owner to be unicode strings. WIP: force tool_shed, name, description and owner to be unicode strings. Feb 9, 2016
@mvdbeek
Copy link
Member Author

mvdbeek commented Feb 9, 2016

Yep, also this is probably not the best place to do the conversion.
I would guess this should be when building the info_dict from the toolshed response.

@mvdbeek mvdbeek changed the title WIP: force tool_shed, name, description and owner to be unicode strings. WIP: Convert tool_shed response to unicode Feb 9, 2016
if a_string_type is str:
return unicode( a_string, 'utf-8' )
elif a_string_type is unicode:
return a_string
Copy link
Member

Choose a reason for hiding this comment

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

What happens if a_string is None?

Copy link
Member Author

Choose a reason for hiding this comment

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

You will get an exception. I copied this function from https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/web/framework/helpers/__init__.py#L101
I was thinking about adding a check for None, but since this is already in use in other places in the code I was thinking to best not change its behaviour.
In fact I think the to_unicode function should be moved out from lib/galaxy/web/framework/helpers/__init__.py ... what do you think about that @nsoranzo ?

Copy link
Member

Choose a reason for hiding this comment

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

It will not raise an exception, it will just return nothing.

Please also keep in mind that everything in lib/galaxy/util/ should be Python3-safe, this function is not (as spotted by py34-lint tox in TravisCI).

Copy link
Member Author

Choose a reason for hiding this comment

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

It will not raise an exception, it will just return nothing.

ah yes, sorry, I got this mixed up.

Please also keep in mind that everything in lib/galaxy/util/ should be Python3-safe, this function is not (as spotted by py34-lint tox in TravisCI).

Thanks, I will check this.

Copy link
Member

Choose a reason for hiding this comment

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

What about something like:

from six import text_type

def to_unicode(a_string):
    if a_string is None:
        return None
    return text_type(a_string)

Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome, that's better than

if sys.version_info[0] > 2:
    def unicode(value):
        """
        Passes on value in python3, as all strings are unicode.
        """
        return value

Thanks!

@martenson
Copy link
Member

@galaxybot test this

@mvdbeek
Copy link
Member Author

mvdbeek commented Feb 10, 2016

@nsoranzo the remaining failure is in https://github.com/galaxyproject/galaxy/blame/dev/lib/galaxy/tools/parameters/basic.py#L166 .
I see that the comment was added by @jxtx in 2007. Do you think it's safe to remove that conversion ?

@mvdbeek mvdbeek changed the title WIP: Convert tool_shed response to unicode Convert tool_shed response to unicode Feb 10, 2016
@@ -2039,7 +2036,7 @@ def single_to_python(value):
else:
return app.model.context.query( app.model.HistoryDatasetAssociation ).get( int( value ) )

if isinstance(value, str) and value.find(",") > -1:
if (isinstance(value, basestring) and value.find(",") > -1):
Copy link
Member

Choose a reason for hiding this comment

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

This file is not checked for Python3 compatibility yet, but I would suggest to use six.string_types instead of basestring anyway (same applies for lib/galaxy/tools/test.py).

The external parentheses are not needed.

@nsoranzo
Copy link
Member

@mvdbeek No idea about that conversion from 2007.

…nstead of basestring or str when checking instance class.
@mvdbeek
Copy link
Member Author

mvdbeek commented Feb 11, 2016

The failing test on os x is just a timeout while fetching wheels.
@nsoranzo
If this is a good/accepted accepted way to handle isinstance(.*, basestring) and explicit calls to unicode in a python 2 and python 3 compatible way, I could also apply this to the whole codebase.

@nsoranzo
Copy link
Member

@mvdbeek Restarted the OS X build, now it's ok.

I surely think this is the right approach for the Python 2 & 3 support, but that should be another PR, I guess. I plan to start working on #1715 after finishing #391.

@mvdbeek
Copy link
Member Author

mvdbeek commented Feb 11, 2016

@nsoranzo another PR, I agree. I already have this ... it needs some work to get the import statements at the right place, and to remove calls to unicode.
If it has a chance of being accepted I can work on this.

@nsoranzo
Copy link
Member

I think so, but let's ping @jmchilton for confirmation.

@jmchilton
Copy link
Member

dev...mvdbeek:replace_basestring Looks good - is that what I am confirming?

@bgruening
Copy link
Member

👍

@mvdbeek
Copy link
Member Author

mvdbeek commented Feb 17, 2016

I'm closing this in favour of PR #1731, which also contains the necessary unicode conversion.

@mvdbeek mvdbeek closed this Feb 17, 2016
@mvdbeek mvdbeek deleted the force_unicode branch April 8, 2016 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants