Skip to content

TypeError: decode() argument 1 must be string, not None #67

Closed

Description

@kpmgceeftech

Constant.titles.add function doesn't have default encoding so it throws this error.

from nameparser.config import Constants
czconst = Constants()
czconst.titles.add('BA'.encode('utf-8'))

Activity

  1. derek73 commented on Dec 7, 2017

    @derek73
    Owner

    Thanks for the bug report. I'm not super clear what's going on. I'm guessing you're using Python 2.7? I would expect this to work in Python 2.7:

    # -*- coding: utf-8 -*-
    from nameparser.config import Constants
    czconst = Constants()
    czconst.titles.add('BA')

    However, the error message that you used as the title of this bug is unexpected, and when I run your code on my local machine using Python 2.7 it runs without error, so I think yours and mine are equivalent.

    This is the code in the add() method:

        def add(self, *strings):
            """
            Add the lower case and no-period version of the string arguments to the set.
            Returns ``self`` for chaining.
            """
            for s in strings:
                if type(s) == binary_type:
                    s = s.decode(sys.stdin.encoding)
                self.elements.add(lc(s))
            return self

    So the error message makes me think that the real problem is that your system does not set a value for sys.stdin.encoding. Try running this command from your environment:

    python -c "import sys; print(sys.stdin.encoding)"
    

    I'm guessing it returns None or empty string. Not sure what is an appropriate value to use as a default for your system. And it looks like that class does not implement any way to explicitly set the encoding like the parser class does. I should probably add that.

  2. derek73 commented on Dec 7, 2017

    @derek73
    Owner

    I just pushed a change to master that I think will fix this on your system, it falls back to utf-8. I also added a new add_with_encoding() method that you can use to specify an encoding.

        def test_add_constant_with_explicit_encoding(self):
            c = Constants()
            c.titles.add_with_encoding(b'b\351ck', encoding='latin_1')
            self.assertIn('béck', c.titles)

    It seems to have broken the python 2.6 build, not sure why. Guess I'll have to track that down before I push this to pip.

  3. added this to the v0.5.4 milestone on Dec 7, 2017
  4. derek73 commented on Dec 7, 2017

    @derek73
    Owner

    I was able to push v0.5.4 to pypi so try a fresh pip install and lmk how it goes.

  5. kpmgceeftech commented on Dec 7, 2017

    @kpmgceeftech
    Author

    With v0.5.4 all works well. Thanks a lot for quick help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions