import sys

#sys.path.insert(0, '/home/crazy/src/pycurl-7.19.0/build/lib.linux-x86_64-2.7')

import pycurl

def trigger_unref_virtual_var():
    """trigger BUG since post data is unref after the call"""
    client = pycurl.Curl()
    client.setopt(pycurl.URL, "http://localhost:8080/")
    client.setopt(pycurl.POST, 1)
    client.setopt(pycurl.POSTFIELDS, "A"*1000) # <-- fake var
    client.setopt(pycurl.VERBOSE, 1)
    client.perform()

def trigger_unref_local_var():
    """trigger BUG since X data is unref on return"""

    def prepare_client():
        client = pycurl.Curl()
        client.setopt(pycurl.URL, "http://localhost:8080/")
        client.setopt(pycurl.POST, 1)
        x = "A"*1000 # <-- local var
        client.setopt(pycurl.POSTFIELDS, x)
        client.setopt(pycurl.VERBOSE, 1)
        return client # <-- deref var "x"

    client = prepare_client()
    client.perform()

def no_trigger_keep_ref():
    """DO NOT trigger BUG since X ref is still > 0 when perform() is called"""

    x = "A"*1000 # <-- local var

    def prepare_client(x):
        client = pycurl.Curl()
        client.setopt(pycurl.URL, "http://localhost:8080/")
        client.setopt(pycurl.POST, 1)
        client.setopt(pycurl.POSTFIELDS, x)
        client.setopt(pycurl.VERBOSE, 1)
        return client

    client = prepare_client(x)
    client.perform()

#trigger_unref_virtual_var()
#trigger_unref_local_var()
no_trigger_keep_ref()

