leamas / ddupdate

Update DNS Data for Dynamic IP Addresses
MIT License
40 stars 28 forks source link

Replace "is not" used with literal string. #37

Closed aerusso closed 4 years ago

aerusso commented 4 years ago

This trivial patch silences a warning:

SyntaxWarning: "is not" with a literal. Did you mean "!="?

Otherwise, this shows up every time ddupdate is called.

leamas commented 4 years ago

Yes, the patch is trivial, agreed.

But since I don't get it I'm a bit curious: where does this syntax warning come from? If it's from python itself, what version are you running?

aerusso commented 4 years ago

This warning is new in Python 3.8 (it doesn't show up in 3.7 and older). See the release notes:

The compiler now produces a SyntaxWarning when identity checks (is and is not) are used with certain types of literals (e.g. strings, numbers). These can often work by accident in CPython, but are not guaranteed by the language spec. The warning advises users to use equality tests (== and !=) instead. (Contributed by Serhiy Storchaka in bpo-34850.)

If you want to understand why this makes sense, here's my cheap attempt: you are filtering out empty strings. Under the hood, you can imagine that Python is doing something like

void something(char* line, int line_len) {
   char* word = line;
   int word_len = 0;
   for (char *next_word = line ; next_word < line+line_len; next_word++){
      if(next_word[0] == ' ') {
         if FILTER_STEP(word, word_len)
            YIELD_EMIT(word, word_len);
         word = next_word;
      }
      word_len += 1;
   } 
}

Should you check if the pointer, word is equal to &("")? No! But this is what is and is not claim to check: object identity.

Obviously, CPython does something very different (and slow), but hopefully this makes sense as an illustration.

leamas commented 4 years ago

ok, thanks for thorough explanation! And thanks for the PR.