Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: maximum recursion depth exceeded while calling a Python object #26

Closed
papachoco opened this issue Sep 10, 2018 · 8 comments

Comments

@papachoco
Copy link
Contributor

papachoco commented Sep 10, 2018

Jason,

with the latest release of nti.schema, we are getting a maximum recursion depth error.. in one of our legacy class.

from nti.schema.interfaces import InvalidValue

class InvalidData(InvalidValue):
    """
    Invalid Value
    """

    i18n_message = None

    def __str__(self):
        if self.i18n_message:
            return translate(self.i18n_message)
        return super(InvalidData, self).__str__()

    def doc(self):
        if self.i18n_message:
            return self.i18n_message
        return self.__class__.__doc__

class EmailAddressInvalid(InvalidData):
    """
    Invalid email address.
    """

    i18n_message = _(u"The email address you have entered is not valid.")

    def __init__(self, address):
        super(EmailAddressInvalid, self).__init__(address, value=address)

we raise a EmailAddressInvalid(address) if an invalid address is given. but this generates the recursion error

I take we need to overwrite the with_field_and_value method?

Carlos

@papachoco
Copy link
Contributor Author

papachoco commented Sep 10, 2018

class InvalidValue(sch_interfaces.InvalidValue):
    """
    InvalidValue(*args, field=None, value=None)

    Adds a field specifically to carry the value that is invalid.

    .. deprecated:: 1.4.0
       This is now just a convenience wrapper around
       :class:`zope.schema.interfaces.InvalidValue` that calls
       :meth:`.zope.schema.interfaces.ValidationError.with_field_and_value`
       before returning the exception. You should always catch
       :class:`zope.schema.interfaces.InvalidValue`.
    """
    # We can't write the syntax we want to in Python 2.

    def __init__(self, *args, **kwargs):
        field = kwargs.pop('field', None)
        value = kwargs.pop('value', None)
        if kwargs:
            raise TypeError("Too many kwargs for function InvalidValue")
        **super(InvalidValue, self).__init__(self, *args)**
        self.with_field_and_value(field, value)

@jamadden
Copy link
Member

(FYI, code snippets are introduced with three backticks and the name, not double quotes.)

@jamadden
Copy link
Member

Can you provide an edited traceback? I can't reproduce any problems in a test.

   def test_subclass_constructor(self):
        from nti.schema.interfaces import InvalidValue
        class InvalidData(InvalidValue):
            """
            Invalid Value
            """

        i18n_message = None

        def __str__(self):
            if self.i18n_message:
                return translate(self.i18n_message)
            return super(InvalidData, self).__str__()

        def doc(self):
            if self.i18n_message:
                return self.i18n_message
            return self.__class__.__doc__

        class EmailAddressInvalid(InvalidData):
            """
            Invalid email address.
            """

            i18n_message = "The email address you have entered is not valid."

            def __init__(self, address):
                super(EmailAddressInvalid, self).__init__(address, value=address)

        EmailAddressInvalid('foo')

@papachoco
Copy link
Contributor Author

papachoco commented Sep 10, 2018

I think the problem is in line 158 of nti.schema.interfaces.py

class InvalidValue(sch_interfaces.InvalidValue):
    """
    InvalidValue(*args, field=None, value=None)

    Adds a field specifically to carry the value that is invalid.

    .. deprecated:: 1.4.0
       This is now just a convenience wrapper around
       :class:`zope.schema.interfaces.InvalidValue` that calls
       :meth:`.zope.schema.interfaces.ValidationError.with_field_and_value`
       before returning the exception. You should always catch
       :class:`zope.schema.interfaces.InvalidValue`.
    """
    # We can't write the syntax we want to in Python 2.

    def __init__(self, *args, **kwargs):
        field = kwargs.pop('field', None)
        value = kwargs.pop('value', None)
        if kwargs:
            raise TypeError("Too many kwargs for function InvalidValue")
        super(InvalidValue, self).__init__(self, *args)
        self.with_field_and_value(field, value)

when calling the super constructor.. (there seems to be an extra self)

@jamadden
Copy link
Member

Yeah, that would mess up the .args property, but I don't see a recursion issue there just constructing the object. Can you post an edited traceback?

@papachoco
Copy link
Contributor Author

papachoco commented Sep 10, 2018

>>> from nti.dataserver.users.interfaces import EmailAddressInvalid
>>> EmailAddressInvalid('foo')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/csanchez/Documents/workspace/nti.dataserver-buildout/eggs/zope.schema-4.6.1-py2.7.egg/zope/schema/_bootstrapinterfaces.py", line 69, in __repr__
    ', '.join(repr(arg) for arg in self.args))
  File "/Users/csanchez/Documents/workspace/nti.dataserver-buildout/eggs/zope.schema-4.6.1-py2.7.egg/zope/schema/_bootstrapinterfaces.py", line 69, in <genexpr>
    ', '.join(repr(arg) for arg in self.args))
  File "/Users/csanchez/Documents/workspace/nti.dataserver-buildout/eggs/zope.schema-4.6.1-py2.7.egg/zope/schema/_bootstrapinterfaces.py", line 69, in __repr__
....
....
  File "/Users/csanchez/Documents/workspace/nti.dataserver-buildout/eggs/zope.schema-4.6.1-py2.7.egg/zope/schema/_bootstrapinterfaces.py", line 69, in <genexpr>
    ', '.join(repr(arg) for arg in self.args))
RuntimeError: maximum recursion depth exceeded while calling a Python object

@jamadden
Copy link
Member

There it is. You're printing the repr, and reprs include .args by default. This should be more of a test issue than anything else (of course it should still be fixed).

@papachoco
Copy link
Contributor Author

Apologies ... I just saw it myself

jamadden added a commit that referenced this issue Sep 10, 2018
Fix the repr of InvalidValue. Fixes #26.
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

No branches or pull requests

2 participants