Repository navigation
Remove HTML format from io.ascii guess list - #3693
Conversation
|
There is a good chance that this will resolve #3280 as well. |
|
I found it a little surprising that BeautifulSoup gets so far into a vanilla In the context of what Rather than completely disabling the HTML parser altogether, you could read the first few bytes to see if it looks like an HTML table and if not, just bail out. |
|
I think this is too heavy-handed, since it doesn't affect all users. I like @mdboom's suggestion as a starter, but in the meantime I'd rather not do anything too heavy-handed unless someone with the debugging skills can reproduce this and figure out what's going on. |
|
It also sounds most likely like a bug in the libxml2.2 that is built for OSX on some versions (who knows?) of Anaconda. |
|
I'm -1 on @mdboom's suggestion. At least for the LaTeX Reader (which is a similar case in the sense that the user will always know that the format is html or latex as opposed to the more generic ascii formats) it is very valuable that the reader works on snippets taken from a LaTeX file. We could fail the LaTeX Reader for all files that don't contain e.g. |
|
Maybe we can have the requirement from @mdboom only when guessing. |
|
I don't know how many people use cut and paste snippets as I described above, but in that case it would be handy if guessing worked for those. You might not be able to use the filename as identifier in this case. On the other hand, I don't think many people even know that we have an interface to read html tables and I guess most would not notice even if we removed the html reader entirely... |
|
How about a solution similar to @mdboom, where we preprocess (just for guessing) a file for the html reader and require that |
|
@hamogu: Your suggestion is even more exclusive than mine, so I don't understand why you're not in favor of mine, but in favor of yours. I wasn't suggesting that we require an tag or a doctype decl or anything. Only that it starts with a |
|
@mdboom Sorry, I may have misunderstood your suggestion. I thought you wanted to require that the tag pattern matches at the beginning of a file. If I copy & paste a couple of lines from the middle of the html file, it might start with free-form text. However, I'm just saying what I think would be useful, I did not actually test if beautiful soup would parse such files that are certainly not valid html. |
|
@hamogu: Just to be clear -- I'm not opposed to your even more specific regex -- but ideally we wouldn't have to read too much of the file before bailing, just for efficiency's sake. |
|
Maybe something like Or plan B which is to pass the first N characters to the parser and see if it detects any HTML elements. This presumes that most parsers are pretty liberal and will come up with something, even if there are no ending tags. As I mentioned, a reader has access to a |
|
What Mike is suggesting is in fact exactly the same thing libxml is doing. If it fails to find the start of an XML/HTML tag it goes into a routine that parses it as a CDATA block. That code has a fair bit of buffer manipulation chicanery that I don't fully understand yet, but its buffer manipulation is definitely where the segfault is occurring. By making that check for |
|
I agree with @hamogu that we want the following to still work: |
|
This may be a naive question, but is it possible for the first character of an HTML file (excluding whitespace) to not be |
Well, with the difference that you guys could probably perform the same check without crashing my program :) |
|
@astrofrog: Yes, if you exclude the doctype declaration (which is technically required, but often omitted in practice), |
|
Doesn't the doctype declaration include |
|
Slightly OT: do you think there's room for a new format hint or two, for slightly more fine-grained control over guessing? format 1: Basically the current logic: tries HTML, XML, csv, ... |
|
@astrofrog - to reiterate what @mdboom said, we support HTML fragments. Think of the use-case of going to a page with one or more tables, click on "View Source", then copy/paste just the table into a local file. |
|
@ChrisBeaumont - that's where I was going with the idea of allowing |
I like this idea. Generally I want to put ~0 effort into the keywords passed to my parsing function, and have it "just work". However it's very little extra burden for me to decide and signal whether my file is fixed with, delimited, or dom-like. If that makes the subsequent parsing more reliable, then that sounds like a win. |
|
I don't think you need to look for |
|
See also comment in #2881 for a way to generalize this idea to all formats to improve guessing performance overall. |
|
PR attached. |
|
-1 on this. It might break people's code who right now rely on guessing of HTML tables. In the mid term fast_identify is a lot more promising and we should aim to do that before 1.1 (at least for HTML tables), so that this code would never be released. In the short term, I suggest that @ChrisBeaumont just monkey patches the guess list in his use case to look as suggested in this PR. Would that work? |
|
I thought I made this comment somewhere but can't find it - this will not affect people using Table.read since I think that is done based on the extension for HTML? In any case, as a workaround, we could also make ascii.read behave this way - i.e. disable the guessing as suggested by this PR, but add a special case that if the extension is htm or html, it will actually try the HTML parser and try it first? This will be backward-compatible for 99% of cases. |
|
What happened to the idea of pre-scanning for presence of a |
|
@hamogu - to my mind this is a critical bug since it results in segfault and has been reported by at least 5 users. Would you be at least +0 if we did the suggestion from @astrofrog to reduce (to a very low level) the breakage? With his suggestion in place the only thing that would break is if you are passing some HTML table input besides a file name and asking for that to be guessed. @embray - the problem with pre-scanning is that it still leaves open the loophole of a CSV file that happens to contain valid HTML. If you lower the bar as low as just a |
|
OK, convinced, but we might still want to reverse this once we have improved the guessing with a pretty scanning step. |
|
👍 to @astrofrog's suggestion on this. I should also note that I've had 2 other people specifically come to me with this problem within the last couple weeks (both are not active on github but it caused them significant problems which they might not have solved w/o me to ask...), so I agree with @taldcroft that this is critical. (Perhaps we should also get this in for 1.0.3, @embray ?) |
|
I'll try to implement the suggestion for |
|
OK, maybe we have an acceptable solution to this issue. The default now is to not guess HTML, but if the input is "probably" HTML then the HTML reader is put at the head of the guess list. Please check the code for what is meant by "probably" and let me know if that seems fine. The idea is to set the bar relatively high for "probably" to avoid false positives with randomly-occurring HTML-like characteristics, but still get most real HTML table inputs for guessing. Nothing is changed in the HTML reader itself. I think this will result in a small enough change to the behavior in 99% of real use cases that we could put this into a bug fix release, but comments welcome. |
|
Looks good to me (if the tests pass). |
There was a problem hiding this comment.
I think this one needs re.VERBOSE in the flags also, since it includes spaces for clarity.
There was a problem hiding this comment.
Also, I think should have ^ at beginning so it won't match on bogus protocols like Xhttp://. Should probably also accept ftp and file. Or maybe just [a-z]{1-9} for the protocol.
|
Okay, I'll merge for v1.0.3, but note the changelog entry is misplaced, in that case. I will move it after merging. |
Remove HTML format from io.ascii guess list
Remove HTML format from io.ascii guess list Conflicts: astropy/io/ascii/ui.py
From #3691 it appears that the HTML-format reader (with the libxml backend for beautifulsoup) can segfault for normal CSV-format input.
I would suggest mitigating this by taking HTML out of the standard guess list. It is a bit of an outlier and not something that would commonly appear in a data file of unknown format.
@hamogu @ChrisBeaumont ?