Implement copy-on-incref for Python bindings

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/335d9188
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/335d9188
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/335d9188

Branch: refs/heads/master
Commit: 335d918817b269d27e6d155cf52608c8fb896c18
Parents: 09e9fc6
Author: Nick Wellnhofer <[email protected]>
Authored: Fri Aug 26 18:29:17 2016 +0200
Committer: Nick Wellnhofer <[email protected]>
Committed: Fri Aug 26 19:19:32 2016 +0200

----------------------------------------------------------------------
 runtime/python/cfext/CFBind.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/335d9188/runtime/python/cfext/CFBind.c
----------------------------------------------------------------------
diff --git a/runtime/python/cfext/CFBind.c b/runtime/python/cfext/CFBind.c
index ab0669f..e1ded4e 100644
--- a/runtime/python/cfext/CFBind.c
+++ b/runtime/python/cfext/CFBind.c
@@ -837,6 +837,19 @@ cfish_get_refcount(void *vself) {
 
 cfish_Obj*
 cfish_inc_refcount(void *vself) {
+    cfish_Obj *self = (cfish_Obj*)vself;
+
+    // Handle special cases.
+    if (self->klass == CFISH_STRING) {
+        // Only copy-on-incref Strings get special-cased.  Ordinary
+        // Strings fall through to the general case.
+        if (CFISH_Str_Is_Copy_On_IncRef((cfish_String*)self)) {
+            const char *utf8 = CFISH_Str_Get_Ptr8((cfish_String*)self);
+            size_t size = CFISH_Str_Get_Size((cfish_String*)self);
+            return (cfish_Obj*)cfish_Str_new_from_trusted_utf8(utf8, size);
+        }
+    }
+
     Py_INCREF(vself);
     return (cfish_Obj*)vself;
 }

Reply via email to