Skip to content

Commit

Permalink
delay checking for pyproj data directory until importing pyproj (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored Aug 29, 2019
1 parent be0ddab commit 8120142
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
2.3.1
~~~~~
* Added cleanup for internal PROJ errors (issue #413)
* Delay checking for pyproj data directory until importing pyproj (issue #415)

2.3.0
~~~~~
Expand Down
3 changes: 3 additions & 0 deletions pyproj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
]
import sys

from pyproj._datadir import PYPROJ_CONTEXT
from pyproj._list import ( # noqa: F401
get_angular_units_map,
get_ellps_map,
Expand All @@ -80,6 +81,8 @@
from pyproj.proj import Proj, pj_list, proj_version_str # noqa: F401
from pyproj.transformer import Transformer, itransform, transform # noqa: F401

PYPROJ_CONTEXT.set_search_paths()


def test(**kwargs):
"""run the examples in the docstrings using the doctest module"""
Expand Down
1 change: 1 addition & 0 deletions pyproj/_datadir.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cdef ContextManager PROJ_CONTEXT

cdef class ContextManager:
cdef PJ_CONTEXT *context
cdef object _set_search_paths
7 changes: 5 additions & 2 deletions pyproj/_datadir.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ cdef class ContextManager:

def __init__(self):
self.context = proj_context_create()
self.set_search_paths()
self._set_search_paths = False
proj_context_use_proj4_init_rules(self.context, 1)
proj_log_func(self.context, NULL, pyproj_log_function)

def set_search_paths(self):
def set_search_paths(self, reset=False):
"""
This method sets the search paths
based on pyproj.datadir.get_data_dir()
"""
if self._set_search_paths and not reset:
return
data_dir_list = get_data_dir().split(os.pathsep)
cdef char **c_data_dir = <char **>malloc(len(data_dir_list) * sizeof(char*))
try:
Expand All @@ -43,6 +45,7 @@ cdef class ContextManager:
proj_context_set_search_paths(self.context, len(data_dir_list), c_data_dir)
finally:
free(c_data_dir)
self._set_search_paths = True


cdef ContextManager PROJ_CONTEXT = ContextManager()
Expand Down
2 changes: 1 addition & 1 deletion pyproj/datadir.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def set_data_dir(proj_data_dir):
# reset search paths
from pyproj._datadir import PYPROJ_CONTEXT

PYPROJ_CONTEXT.set_search_paths()
PYPROJ_CONTEXT.set_search_paths(reset=True)


def append_data_dir(proj_data_dir):
Expand Down
9 changes: 8 additions & 1 deletion test/test_datadir.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import shutil
import tempfile
import unittest
from contextlib import contextmanager

import pytest
from mock import patch

from pyproj import CRS
from pyproj._datadir import ContextManager
from pyproj.datadir import DataDirError, append_data_dir, get_data_dir, set_data_dir


Expand Down Expand Up @@ -71,6 +71,13 @@ def test_get_data_dir__missing():
assert get_data_dir() is None


def test_condext_manager_datadir_missing():
with proj_env(), pytest.raises(DataDirError), patch(
"pyproj._datadir.get_data_dir", side_effect=DataDirError("test")
):
ContextManager().set_search_paths()


def test_get_data_dir__from_user():
with proj_env(), temporary_directory() as tmpdir, patch(
"pyproj.datadir.os"
Expand Down

0 comments on commit 8120142

Please sign in to comment.