github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/GitPullS3/pygit2/py3.py (about) 1 # -*- coding: utf-8 -*- 2 # 3 # Copyright 2010-2015 The pygit2 contributors 4 # 5 # This file is free software; you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License, version 2, 7 # as published by the Free Software Foundation. 8 # 9 # In addition to the permissions in the GNU General Public License, 10 # the authors give you unlimited permission to link the compiled 11 # version of this file into combinations with other programs, 12 # and to distribute those combinations without any restriction 13 # coming from the use of this file. (The General Public License 14 # restrictions do apply in other respects; for example, they cover 15 # modification of the file, and distribution when not linked into 16 # a combined executable.) 17 # 18 # This file is distributed in the hope that it will be useful, but 19 # WITHOUT ANY WARRANTY; without even the implied warranty of 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 # General Public License for more details. 22 # 23 # You should have received a copy of the GNU General Public License 24 # along with this program; see the file COPYING. If not, write to 25 # the Free Software Foundation, 51 Franklin Street, Fifth Floor, 26 # Boston, MA 02110-1301, USA. 27 28 # Import from the future 29 from __future__ import absolute_import 30 31 # Import from pygit2 32 from .ffi import ffi 33 34 35 def to_bytes(s, encoding='utf-8', errors='strict'): 36 if s == ffi.NULL or s is None: 37 return ffi.NULL 38 39 if isinstance(s, bytes): 40 return s 41 42 return s.encode(encoding, errors) 43 44 45 def is_string(s): 46 return isinstance(s, str) 47 48 49 def to_str(s): 50 if type(s) is str: 51 return s 52 53 if type(s) is bytes: 54 return s.decode() 55 56 raise TypeError('unexpected type "%s"' % repr(s))