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

invalid literal for int() with base 2: '110000001010100000000000% ' #6

Open
jwshive opened this issue Aug 10, 2024 · 3 comments
Open

Comments

@jwshive
Copy link

jwshive commented Aug 10, 2024

First time user. I've setup my model as in the documentation, but attempting to save 192.168.0.1/24 as my test gets this result.

My model

class Networks(models.Model):
    ip_network = IPNetworkField()
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
ip_management-1  |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
ip_management-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 688, in wrapper
ip_management-1  |     return self.admin_site.admin_view(view)(*args, **kwargs)
ip_management-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
ip_management-1  |     response = view_func(request, *args, **kwargs)
ip_management-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/views/decorators/cache.py", line 62, in _wrapper_view_func
ip_management-1  |     response = view_func(request, *args, **kwargs)
ip_management-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/sites.py", line 242, in inner
ip_management-1  |     return view(request, *args, **kwargs)
ip_management-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 46, in _wrapper
ip_management-1  |     return bound_method(*args, **kwargs)
ip_management-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
ip_management-1  |     response = view_func(request, *args, **kwargs)
ip_management-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 2065, in changelist_view
ip_management-1  |     "selection_note": _("0 of %(cnt)s selected") % {"cnt": len(cl.result_list)},
ip_management-1  |                                                            ^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 380, in __len__
ip_management-1  |     self._fetch_all()
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1881, in _fetch_all
ip_management-1  |     self._result_cache = list(self._iterable_class(self))
ip_management-1  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 121, in __iter__
ip_management-1  |     for row in compiler.results_iter(results):
ip_management-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1500, in apply_converters
ip_management-1  |     value = converter(value, expression, connection)
ip_management-1  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  |   File "/usr/local/lib/python3.12/site-packages/cidrfield/models.py", line 36, in from_db_value
ip_management-1  |     string_ip = ipaddress.ip_address(int(value + '0' * (max_length-len(value)), 2)).__str__()
ip_management-1  |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ip_management-1  | ValueError: invalid literal for int() with base 2: '110000001010100000000000%                                                                                                                        '

Using:

asgiref==3.8.1
Django==4.2.15
django-cidrfield==0.2.0
psycopg2-binary==2.9.9
sqlparse==0.5.1
typing_extensions==4.12.2

Is there something I'm missing

@jwshive
Copy link
Author

jwshive commented Aug 10, 2024

I found the issue, at least in my instance. I noticed this in your models.py
value = value[4:-1] # remove prefix and %
But noticed in the error the % was still visible.
I changed it to
value = value.strip()[4:-1] # remove prefix and %
and that seemed to work for me. I'm not sure how whitespace got in there, I entered the ip through the admin section of my django app, but there it is.

@jwshive
Copy link
Author

jwshive commented Aug 10, 2024

I see that the latest version looks to have this fix already in, but when I pip install this module, it's not coming over.

/usr/local/lib/python3.12/site-packages/cidrfield/models.py, line 36, in from_db_value

                return None

            prefix = value[:4]

            value = value[4:-1]    # remove prefix and %

            if prefix == 'IPv4':

                max_length = ipaddress.IPV4LENGTH

            else:

                max_length = ipaddress.IPV6LENGTH

            string_ip = ipaddress.ip_address(int(value + '0' * (max_length-len(value)), 2)).__str__()
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

     …

            string_network = string_ip + '/{}'.format(len(value))

            return ipaddress.ip_network(string_network)

        def _get_prep_value(self, value):

            if value is None or value == '':

                return None

@jwshive
Copy link
Author

jwshive commented Aug 10, 2024

I feel foolish for not noticing earlier, pip install gets you 0.2.0 and not the release built off the master branch, which must have been updated after 11/2019.

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

1 participant