thriftpy icon indicating copy to clipboard operation
thriftpy copied to clipboard

fix for absolute paths on windows

Open majibow opened this issue 8 years ago • 6 comments

An absolute path on windows looks like 'C:\foo\bar.thrift' and urlparse returns 'c' as the scheme which causes a parse error to be thrown when given an absolute path.

>>> import thriftpy
>>> thriftpy.load('C:\\foo\\bar.thrift', 'Hbase_thrift')
Traceback (most recent call last):
  ...
  File "C:\Python27\lib\site-packages\thriftpy\parser\parser.py", line 501, in parse
    url_scheme))
ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'

On Windows any letter 'a-zA-Z' is a valid drive letter and empty string means the current drive and so we can handle this completely with the following test.

if len(url_scheme) <= 1:

is roughly equivalent to, 

if url_scheme == '' or \
   url_scheme in tuple('abcdefghijklmnopqrstuvwxyz'):

In urlparse these are the valid scheme chars,

'abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789'
'+-.'

If any other character appears, urlparse().scheme will return an empty string. A single digit number will never be returned in the scheme since urlparse handles this as a port number and would also return an empty string. Similarly urlparse always returns the lower case version of the scheme and so uppercase letters never appear. Lastly dot '.' is a valid scheme for example .\foo\bar.thrift

This leaves only two characters '+-'

Its not really necessary to handle these two characters since the worst case is the user gets a detailed IOError Exception

>>> open('+:\\fakefile')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    open('+:\\fakefile')
IOError: [Errno 2] No such file or directory: '+:\\fakefile'
>>>  

which makes more sense than the "ThriftParserError: ... protocol 'c' " exception and it stops it from failing for absolute paths on windows.

Please incorporate this fix in the next release of thriftypy. Thanks

majibow avatar Sep 02 '16 10:09 majibow

I still think you should incorporate my suggested change... sure you now handle the 'file://' scheme but with my fix you can also handle being given direct paths and it now works on linux and windows.

majibow avatar May 07 '17 05:05 majibow

if this problem won't be fixed any more?

xulei890817 avatar Nov 13 '17 02:11 xulei890817

I can confirm I am facing the issue on the Windows platform and have had to manually update parser.py for the time being. Hopefully, a proper fix will be promoted soon.

ddonetskov avatar May 22 '18 11:05 ddonetskov

Guys, I'm also hitting this issue when trying to use py_zipkin on windows. I'd appreciate if this 2 year old commit gets merged. Thanks!

rubenhak avatar Jun 21 '18 05:06 rubenhak

Thriftpy was not supported any more, please migrate to thriftpy2, and tornado 5.0 or later has been supported, please modify this pull request, thanks!

ethe avatar Sep 26 '18 14:09 ethe