Comments (8)
What is happening: \x80...
is not unicode (an unicode cannot start by that, as documented in the Python-docs, andb'\x80abc'.decode("utf-8", "strict")
would fail, for example).
Now the file contains the string “closure”
, and the final ”
gets translated into b'\xe2\x80\x9d'
. When we parse a Python file unicode.py
which only contains this:
# a “closure”
with bblfsh-cli -m=semantic ./unicode.py
, we get the following tree:
{ '@type': "python:Module",
'@role': [File, Module],
'@pos': { '@type': "uast:Positions",
},
body: [],
'noops_remainder': { '@type': "python:RemainderNoops",
'@role': [Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
end: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
},
lines: [
{ '@type': "uast:Comment",
'@role': [Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
},
Block: false,
Prefix: " ",
Suffix: "\x80\x9d\n",
Tab: "",
Text: "a “closure\xe2",
},
],
},
}
And the string in Suffix: "\x80\x9d\n"
is specifically the part we cannot create (since it starts by x80
). It seems we are not parsing utf-8
correctly when generating the Suffix
and Preffix
, because we are splitting the \xe2\x80\x9d
as if they were several characters. It's a really nice catch @smacker (and really bizarre too 🤣 )
from sdk.
The problem isn't that bblfsh can't parse non UTF-8. It's a known restriction. The problem is it actually parses the file and allows to filter it partially in python and fully in go. It would be nice if bblfsh could do utf8.IsValid
and reject the file early with a correct error message.
from sdk.
It is not the filter really, the parse itself fails. If we do:
import bblfsh
import requests
FILE_URL = "https://github.com/pytorch/tutorials/raw/master/advanced_source/neural_style_tutorial.py"
content = requests.get(FILE_URL).text
client = bblfsh.BblfshClient("localhost:9432")
ctx = client.parse(FILE_URL.split("/")[-1], None, content)
ctx
we get the same error
from sdk.
True. Though I also see:
ctx = decode(raw_uast, format=0) # no error
print(ctx) # error
from sdk.
In general, Babelfish doesn't work with encodings other than UTF-8. I think we should probably at the least have better error diagnostics for that case. (We might already have an issue for that, but I didn't find it in a cursory scan; I'll look more closely tomorrow).
Handling other encodings is a fairly deep design issue (it impacts not only the API, but the round-tripping of transformations, location as observed by the client vs. the parser, and so on), so we're not likely to do that. As a workaround, though, if the client can transcode the file into UTF-8 before sending it, that should at least ensure consistent results.
from sdk.
Is the character utf8 or it isn't? There is a filter in the Python client to prevent non utf8 characters from being parsed. I think that the problem is that Python concept of utf8 character may be different than Go's one. At least Python says that the character is utf8 and translates it into b'\xe2\x80\x9d'
(3 bytes ?). Maybe to compute the Prefix
field Go only takes into account utf8 characters of 2 bytes at most (don't know if that makes sense)
from sdk.
Correct. It's valid utf8 file from Go point of view:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"unicode/utf8"
)
const fileURL = "https://github.com/pytorch/tutorials/raw/master/advanced_source/neural_style_tutorial.py"
func main() {
httpRes, err := http.Get(fileURL)
if err != nil {
panic(err)
}
if httpRes.StatusCode != http.StatusOK {
panic("not 200 status")
}
content, err := ioutil.ReadAll(httpRes.Body)
if err != nil {
panic(err)
}
fmt.Println("Is valid utf?", utf8.Valid(content))
}
$ go run check.go
Is valid utf? true
from sdk.
I think the issues may be caused by this line. We incorrectly address tabs
since the tab may be a non-ASCII character, but we access it as a byte.
The second cause might be related to position info, e.g. transformer may pull partial Unicode character if the byte offset is one-off.
But in general we need to narrow this case down to a single input line.
from sdk.
Related Issues (20)
- driver failure: native driver is not running HOT 6
- drivers output on debug level does not include requests
- schema: Add string interpolation UAST type HOT 1
- Grpc: received message larger than max (5797446 vs. 4194304) HOT 1
- Clarify backward compatibility rules for uast packages
- Parent covers children invariant is not respected HOT 7
- Parent covers children invariant is not respected HOT 2
- uast.TokenOf returns unexpected results for position nodes
- Support persistent UAST hashes HOT 1
- Strange requests during driver discovery tests HOT 1
- Incorrect check for invalid positions HOT 1
- .filter() - Invalid memory address or nil pointer dereference HOT 5
- Move protocol to a separate package
- ChildrenOrder unexpected behaviour HOT 2
- Feature request: UAST diffing HOT 6
- Fix xpath library wrong query failures in case we continue supporting it
- Update drivers template with new performance tests
- Roles are sorted in encoding step but not in decoding one
- Automation: driver releases
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.