@@@ cython and python 3 wip
[mLib-python] / url.pyx
diff --git a/url.pyx b/url.pyx
index 72467f1..b620294 100644 (file)
--- a/url.pyx
+++ b/url.pyx
@@ -31,53 +31,56 @@ cdef class URLEncode:
   def __cinit__(me, *hunoz, **hukairz):
     url_initenc(&me.ctx)
     DCREATE(&me.d)
+  def __dealloc__(me):
+    dstr_destroy(&me.d)
   def __init__(me, strictp = False, laxp = False, semip = False):
     cdef unsigned f
     f = 0
     if strictp:
-      f = f | URLF_STRICT
+      f |= URLF_STRICT
     if laxp:
-      f = f | URLF_LAX
+      f |= URLF_LAX
     if semip:
-      f = f | URLF_SEMI
+      f |= URLF_SEMI
     me.ctx.f = f
-  def encode(me, char *name, char *value):
+  def encode(me, object name, object value):
     """UE.encode(NAME, VALUE): encode a key/value pair"""
-    url_enc(&me.ctx, &me.d, name, value)
+    url_enc(&me.ctx, &me.d, TEXT_PTR(name), TEXT_PTR(value))
     return me
-  property result:
+  @property
+  def result(me):
     """UE.result -> STR: the encoded string"""
-    def __get__(me):
-      return PyString_FromStringAndSize(me.d.buf, me.d.len)
-  property strictp:
+    return TEXT_FROMSTRLEN(me.d.buf, me.d.len)
+  @property
+  def strictp(me):
     """UE.strictp -> BOOL: strictly escape non-alphanumerics?"""
-    def __get__(me):
-      return _tobool(me.ctx.f & URLF_STRICT)
-    def __set__(me, val):
-      if val:
-        me.ctx.f = me.ctx.f | URLF_STRICT
-      else:
-        me.ctx.f = me.ctx.f & ~URLF_STRICT
-  property laxp:
+    return <bint>(me.ctx.f&URLF_STRICT)
+  @strictp.setter
+  def strictp(me, val):
+    if val:
+      me.ctx.f |= URLF_STRICT
+    else:
+      me.ctx.f &= ~URLF_STRICT
+  @property
+  def laxp(me):
     """UE.laxp -> BOOL: only escape obviously unsafe characters?"""
-    def __get__(me):
-      return _tobool(me.ctx.f & URLF_LAX)
-    def __set__(me, val):
-      if val:
-        me.ctx.f = me.ctx.f | URLF_LAX
-      else:
-        me.ctx.f = me.ctx.f & ~URLF_LAX
-  property semip:
+    return <bint>(me.ctx.f&URLF_LAX)
+  @laxp.setter
+  def laxp(me, val):
+    if val:
+      me.ctx.f |= URLF_LAX
+    else:
+      me.ctx.f &= ~URLF_LAX
+  @property
+  def semip(me):
     """UE.semip -> BOOL: separate key/value pairs with semicolons?"""
-    def __get__(me):
-      return _tobool(me.ctx.f & URLF_SEMI)
-    def __set__(me, val):
-      if val:
-        me.ctx.f = me.ctx.f | URLF_SEMI
-      else:
-        me.ctx.f = me.ctx.f & ~URLF_SEMI
-  def __del__(me):
-    dstr_destroy(&me.d)
+    return <bint>(me.ctx.f&URLF_SEMI)
+  @semip.setter
+  def semip(me, val):
+    if val:
+      me.ctx.f |= URLF_SEMI
+    else:
+      me.ctx.f &= ~URLF_SEMI
 
 cdef class URLDecode:
   """URLDecode(STR, [semip = False]): iterator over (KEY, VALUE) pairs"""
@@ -85,15 +88,17 @@ cdef class URLDecode:
   cdef char *p
 
   def __cinit__(me, *hunoz, **hukairz):
-    me.p = xstrdup('')
+    me.p = NULL
     url_initdec(&me.ctx, me.p)
-  def __init__(me, char *string, semip = False):
+  def __dealloc__(me):
+    xfree(me.p)
+  def __init__(me, object string, semip = False):
     cdef unsigned f
     f = 0
     if semip:
-      f = f | URLF_SEMI
+      f |= URLF_SEMI
     xfree(me.p)
-    me.p = xstrdup(string)
+    me.p = xstrdup(TEXT_PTR(string))
     me.ctx.p = me.p
     me.ctx.f = f
   def __iter__(me):
@@ -108,23 +113,22 @@ cdef class URLDecode:
     DCREATE(&v)
     anyp = url_dec(&me.ctx, &n, &v)
     if anyp:
-      nn = PyString_FromStringAndSize(n.buf, n.len)
-      vv = PyString_FromStringAndSize(v.buf, v.len)
+      nn = TEXT_FROMSTRLEN(n.buf, n.len)
+      vv = TEXT_FROMSTRLEN(v.buf, v.len)
     dstr_destroy(&n)
     dstr_destroy(&v)
     if not anyp:
       raise StopIteration
     return nn, vv
-  property semip:
-    """UD.semip -> BOOL: key/value pairs separated with semicolons?"""
-    def __get__(me):
-      return _tobool(me.ctx.f & URLF_SEMI)
-    def __set__(me, val):
-      if val:
-        me.ctx.f = me.ctx.f | URLF_SEMI
-      else:
-        me.ctx.f = me.ctx.f & ~URLF_SEMI
-  def __del__(me):
-    xfree(me.p)
+  @property
+  def semip(me):
+    """UE.semip -> BOOL: separate key/value pairs with semicolons?"""
+    return <bint>(me.ctx.f&URLF_SEMI)
+  @semip.setter
+  def semip(me, val):
+    if val:
+      me.ctx.f |= URLF_SEMI
+    else:
+      me.ctx.f &= ~URLF_SEMI
 
 ###----- That's all, folks --------------------------------------------------