thriftpy icon indicating copy to clipboard operation
thriftpy copied to clipboard

Parser doesn't support absolute path on windows

Open tanwov opened this issue 8 years ago • 14 comments

Using urlparse to get the path's schema ,but on windows ,it doesn't work well. When loads file using absolute path , it would raise

raise ThriftParserError('ThriftPy does not support generating module ' 'with path in protocol '{}''.format( url_scheme))

tanwov avatar Sep 01 '16 15:09 tanwov

Could it be wrotten like

if url_scheme in ('http', 'https'):
    data = urlopen(path).read()
else:
    try:
        with open(path) as fh:
            data = fh.read()
    except Exception as e:
        raise ThriftParserError('ThriftPy does not support generating module '
                                'with path in protocol \'{}\''.format(
                                    url_scheme),e)

tanwov avatar Sep 01 '16 15:09 tanwov

Can you be more specific about the parameter on how to reproduce and why this code fixed the bug?

lxyu avatar Sep 02 '16 07:09 lxyu

test_thrift= thriftpy.load(r'C:\test.thrift',module_name='test_thrift') urlparse with return

ParseResult(scheme='e', netloc='', path='\test.thrift', params='', query='', fragment='')

# parser.py  line 487
url_scheme = urlparse(path).scheme
if url_scheme == '':
    with open(path) as fh:
        data = fh.read()
elif url_scheme in ('http', 'https'):
    data = urlopen(path).read()
else:
    raise ThriftParserError('ThriftPy does not support generating module '
                            'with path in protocol \'{}\''.format(
                                url_scheme))

because the url_scheme is e, it goes into the else branch and raise ThriftParserError

tanwov avatar Sep 02 '16 09:09 tanwov

I see, I'll look into this problem later.

For now, you may want to check parser_fp function, located at https://github.com/eleme/thriftpy/blob/develop/thriftpy/parser/parser.py#L518

lxyu avatar Sep 02 '16 09:09 lxyu

I am also encountering this issue. My temporary fix was to edit thriftpy/parser/parser.py, and change line 488 from if url_scheme == '': to if url_scheme in ('c', ''):

This will fix it for files located in the C: drive as this resolves to a uri protocol of c, we probably need to also add 'd', 'e', 'f' etc to be safe - or do something a bit more sensible.

euandekock avatar Sep 27 '16 08:09 euandekock

The problem only occurs when I use pip to install thriftpy. No such problem when I use conda to install.

Tested on: Windows 7 Anaconda Python 3.5 thriftpy 0.3.8 (installed via conda)

ha62791 avatar Jan 27 '17 01:01 ha62791

I confirm the suggested patch "https://github.com/eleme/thriftpy/issues/234#issuecomment-249794576" is mandatory to have a working Thriftpy on WinPython.

I suspect the bug was not in 0.3.8, only in 0.3.9

stonebig avatar Apr 17 '17 17:04 stonebig

Theoretically, you try to open the path as a file as the fallback to unsupported schemes, rather than simply giving up. Let the filesystem figure out what it can and can't open rather than trying to preempt it.

aawilson avatar Sep 15 '17 17:09 aawilson

Alternatively, maybe it's a bad idea for "load" to magically coerce different path types to begin with, and the function should have separate treatments for "filesystem paths" and "URLs".

aawilson avatar Sep 15 '17 19:09 aawilson

What is the plan to fix this issue? Why wasn't the PR accepted?

As it stands Windows users cannot use packages that use thirftpy, e.g. happybase, because they import thriftpy and it falls over..

markmnl avatar Nov 13 '17 05:11 markmnl

Any update on the status of this issue?

jbaayen avatar Feb 22 '18 09:02 jbaayen

Guys, I'm also hitting this issue when trying to use py_zipkin on windows.

rubenhak avatar Jun 21 '18 05:06 rubenhak

@wooparadog stays pretty busy, from what it sounds like. I agree that #285 is a good enough approach, we should accept it.

aawilson avatar Jun 21 '18 22:06 aawilson

Hi all, I accept https://github.com/markmnl/thriftpy/commit/0801f462c044e2def9a305b38f77fb89ead2c6cb in thriftpy2 to fix it, thanks!

ethe avatar Dec 09 '18 15:12 ethe