mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-07 17:08:42 +00:00
exceptions
This commit is contained in:
parent
17ea0c2e6b
commit
ae72c2c902
3 changed files with 49 additions and 41 deletions
|
@ -84,11 +84,6 @@ using namespace libtorrent;
|
||||||
|
|
||||||
#define DHT_ROUTER_PORT 6881
|
#define DHT_ROUTER_PORT 6881
|
||||||
|
|
||||||
#define ERROR_INVALID_ENCODING -10
|
|
||||||
#define ERROR_FILESYSTEM -20
|
|
||||||
#define ERROR_DUPLICATE_TORRENT -30
|
|
||||||
#define ERROR_INVALID_TORRENT -40
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------
|
//-----------------
|
||||||
// TYPES
|
// TYPES
|
||||||
|
@ -122,13 +117,18 @@ torrents_t *M_torrents = NULL;
|
||||||
|
|
||||||
|
|
||||||
//------------------------
|
//------------------------
|
||||||
// Exception type & macro
|
// Exception types & macro
|
||||||
//------------------------
|
//------------------------
|
||||||
|
|
||||||
static PyObject *PyTorrentError;
|
static PyObject *PyTorrentError;
|
||||||
|
|
||||||
#define PYTORRENT_RAISE_PTR(c) { printf("Raising error: %s\r\n", c); PyErr_SetString(PyTorrentError, c); return NULL; }
|
static PyObject *InvalidEncodingError;
|
||||||
#define PYTORRENT_RAISE_INT(c) { printf("Raising error: %s\r\n", c); PyErr_SetString(PyTorrentError, c); return -1; }
|
static PyObject *FilesystemError;
|
||||||
|
static PyObject *DuplicateTorrentError;
|
||||||
|
static PyObject *InvalidTorrentError;
|
||||||
|
|
||||||
|
#define PYTORRENT_RAISE_PTR(e,s) { printf("Raising error: %s\r\n", s); PyErr_SetString(e, s); return NULL; }
|
||||||
|
#define PYTORRENT_RAISE_INT(e,s) { printf("Raising error: %s\r\n", s); PyErr_SetString(e, s); return -1; }
|
||||||
|
|
||||||
|
|
||||||
//---------------------
|
//---------------------
|
||||||
|
@ -158,7 +158,7 @@ long get_torrent_index(torrent_handle &handle)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
PYTORRENT_RAISE_INT("Handle not found.");
|
PYTORRENT_RAISE_INT(PyTorrentError, "Handle not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
long get_index_from_unique_ID(long unique_ID)
|
long get_index_from_unique_ID(long unique_ID)
|
||||||
|
@ -169,7 +169,7 @@ long get_index_from_unique_ID(long unique_ID)
|
||||||
if ((*M_torrents)[i].unique_ID == unique_ID)
|
if ((*M_torrents)[i].unique_ID == unique_ID)
|
||||||
return i;
|
return i;
|
||||||
|
|
||||||
PYTORRENT_RAISE_INT("No such unique_ID.");
|
PYTORRENT_RAISE_INT(PyTorrentError, "No such unique_ID.");
|
||||||
}
|
}
|
||||||
|
|
||||||
long internal_add_torrent(std::string const& torrent_name,
|
long internal_add_torrent(std::string const& torrent_name,
|
||||||
|
@ -179,7 +179,9 @@ long internal_add_torrent(std::string const& torrent_name,
|
||||||
{
|
{
|
||||||
std::ifstream in(torrent_name.c_str(), std::ios_base::binary);
|
std::ifstream in(torrent_name.c_str(), std::ios_base::binary);
|
||||||
in.unsetf(std::ios_base::skipws);
|
in.unsetf(std::ios_base::skipws);
|
||||||
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
entry e;
|
||||||
|
e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
||||||
|
|
||||||
torrent_info t(e);
|
torrent_info t(e);
|
||||||
|
|
||||||
entry resume_data;
|
entry resume_data;
|
||||||
|
@ -191,8 +193,7 @@ long internal_add_torrent(std::string const& torrent_name,
|
||||||
resumeFile.unsetf(std::ios_base::skipws);
|
resumeFile.unsetf(std::ios_base::skipws);
|
||||||
resume_data = bdecode(std::istream_iterator<char>(resumeFile),
|
resume_data = bdecode(std::istream_iterator<char>(resumeFile),
|
||||||
std::istream_iterator<char>());
|
std::istream_iterator<char>());
|
||||||
}
|
} catch (invalid_encoding&) {}
|
||||||
catch (invalid_encoding&) {}
|
|
||||||
catch (boost::filesystem::filesystem_error&) {}
|
catch (boost::filesystem::filesystem_error&) {}
|
||||||
|
|
||||||
// Create new torrent object
|
// Create new torrent object
|
||||||
|
@ -200,6 +201,7 @@ long internal_add_torrent(std::string const& torrent_name,
|
||||||
torrent_t new_torrent;
|
torrent_t new_torrent;
|
||||||
|
|
||||||
torrent_handle h = M_ses->add_torrent(t, save_path, resume_data, compact_mode, 16 * 1024);
|
torrent_handle h = M_ses->add_torrent(t, save_path, resume_data, compact_mode, 16 * 1024);
|
||||||
|
|
||||||
// h.set_max_connections(60); // at some point we should use this
|
// h.set_max_connections(60); // at some point we should use this
|
||||||
h.set_max_uploads(-1);
|
h.set_max_uploads(-1);
|
||||||
h.set_ratio(preferred_ratio);
|
h.set_ratio(preferred_ratio);
|
||||||
|
@ -333,7 +335,7 @@ static PyObject *torrent_init(PyObject *self, PyObject *args)
|
||||||
M_ses->set_settings(*M_settings);
|
M_ses->set_settings(*M_settings);
|
||||||
M_ses->set_severity_level(alert::debug);
|
M_ses->set_severity_level(alert::debug);
|
||||||
|
|
||||||
M_constants = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i}",
|
M_constants = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i}",
|
||||||
"EVENT_NULL", EVENT_NULL,
|
"EVENT_NULL", EVENT_NULL,
|
||||||
"EVENT_FINISHED", EVENT_FINISHED,
|
"EVENT_FINISHED", EVENT_FINISHED,
|
||||||
"EVENT_PEER_ERROR", EVENT_PEER_ERROR,
|
"EVENT_PEER_ERROR", EVENT_PEER_ERROR,
|
||||||
|
@ -351,11 +353,7 @@ static PyObject *torrent_init(PyObject *self, PyObject *args)
|
||||||
"STATE_DOWNLOADING", STATE_DOWNLOADING,
|
"STATE_DOWNLOADING", STATE_DOWNLOADING,
|
||||||
"STATE_FINISHED", STATE_FINISHED,
|
"STATE_FINISHED", STATE_FINISHED,
|
||||||
"STATE_SEEDING", STATE_SEEDING,
|
"STATE_SEEDING", STATE_SEEDING,
|
||||||
"STATE_ALLOCATING", STATE_ALLOCATING,
|
"STATE_ALLOCATING", STATE_ALLOCATING);
|
||||||
"ERROR_INVALID_ENCODING", ERROR_INVALID_ENCODING,
|
|
||||||
"ERROR_INVALID_TORRENT", ERROR_INVALID_TORRENT,
|
|
||||||
"ERROR_FILESYSTEM", ERROR_FILESYSTEM,
|
|
||||||
"ERROR_DUPLICATE_TORRENT", ERROR_DUPLICATE_TORRENT);
|
|
||||||
|
|
||||||
Py_INCREF(Py_None); return Py_None;
|
Py_INCREF(Py_None); return Py_None;
|
||||||
};
|
};
|
||||||
|
@ -469,24 +467,20 @@ static PyObject *torrent_add_torrent(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Py_BuildValue("i", internal_add_torrent(name, 0, compact, save_dir_2));
|
long ret = internal_add_torrent(name, 0, compact, save_dir_2);
|
||||||
|
if (PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
return Py_BuildValue("i", ret);
|
||||||
}
|
}
|
||||||
catch (invalid_encoding&)
|
catch (invalid_encoding&)
|
||||||
{
|
{ PYTORRENT_RAISE_PTR(InvalidEncodingError, ""); }
|
||||||
return Py_BuildValue("i", ERROR_INVALID_ENCODING);
|
|
||||||
}
|
|
||||||
catch (invalid_torrent_file&)
|
catch (invalid_torrent_file&)
|
||||||
{
|
{ PYTORRENT_RAISE_PTR(InvalidTorrentError, ""); }
|
||||||
return Py_BuildValue("i", ERROR_INVALID_TORRENT);
|
|
||||||
}
|
|
||||||
catch (boost::filesystem::filesystem_error&)
|
catch (boost::filesystem::filesystem_error&)
|
||||||
{
|
{ PYTORRENT_RAISE_PTR(FilesystemError, ""); }
|
||||||
return Py_BuildValue("i", ERROR_FILESYSTEM);
|
|
||||||
}
|
|
||||||
catch (duplicate_torrent&)
|
catch (duplicate_torrent&)
|
||||||
{
|
{ PYTORRENT_RAISE_PTR(DuplicateTorrentError, ""); }
|
||||||
return Py_BuildValue("i", ERROR_DUPLICATE_TORRENT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *torrent_remove_torrent(PyObject *self, PyObject *args)
|
static PyObject *torrent_remove_torrent(PyObject *self, PyObject *args)
|
||||||
|
@ -1099,8 +1093,9 @@ static PyObject *torrent_create_torrent(PyObject *self, PyObject *args)
|
||||||
return Py_BuildValue("l", 1);
|
return Py_BuildValue("l", 1);
|
||||||
} catch (std::exception& e)
|
} catch (std::exception& e)
|
||||||
{
|
{
|
||||||
std::cerr << e.what() << "\n";
|
// std::cerr << e.what() << "\n";
|
||||||
return Py_BuildValue("l", 0);
|
// return Py_BuildValue("l", 0);
|
||||||
|
PYTORRENT_RAISE_PTR(PyTorrentError, e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1190,7 +1185,20 @@ initpytorrent(void)
|
||||||
PyObject *m, *d;
|
PyObject *m, *d;
|
||||||
|
|
||||||
m = Py_InitModule("pytorrent", pytorrent_methods);
|
m = Py_InitModule("pytorrent", pytorrent_methods);
|
||||||
d = PyModule_GetDict(m);
|
|
||||||
PyTorrentError = PyErr_NewException("pytorrent.Error", NULL, NULL);
|
PyTorrentError = PyErr_NewException("pytorrent.Error", NULL, NULL);
|
||||||
PyDict_SetItemString(d, "error", PyTorrentError);
|
|
||||||
|
InvalidEncodingError = PyErr_NewException("pytorrent.InvalidEncodingError", NULL, NULL);
|
||||||
|
FilesystemError = PyErr_NewException("pytorrent.FilesystemError", NULL, NULL);
|
||||||
|
DuplicateTorrentError = PyErr_NewException("pytorrent.DuplicateTorrentError", NULL, NULL);
|
||||||
|
InvalidTorrentError = PyErr_NewException("pytorrent.InvalidTorrentError", NULL, NULL);
|
||||||
|
|
||||||
|
d = PyModule_GetDict(m);
|
||||||
|
|
||||||
|
PyDict_SetItemString(d, "PyTorrentError", PyTorrentError);
|
||||||
|
|
||||||
|
PyDict_SetItemString(d, "InvalidEncodingError", InvalidEncodingError);
|
||||||
|
PyDict_SetItemString(d, "FilesystemError", FilesystemError);
|
||||||
|
PyDict_SetItemString(d, "DuplicateTorrentError", DuplicateTorrentError);
|
||||||
|
PyDict_SetItemString(d, "InvalidTorrentError", InvalidTorrentError);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#
|
#
|
||||||
# Copyright © 2006 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>
|
# Copyright (c) 2006 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>
|
||||||
#
|
#
|
||||||
# 2006-15-9
|
# 2006-15-9
|
||||||
#
|
#
|
||||||
|
|
|
@ -12,13 +12,13 @@
|
||||||
import pytorrent
|
import pytorrent
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
pytorrent.init("PT", 0, 5, 0, 0, "pytorrent")
|
pytorrent.init("PT", 0, 5, 0, 0, "pytorrent - testing only")
|
||||||
|
|
||||||
myTorrent = pytorrent.addTorrent("ubuntu.torrent")
|
myTorrent = pytorrent.add_torrent("ubuntu.torrent", ".", True)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print "STATE:"
|
print "STATE:"
|
||||||
print pytorrent.getState(myTorrent)
|
print pytorrent.get_state(myTorrent)
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue