Re: Convert PyIDispatch object to struct IDispatch*

2008-04-11 Thread Huayang Xia
On Apr 11, 9:47 am, Tim Golden <[EMAIL PROTECTED]> wrote:
> Huayang Xia wrote:
> > On Apr 11, 12:15 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > wrote:
> >> En Thu, 10 Apr 2008 18:45:04 -0300, Huayang Xia <[EMAIL PROTECTED]>
> >> escribió:
>
> >>> I am trying to use ctypes to call dll functions. One of the functions
> >>> requires argument "struct IDispatch* ". I do have a PyIDispatch object
> >>> in python. How can I convert this "PyIDispatch object" to "struct
> >>> IDispatch* "?
> >> I think a PyIDispatch object is an IDispatch* itself.
> >> But you'll get better answers from the python-win32 
> >> list:http://mail.python.org/mailman/listinfo/python-win32
>
> >> --
> >> Gabriel Genellina
>
> > Thanks for the info.
>
> > To call a dll function, it needs a C style IDispatch*. PyIDispatch is
> > a python wrapped one. I found a reference from:
>
> >http://svn.python.org/projects/ctypes/trunk/comtypes/comtypes/test/te...
>
> > which shows how to convert C style to python style. Unfortunately i
> > need the reversed version.
>
> > I will post the question to python-win32.
>
> I've had a quick look at the PyIDispatch source and I can't see any obvious
> way in which the underlying IDispatch is exposed. May have missed something,
> but it's possible that there's not way out.
>
> TJG

Thanks for the info.
I read somewhere that the PyIDispatch is converted to VT_DISPATCH
automatically when passed to a C style function call. So from
VT_DISPATCH, it should be possible to get the IDispatch. That's only a
vague idea.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert PyIDispatch object to struct IDispatch*

2008-04-11 Thread Huayang Xia
On Apr 11, 12:15 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 10 Apr 2008 18:45:04 -0300, Huayang Xia <[EMAIL PROTECTED]>
> escribió:
>
> > I am trying to use ctypes to call dll functions. One of the functions
> > requires argument "struct IDispatch* ". I do have a PyIDispatch object
> > in python. How can I convert this "PyIDispatch object" to "struct
> > IDispatch* "?
>
> I think a PyIDispatch object is an IDispatch* itself.
> But you'll get better answers from the python-win32 
> list:http://mail.python.org/mailman/listinfo/python-win32
>
> --
> Gabriel Genellina

Thanks for the info.

To call a dll function, it needs a C style IDispatch*. PyIDispatch is
a python wrapped one. I found a reference from:

http://svn.python.org/projects/ctypes/trunk/comtypes/comtypes/test/test_win32com_interop.py

which shows how to convert C style to python style. Unfortunately i
need the reversed version.

I will post the question to python-win32.
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert PyIDispatch object to struct IDispatch*

2008-04-10 Thread Huayang Xia
I am trying to use ctypes to call dll functions. One of the functions
requires argument "struct IDispatch* ". I do have a PyIDispatch object
in python. How can I convert this "PyIDispatch object" to "struct
IDispatch* "?

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to use .NET DLL from Python

2008-02-07 Thread Huayang Xia
On Feb 7, 3:30 pm, Fuzzyman <[EMAIL PROTECTED]> wrote:
> On Feb 7, 2:35 pm, "Luis M. González" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On 7 feb, 05:52, Fuzzyman <[EMAIL PROTECTED]> wrote:
>
> > > Luis M. González wrote:
> > > > On 6 feb, 21:17, Fuzzyman <[EMAIL PROTECTED]> wrote:
> > > > > On Feb 6, 9:59 pm, "Luis M. Gonz�lez" <[EMAIL PROTECTED]> wrote:
>
> > > > > > On Feb 6, 6:27 pm, Huayang Xia <[EMAIL PROTECTED]> wrote:
>
> > > > > > > Hello All,
>
> > > > > > > I have several .NET DLL (I have no source code for them), is there
> > > > > > > anyway to use them from python instead of from C#.
>
> > > > > > > Thanks,
> > > > > > > Huayang
>
> > > > > > I used to put my .dll files into the .DLL folder, so I could simply
> > > > > > import them as I would with any other python module.
> > > > > > But since I started using Ironpython 2.0 Alpha* this doesn't work
> > > > > > anymore...
> > > > > > Any hint?
>
> > > > > The rule is probably still that the DLLs must be in a directory on
> > > > > sys.path for the interpreter to find them. Try adding the directory
> > > > > containing the assemblies to sys.path and see if you can add
> > > > > references to them.
>
> > > > > Michael Foordhttp://www.manning.com/foord
>
> > > > > > Luis
>
> > > > I tried adding the directory to sys.path.
> > > > Still not working...
>
> > > > >>> import sys
> > > > >>> sys.path.append('C:\Documents and 
> > > > >>> Settings\luismg\Escritorio\IronPython-2.0A
> > > > 8\DLLs')
> > > > >>> from ClassLibrary1 import *
> > > > Traceback (most recent call last):
> > > >   File , line unknown, in ##235
> > > >   File , line unknown, in _stub_##2
> > > > ImportError: No module named ClassLibrary1
>
> > > You need to add references to assemblies before you can import from
> > > the namespaces they contain.
>
> > > import clr
> > > clr.AddReference('ClassLibrary1')
>
> > > HTH
>
> > > Fuzzymanhttp://www.manning.com/foord
>
> > Oh, I know what you mean.
> > But that was exactly the reason for having a .DLLs folder, isn't it?
> > When you place an assembly into this folder, you avoid having to write
> > this boilerplate code, and simply import the assembly as you would
> > with a normal python module. At least, that´s how it worked in
> > previous versions...
>
> No. You have always had to add references to assemblies before being
> able to use the namespaces they contain. You even have to do this with
> C# in Visual Studio.
>
> Michaelhttp://www.manning.com/foord

Is there any special .NET DLL format for IronPython to import (e.g.
rename the DLL to .pyd). Why when I try to import the DLL, it always
complain the module doesn't exist?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is there a way to use .NET DLL from Python

2008-02-07 Thread Huayang Xia
What's the difference between .NET DLL and normal C DLL? Do you mean
after clr.AddReference('ClassLibrary1'), there is no need to import
ClassLibrary1?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to use .NET DLL from Python

2008-02-06 Thread Huayang Xia
Or maybe we can do it in IronPython?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to use .NET DLL from Python

2008-02-06 Thread Huayang Xia
On Feb 6, 4:59 pm, "Luis M. González" <[EMAIL PROTECTED]> wrote:
> On Feb 6, 6:27 pm, Huayang Xia <[EMAIL PROTECTED]> wrote:
>
> > Hello All,
>
> > I have several .NET DLL (I have no source code for them), is there
> > anyway to use them from python instead of from C#.
>
> > Thanks,
> > Huayang
>
> I used to put my .dll files into the .DLL folder, so I could simply
> import them as I would with any other python module.
> But since I started using Ironpython 2.0 Alpha* this doesn't work
> anymore...
> Any hint?
>
> Luis

Is there anyway to import class (to generate objects) from .NET DLL?
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there a way to use .NET DLL from Python

2008-02-06 Thread Huayang Xia
Hello All,

I have several .NET DLL (I have no source code for them), is there
anyway to use them from python instead of from C#.

Thanks,
Huayang
-- 
http://mail.python.org/mailman/listinfo/python-list


PyPerforce: How to open file for editing

2008-01-24 Thread Huayang Xia
I'm completely new to pyperforce. I want to open a file (check out a
file) for editing. How can I do it from PyPerforce?

Another thing is how can I add files to my created label?

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Memory Management in Embedded Python

2007-01-18 Thread Huayang Xia
Hi there,

I have a piece of code like this:

  void funct(PyObject* pyobj)
  {
 char   str[128];
 strncpy(str, "just a test string", sizeof(str));
 PyObject* pydata = PyObject_CallMethod(pyobj, "method_x",
"s", str);
 Py_DECREF(pydata);
  }

After the function is exited, the str is not there anymore. Will this
affect python operation. How does python use the str? It's copied or it
just uses the pointer?

Thanks in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify object attribute by python C API

2007-01-12 Thread Huayang Xia
Thanks for the replies.

For the checking, I found we have to spend a lot of codes to check the
Python API's results. That is a pain. Is it reasonable to ignore the
result check of APIs like PyString_FromString and PyString_AsString
which will fail only when out of memory. Unfortunately there is no
exception thrown (in C++) so that we can integrate the error handling.
What is the best way(less code) to handle the error checking in normal
practice?

On Jan 11, 4:37 pm, "Huayang Xia" <[EMAIL PROTECTED]> wrote:
> I get a python object by running a class' constructor. Then I need to
> modify the instance's attribute just like obj.attr1.attr2 = 'a' if in
> python's term.
>
> PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
> PyObject_SetAttrString(py_obj_attr1, "attr2",
> PyString_FromString("a"));
> Py_DECREF(py_obj_attr1);
>
> The object py_obj_attr1 is said to be a "New reference". It's
> confusing, does it refer to the same object as "obj.attr1" in python's
> term? So that the above code equals: obj.attr1.attr2 = 'a' in python's
> term.
> 
> I

-- 
http://mail.python.org/mailman/listinfo/python-list


How to modify object attribute by python C API

2007-01-11 Thread Huayang Xia
I get a python object by running a class' constructor. Then I need to
modify the instance's attribute just like obj.attr1.attr2 = 'a' if in
python's term.

PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
PyObject_SetAttrString(py_obj_attr1, "attr2",
PyString_FromString("a"));
Py_DECREF(py_obj_attr1);

The object py_obj_attr1 is said to be a "New reference". It's
confusing, does it refer to the same object as "obj.attr1" in python's
term? So that the above code equals: obj.attr1.attr2 = 'a' in python's
term.

I

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where to find the spec of format in PyObject_CallMethod

2007-01-10 Thread Huayang Xia
Thanks. I have mixed arguments.

I found the format spec @ http://docs.python.org/api/arg-parsing.html

On Jan 10, 5:31 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Wed, 2007-01-10 at 14:05 -0800, Huayang Xia wrote:
> > I am trying to use PyObject_CallMethod. It needs a format string to
> > specify what are the followed arguments. Is it possible to use a
> > PyObject* as an argument?
>
> > Where can I find the spec for the format?
>
> > Thanks in advance.Seehttp://docs.python.org/api/object.html#l2h-247:
>
> Note in particular "The C arguments are described by a Py_BuildValue()
> format string that should produce a tuple" and "Note that if you only
> pass PyObject * args, PyObject_CallMethodObjArgs is a faster
> alternative."
>
> So, If all your arguments are PyObject pointers, use
> PyObject_CallMethodObjArgs instead. Otherwise, use PyObject_CallMethod
> and consult the documentation for Py_BuildValue on how to construct the
> format string.
> 
> Hope this helps,
> 
> Carsten.

-- 
http://mail.python.org/mailman/listinfo/python-list


where to find the spec of format in PyObject_CallMethod

2007-01-10 Thread Huayang Xia
I am trying to use PyObject_CallMethod. It needs a format string to
specify what are the followed arguments. Is it possible to use a
PyObject* as an argument?

Where can I find the spec for the format?

Thanks in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subclassing a module: misleading(?) error message

2007-01-04 Thread Huayang Xia
So you know you are subclassing a module.

There is an answer @
http://www.velocityreviews.com/forums/showpost.php?p=1819038&postcount=2

On Jan 4, 3:49 pm, "Erik Johnson"  wrote:
> I ran into a problem I didn't understand at first. I got part of it figured
> out. Let me first demonstrate the original problem:
>
> > cat Super.pyclass Super(object):
> def __init__(self):
> self._class = 'Super'
> def hello(self):
> print "%s says 'Hello'" % self._class
>
> > cat Sub.pyimport Super
>
> class Sub(Super):
> def __init__(self):
> self._class = 'Sub'
>
> > pythonPython 2.3.4 (#1, Feb  7 2005, 15:50:45)
> [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> from Super import Super
> >>> from Sub import SubTraceback (most recent call last):
>   File "", line 1, in ?
>   File "Sub.py", line 4, in ?
> class Sub(Super):
> TypeError: function takes at most 2 arguments (3 given)
>
> My question is NOT "What's wrong here?"
> (The answer to that is that the import in Sub.py should be:  from Super
> import Super
> i.e., I tried to use the module itself where I meant to subclass the class
> defined in that module).
>
> My questions are:
>
> Why does python complain about a function here? (it's a class definition
> statement, right?)
> Is there really a function being called here?
> If so:
> What function was called?
> What two arguments is it expecting?
> What three were given?
> 
> Thanks,
> -ej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is this?

2007-01-04 Thread Huayang Xia
Sequence slicing [starting-at-index :  but-less-than-index [ : step]].
Start defaults to 0, end to len(sequence), step to 1.
So l[i::rows] means: slicing start from i, ending with len(l) and step
with rows. So function colsplit(l, cols) returns a list of sequence
with conversion of:

Assume cols = 4:
From:

ABCD
ABCD
ABCD
AB

To:
CCCDDD



On Jan 4, 11:42 am, "Eric Price"
<[EMAIL PROTECTED]> wrote:
> Hello;
> I'm studying some code examples from the python cookbook site. I came across
> this:
>
> def colsplit(l, cols):
> rows = len(l) / cols
> if len(l) % cols:
> rows += 1
> m = []
> for i in range(rows):
> m.append(l[i::rows])
> return m
>
> What I'd like to know is what is the double colon? What does it do?
> m.append(l[i::rows])
>
> Thanks,
> Eric
>
> _
> Type your favorite song.  Get a customized station.  Try MSN Radio powered
> by Pandora.http://radio.msn.com/?icid=T002MSN03A07001

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When Closure get external variable's value?

2006-12-19 Thread Huayang Xia
My understanding was:

Closure is a nested function first. If it refers free variable, then it
is closure. If it doesn't refer free variable, it doesn't have to be
nested. That is probably the reason, the free variable is emphasized.
Normally it makes sense to return a closure, but not a non-closure
nested function.

I don't understand why while a nested function perfectly matches the
definition of closure, it is not closure simply because it is not used
by external world.

BTW, IMHO, The meaning of  the "most important sentence" was contained
in the original quote. That was the reason I skipped it.

On Dec 19, 4:14 pm, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Huayang Xia a écrit :
>
> > I'm confused. What is the definition of closure.
>
> > I'm not sure if it's correct, I get the definition from wikipedia:
>
> > "A closure typically comes about when one function is declared entirely
> > within the body of another, and the inner function refers to local
> > variables of the outer function. At runtime, when the outer function
> > executes, a closure is formed. It consists of the inner function's code
> > and references to any variables in the outer function's scope that the
> > closure needs."You skipped the first and most important sentence:
> "In programming languages, a closure is a function that refers to free
> variables in its lexical context."
>
> IOW, a closure is a function that carry it's own environment. In the
> following code, the function returned by make_adder is a closure :
>
> def make_adder(adding):
>def adder(num):
>  return num + adding
>return adder
>
> add_three = make_adder(3)
> print add_three(4)
> => 7
>
> > I agree it is not declaration, it's definition. However it's closure
> > based on the above definition. It uses free variable.Actually, it uses a 
> > variable defined in the enclosing scope. But as long
> as it's also executed in the same enclosing scope, it's just a nested
> function.
>
> > Or you mean it's
> > a closure only when the outer function returns it and be exposed to
> > external world?Bingo.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When Closure get external variable's value?

2006-12-19 Thread Huayang Xia
I'm confused. What is the definition of closure.

I'm not sure if it's correct, I get the definition from wikipedia:

"A closure typically comes about when one function is declared entirely
within the body of another, and the inner function refers to local
variables of the outer function. At runtime, when the outer function
executes, a closure is formed. It consists of the inner function's code
and references to any variables in the outer function's scope that the
closure needs."

I agree it is not declaration, it's definition. However it's closure
based on the above definition. It uses free variable. Or you mean it's
a closure only when the outer function returns it and be exposed to
external world?

The code snippet was just for test purpose. My question was how the
free variable inside inner function (the closure) binds with object.
That was answered by Fredrik perfectly.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When Closure get external variable's value?

2006-12-19 Thread Huayang Xia
That is a really concise and precise answer. Thanks.

So the object binding can only happen explicitly at the closure
declaration argument list(non-free variable).

On Dec 19, 10:37 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Huayang Xia wrote:
> > When does the closure get the value of the maxIndex in the following
> > code snippet?
>
> >  def testClosure(maxIndex) :
>
> >  def closureTest():
> >  return maxIndex
>
> >  maxIndex += 5
>
> >  return closureTest()
>
> >  print testClosure(10)
>
> > I thought it should be 10 instead of 15. That was wrong.free variables in 
> > an inner scope bind to variables in the outer scope,
> not objects.
>
> if you want to bind to objects, use explicit binding:
>
>  def closureTest(maxIndex=maxIndex):
>  return maxIndex
> 
> 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When Closure get external variable's value?

2006-12-19 Thread Huayang Xia
Thanks for the clarification.

But my question is:

When does the closure get the value of the maxIndex in the following
code snippet?

 def testClosure(maxIndex) :

 def closureTest():
 return maxIndex

 maxIndex += 5

 return closureTest()

 print testClosure(10)


I thought it should be 10 instead of 15. That was wrong.

After several tests, I found maxIndex is, though, local to
testClosure() but is external to the closureTest(). closureTest() gets
the value of maxIndex at run time. So that it's 15 instead of 10. The
following snippet will verify that further:

def testClosure1(lst):

   def closureTest():
  lst.append(lst[-1]+1)

   lst.append(lst[-1]+1)
   return closureTest()

alist = [1]
testClosure1(alist)
alist.append(3)
testClosure1(alist)

The 'lst' in function testClosure1() and the closure closureTest() are
same thing as alist. So everything is dynamic. Variable's value is
determined at run time.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When Closure get external variable's value?

2006-12-18 Thread Huayang Xia
It will print 15. The closure gets the value at run time.

Could we treat closure as part of the external function and it shares
the local variable with its holder function?

-- 
http://mail.python.org/mailman/listinfo/python-list


When Closure get external variable's value?

2006-12-18 Thread Huayang Xia
What will the following piece of code print? (10 or 15)

def testClosure(maxIndex) :

def closureTest():
return maxIndex

maxIndex += 5

return closureTest()

print testClosure(10)

My question is when the closure function gets value for maxindex? Run
time or compile time?

Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Security Descriptor and CoInitializeSecurity

2006-12-01 Thread Huayang Xia
I'd like to call pythoncom.CoInitializeSecurity with a
PySecurityDescriptor object to set the process-wide security values.
But I'm not able to find a way to let the code go through.

I have read MSDN and searched web, I've not been able to find answer. I
cooked a security descriptor like this (assume aces is a tuple of tuple
(access, sid) :



sd = win32security.SECURITY_DESCRIPTOR()
sd.Initialize()
sd.SetSecurityDescriptorOwner(sid_owner, False)
sd.SetSecurityDescriptorGroup(sid_group, False)


# create DACL
dacl = win32security.ACL()
dacl.Initialize()
for (access, acc_sid) in aces:
# Add ACE which is access and SID
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, access,
isinstance(acc_sid, (unicode, str)) and
win32security.ConvertStringSidToSid(acc_sid) or acc_sid)

sd.SetDacl(True, dacl, False)   # SetSecurityDescriptorDacl
print sd.IsSelfRelative()# result is 1

The sd is a self relative one.

>From MSDN, after calling InitializeSecurityDescriptor, the sd is
absolute sd, and CoInitializeSecurity needs absolute sd. Pythonwin has
not wrapped function like 'MakeAbsoluteSD'.

Has someone ever had same problem. Could you give a hint for solving
the problem. Thanks.

Regards

-- 
http://mail.python.org/mailman/listinfo/python-list