Skip to content

Commit a7d4e1d

Browse files
committed
fix: pybind11::native_enum<...>("CompositeOp") is already registered
1 parent 6d9557b commit a7d4e1d

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

packaging/mapnik/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import itertools
4040
import os
41+
import sys
4142
import warnings
4243

4344
def bootstrap_env():
@@ -63,6 +64,22 @@ def bootstrap_env():
6364

6465
bootstrap_env()
6566

67+
# In some build/test setups the compiled extension can be imported under the
68+
# top-level name `_mapnik` (e.g. due to sys.path/build output layout) and then
69+
# again as `mapnik._mapnik`. Loading the same pybind11 extension twice in one
70+
# interpreter can raise errors like:
71+
# pybind11::native_enum<...>("CompositeOp") is already registered!
72+
# If a compatible `_mapnik` is already loaded, alias it to the canonical
73+
# `mapnik._mapnik` name before importing symbols.
74+
_canonical_ext_name = f"{__name__}._mapnik"
75+
if _canonical_ext_name in sys.modules:
76+
pass
77+
elif "_mapnik" in sys.modules:
78+
_candidate = sys.modules["_mapnik"]
79+
# Heuristic guard: only alias if it looks like our Mapnik extension.
80+
if hasattr(_candidate, "Map") and hasattr(_candidate, "version_string"):
81+
sys.modules[_canonical_ext_name] = _candidate
82+
6683
from ._mapnik import *
6784

6885
def Shapefile(**keywords):

0 commit comments

Comments
 (0)