Skip to content

Add checks for unicode byte order marks - #241

Closed
evolvedmicrobe wants to merge 9 commits into
biopython:masterfrom
evolvedmicrobe:master
Closed

evolvedmicrobe wants to merge 9 commits into
biopython:masterfrom
evolvedmicrobe:master

Conversation

@evolvedmicrobe

Copy link
Copy Markdown

No description provided.

@peterjc

peterjc commented Sep 28, 2013

Copy link
Copy Markdown
Member

Hi Nigel,

Code point: The string startswith method accepts a tuple of prefixes (since Python 2.5), so you only need to call it once with your four unicode markers - making this much simpler. See http://docs.python.org/2/library/stdtypes.html

Style points: Please use spaces either side of the assignment (equals), and after the commas in the unicode markers. Also remove the trailing semi-colon on the raise line. See PEP8 Style Guide http://www.python.org/dev/peps/pep-0008/

I'd also really like to see a unit test for this - do you have a small real world example file?

@evolvedmicrobe

Copy link
Copy Markdown
Author

Hi Peter,

Code point: The string startswith method accepts a tuple of prefixes (since Python 2.5), so you only need to call it once with your four unicode markers - making this much simpler. See http://docs.python.org/2/library/stdtypes.html

Oh nice. One of these days I would spend time setting up the vim autocomplete so it tells me about changes like this. Right now I am writing code assuming methods can take the same arguments I heard of, good to know!

Style points: Please use spaces either side of the assignment (equals), and after the commas in the unicode markers. Also remove the trailing semi-colon on the raise line. See PEP8 Style Guide http://www.python.org/dev/peps/pep-0008/

Simple enough. One other style point as well, the string in the exception goes past 80 characters per line, I have found editors can be very brittle in how they handle strings split across lines in white space aware languages so left it that way, but if you have a preferred way of breaking it up let me know.

I'd also really like to see a unit test for this - do you have a small real world example file?

Yep, in fact this problem came up when I was cross validating the .NET Bio unit tests against the BioPython library (had some failed tests there when I implemented the extended newick format and so was curious about accepted practices to date). Does Biopython have a unit test framework setup? If you can point me towards some documentation I can drop the .newick file and a unit test in.

-N

—
Reply to this email directly or view it on GitHub #241 (comment) . https://github.com/notifications/beacon/Nu06POyoUpKe1js9iKyLwQBcACyReG4Dby72rxsjkB-QBj9SrfTq2WTZwa53zMfT.gif

@peterjc

peterjc commented Sep 28, 2013

Copy link
Copy Markdown
Member

Minor code point: Move the definition of unicodeLines above the for loop.

Minor style point: We're not strict about the 80 line thing but I would split the exception text up something like this:

raise NewickError("The file or stream you attempted to parse includes "
                  "unicode byte order marks.  You must convert it to "
                  "ASCII before it can be parsed.")

For an introduction to our test framework, see the chapter in the main Tutorial, http://biopython.org/DIST/docs/tutorial/Tutorial.html or http://biopython.org/DIST/docs/tutorial/Tutorial.pdf

@peterjc

peterjc commented Oct 7, 2013

Copy link
Copy Markdown
Member

Nigel - could you point us at a (small) public sample data file that could be used to create a unit test? Thanks!

@evolvedmicrobe

Copy link
Copy Markdown
Author

Hi Peter,

Sorry for leaving that hanging, am traveling and haven’t had time to take a look at the unit tests. I can’t actually seem to get the unit tests framework to work from here (some import problems), but I uploaded an example file and the skeleton of one which I gather should work (does the build process on github check for the test automatically?)

Out of curiosity, can I also ask why you wanted the array initialization lifted out of the loop? I would normally have placed it inside to keep all the data relevant to the exception next to it in and as local as possible, and since that branch only executes once I would also have assumed that any performance increment would be trivial. I don’t think it matters either way, but would be curious if there is something about the python internals that makes this a good idea.

Cheers,
Nigel

From: Peter Cock [mailto:[email protected]]
Sent: Monday, October 07, 2013 7:26 AM
To: biopython/biopython
Cc: EvolvedMicrobe
Subject: Re: [biopython] Add checks for unicode byte order marks (#241)

Nigel - could you point us at a (small) public sample data file that could be used to create a unit test? Thanks!
—
Reply to this email directly or #241 (comment) view it on GitHub. https://github.com/notifications/beacon/Nu06POyoUpKe1js9iKyLwQBcACyReG4Dby72rxsjkB-QBj9SrfTq2WTZwa53zMfT.gif

Nigel Delaney added 2 commits October 9, 2013 14:17
hmm… hex editor on mac shows that this file contains unicode characters
EF BB BF which open().read() interprets as \xef\xbb\xbf, however unlike
on my other machine this is parsed okay, and only throws an error at
tree.get_terminals() about it being unicode, this is very annoying.
@evolvedmicrobe

Copy link
Copy Markdown
Author

Okay, it looks like there wasn't a way to do this without avoiding the unicode hell. The exception is raised in 2.X versions of python as it should be, but it appears no exception is raised in the 3.X versions, causing the test to fail. I assume this is because 2.X often uses ASCII while 3.X uses unicode-8. My guess would be that the unicode works just fine in 3.X, but don't have an install of that to test.

I think it would be nice to leave this in for the 2.X users and maybe skip the test for 3.X, but don't have an install of 3.X to test with so will leave this lie for now.

@peterjc

peterjc commented Apr 21, 2014

Copy link
Copy Markdown
Member

OK, I'll look at #310 now.

P.S. What was did "array initialization lifted out of the loop? " refer to?

@evolvedmicrobe

Copy link
Copy Markdown
Author

Sorry, just saw this email, the array initialization referred to moving this tuple

unicodeLines = ("\xef", "\xff", "\xfe", "\x00")

outside of the for loop.

peterjc pushed a commit that referenced this pull request Apr 21, 2014
@peterjc

peterjc commented Apr 21, 2014

Copy link
Copy Markdown
Member

Thanks - it is outside the loop now :)

@peterjc

peterjc commented Oct 17, 2019

Copy link
Copy Markdown
Member

@evolvedmicrobe would you agree to dual license your contributions to Biopython to date (including this and #239) as per #898?

@evolvedmicrobe

Copy link
Copy Markdown
Author

@peterjc absolutely!

@peterjc

peterjc commented Oct 18, 2019

Copy link
Copy Markdown
Member

Thank you 👍

peterjc added a commit to peterjc/biopython that referenced this pull request Nov 1, 2019
Original author Eric Talevich, authors on whose code it was based
(Frank and Cymon), and contributors since as tracked with version
control have agreed:

 - Aaron Gallagher (@habnabit)
   biopython#898 (comment)
 - Allis Tauri (@allista)
   biopython#898 (comment)
 - Ben Morris (@bendmorris)
   biopython#898 (comment)
 - Byron Smith (@bsmith89)
   biopython#898 (comment)
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Cymon J. Cox (@cymon)
   biopython#898 (comment)
 - Eric Talevich (@etal)
   biopython#898 (comment)
 - Frank Kauff (@frankkl)
   biopython#2246 (comment)
 - Francesco Gastaldello (@Gasta88)
   biopython#1063
 - @morrme
   biopython#1172 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   biopython#241 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Sourav Singh (@souravsingh)
   biopython#957
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
 - Vishnu Chilakamarri (@vvc11)
   biopython#898 (comment)
peterjc added a commit to peterjc/biopython that referenced this pull request Nov 29, 2019
Original author Eric Talevich, authors on whose code it was based
(Frank and Cymon), and contributors since as tracked with version
control have agreed:

 - Aaron Gallagher (@habnabit)
   biopython#898 (comment)
 - Allis Tauri (@allista)
   biopython#898 (comment)
 - Ben Morris (@bendmorris)
   biopython#898 (comment)
 - Byron Smith (@bsmith89)
   biopython#898 (comment)
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Cymon J. Cox (@cymon)
   biopython#898 (comment)
 - Eric Talevich (@etal)
   biopython#898 (comment)
 - Frank Kauff (@frankkl)
   biopython#2246 (comment)
 - Francesco Gastaldello (@Gasta88)
   biopython#1063
 - @morrme
   biopython#1172 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   biopython#241 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Sourav Singh (@souravsingh)
   biopython#957
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
 - Vishnu Chilakamarri (@vvc11)
   biopython#898 (comment)
peterjc added a commit that referenced this pull request Dec 3, 2019
Original author Eric Talevich, authors on whose code it was based
(Frank and Cymon), and contributors since as tracked with version
control have agreed:

 - Aaron Gallagher (@habnabit)
   #898 (comment)
 - Allis Tauri (@allista)
   #898 (comment)
 - Ben Morris (@bendmorris)
   #898 (comment)
 - Byron Smith (@bsmith89)
   #898 (comment)
 - Chris Rands (@chris-rands)
   #1301
 - Christian Brueffer (@cbrueffer)
   #898 (comment)
 - Cymon J. Cox (@cymon)
   #898 (comment)
 - Eric Talevich (@etal)
   #898 (comment)
 - Frank Kauff (@frankkl)
   #2246 (comment)
 - Francesco Gastaldello (@Gasta88)
   #1063
 - @morrme
   #1172 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   #241 (comment)
 - Peter Cock (@peterjc)
   #898 (comment)
 - Sergio Valqui (@svalqui)
   #1749
 - Sourav Singh (@souravsingh)
   #957
 - Suresh Panneerselvam (@Amrithasuresh)
   #1575
 - Vishnu Chilakamarri (@vvc11)
   #898 (comment)
peterjc added a commit to peterjc/biopython that referenced this pull request Nov 15, 2020
Original author Eric Talevich and contributors
since as tracked by version control have agreed:

- Adhemar Zerlotini (@azneto)
   biopython#1412 (comment)
 - Andrey Raspopov (@Andrey-Raspopov)
   biopython#2444
 - Carlos Pena (@carlosp420)
   biopython#898 (comment)
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Eric Talevich (@etal)
   biopython#898 (comment)
 - Michiel de Hoon (@mdehoon)
   biopython#898 (comment)
 - @morrme
   biopython#1172 (comment)
 - Nick Negretti (@nimne)
   biopython#1768 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   biopython#241 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
peterjc added a commit that referenced this pull request Dec 3, 2020
Original author Eric Talevich and contributors
since as tracked by version control have agreed:

- Adhemar Zerlotini (@azneto)
   #1412 (comment)
 - Andrey Raspopov (@Andrey-Raspopov)
   #2444
 - Carlos Pena (@carlosp420)
   #898 (comment)
 - Chris Rands (@chris-rands)
   #1301
 - Christian Brueffer (@cbrueffer)
   #898 (comment)
 - Eric Talevich (@etal)
   #898 (comment)
 - Michiel de Hoon (@mdehoon)
   #898 (comment)
 - @morrme
   #1172 (comment)
 - Nick Negretti (@nimne)
   #1768 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   #241 (comment)
 - Peter Cock (@peterjc)
   #898 (comment)
 - Sergio Valqui (@svalqui)
   #1749
 - Suresh Panneerselvam (@Amrithasuresh)
   #1575
JoaoRodrigues pushed a commit to JoaoRodrigues/biopython that referenced this pull request Jun 3, 2021
Original author Eric Talevich and contributors
since as tracked by version control have agreed:

- Adhemar Zerlotini (@azneto)
   biopython#1412 (comment)
 - Andrey Raspopov (@Andrey-Raspopov)
   biopython#2444
 - Carlos Pena (@carlosp420)
   biopython#898 (comment)
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Eric Talevich (@etal)
   biopython#898 (comment)
 - Michiel de Hoon (@mdehoon)
   biopython#898 (comment)
 - @morrme
   biopython#1172 (comment)
 - Nick Negretti (@nimne)
   biopython#1768 (comment)
 - Nigel Delaney (@evolvedmicrobe)
   biopython#241 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants