Re: [Pythonmac-SIG] Carbon bindings' future

2008-04-11 Thread Nicholas Riley
On Apr 11, 2008, at 3:50 PM, Daniel Miller wrote:
> BTW, I'm not sure if I'm doing the memory management correctly,  
> especially with the values returned from CFDictionaryGetValue (it  
> might need a CFRelease in there). Could someone double-check me on  
> that please?


The general CF rule is that if you use a function named *Get*, then  
you don't need to CFRelease; if you use a function named *Copy* or  
*Create*, you do.  So, what you've written looks fine.

-- 
Nicholas Riley <[EMAIL PROTECTED]> | 

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Carbon bindings' future

2008-04-11 Thread Daniel Miller

Take two (attached).

BTW, I'm not sure if I'm doing the memory management correctly,  
especially with the values returned from CFDictionaryGetValue (it  
might need a CFRelease in there). Could someone double-check me on  
that please?


~ Daniel


def getproxies_internetconfig():
"""Return a dictionary of scheme -> proxy server URL mappings.

By convention the mac uses Internet Config to store
proxies.  An HTTP proxy, for instance, is stored under
the HttpProxy key.

"""
from ctypes import cdll, byref, c_void_p, c_int, c_long, create_string_buffer
from ctypes.util import find_library

sc = cdll.LoadLibrary(find_library("SystemConfiguration"))

if not sc:
return {}

def create_cfstringref(cstr):
return sc.CFStringCreateWithCString(0, cstr, 0)

kCFNumberSInt32Type = 3
kSCPropNetProxiesHTTPEnable = create_cfstringref("HTTPEnable")
kSCPropNetProxiesHTTPProxy = create_cfstringref("HTTPProxy")
kSCPropNetProxiesHTTPPort = create_cfstringref("HTTPPort")

def CStringFromCFString(value):
length = sc.CFStringGetLength(value) + 1
buff = create_string_buffer(length)
sc.CFStringGetCString(value, buff, length, 0)
return buff.value

def CFNumberToInt32(cfnum):
val = c_int()
sc.CFNumberGetValue(cfnum, kCFNumberSInt32Type, byref(val))
return val.value

proxies = {}
proxyDict = sc.SCDynamicStoreCopyProxies(None)
try:
enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPEnable)
if sc.CFBooleanGetValue(enabled):
proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPProxy)
proxy = CStringFromCFString(proxy)
port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPPort)
port = CFNumberToInt32(port)
proxies["http"] = "http://%s:%i"; % (proxy, port)
finally:
sc.CFRelease(proxyDict)

return proxies
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Carbon bindings' future

2008-04-11 Thread Nicholas Riley
Internet Config is deprecated in 10.5; you should use
SCDynamicStoreCopyProxies instead (see SCDynamicStoreCopySpecific.h).

-- 
Nicholas Riley <[EMAIL PROTECTED]> | 
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Carbon bindings' future

2008-04-11 Thread Daniel Miller

Hi Ronald,

Will this work (see attached)?

~ Daniel

def getproxies_internetconfig():
"""Return a dictionary of scheme -> proxy server URL mappings.

By convention the mac uses Internet Config to store
proxies.  An HTTP proxy, for instance, is stored under
the HttpProxy key.

"""
from ctypes import cdll, byref, c_void_p, c_int, c_long, create_string_buffer
from ctypes.util import find_library

ic = cdll.LoadLibrary(find_library("Internet_Config"))
if not ic:
return {}

proxies = {}
icReadOnlyPerm = 1
icinst = c_void_p(None)
attr = c_int(0)
ic.ICStart(byref(icinst), 0)
ic.ICBegin(icinst, icReadOnlyPerm)
try:
# HTTP:
bsize = c_long(255)
buff = create_string_buffer(255)
error = ic.ICGetPref(icinst, "\x0cUseHTTPProxy", byref(attr), byref(buff), byref(bsize))
useHTTPProxy = not error and ord(buff[0])
if useHTTPProxy:
bsize = c_long(255)
error = ic.ICGetPref(icinst, "\rHTTPProxyHost", byref(attr), byref(buff), byref(bsize))
if not error and bsize.value > 0:
# not sure why there's an extra character on the beginning of buff
proxies["http"] = "http://"; + buff.value[1:bsize.value]

# FTP:  To be done.
# Gopher:  To be done.
finally:
ic.ICEnd(icinst)
ic.ICStop(icinst)

return proxies




On Apr 11, 2008, at 2:05 AM, Ronald Oussoren wrote:

Folks,

The removal of the Carbon bindings shouldn't  impact the  
functionality of the rest of the standard library. This means we  
need to do some work though, because urllib relies on the Carbon  
bindings to get the current proxy settings.


The best way to fix that (IMO of course) is to write a small C  
extension that uses the Carbon API's to fetch that data into a  
dictionary. All code for that is present in the Python source tree,  
but in Python instead of in C. Hence writing the extension should  
be easy enough.


Is anyone game for writing this extension?

Ronald


___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig