Skip to content

Commit

Permalink
internal: rename "to_str" to "to_bytes" for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavid committed Jul 28, 2014
1 parent 9882044 commit 7df11bf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 57 deletions.
20 changes: 10 additions & 10 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from .config import Config
from .index import Index, IndexEntry
from .errors import check_error
from .ffi import ffi, C, to_str
from .ffi import ffi, C, to_bytes


def init_repository(path, bare=False,
Expand Down Expand Up @@ -84,15 +84,15 @@ def init_repository(path, bare=False,
options.version = 1
options.flags = flags
options.mode = mode
options.workdir_path = to_str(workdir_path)
options.description = to_str(description)
options.template_path = to_str(template_path)
options.initial_head = to_str(initial_head)
options.origin_url = to_str(origin_url)
options.workdir_path = to_bytes(workdir_path)
options.description = to_bytes(description)
options.template_path = to_bytes(template_path)
options.initial_head = to_bytes(initial_head)
options.origin_url = to_bytes(origin_url)

# Call
crepository = ffi.new('git_repository **')
err = C.git_repository_init_ext(crepository, to_str(path), options)
err = C.git_repository_init_ext(crepository, to_bytes(path), options)
check_error(err)

# Ok
Expand Down Expand Up @@ -156,7 +156,7 @@ def clone_repository(
checkout_branch_ref = ffi.new('char []', branch)
opts.checkout_branch = checkout_branch_ref

remote_name_ref = ffi.new('char []', to_str(remote_name))
remote_name_ref = ffi.new('char []', to_bytes(remote_name))
opts.remote_name = remote_name_ref

opts.version = 1
Expand All @@ -168,7 +168,7 @@ def clone_repository(
opts.remote_callbacks.credentials = _credentials_cb
opts.remote_callbacks.payload = d_handle

err = C.git_clone(crepo, to_str(url), to_str(path), opts)
err = C.git_clone(crepo, to_bytes(url), to_bytes(path), opts)
C.git_repository_free(crepo[0])

if 'exception' in d:
Expand Down Expand Up @@ -196,7 +196,7 @@ def clone_into(repo, remote, branch=None):
"""

err = C.git_clone_into(repo._repo, remote._remote, ffi.NULL,
to_str(branch), ffi.NULL)
to_bytes(branch), ffi.NULL)

if remote._stored_exception:
raise remote._stored_exception
Expand Down
29 changes: 15 additions & 14 deletions pygit2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Import from the future
from __future__ import absolute_import, unicode_literals

from .ffi import ffi, C, to_str, is_string
from .ffi import ffi, C, to_bytes, is_string
from .errors import check_error


Expand Down Expand Up @@ -83,7 +83,7 @@ def __init__(self, path=None):
err = C.git_config_new(cconfig)
else:
assert_string(path, "path")
err = C.git_config_open_ondisk(cconfig, to_str(path))
err = C.git_config_open_ondisk(cconfig, to_bytes(path))

check_error(err, True)
self._config = cconfig[0]
Expand All @@ -103,7 +103,7 @@ def _get(self, key):
assert_string(key, "key")

cstr = ffi.new('char **')
err = C.git_config_get_string(cstr, self._config, to_str(key))
err = C.git_config_get_string(cstr, self._config, to_bytes(key))

return err, cstr

Expand Down Expand Up @@ -136,19 +136,19 @@ def __setitem__(self, key, value):

err = 0
if isinstance(value, bool):
err = C.git_config_set_bool(self._config, to_str(key), value)
err = C.git_config_set_bool(self._config, to_bytes(key), value)
elif isinstance(value, int):
err = C.git_config_set_int64(self._config, to_str(key), value)
err = C.git_config_set_int64(self._config, to_bytes(key), value)
else:
err = C.git_config_set_string(self._config, to_str(key),
to_str(value))
err = C.git_config_set_string(self._config, to_bytes(key),
to_bytes(value))

check_error(err)

def __delitem__(self, key):
assert_string(key, "key")

err = C.git_config_delete_entry(self._config, to_str(key))
err = C.git_config_delete_entry(self._config, to_bytes(key))
check_error(err)

def __iter__(self):
Expand All @@ -169,7 +169,8 @@ def get_multivar(self, name, regex=None):

citer = ffi.new('git_config_iterator **')
err = C.git_config_multivar_iterator_new(citer, self._config,
to_str(name), to_str(regex))
to_bytes(name),
to_bytes(regex))
check_error(err)

return ConfigMultivarIterator(self, citer[0])
Expand All @@ -184,8 +185,8 @@ def set_multivar(self, name, regex, value):
assert_string(regex, "regex")
assert_string(value, "value")

err = C.git_config_set_multivar(self._config, to_str(name),
to_str(regex), to_str(value))
err = C.git_config_set_multivar(self._config, to_bytes(name),
to_bytes(regex), to_bytes(value))
check_error(err)

def get_bool(self, key):
Expand Down Expand Up @@ -225,7 +226,7 @@ def add_file(self, path, level=0, force=0):
Add a config file instance to an existing config."""

err = C.git_config_add_file_ondisk(self._config, to_str(path), level,
err = C.git_config_add_file_ondisk(self._config, to_bytes(path), level,
force)
check_error(err)

Expand All @@ -249,15 +250,15 @@ def snapshot(self):
@staticmethod
def parse_bool(text):
res = ffi.new('int *')
err = C.git_config_parse_bool(res, to_str(text))
err = C.git_config_parse_bool(res, to_bytes(text))
check_error(err)

return res[0] != 0

@staticmethod
def parse_int(text):
res = ffi.new('int64_t *')
err = C.git_config_parse_int64(res, to_str(text))
err = C.git_config_parse_int64(res, to_bytes(text))
check_error(err)

return res[0]
Expand Down
6 changes: 3 additions & 3 deletions pygit2/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(major_version, _, _, _, _) = sys.version_info

if major_version < 3:
def to_str(s, encoding='utf-8', errors='strict'):
def to_bytes(s, encoding='utf-8', errors='strict'):
if s == ffi.NULL or s is None:
return ffi.NULL

Expand All @@ -47,7 +47,7 @@ def to_str(s, encoding='utf-8', errors='strict'):

return s
else:
def to_str(s, encoding='utf-8', errors='strict'):
def to_bytes(s, encoding='utf-8', errors='strict'):
if s == ffi.NULL or s is None:
return ffi.NULL

Expand Down Expand Up @@ -97,7 +97,7 @@ def strings_to_strarray(l):
if not is_string(l[i]):
raise TypeError("Value must be a string")

s = ffi.new('char []', to_str(l[i]))
s = ffi.new('char []', to_bytes(l[i]))
refs[i] = s
strings[i] = s

Expand Down
18 changes: 9 additions & 9 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from _pygit2 import Oid, Tree, Diff

from .ffi import ffi, C, to_str, is_string, strings_to_strarray
from .ffi import ffi, C, to_bytes, is_string, strings_to_strarray
from .errors import check_error


Expand All @@ -43,7 +43,7 @@ def __init__(self, path=None):
to read from and write to.
"""
cindex = ffi.new('git_index **')
err = C.git_index_open(cindex, to_str(path))
err = C.git_index_open(cindex, to_bytes(path))
check_error(err)

self._index = cindex[0]
Expand All @@ -69,7 +69,7 @@ def __len__(self):
return C.git_index_entrycount(self._index)

def __contains__(self, path):
err = C.git_index_find(ffi.NULL, self._index, to_str(path))
err = C.git_index_find(ffi.NULL, self._index, to_bytes(path))
if err == C.GIT_ENOTFOUND:
return False

Expand All @@ -79,7 +79,7 @@ def __contains__(self, path):
def __getitem__(self, key):
centry = ffi.NULL
if is_string(key):
centry = C.git_index_get_bypath(self._index, to_str(key), 0)
centry = C.git_index_get_bypath(self._index, to_bytes(key), 0)
elif not key >= 0:
raise ValueError(key)
else:
Expand Down Expand Up @@ -165,7 +165,7 @@ def write_tree(self, repo=None):
def remove(self, path):
"""Remove an entry from the Index.
"""
err = C.git_index_remove(self._index, to_str(path), 0)
err = C.git_index_remove(self._index, to_bytes(path), 0)
check_error(err, True)

def add_all(self, pathspecs=[]):
Expand Down Expand Up @@ -193,7 +193,7 @@ def add(self, path_or_entry):

if is_string(path_or_entry):
path = path_or_entry
err = C.git_index_add_bypath(self._index, to_str(path))
err = C.git_index_add_bypath(self._index, to_bytes(path))
elif isinstance(path_or_entry, IndexEntry):
entry = path_or_entry
centry, str_ref = entry._to_c()
Expand Down Expand Up @@ -345,7 +345,7 @@ def _to_c(self):
# basically memcpy()
ffi.buffer(ffi.addressof(centry, 'id'))[:] = self.id.raw[:]
centry.mode = self.mode
path = ffi.new('char[]', to_str(self.path))
path = ffi.new('char[]', to_bytes(self.path))
centry.path = path

return centry, path
Expand Down Expand Up @@ -394,7 +394,7 @@ def __getitem__(self, path):
ctheirs = ffi.new('git_index_entry **')

err = C.git_index_conflict_get(cancestor, cours, ctheirs,
self._index._index, to_str(path))
self._index._index, to_bytes(path))
check_error(err)

ancestor = IndexEntry._from_c(cancestor[0])
Expand All @@ -404,7 +404,7 @@ def __getitem__(self, path):
return ancestor, ours, theirs

def __delitem__(self, path):
err = C.git_index_conflict_remove(self._index._index, to_str(path))
err = C.git_index_conflict_remove(self._index._index, to_bytes(path))
check_error(err)

def __iter__(self):
Expand Down
8 changes: 4 additions & 4 deletions pygit2/refspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Import from the future
from __future__ import absolute_import

from .ffi import ffi, C, to_str
from .ffi import ffi, C, to_bytes
from .errors import check_error


Expand Down Expand Up @@ -66,18 +66,18 @@ def src_matches(self, ref):
"""src_matches(str) -> Bool
Returns whether the given string matches the source of this refspec"""
return bool(C.git_refspec_src_matches(self._refspec, to_str(ref)))
return bool(C.git_refspec_src_matches(self._refspec, to_bytes(ref)))

def dst_matches(self, ref):
"""dst_matches(str) -> Bool
Returns whether the given string matches the destination of this
refspec"""
return bool(C.git_refspec_dst_matches(self._refspec, to_str(ref)))
return bool(C.git_refspec_dst_matches(self._refspec, to_bytes(ref)))

def _transform(self, ref, fn):
buf = ffi.new('git_buf *', (ffi.NULL, 0))
err = fn(buf, self._refspec, to_str(ref))
err = fn(buf, self._refspec, to_bytes(ref))
check_error(err)

try:
Expand Down
26 changes: 13 additions & 13 deletions pygit2/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from _pygit2 import Oid

from .ffi import ffi, C, to_str, strarray_to_strings, strings_to_strarray
from .ffi import ffi, C, to_bytes, strarray_to_strings, strings_to_strarray
from .errors import check_error, GitError
from .refspec import Refspec

Expand Down Expand Up @@ -160,7 +160,7 @@ def rename(self, new_name):
raise ValueError("New remote name must be a non-empty string")

problems = ffi.new('git_strarray *')
err = C.git_remote_rename(problems, self._remote, to_str(new_name))
err = C.git_remote_rename(problems, self._remote, to_bytes(new_name))
check_error(err)

ret = strarray_to_strings(problems)
Expand All @@ -176,7 +176,7 @@ def url(self):

@url.setter
def url(self, value):
err = C.git_remote_set_url(self._remote, to_str(value))
err = C.git_remote_set_url(self._remote, to_bytes(value))
check_error(err)

@property
Expand All @@ -187,7 +187,7 @@ def push_url(self):

@push_url.setter
def push_url(self, value):
err = C.git_remote_set_pushurl(self._remote, to_str(value))
err = C.git_remote_set_pushurl(self._remote, to_bytes(value))
check_error(err)

def save(self):
Expand All @@ -210,7 +210,7 @@ def fetch(self, signature=None, message=None):
ptr = ffi.NULL

self._stored_exception = None
err = C.git_remote_fetch(self._remote, ptr, to_str(message))
err = C.git_remote_fetch(self._remote, ptr, to_bytes(message))
if self._stored_exception:
raise self._stored_exception

Expand Down Expand Up @@ -269,15 +269,15 @@ def add_fetch(self, spec):
Add a fetch refspec to the remote"""

err = C.git_remote_add_fetch(self._remote, to_str(spec))
err = C.git_remote_add_fetch(self._remote, to_bytes(spec))
check_error(err)

def add_push(self, spec):
"""add_push(refspec)
Add a push refspec to the remote"""

err = C.git_remote_add_push(self._remote, to_str(spec))
err = C.git_remote_add_push(self._remote, to_bytes(spec))
check_error(err)

@ffi.callback("int (*cb)(const char *ref, const char *msg, void *data)")
Expand All @@ -304,7 +304,7 @@ def push(self, spec, signature=None, message=None):
push = cpush[0]

try:
err = C.git_push_add_refspec(push, to_str(spec))
err = C.git_push_add_refspec(push, to_bytes(spec))
check_error(err)

err = C.git_push_finish(push)
Expand All @@ -325,7 +325,7 @@ def push(self, spec, signature=None, message=None):
else:
ptr = ffi.NULL

err = C.git_push_update_tips(push, ptr, to_str(message))
err = C.git_push_update_tips(push, ptr, to_bytes(message))
check_error(err)

finally:
Expand Down Expand Up @@ -426,13 +426,13 @@ def get_credentials(fn, url, username, allowed):
ccred = ffi.new('git_cred **')
if cred_type == C.GIT_CREDTYPE_USERPASS_PLAINTEXT:
name, passwd = creds.credential_tuple
err = C.git_cred_userpass_plaintext_new(ccred, to_str(name),
to_str(passwd))
err = C.git_cred_userpass_plaintext_new(ccred, to_bytes(name),
to_bytes(passwd))

elif cred_type == C.GIT_CREDTYPE_SSH_KEY:
name, pubkey, privkey, passphrase = creds.credential_tuple
err = C.git_cred_ssh_key_new(ccred, to_str(name), to_str(pubkey),
to_str(privkey), to_str(passphrase))
err = C.git_cred_ssh_key_new(ccred, to_bytes(name), to_bytes(pubkey),
to_bytes(privkey), to_bytes(passphrase))

else:
raise TypeError("unsupported credential type")
Expand Down
Loading

0 comments on commit 7df11bf

Please sign in to comment.