Skip to content

Commit

Permalink
Use init_once around library initialization (#199)
Browse files Browse the repository at this point in the history
* Raise minimum required version of cffi

Require the fix release of 1.4, which is the first version providing
the init_once() call.

* Wrap library initialization whith cff.init_once()

and use the correct test as specified in libsodium docs. See #187
  • Loading branch information
lmctv authored and reaperhulk committed Dec 11, 2016
1 parent ae9707f commit b2408a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"upgrade PyPy to use this library."
)
else:
requirements.append("cffi>=1.1.0")
setup_requirements.append("cffi>=1.1.0")
requirements.append("cffi>=1.4.1")
setup_requirements.append("cffi>=1.4.1")


def here(*paths):
Expand Down
10 changes: 7 additions & 3 deletions src/nacl/bindings/sodium_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
# limitations under the License.
from __future__ import absolute_import, division, print_function

from nacl._sodium import lib
from nacl._sodium import ffi, lib
from nacl.exceptions import CryptoError


def _sodium_init():
if lib.sodium_init() == -1:
raise CryptoError("Could not initialize sodium")


def sodium_init():
"""
Initializes sodium, picking the best implementations available for this
machine.
"""
if lib.sodium_init() != 0:
raise CryptoError("Could not initialize sodium")
ffi.init_once(_sodium_init, "libsodium")

0 comments on commit b2408a3

Please sign in to comment.