fix python binding for create_torrent

This commit is contained in:
Arvid Norberg 2019-03-23 11:27:48 +01:00 committed by Arvid Norberg
parent 3a4a7b742a
commit 38ca4113ea
4 changed files with 42 additions and 4 deletions

View File

@ -1,3 +1,4 @@
* fix create_torrent python binding
* update symlinks to conform to BEP 47
* fix python bindings for peer_info
* support creating symlinks, for torrents with symlinks in them

View File

@ -140,14 +140,33 @@ struct to_string_view
static void* convertible(PyObject* x)
{
return PyUnicode_Check(x) ? x: nullptr;
#if PY_VERSION_HEX >= 0x03020000
return PyBytes_Check(x)
#else
return PyString_Check(x)
#endif
? x : PyUnicode_Check(x) ? x : nullptr;
}
static void construct(PyObject* x, converter::rvalue_from_python_stage1_data* data)
{
void* storage = ((converter::rvalue_from_python_storage<
lt::string_view>*)data)->storage.bytes;
data->convertible = new (storage) lt::string_view(PyUnicode_AS_DATA(x), PyUnicode_GET_DATA_SIZE(x));
if (PyUnicode_Check(x))
{
data->convertible = new (storage) lt::string_view(PyUnicode_AS_DATA(x), PyUnicode_GET_DATA_SIZE(x));
}
else
{
data->convertible = new (storage) lt::string_view(
#if PY_VERSION_HEX >= 0x03020000
PyBytes_AsString(x), PyBytes_Size(x)
#else
PyString_AsString(x), PyString_Size(x)
#endif
);
}
}
};
@ -264,12 +283,12 @@ struct list_to_bitfield
T p;
int const size = int(PyList_Size(x));
p.resize(size);
p.resize(size);
for (int i = 0; i < size; ++i)
{
object o(borrowed(PyList_GetItem(x, i)));
if (extract<bool>(o)) p.set_bit(IndexType{i});
else p.clear_bit(IndexType{i});
else p.clear_bit(IndexType{i});
}
data->convertible = new (storage) T(std::move(p));
}
@ -496,4 +515,5 @@ void bind_converters()
to_bitfield_flag<lt::create_flags_t>();
to_bitfield_flag<lt::pex_flags_t>();
to_bitfield_flag<lt::reannounce_flags_t>();
to_string_view();
}

View File

@ -229,6 +229,9 @@ void bind_create_torrent()
.def("piece_size", &create_torrent::piece_size)
.def("priv", &create_torrent::priv)
.def("set_root_cert", &create_torrent::set_root_cert, (arg("pem")))
.def("add_collection", &create_torrent::add_collection)
.def("add_similar_torrent", &create_torrent::add_similar_torrent)
;
s.attr("optimize_alignment") = create_torrent::optimize_alignment;

View File

@ -32,6 +32,7 @@ class test_create_torrent(unittest.TestCase):
def test_from_torrent_info(self):
ti = lt.torrent_info('unordered.torrent')
print(ti.ssl_cert())
ct = lt.create_torrent(ti)
entry = ct.generate()
content = lt.bencode(entry).strip()
@ -42,6 +43,19 @@ class test_create_torrent(unittest.TestCase):
print(entry)
self.assertEqual(content, file_content)
def test_from_scratch(self):
fs = lt.file_storage()
fs.add_file('test/file1', 1000)
fs.add_file('test/file2', 2000)
ct = lt.create_torrent(fs)
ct.add_url_seed('foo')
ct.add_http_seed('bar')
ct.add_tracker('bar')
ct.set_root_cert('1234567890')
ct.add_collection('1337')
entry = ct.generate()
print(entry)
class test_session_stats(unittest.TestCase):