Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
History
-------

5.4.0 (2026-11-17)
++++++++++++++++++

* The minimum version of ``maxminddb`` is now 3.3.0. This version raises
``InvalidDatabaseError`` for more kinds of corrupt databases, and it fixes
memory-safety and free-threading bugs in the C extension. It also lets
``geoip2.database.Reader`` open a binary file object with ``MODE_AUTO``. See
the `maxminddb 3.3.0 release notes
<https://github.com/maxmind/MaxMind-DB-Reader-python/releases/tag/v3.3.0>`_.
* The type hint for the ``fileish`` argument of ``geoip2.database.Reader`` is
now ``maxminddb.types.DatabaseSource``. With ``MODE_FD`` or ``MODE_AUTO``,
it accepts any object whose ``read()`` method returns ``bytes``, such as a
``gzip.GzipFile``. It no longer accepts a text file object, which never
worked.

5.3.0 (2026-07-16)
++++++++++++++++++

Expand Down
44 changes: 22 additions & 22 deletions mise.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[project]
name = "geoip2"
version = "5.3.0"
version = "5.4.0"
description = "MaxMind GeoIP API"
authors = [
{name = "Gregory Oschwald", email = "goschwald@maxmind.com"},
]
dependencies = [
"aiohttp>=3.14.1,<4.0.0",
"maxminddb>=3.0.0,<4.0.0",
"maxminddb>=3.3.0,<4.0.0",
"requests>=2.24.0,<3.0.0",
]
requires-python = ">=3.10"
Expand Down
22 changes: 11 additions & 11 deletions src/geoip2/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import inspect
from typing import IO, TYPE_CHECKING, AnyStr, cast
from typing import TYPE_CHECKING, cast

import maxminddb
from maxminddb import (
Expand All @@ -21,9 +21,9 @@
import geoip2.models

if TYPE_CHECKING:
import os
from collections.abc import Sequence

from maxminddb.types import DatabaseSource
from typing_extensions import Self

from geoip2.models import (
Expand Down Expand Up @@ -57,7 +57,7 @@ class Reader:
IP addresses can be looked up using the ``country`` and ``city`` methods.

The basic API for this class is the same for every database. First, you
create a reader object, specifying a file name or file descriptor.
create a reader object, specifying a path or a binary file object.
You then call the method corresponding to the specific database, passing
it the IP address you want to look up.

Expand All @@ -75,17 +75,16 @@ class Reader:

def __init__(
self,
fileish: (
AnyStr | int | os.PathLike[str] | os.PathLike[bytes] | IO[str] | IO[bytes]
),
fileish: DatabaseSource,
locales: Sequence[str] | None = None,
mode: int = MODE_AUTO,
) -> None:
"""Create GeoIP Reader.

:param fileish: A path to the GeoIP database or an existing file
descriptor pointing to the database. Note that a file descriptor
is only valid when mode is MODE_FD.
:param fileish: A path to the GeoIP database, or a binary file object
for MODE_FD or MODE_AUTO. MODE_MMAP, MODE_FILE, and MODE_MEMORY also
accept the file descriptor of a regular file. See
``maxminddb.open_database`` for details.
:param locales: This is list of locale codes. This argument will be
passed on to record classes to use when their name properties are
called. The default value is ['en'].
Expand Down Expand Up @@ -115,9 +114,10 @@ def __init__(
* MODE_MMAP - read from memory map. Pure Python.
* MODE_FILE - read database as standard file. Pure Python.
* MODE_MEMORY - load database into memory. Pure Python.
* MODE_FD - the param passed via fileish is a file descriptor, not a
path. This mode implies MODE_MEMORY. Pure Python.
* MODE_FD - the param passed via fileish is a binary file object,
not a path. This mode implies MODE_MEMORY. Pure Python.
* MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
With a file object, read it into memory, as MODE_FD does.
Default.

"""
Expand Down
9 changes: 9 additions & 0 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ipaddress
import sys
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch

sys.path.append("..")
Expand Down Expand Up @@ -305,6 +306,14 @@ def test_context_manager(self) -> None:
ipaddress.ip_address("81.2.69.160"),
)

def test_file_object_in_auto_mode(self) -> None:
with (
Path("tests/data/test-data/GeoIP2-City-Test.mmdb").open("rb") as f,
geoip2.database.Reader(f) as reader,
):
record = reader.city("81.2.69.160")
self.assertEqual(record.country.iso_code, "GB")

@patch("maxminddb.open_database")
def test_modes(self, mock_open: MagicMock) -> None:
mock_open.return_value = MagicMock()
Expand Down
Loading
Loading