Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove LoadError/DumpError in favour of plain ValueError.

This is what the stdlib json module uses, and it seems appropriate.
Custom error classes don't really buy us anything.
  • Loading branch information...
commit 68d27317f51130e2995dfda8f1887815279276af 1 parent aee8a7b
@rfk authored
Showing with 25 additions and 86 deletions.
  1. +17 −34 tnetstring/__init__.py
  2. +6 −50 tnetstring/_tnetstring.c
  3. +2 −2 tnetstring/dbg.h
View
51 tnetstring/__init__.py
@@ -52,20 +52,6 @@
from collections import deque
-class Error(Exception):
- """Base error class for the tnetstring module."""
- pass
-
-class LoadError(Error):
- """Error class raised when there's a problem loading a tnetstring."""
- pass
-
-class DumpError(Error):
- """Error class raised when there's a problem dumping a tnetstring."""
- pass
-
-
-
def dumps(value):
"""dumps(object) -> string
@@ -166,7 +152,7 @@ def _rdumpq(q,size,value):
write(span)
return size + 1 + len(span)
else:
- raise DumpError("unserializable object")
+ raise ValueError("unserializable object")
def _gdumps(value):
@@ -221,7 +207,7 @@ def _gdumps(value):
yield sub
yield "}"
else:
- raise DumpError("unserializable object")
+ raise ValueError("unserializable object")
def loads(string):
@@ -245,20 +231,20 @@ def load(file):
# Read the length prefix one char at a time.
c = file.read(1)
if not c.isdigit():
- raise LoadError("not a tnetstring: missing or invalid length prefix")
+ raise ValueError("not a tnetstring: missing or invalid length prefix")
datalen = ord(c) - ord("0")
c = file.read(1)
while c.isdigit():
datalen = (10 * datalen) + (ord(c) - ord("0"))
c = file.read(1)
if c != ":":
- raise LoadError("not a tnetstring: missing or invalid length prefix")
+ raise ValueError("not a tnetstring: missing or invalid length prefix")
# Now we can read and parse the payload.
# This repeats the dispatch logic of pop() so we can avoid
# re-constructing the outermost tnetstring.
data = file.read(datalen)
if len(data) != datalen:
- raise LoadError("not a tnetstring: length prefix too big")
+ raise ValueError("not a tnetstring: length prefix too big")
type = file.read(1)
if type == ",":
return data
@@ -267,22 +253,22 @@ def load(file):
try:
return float(data)
except ValueError:
- raise LoadError("not a tnetstring: invalid float literal")
+ raise ValueError("not a tnetstring: invalid float literal")
else:
try:
return int(data)
except ValueError:
- raise LoadError("not a tnetstring: invalid integer literal")
+ raise ValueError("not a tnetstring: invalid integer literal")
if type == "!":
if data == "true":
return True
elif data == "false":
return False
else:
- raise LoadError("not a tnetstring: invalid boolean literal")
+ raise ValueError("not a tnetstring: invalid boolean literal")
if type == "~":
if data:
- raise LoadError("not a tnetstring: invalid null literal")
+ raise ValueError("not a tnetstring: invalid null literal")
return None
if type == "]":
l = []
@@ -297,7 +283,7 @@ def load(file):
(val,data) = pop(data)
d[key] = val
return d
- raise LoadError("unknown type tag")
+ raise ValueError("unknown type tag")
@@ -313,13 +299,13 @@ def pop(string):
(dlen,rest) = string.split(":",1)
dlen = int(dlen)
except ValueError:
- raise LoadError("not a tnetstring: missing or invalid length prefix")
+ raise ValueError("not a tnetstring: missing or invalid length prefix")
try:
(data,type,remain) = (rest[:dlen],rest[dlen],rest[dlen+1:])
except IndexError:
# This fires if len(rest) < dlen, meaning we don't need
# to further validate that data is the right length.
- raise LoadError("not a tnetstring: invalid length prefix")
+ raise ValueError("not a tnetstring: invalid length prefix")
# Parse the data based on the type tag.
if type == ",":
return (data,remain)
@@ -328,22 +314,22 @@ def pop(string):
try:
return (float(data),remain)
except ValueError:
- raise LoadError("not a tnetstring: invalid float literal")
+ raise ValueError("not a tnetstring: invalid float literal")
else:
try:
return (int(data),remain)
except ValueError:
- raise LoadError("not a tnetstring: invalid integer literal")
+ raise ValueError("not a tnetstring: invalid integer literal")
if type == "!":
if data == "true":
return (True,remain)
elif data == "false":
return (False,remain)
else:
- raise LoadError("not a tnetstring: invalid boolean literal")
+ raise ValueError("not a tnetstring: invalid boolean literal")
if type == "~":
if data:
- raise LoadError("not a tnetstring: invalid null literal")
+ raise ValueError("not a tnetstring: invalid null literal")
return (None,remain)
if type == "]":
l = []
@@ -358,7 +344,7 @@ def pop(string):
(val,data) = pop(data)
d[key] = val
return (d,remain)
- raise LoadError("unknown type tag")
+ raise ValueError("unknown type tag")
@@ -368,9 +354,6 @@ def pop(string):
except ImportError:
pass
else:
- Error = _tnetstring.Error
- LoadError = _tnetstring.LoadError
- DumpError = _tnetstring.DumpError
dumps = _tnetstring.dumps
load = _tnetstring.load
loads = _tnetstring.loads
View
56 tnetstring/_tnetstring.c
@@ -11,10 +11,6 @@
#include <Python.h>
-static PyObject *_tnetstring_Error;
-static PyObject *_tnetstring_LoadError;
-static PyObject *_tnetstring_DumpError;
-
#include "tns_core.c"
@@ -79,14 +75,14 @@ _tnetstring_load(PyObject* self, PyObject *args)
}
Py_INCREF(res);
if(!PyString_Check(res) || !PyString_GET_SIZE(res)) {
- PyErr_SetString(_tnetstring_Error,
+ PyErr_SetString(PyExc_ValueError,
"Not a tnetstring: invlaid or missing length prefix");
goto error;
}
c = PyString_AS_STRING(res)[0];
Py_DECREF(res); res = NULL;
if(c < '0' || c > '9') {
- PyErr_SetString(_tnetstring_Error,
+ PyErr_SetString(PyExc_ValueError,
"Not a tnetstring: invlaid or missing length prefix");
goto error;
}
@@ -98,7 +94,7 @@ _tnetstring_load(PyObject* self, PyObject *args)
}
Py_INCREF(res);
if(!PyString_Check(res) || !PyString_GET_SIZE(res)) {
- PyErr_SetString(_tnetstring_Error,
+ PyErr_SetString(PyExc_ValueError,
"Not a tnetstring: invlaid or missing length prefix");
goto error;
}
@@ -108,7 +104,7 @@ _tnetstring_load(PyObject* self, PyObject *args)
// Validate end-of-length-prefix marker.
if(c != ':') {
- PyErr_SetString(_tnetstring_Error,
+ PyErr_SetString(PyExc_ValueError,
"Not a tnetstring: missing length prefix");
goto error;
}
@@ -127,7 +123,7 @@ _tnetstring_load(PyObject* self, PyObject *args)
Py_DECREF(methnm); methnm = NULL;
Py_DECREF(metharg); metharg = NULL;
if(!PyString_Check(res) || PyString_GET_SIZE(res) != datalen + 1) {
- PyErr_SetString(_tnetstring_Error,
+ PyErr_SetString(PyExc_ValueError,
"Not a tnetstring: invalid length prefix");
goto error;
}
@@ -262,52 +258,12 @@ PyDoc_STRVAR(module_doc,
PyMODINIT_FUNC
init_tnetstring(void)
{
- PyObject *m;
-
- m = Py_InitModule3("_tnetstring", _tnetstring_methods, module_doc);
- if(m == NULL) {
- return;
- }
-
- _tnetstring_Error = PyErr_NewException("_tnetstring.Error", NULL, NULL);
- if(_tnetstring_Error == NULL) {
- return;
- }
- Py_INCREF(_tnetstring_Error);
- PyModule_AddObject(m, "Error", _tnetstring_Error);
-
- _tnetstring_LoadError = PyErr_NewException("_tnetstring.LoadError",
- _tnetstring_Error,NULL);
- if(_tnetstring_LoadError == NULL) {
- return;
- }
- Py_INCREF(_tnetstring_LoadError);
- PyModule_AddObject(m, "LoadError", _tnetstring_LoadError);
-
- _tnetstring_DumpError = PyErr_NewException("_tnetstring.DumpError",
- _tnetstring_Error,NULL);
- if(_tnetstring_DumpError == NULL) {
- return;
- }
- Py_INCREF(_tnetstring_DumpError);
- PyModule_AddObject(m, "DumpError", _tnetstring_DumpError);
+ Py_InitModule3("_tnetstring", _tnetstring_methods, module_doc);
}
// Functions to hook the parser core up to python.
-static inline void
-tns_parse_error(const char *errstr)
-{
- PyErr_SetString(_tnetstring_LoadError, errstr);
-}
-
-static inline void
-tns_render_error(const char *errstr)
-{
- PyErr_SetString(_tnetstring_DumpError, errstr);
-}
-
static inline void*
tns_parse_string(const char *data, size_t len)
{
View
4 tnetstring/dbg.h
@@ -9,10 +9,10 @@
#ifndef __dbg_h__
#define __dbg_h__
-#define check(A, M, ...) if(!(A)) { PyErr_Format(_tnetstring_Error, M, ##__VA_ARGS__); goto error; }
+#define check(A, M, ...) if(!(A)) { PyErr_Format(PyExc_ValueError, M, ##__VA_ARGS__); goto error; }
#define sentinel(M, ...) check(0, M, ##__VA_ARGS__)
-#define check_mem(A) check((A), "Out of memory.")
+#define check_mem(A) if(A==NULL) { PyErr_SetString(PyExc_MemoryError, "Out of memory."); goto error; }
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.