Re: very lightweight gui for win32 + python 3.4

2014-09-13 Thread Thomas Heller

Am 13.09.2014 03:19, schrieb Grant Edwards:

On 2014-09-12, Thomas Heller  wrote:

Am 12.09.2014 18:38, schrieb Chris Angelico:

Does Tkinter really work well with cx_Freeze?  I doubt it (from my
experiences with py2exe).


I never had any problems with Tkinter and py2exe, but you do get
a considerably larger distribution than you do with wxWindows.



That was what I meant - sorry for being unclear.  Especially you
cannot create single-file executables with py2exe, you get a directory
full with the tcl/tk files.

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


Re: very lightweight gui for win32 + python 3.4

2014-09-12 Thread Thomas Heller

Am 12.09.2014 18:38, schrieb Chris Angelico:

On Sat, Sep 13, 2014 at 2:35 AM, Nagy László Zsolt  wrote:

So I need to create a GUI mode version of my program. That the customer
should be able to see a progress bar. What kind of GUI toolkit should I use
for this? I would like this to be lightweight, preferably under 5MB with a
very easy API for simple things like create a dialog, put some labels and a
progress bar on it, and finally close the dialog when the program is
finished. (And of course, needs to work with cx Freeze.)

I do not need a full featured cross platform GUI toolkit. What are my
options?

I have been looking at this:

https://wiki.python.org/moin/GuiProgramming

The only Windows specific lightweight frameworks are venster and Ocean but
they have not been updated since ages. I know that I can do this using the
win32 API, but I don't really want to learn how to build a dialog using
win32 API calls and then process window message queues  wxPython and Qt
are well known but they are not exactly lightweight.




I would recommend to look into Pyglet - it uses the native windows api
via ctypes calls (and is cross-platform as well).
I have never used it myself, all this info is from the web pages.


There's absolutely no reason to go Windows-specific. Use Tkinter -
it's pretty light-weight. Comes with most Python distros. See how it
goes in terms of code size - if it's unsuitable, then look at others,
but start with the obvious option.


Does Tkinter really work well with cx_Freeze?  I doubt it (from my 
experiences with py2exe).



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


Re: Distributing python applications as a zip file

2014-07-23 Thread Thomas Heller

Am 23.07.2014 06:23, schrieb Steven D'Aprano:

A little known feature of Python: you can wrap your Python application in
a zip file and distribute it as a single file. The trick to make it
runnable is to put your main function inside a file called __main__.py
inside the zip file.


Look here:

http://legacy.python.org/dev/peps/pep-0441/
https://pypi.python.org/pypi/pyzzer
https://pypi.python.org/pypi/pyzaa/0.1.0

Or write your own little utility to create such a thing, it's not 
complicated.


Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Re: comtypes

2014-06-21 Thread Thomas Heller

Am 21.06.2014 09:08, schrieb peter.balazo...@emspin.com:

Dears,

I am not sure I am at right place here. Now I've started working with comtypes 
1.1.0 package within python 2.7.6.1. I have ActiveX COM object I want access 
and work with it.

I do following

from comtypes.client import CreateObject
fm = CreateObject("MCB.PCM")
dwt = fm.ReadVariable("dwt")
print dwt

(, 
, 
, True)

by using print gives me return values. I want to access return data (array)and  
variable. How can I correctly handle it?


dwt apparently is an array containing pointers to VARIANTs.  In
comtypes (or ctypes) you dereference pointer by indexing them with zero.
So something like this could work:

for item in dwt:
print item[0]

Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Re: Best practices to overcome python's dynamic data type nature

2014-02-14 Thread Thomas Heller

Am 14.02.2014 17:32, schrieb Ethan Furman:

On 02/14/2014 08:10 AM, Sam wrote:


Dynamic data type has pros and cons. It is easier to program but
 also easier to create bugs. What are the best practices to reduce
 bugs caused by Python's dynamic data-type characteristic? Can the
 experienced Python programmers here advise?


Unit tests.


Lint-like tools - there are a few - also help to discover bugs,
even before running or testing the code.  They also help in
other ways to write better code.

Myself I use the 'frosted' tool.

Thomas

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


Re: Packaging a private project

2013-12-17 Thread Thomas Heller

Am 16.12.2013 12:18, schrieb Nicholas Cole:

Dear List,

What is the best way to distribute a private, pure python, Python 3
project that needs several modules (some available on pypi but some
private and used by several separate projects) in order to run?

I'd like to include everything that my project needs to run in a
single package.  The best way to do this seems to be to be to create
symlinks to the source code of the "3rd party" modules I need and
create a setup.py file that includes them in its "packages" list.  Is
this what other people do?

But even more ideally, I'd like to package my script and its
dependencies in a single zip file that can be executed by python
directly.  I see several declarations that this is possible online,
but I can't find a simple recipe for converting a script and its
dependencies into this kind of distribution. Could someone give me a
pointer to a description of "the right way to do it".

I'm making life harder for myself by using python 3 (PyInstaller still
only supports Python 2) and by the fact that I can't release some of
the necessary code publicly.  Releasing modules and scripts on pypi
has become very easy -- I'd forgotten how hard packaging private code
is!


Well, pyzzer comes to mind (you find it on pypi).  It creates an
executable zip-archive containing all the stuff that you need (at least
the pure python modules).

The problem remains: how to find all the modules and packages that need
to be included?

Python's modulefinder can do this for you.  If you want to live on the
cutting edge you could try the new one that I've written for Python3.
It currently lives in

http://code.google.com/p/ctypes-stuff/source/browse/trunk/mf/py2exe/mf3.py

It has several advantages against the standard library's modulefinder;
for example it finds and is able to extract the bytecode even from
zipped eggs.
For examples how it can be used, you could peek into the runtime.py
module in the same directory, method build_library of class Runtime.

I guess for an experienced developer it takes around one day to take
these pieces and build a script that scans a python script, finds all
the needed modules and packages, and uses pyzzer to build an executable
archive from it.

Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get raw bytes for ctypes functions that return c_wchar_p

2013-11-19 Thread Thomas Heller

Am 19.11.2013 17:58, schrieb Mark Summerfield:

Hi,

I am using ctypes to access a function in a DLL using Python 3.3
32-bit on Windows 7 64-bit:

dplGetPageText = dpl.DPLGetPageText dplGetPageText.argtypes =
(ctypes.c_int, ctypes.c_int) dplGetPageText.restype =
ctypes.c_wchar_p

Python returns this as a str with the raw bytes already decoded.

Unfortunately, when the returned text contains some special
characters (e.g. © or fi) it is not encoded correctly. This may be a
problem with Windows or with ctypes or with the library I'm using; or
of course, it could be my own mistake.

To find out, I'd like to change the restype to give me the raw bytes
so that I can view them and if necessary decode them myself.

Can anyone tell me how to change the restype to get the bytes?


ctypes on Python 2.7 has the set_conversion_mode(coding, errors)
which could be used to change the way c_wchar_p is converted
from/to Python strings.
Unfortunately it seems to be gone in Python 3.3.

However, you can set restype to POINTER(c_char) and then
index the result:

result = dplGetPageText(...)
print(result[0], result[1], result[2])

Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Terry Jones: "Monty Python to reunite for stage show"

2013-11-19 Thread Thomas Heller
"All of the surviving members of comedy group Monty Python are to reform 
for a stage show, one of the Pythons, Terry Jones, has confirmed."


See: http://www.bbc.co.uk/news/entertainment-arts-24999401

Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with subclassing enum34

2013-06-28 Thread Thomas Heller

Am 28.06.2013 17:25, schrieb Thomas Heller:

Robert Kern:



enum.EnumMeta uses super() in its __new__() implementation but
_ctypes.PyCSimpleType doesn't. Thus, only
_ctypes.PyCSimpleType.__new__() gets a chance to run. Switching the
order of the two might work.



Robert found the problem but I'm unsure if there is a solution.
Also I'm unsure whether this is a bug in ctypes or in enum or if
they are simply incompatible.


I forgot to mention that switching the order of metaclasses didn't work.

Thomas

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


Re: Problems with subclassing enum34

2013-06-28 Thread Thomas Heller

Am 28.06.2013 17:16, schrieb Ethan Furman:

On 06/28/2013 03:48 AM, Thomas Heller wrote:

trying out the enum34 module.

What I want to create is a subclass of enum.Enum that is also based
on ctypes.c_int so that I can better use enum instances in ctypes
api calls.


Have you tried using enum.IntEnum?  If you were able to pass ints in
 before, IntEnum should work.


I'm sure that IntEnum works as expected, but I need enums that are
subclasses of ctypes.c_int (so that argument type checking and return
value conversions in ctypes api calls work).

Robert Kern:



enum.EnumMeta uses super() in its __new__() implementation but
_ctypes.PyCSimpleType doesn't. Thus, only
_ctypes.PyCSimpleType.__new__() gets a chance to run. Switching the
order of the two might work.



Robert found the problem but I'm unsure if there is a solution.
Also I'm unsure whether this is a bug in ctypes or in enum or if
they are simply incompatible.

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


Problems with subclassing enum34

2013-06-28 Thread Thomas Heller

trying out the enum34 module.

What I want to create is a subclass of enum.Enum that is also
based on ctypes.c_int so that I can better use enum instances
in ctypes api calls.

When I do this, I get a metaclass conflict:


>>> class MyEnum(ctypes.c_int, enum.Enum):
...FOOBAR = 0
...
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a 
(non-strict) subclass of the metaclasses of all its bases

>>>



When I do this, it does not work either:

>>> class MyEnum_meta(type(ctypes.c_int), type(enum.Enum)):
... pass
...
>>> class MyEnum(ctypes.c_int, enum.Enum):
... FOOBAR = 42
... __metaclass__ = MyEnum_meta
...
>>> MyEnum.FOOBAR
42
>>>

It should have printed ''.

Any ideas?

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


Re: Unicode

2013-03-15 Thread Thomas Heller

Am 15.03.2013 11:58, schrieb Steven D'Aprano:

On Fri, 15 Mar 2013 11:46:36 +0100, Thomas Heller wrote:

[Windows: Problems with unicode output to console]


You can isolate the error by noting that the second one only raises an
exception when you try to print it. That suggests that the problem is
that it contains a character which is not defined in your terminal's
codepage. So let's inspect the strings more carefully:


py> a = u"µm"
py> b = u"\u03bcm"
py> a == b
False
py> ord(a[0]), ord(b[0])
(181, 956)
py> import unicodedata
py> unicodedata.name(a[0])
'MICRO SIGN'
py> unicodedata.name(b[0])
'GREEK SMALL LETTER MU'

Does codepage 850 include Greek Small Letter Mu? The evidence suggests it
does not.

If you can, you should set the terminal's encoding to UTF-8. That will
avoid this sort of problem.


Thanks for the clarification.

For the archives: Setting the console codepage to 65001 and the font to 
lucida console helps.


Thomas

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


Unicode

2013-03-15 Thread Thomas Heller

I thought I understand unicode (somewhat, at least), but this seems
not to be the case.

I expected the following code to print 'µm' two times to the console:


# -*- coding: cp850 -*-

a = u"µm"
b = u"\u03bcm"

print(a)
print(b)


But what I get is this:


µm
Traceback (most recent call last):
  File "x.py", line 7, in 
print(b)
  File "C:\Python33-64\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03bc' in 
position 0: character maps to 



Using (german) windows, command prompt, codepage 850.

The same happens with Python 2.7.  What am I doing wrong?

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


Re: A new webpage promoting Compiler technology for CPython

2013-02-17 Thread Thomas Heller

Am 15.02.2013 08:11, schrieb Travis Oliphant:

Hey all,

With Numba and Blaze we have been doing a lot of work on what
essentially is compiler technology and realizing more and more that
we are treading on ground that has been plowed before with many other
projects.   So, we wanted to create a web-site and perhaps even a
mailing list or forum where people could coordinate and communicate
about compiler projects, compiler tools, and ways to share efforts
and ideas.

The website is:  http://compilers.pydata.org/


Travis,

I think that the pycparser project should be mentioned too on this page:

http://pypi.python.org/pypi/pycparser

Thomas

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


Re: Python launcher (PEP 397) and emacs python-mode.el

2013-01-31 Thread Thomas Heller

Am 31.01.2013 12:05, schrieb Andreas Röhler:

Am 31.01.2013 10:03, schrieb Thomas Heller:

Has someone managed to patch python-mode.el to use
the PEP 397 python launcher when you hit C-c C-c?

It seems that emacs should parse the shebang line in the edited
python script and pass the corresponding arguments to py.exe.



Yes, that's the way python-mode.el acts by default.

AFAIU that launcher is implemented in Python3.3 and should not need any
patch at all.
Should it not work, please file a bug-report at

https://bugs.launchpad.net/python-mode


Well, let me make these remarks:

1. I'm on Windows, using gnu-emacs 24.2.1, and python-mode.el 6.1.0.
I do not understand how the shebang line is used by python-mode.el,
depending on what I write into it either 'python.exe' is started when
I hit C-c C-c, or I get 'Spawning child process: invalid argument'.
The latter occurs most often when the shebang string contains 'jython'.


2. I would like to use the PEP 397 python launcher to start the python
version that is specified in the shebang line.
The python launcher py.exe parses this line when I run 'py script.py',
and then starts the corresponding python interpreter version.
The launcher parses the shebang line by its own rules (see pep397 for
exact details).  One example is this:


#!/usr/bin/python3.1-32
import sys; print(sys.version)


The python launcher starts 'c:\Python31\python.exe -3.1-32 script.py'.

I would like emacs to do the same; however this would require emacs
to do the same shebang line parsing as the launcher does.

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


Python launcher (PEP 397) and emacs python-mode.el

2013-01-31 Thread Thomas Heller

Has someone managed to patch python-mode.el to use
the PEP 397 python launcher when you hit C-c C-c?

It seems that emacs should parse the shebang line in the edited
python script and pass the corresponding arguments to py.exe.

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


Re: The best, friendly and easy use Python Editor.

2013-01-24 Thread Thomas Heller

Am 24.01.2013 16:54, schrieb rusi:

[I personally use emacs. It would be sadistic to make that into a
recommendation]


It would be truly sadistic to force a long-time emacs user to any
other editor.

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


Re: 2to6 ?

2012-07-02 Thread Thomas Heller

Am 27.06.2012 20:06, schrieb Terry Reedy:

On 6/27/2012 10:36 AM, Thomas Heller wrote:

Is there a tool, similar to 2to3, which converts python2 code
to code using six.py, so that it runs unchanged with python2
*and* python 3?


Others have expressed a similar wish, but I do not know that anyone has
actually revised 2to3 to make a 2to6, or how feasible that would be.



Has nobody heard of, or even tried, modernize?

http://pypi.python.org/pypi/modernize


Thomas



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


2to6 ?

2012-06-27 Thread Thomas Heller

Is there a tool, similar to 2to3, which converts python2 code
to code using six.py, so that it runs unchanged with python2
*and* python 3?

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


Re: mmap and bit wise twiddling - Raspberry Pi

2012-05-02 Thread Thomas Heller

Am 02.05.2012 22:05, schrieb Thomas Heller:

class GPIO(BitVector):
def __init__(self, address, value=0xFF, nbits=8):
self.address = address
super(GPIO, self).__init__(value, nbits)
def _get_value(self):
"read an 8-bit value from the hardware as 8-bit integer"
...
def _set_value(self, v):
"write the 8-bit value 'v' to the hardware"
...


Sorry, forgot to create the property; so please add this
to the class definition:

_value = property(_get_value, _set_value)


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


Re: mmap and bit wise twiddling - Raspberry Pi

2012-05-02 Thread Thomas Heller

Am 02.05.2012 19:40, schrieb Petr Jakes:

Hi,
I am trying to work with HW peripherals on Raspberry Pi
To achieve this, it is necessary read/write some values from/to the
memory directly.

I am looking for some wise way how to organize the bit twiddling.

To set some specific bit, for example, it is necessary:
 - read 4 bytes string representation (I am using mmap)
 - transform it to the corresponding integer (I am using numpy)
 - do some bit masking over this integer
 - transport integer to the string representation (numpy again)
 - write it back to the memory

In other words I mean: is there wise way to create an instrument/
machinery to define Class and then simply define all necessary objects
and set the bit values over object attributes so the whole bit-
twiddling remains under the hood.

say:
LED01 = GPIO(4)  # gpio PIN number 4 is assigned to the LED01 name
(attribute)
LED01.on()
LED01.off()
  or

gpio = GPIO()
LED01 = gpio.pin04
LED01 = 1 # led diode is shining (or gpio pin 4 is set)
LED01 = 0 # led diode is off


I have an abstract BitVector base-class that allows to get/set single
bits or several bits in a convenient way.  You must define concrete
subclasses which define a _value get/set property that actually
updates the byte or word in the hardware.  I use it to access bits
or groups of bits of I2C devices.

You would basically code like this, assuming an 8-bit GPIO port:

class GPIO(BitVector):
def __init__(self, address, value=0xFF, nbits=8):
self.address = address
super(GPIO, self).__init__(value, nbits)
def _get_value(self):
"read an 8-bit value from the hardware as 8-bit integer"
...
def _set_value(self, v):
"write the 8-bit value 'v' to the hardware"
...

then you can do:

gpio = GPIO(0x12345678)

led0 = gpio[4] # bit 4
led0.value = 1 # switch led on
print led0.value # get led status

For multiple bits use this (note that different from standard Python
practices, indexing works inclusive and uses [high_bitnum:low_bitnum]:

port = GPIO(0x12345678)
high_nibble = port[7:4]
print high_nibble.value
low_nibble = port[3:0]
low_nibble.value = 0xF

Thomas
class BitVector(object):
"""BitVector class, represents mutable integers
constricted to a certain range.

Single bits can be get/set by indexing with integers.

Slices can be used to get/set the bits in this range, as an
integer constricted to this range also.

Slices must use [MSB:LSB], and are inclusive.

>>> [x for x in BitVector(0xAA)]
[0, 1, 0, 1, 0, 1, 0, 1]
>>> [x for x in BitVector(0xFF)]
[1, 1, 1, 1, 1, 1, 1, 1]
>>>
>>> bv = BitVector(0)
>>> bv[6:3] = 0xf
>>> [x for x in bv]
[0, 0, 0, 1, 1, 1, 1, 0]
>>> bv[6:3] = -1
Traceback (most recent call last):
  ...
ValueError: value out of allowed range
>>> bv[6:3] = 16
Traceback (most recent call last):
  ...
ValueError: value out of allowed range
>>> hex(bv)
'0x78'
>>> bin(bv)
'0b000'
>>> 
"""
def __init__(self, value=0, nbits=8):
self._nbits = nbits
self._value = value

def __index__(self):
return self._value

def __hex__(self):
return hex(self._value)

def __getitem__(self, index):
if isinstance(index, slice):
return self._getslice(index)
if not 0 <= index < self._nbits:
raise IndexError("invalid index")
return int(bool(self._value & (1 << index)))

@property
def bits(self):
"""
Return some bits as an object that has a .value property.  The
.value property represents the current value of the specied
bits.

b = BitVector().bits[7:0]
b.value = ...
print b.value
"""
class Bits(object):
def __init__(self, bv):
self.bv = bv
def __getitem__(self, index):
class GetSet(object):
def __init__(self, bv):
self.bv = bv
def _get(self):
return self.bv[index]
def _set(self, value):
self.bv[index] = value
value = property(_get, _set)
return GetSet(self.bv)
return Bits(self)

def _getslice(self, index):
if index.step is not None:
raise ValueError("extended slicing not supported")
stop, start = index.stop, index.start
if start <= stop or start < 0 or stop >= self._nbits:
print "START, STOP", start, stop
raise ValueError("invalid slice range")
mask = (1 << (start+1)) - 1
return (self._value & mask) >> stop

def _setslice(self, index, value):
if index.step is not None:
raise ValueError("extended slicing not supported")
stop, start = index.stop, index.start
if start <= stop or start < 0 or stop >= se

Re: Finding x is 1, and x is 'foo' comparisons in a code base

2012-01-17 Thread Thomas Heller

Am 17.01.2012 18:10, schrieb Alex Willmer:

Hello,

I'm looking for a way to find the occurrences of x is y comparisons in
an existing code base. Except for a few special cases (e.g. x is [not]
None) they're a usually mistakes, the correct test being x == y.
However they happen to work most of the time on CPython (e.g. when y
is a small integer or string) so they slip into production code
unnoticed.

PyLint and PyFlakes don't check this AFAICT. Any suggestions for such
a tool, or a pointer how to add the check to an existing tool would be
most welcome.


Pychecker contains a check for this mistake.
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.startfile: Why is there no arguments option?

2011-10-12 Thread Thomas Heller

Am 12.10.2011 10:22, schrieb Christian Wutte:

Hello all,
as stated in the docs [1] os.startfile relies on Win32 ShellExecute().
So maybe someone can explain it to me, why there is no support for
program arguments.
That's quite a pity since os.startfile is the easiest way for an
elevated run (with 'runas' as option) and without arguments of limited
use.

[1] http://docs.python.org/library/os.html?highlight=startfile#os.startfile

Thanks,
Christian


It is trivial to call ShellExecute with ctypes.

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


Re: List of WindowsError error codes and meanings

2011-05-26 Thread Thomas Heller

Am 20.05.2011 19:56, schrieb Andrew Berg:

This is probably somewhat off-topic, but where would I find a list of
what each error code in WindowsError means? WindowsError is so broad
that it could be difficult to decide what to do in an except clause.
Fortunately, sys.exc_info()[1][0] holds the specific error code, so I
could put in an if...elif...else clause inside the except clause if I
needed to, but I don't know what all the different errors are.


On Windows, you can use ctypes.FormatError(code) to map error codes
to strings:

>>> import ctypes
>>> ctypes.FormatError(32)
'Der Prozess kann nicht auf die Datei zugreifen, da sie von einem 
anderen Prozess verwendet wird.'

>>>

For HRESULT codes, you (unfortunately) have to subtract 2**32-1 from
the error code:

>>> ctypes.FormatError(0x80040005)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to int
>>> ctypes.FormatError(0x80040005 - (2**32-1))
'Kein Cache zum Verarbeiten vorhanden.'
>>>

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


Re: Problem receiving UDP broadcast packets.

2011-04-20 Thread Thomas Heller

Am 20.04.2011 00:21, schrieb Grant Edwards:

I'm have problems figuring out how to receive UDP broadcast packets on
Linux.

[...]


On the receiving machine, I've used tcpdump to verify that broadcast
packets are being seen and have a destination IP of 255.255.255.255 and
destination MAC of ff:ff:ff:ff:ff:ff

[...]

But, the receiving Python program never sees any packets unless the
_source_ IP address in the packets is on the same subnet as the
receiving machine.  In this test case, the receiving machine has an IP
address of 172.16.12.34/16.  If I change the receiving machine's IP
address to 10.0.0.123, then the receiving program sees the packets.

Even though the destination address is 255.255.255.255, the receiving
machine appears to discard the packets based on the _source_ IP.  Can
anybody provide example Python code for Linux that receives UDP
broadcast packets regardless of their source IP address?

This probably is more of a Linux networking question than a Python
question, but I'm hoping somebody has solved this problem in Python.



You must set the network interface to promiscous mode on the receiving side:

os.system("ifconfig eth0 promisc")

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


Re: Windows - select.select, timeout and KeyboardInterrupt

2010-05-06 Thread Thomas Heller
Paul Moore schrieb:
>>From a quick experiment, it seems that select.select with a timeout
> doesn't react to a keyboard interrupt until the timeout expires.
> Specifically, if I do
> 
> s = socket.socket()
> select.select([s], [], [], 30)
> 
> and then press Ctrl-C, Python waits for the 30 seconds before raising
> KeyboardInterrupt.
> 
> Is this a known limitation on Windows? I see no mention of it in the
> documentation. Assuming it is a known limitation, is there a way round
> it? (I'm writing a tiny server using asyncore/asynchat, and the
> delayed response to Ctrl-C is a mild nuisance. Solutions such as "use
> twisted", while probably the sensible option in a wider context, don't
> really apply here - I need something within the confines of the stdlib
> if it's to be worth doing).

If you look at the source code for time.sleep(), which CAN be interrupted
by pressing Ctrl-C, you will find that it is carefully programmed to be
interruptible (sp?).  Which is not the case for select.select(), obviously.

I guess the best way might be to split your select.select() call into several
ones, using a smaller timeout like 1 second for example.

BTW: I have experimented with calling the win32 function SetConsoleCtrlHandler()
before the call to select.select().  This allows to install a python callback 
function which is called when Ctrl+C is pressed.  However it seems this callback
is not able to interrupt the select() call - but it can 'raise SystemExit()'
which will terminate the script.  Here is the code:

"""
import ctypes, select, socket

@ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint)
def HandlerRoutine(dwCtrlType):
print "hoppla", dwCtrlType
if dwCtrlType == 0: # CTRL+C
raise SystemExit()
return 1

s = socket.socket()

print "Waiting."
ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 1)
select.select([s], [], [], 30)
ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 0)
print "Done."
"""

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


Re: ctypes: delay conversion from c_char_p to string

2010-04-24 Thread Thomas Heller
Brendan Miller schrieb:
> I have a function exposed through ctypes that returns a c_char_p.
> Since I need to deallocate that c_char_p, it's inconvenient that
> ctypes copies the c_char_p into a string instead of giving me the raw
> pointer. I believe this will cause a memory leak, unless ctypes is
> smart enough to free the string itself after the copy... which I
> doubt.
> 
> Is there some way to tell ctypes to return an actual c_char_p, or is
> my best bet to return a c_void_p and cast to c_char_p when I'm reading
> to convert to a string?

Yes, there is.  When you create a subclass of c_char_p (or any other 'simple'
ctypes type like c_wchar_p or even c_int and alike) then the automatic 
conversion
to native Python types like string, unicode, integer is not done.
The function will return an instance of that specific class; you can
retrive the value via the .value property and deallocate the resources
in the destructor for example.


-- 
Thanks,
Thomas

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


Re: Splitting a string

2010-04-02 Thread Thomas Heller
Patrick Maupin schrieb:
> On Apr 2, 6:24 am, Peter Otten <__pete...@web.de> wrote:
>> Thomas Heller wrote:
>> > Maybe I'm just lazy, but what is the fastest way to convert a string
>> > into a tuple containing character sequences and integer numbers, like
>> > this:
>>
>> > 'si_pos_99_rep_1_0.ita'  -> ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita')
>> >>> parts = re.compile("([+-]?\d+)").split('si_pos_99_rep_1_0.ita')
>> >>> parts[1::2] = map(int, parts[1::2])
>> >>> parts
>>
>> ['si_pos_', 99, '_rep_', 1, '_', 0, '.ita']
>>
>> Peter
> 
> You beat me to it.  re.split() seems underappreciated for some
> reason.  When I first started using it (even though it was faster for
> the tasks I was using it for than other things) I was really annoyed
> at the empty strings it was providing between matches.  It is only
> within the past couple of years that I have come to appreciate the
> elegant solutions that those empty strings allow for.  In short,
> re.split() is by far the fastest and most elegant way to use the re
> module for a large class of problems.
> 
> So, the only thing I have to add to this solution is that, for this
> particular regular expression, if the source string starts with or
> ends with digits, you will get empty strings at the beginning or end
> of the resultant list, so if this is a problem, you will want to check
> for and discard those.

Thanks to all for these code snippets.  Peter's solution is the winner -
most elegant and also the fastest.  With an additional list comprehension
to remove the possible empty strings at the start and at the end I get
16 us.  Interesting is that Xavier's solution (which is similar to
some code that I wrote myself) isn't so much slower; it get timings of
around 22 us.

-- 
Thanks,
Thomas

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


Splitting a string

2010-04-02 Thread Thomas Heller
Maybe I'm just lazy, but what is the fastest way to convert a string
into a tuple containing character sequences and integer numbers, like this:


'si_pos_99_rep_1_0.ita'  -> ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita')

Thanks for ideas,
Thomas

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


Re: libc Sleep api performs a busy waiting

2010-03-09 Thread Thomas Heller
Joe Fox schrieb:
> Hi,
> 
>  actually i have simplified my scenario a lot here ,
> 
> In my actual case , i have to call a C-api which blocks on  c select , in a
> separate thread.
> 
> my thread is getting struck in that api , and thus blocking all the other
> threads.
> Can you point to something which will help me call this blocking C-api call
> without my thread getting struck.
> 
ctypes.  Releases the GIL before calling C api functions.

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


Re: ctypes: How to call unexported functions in a dll

2010-01-05 Thread Thomas Heller
Am 05.01.2010 12:19, schrieb Coert Klaver (DT):
> Hi,
> 
> I am using ctypes in python 3 on a WXP machine
> 
> Loading a dll and using its exported functions works fine.
> 
> Now I want to use a function in the dll that is not exported.
> 
> In C this can be done by just casting the address in the dll of that
> function to an apropriate function pointer and call it (you need to be
> sure about the address). Can a similar thing be done directly with
> ctypes?
> 
> A work around I see is writing in wrapper dll in c that loads the main
> dll, exports a function that calls the unexported function in the main
> dll, but I don't find that an elegant solution.

No need for a workaround.

One solution is to first create a prototype for the function by calling 
WINFUNCTYPE
or CFUNCTYPE, depending on the calling convention: stdcall or cdecl, then call 
the
prototype with the address of the dll function which will return a Python 
callable.
Demonstrated by the following script using GetProcAddress to get the address of
the GetModuleHandleA win32 api function:

>>> from ctypes import *
>>> dll = windll.kernel32
>>> addr = dll.GetProcAddress(dll._handle, "GetModuleHandleA")
>>> print hex(addr)
0x7c80b741
>>> proto = WINFUNCTYPE(c_int, c_char_p)
>>> func = proto(addr)
>>> func

>>> func(None)
486539264
>>> hex(func(None))
'0x1d00'
>>> hex(func("python24.dll"))
'0x1e00'
>>> hex(func("python.exe"))
'0x1d00'
>>>

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


Re: bootstrapping on machines without Python

2009-11-13 Thread Thomas Heller
M.-A. Lemburg schrieb:
> Jonathan Hartley wrote:
>> While examining py2exe et al of late, my thoughts keep returning to
>> the idea of writing, in C or similar, a compiled stand-alone
>> executable 'bootstrapper', which:
>> 1) downloads and install a Python interpreter if none exists
>> 2) runs the application's Python source code using this interpreter.
>> 
>> An application source code could be distributed with this small exe to
>> wrap its top level 'main.py', and the application could then be run on
>> machines which do not (yet) have Python installed.
>> 
>> The goal of this is to provide a way for end-users to download and
>> double-click a Python application, without having to know what Python
>> is or whether (an appropriate version of) it is installed. This method
>> has some disadvantages compared to using py2exe et al, but it has the
>> advantage that a small application would remain small: there is no
>> need to bundle a whole interpreter with every application.
> 
> There are two major issues with such an approach:
> 
>  * users will not necessarily like it if a small application
>downloads a several MB from the web somewhere without asking
> 
>  * installing applications on Windows typically requires admin
>rights or at least user interaction
> 
> I think a good solution to the problem is wrapping the application's
> main.py in a batch file which pops up a window to let the user know
> that s/he will need to install Python first and point him/her to the
> www.python.org web site, if necessary.

Here is a batch file that simulates a similar approach,
for demonstration purposes it doesn't download and install python,
instead it simply copies 'c:\python25\python.exe' to the current directory
under a different name (if it is not yet present), and executes the script with 
it.
Batchfile experts should also be able to improve other things:


@echo off && if exist py_exe.exe (py_exe.exe -x %0 %* && goto exit) else (goto 
download)
# The python script
import sys
print "Hi, this is Python", sys.version
sys.exit()

# rest of the batch file
"""
:download
echo Simulating download...
copy c:\python25\python.exe py_exe.exe
echo Done.
REM start the script again after downloading
%0 %*

:exit
rem """


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


Re: Is it possible to disable readline in the interactive interpreter?

2009-06-08 Thread Thomas Heller
Paul Moore schrieb:
> I run Python on Windows. I have the (pure Python) pyreadline package
> installed for (occasional) use by IPython. However, when I use the
> normal Python interactive prompt, the mere fact that the readline
> module exists means that it gets used.
> 
> Is there a way of disabling this? (Preferably by default, rather than
> on a per-session basis).
> 
> I don't want to get into usability arguments, but the standard Windows
> command line history and editing is fine for me, and consistency with
> other apps is more important in this case than configurability (or any
> other benefits of readline). And yes, the default behaviour of
> readline *is* different, in small but irritating ways.

This has also disturbed me.  However, tab-completion is really nice sometimes.
I wonder if there is a way to to configure readline to that it behaves
on 'up' or 'down' keypresses in the same way as the windows console.
Does someone have such a configuration?

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-05 Thread Thomas Heller
Philip Semanchuk schrieb:
> On Jun 5, 2009, at 10:13 AM, Thomas Heller wrote:
> 
>> s...@pobox.com schrieb:
>>>>> If there is no C++ compiler available then the proposed layout  
>>>>> sniffing just
>>> wouldn't be done and either a configure error would be emitted or a  
>>> run-time
>>> exception raised if a program attempted to use that feature.  (Or the
>>> sniffing could be explicitly enabled/disabled by a configure flag.)
>>>
>>
>> Hm, on Linux, gccxml (if its version is compatible with that of the C 
>> ++ compiler)
>> can probably help a lot.  At runtime, no configure step needed.
>> Unfortunately not on Windows.
> 
> I'm not a gccxml user, but its install page has a section for Windows:
> http://www.gccxml.org/HTML/Install.html

Yes, it runs on Windows (*).  However, there are two problems:

1. gccxml refuses to parse quite some include files from the Window SDK.
That's probably not the fault of gccxml but MS using non-standard C++
constructs.  (There is a workaround inside gccxml: it installs patched
Windows header files, but the patches must be created first by someone)

2. You cannot expect gccxml (which is mostly GCC inside) to use the MSVC
algorithm for C++ object layout, so unfortuately it does not help in the
most important use-case.

Thomas

(*) And there is even use for it on Windows to parse C header files and
generate ctypes wrappers, in the ctypeslib project.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-05 Thread Thomas Heller
s...@pobox.com schrieb:
> >> Requiring that the C++ compiler used to make the dll's/so's to be the
> >> same one Python is compiled with wouldn't be too burdensome would it?
> 
> Scott> And what gave you then impression that Python is compiled with a
> Scott> C++ compiler?
> 
> I don't think it's too much to expect that a C++ compiler be available for
> the configure step if Python is being built in a C++ shop.  The C compiler
> used to build Python proper should be compatible with the C++ compiler
> available to build C++ extension modules or C++ libraries dynamically linked
> into Python.
> 
> If there is no C++ compiler available then the proposed layout sniffing just
> wouldn't be done and either a configure error would be emitted or a run-time
> exception raised if a program attempted to use that feature.  (Or the
> sniffing could be explicitly enabled/disabled by a configure flag.)
> 

Hm, on Linux, gccxml (if its version is compatible with that of the C++ 
compiler)
can probably help a lot.  At runtime, no configure step needed.
Unfortunately not on Windows.

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-05 Thread Thomas Heller
Joseph Garvin schrieb:
> On Thu, Jun 4, 2009 at 3:23 PM, Brian  wrote:
>> What is the goal of this conversation that goes above and beyond what
>> Boost.Python + pygccxml achieve?
> 
> I can't speak for others but the reason I was asking is because it's
> nice to be able to define bindings from within python. At a minimum,
> compiling bindings means a little extra complexity to address with
> whatever build tools you're using.

AFAIU, pybindgen takes this approach.  And, AFAIK, pygccxml can generate
pybindgen code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctype question

2009-06-04 Thread Thomas Heller
Amit Gupta schrieb:
> Hi,
> 
> I have been using ctype.cdll to load a library, but I am unable to
> figure out how to load multiple libraries that depends on each other.
> E.g. I have two libraries A.so and B.so. A.so has some undefined
> references, and those symbols are defined in B.so.
> 
> When I try to load ctypes.cdll.LoadLibrary("A.so"), it gives errors
> about the undefined Symbols. Even if I load B.so before loading A.so,
> the error remains the same (which is expected). Can someone help me to
> find out, how to load A.so by telling it to look for undefined symbols
> in B.so as well?

You could try to pass ctypes.RTLD_GLOBAL as the 'mode' parameter to
ctypes.CDLL when you load the library.

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
Philip Semanchuk schrieb:

> Hi Thomas,
> We're weighing options for accessing C++ objects via Python. I know of  
> SIWG and Boost; are there others that you think deserve consideration?

I haven't used any of them myself.  A common suggestion is SIP,
less known are pybindgen and Robin.  But there may be many more,
and others with more experience might have much more to say about them.
Also there is Roman Yokavenko's pygccxml which has a lot of stuff.

> I've been happy with ctypes in my limited exposure to it and would  
> love to find a way to make that work. This thread has been very  
> interesting to me.

Unfortunately there is no solution in sight, with ctypes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
[Please keep the discussion on the list]

Joseph Garvin schrieb:
> On Thu, Jun 4, 2009 at 3:43 AM, Thomas Heller  wrote:
>> There have been some attempts to use ctypes to access C++ objects.
>> We (Roman Yakovenko and myself) made some progress.  We were able to
>> handle C++ name mangling, the special C++ calling convention,
>> access virtual, non-virtual, overloaded functions, but finally gave up
>> because the binary layout (function tables, member variables, and so on)
>> of C++ objects is way too complicated and undocumented.
> 
> Have you read the book Inside The C++ Object Model?:

I haven't, but considered to buy it ;-)

> http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/ref=sr_1_1?ie=UTF8&s=books&qid=1244139929&sr=8-1
> 
> It's probably out of date now, but I'm about halfway through it and it
> documents a ton of the little unexpected optimizations and such that
> cause the binary layout to be complex. How did you get as far as you
> did without having figured out the layout? (e.g. if you could access
> virtual functions you must have known how to get at the virtual table)

I found a lot of material on the web, also I used the (very good) visual
studio debugger, and finally I did a lot of experimentation.  We were only
able to access some very simple C++ objects.

There is also a patent or patents from MS about the vtable.

All in all, as I said, IMO it is too complicated to figure out the binary
layout of the C++ objects (without using a C++ compiler), also there are
quite some Python packages for accessing them.

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
Joseph Garvin schrieb:
> So I was curious whether it's possible to use the ctypes module with
> C++ and if so how difficult it is.

There have been some attempts to use ctypes to access C++ objects.
We (Roman Yakovenko and myself) made some progress.  We were able to
handle C++ name mangling, the special C++ calling convention,
access virtual, non-virtual, overloaded functions, but finally gave up
because the binary layout (function tables, member variables, and so on)
of C++ objects is way too complicated and undocumented.

Our attempts are documented in posts to the ctypes-users mailing list,
most of them have the word 'cpptypes' in the subject line.

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


Re: json vs. simplejson

2009-05-13 Thread Thomas Heller
Ned Deily schrieb:
> In article <76vs9tf1f6c5...@mid.individual.net>,
>  Thomas Heller  wrote:
>> Diez B. Roggisch schrieb:
>> > Thomas Heller wrote:
>> >> Python 2.6 contains the json module, which I thought was the renamed (and
>> >> improved?) simplejson module that also works on older Python versions.
>> >> 
>> >> However, it seems the json is a lot slower than simplejson.
>> >> This little test, run on Python 2.6.2 and WinXP shows a dramatic
>> >> difference:
>> >>   C:\>py26 -m timeit -s "from json import dumps, loads"
>> >>   "loads(dumps(range(32)))" 1000 loops, best of 3: 618 usec per loop
>> >> 
>> >>   C:\>py26 -m timeit -s "from simplejson import dumps, loads"
>> >>   "loads(dumps(range(32)))" 1 loops, best of 3: 31 usec per loop> >> 
>> >> Does anyone have an explanation for that?
>> > 
>> > Dunno about json, but simplejson comes with an (optional) C-based
>> > speedup-module.
>> > 
>> > Maybe this isn't part of the standard distribution? 
>> json has it's own _json speedup module.  And funny, on Linux, json
>> WITH _json is still somewhat slower (~10%) than simplejson WITHOUT
>> the _speedups module.
> 
> According to the svn history in 2.6, the json module was a copy of 
> simplejson 1.9.  The current version of simplejson is 2.0.9 and, 
> according to its CHANGES.txt, there have been a number of performance 
> improvements in the various releases since 1.9.  Looks like the trunk 
> (2.7) version of json has been updated to the latest simplejson.
> 

Ok, thanks.  I've submitted a bug for python2.6, but it seems they
will not upgrade to a newer version.
<http://bugs.python.org/issue6013>

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


Re: json vs. simplejson

2009-05-13 Thread Thomas Heller
Diez B. Roggisch schrieb:
> Thomas Heller wrote:
> 
>> Python 2.6 contains the json module, which I thought was the renamed (and
>> improved?) simplejson module that also works on older Python versions.
>> 
>> However, it seems the json is a lot slower than simplejson.
>> This little test, run on Python 2.6.2 and WinXP shows a dramatic
>> difference:
>> 
>>   C:\>py26 -m timeit -s "from json import dumps, loads"
>>   "loads(dumps(range(32)))" 1000 loops, best of 3: 618 usec per loop
>> 
>>   C:\>py26 -m timeit -s "from simplejson import dumps, loads"
>>   "loads(dumps(range(32)))" 1 loops, best of 3: 31 usec per loop
>> 
>> Does anyone have an explanation for that?
> 
> Dunno about json, but simplejson comes with an (optional) C-based
> speedup-module.
> 
> Maybe this isn't part of the standard distribution? 


json has it's own _json speedup module.  And funny, on Linux, json
WITH _json is still somewhat slower (~10%) than simplejson WITHOUT
the _speedups module.

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


Re: Fwd: Re: Unable to install Pywin32 for Python 2.6.2

2009-05-13 Thread Thomas Heller
David Lyon schrieb:
> On Wed, 13 May 2009 05:32:16 +0200, "Martin v. Löwis" 
> wrote:
>  
>> I think this was a case of obscure misconfiguration of the system.
>> It is always possible to configure a system in such a way that even
>> the most resilient installation procedure will break.
> 
> Technically, you are right..
> 
> but imho.. windows installers are overkill and shouldn't be used
> for this sort of thing. That is, installing libraries. And not
> the fault of the package authors either...

Well, if you don't like the windows installer than you can always
install from the sources.  Please go ahead and try it out.

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


json vs. simplejson

2009-05-13 Thread Thomas Heller
Python 2.6 contains the json module, which I thought was the renamed (and 
improved?)
simplejson module that also works on older Python versions.

However, it seems the json is a lot slower than simplejson.
This little test, run on Python 2.6.2 and WinXP shows a dramatic difference:

  C:\>py26 -m timeit -s "from json import dumps, loads" 
"loads(dumps(range(32)))"
  1000 loops, best of 3: 618 usec per loop

  C:\>py26 -m timeit -s "from simplejson import dumps, loads" 
"loads(dumps(range(32)))"
  1 loops, best of 3: 31 usec per loop

Does anyone have an explanation for that?

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


Re: Web framework for embedded system

2009-04-29 Thread Thomas Heller
Thomas Heller schrieb:
> I'm looking for a lightweight web-framework for an embedded system.
> The system is running a realtime linux-variant on a 200 MHz ARM
> processor, Python reports a performance of around 500 pystones.
> 
> The web application will not be too fancy, no databases involved
> for example, but it will need to control some simple peripherals
> (some parallel in/out, some dacs) attached to the system.
> 
> It should provide a user interface showing the current state and
> allowing to manipulate it via a browser, and I will probably need
> to support some rpc as well.

Thanks for all the replies I got.

I will take the 'traditional' route, and probably go with cherrypy first,
but will still keep an eye on webpy.

I'm very happy to see that these frameworks deliver ~10 pages per second
(cherrypy) or ~3.5 pages per second (webpy) out of the box on a system
that is 50 times slower than a typical desktop PC.  Of course these
were very short pages.


The other ideas that were posted, the SIMPL toolkit and the
'Batteries Included! Python on Low Cost Tiny Embedded Wireless Devices'
stuff, are very interesting as well, but not for this project.  Food for
thought, at least.


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


Web framework for embedded system

2009-04-28 Thread Thomas Heller
I'm looking for a lightweight web-framework for an embedded system.
The system is running a realtime linux-variant on a 200 MHz ARM
processor, Python reports a performance of around 500 pystones.

The web application will not be too fancy, no databases involved
for example, but it will need to control some simple peripherals
(some parallel in/out, some dacs) attached to the system.

It should provide a user interface showing the current state and
allowing to manipulate it via a browser, and I will probably need
to support some rpc as well.

Does this sound sensible at all? Any suggestions?

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


Re: pythonCE GetSystemPowerState windows api

2009-04-25 Thread Thomas Heller
lorenzo.mentas...@yahoo.it schrieb:
> Hi all,
> I need to call GetSystemPowerState windows api from pythonCE, because
> I need to know if a windows ce 5 device is in sleep/off status.
> I can find this api in ctypes.windll.coredll, but I cannot figure out
> how to pass parameters to this procedure: msdn giude (
> http://msdn.microsoft.com/en-us/library/ms899319.aspx ) speaks about 3
> parameters, of wich 2 are output: a "LPWSTR pBuffer" and a "PDWORD
> pFlags". Returned value of the call is an exit status code
> rappresenting if call succeded or failed.
> 
> I tried to call this api in several ways, but obtained always exit
> code 87 (meaning that parameters are wrong) or 122 (meaning "pBuffer"
> variable is too small, in this case I passed None as pBuffer, since
> I'm interested only in pFlags result).
> 
> Have you any idea? Maybe another api or way to perform this simple
> task?

Python 2.5 (release25-maint, Dec 19 2006, 23:22:00) [MSC v.1201 32 bit (ARM)] 
on win32
>>> from ctypes import *
>>> d=windll.coredll
>>> d.GetSystemPowerState
<_FuncPtr object at 0x0016EC60>
>>> p=create_unicode_buffer(256)
>>> flags=c_ulong()
>>> f=d.GetSystemPowerState
>>> f(p,256,byref(flags))
0
>>> p.value
u'on'
>>> flags
c_ulong(268500992L)
>>> hex(flags.value)
'0x1001L'
>>>


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


Re: Failed to build these modules:_ctypes

2009-04-24 Thread Thomas Heller
PS schrieb:
> Hello all
> 
> I can't install neither python 2.6.1 nor 2.6.2 because an error during
> compilation of _ctypes module, I don't need the module but I don't
> know how to instruct to skip it.

You only get a warning, right?  So a subsequent 'make install' should work.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Windows Shell Extensions - Out of Proc

2009-04-23 Thread Thomas Heller
Ralf schrieb:
>> I think that for whatever reasons, explorer always tries to create
>> shell extensions as InProc.  CoCreateInstance, which is the usual
>> API to create COM instances, allows to specify which one you want.
>>
>> Thomas
> 
> So there is no way to do this, other than to create a "proxy" COM
> object as InProc, which then forwards the requests to my COM object in
> the separate process?

Not that I know of - anyone else?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Windows Shell Extensions - Out of Proc

2009-04-23 Thread Thomas Heller
Ralf schrieb:
> I'm trying to develop Windows Shell Extensions with the Python Win32
> Extensions. Most of the samples are working. However, I have a
> slightly different need: I want the Python COM server to run as a
> separate process ("LocalServer" or "OutOfProc").
> 
> As I understand, both the InProc and LocalServer versions are created
> automatically. So to disable the InProc version, I removed the
> InProc32 key from the registry, for the context menu example
> distributed with the Python Win32 Extensions. However, this removes
> the context menu from Explorer, even though the LocalServer32 key
> remains. I could access the COM objects in the example with a Python
> client, so I have no idea why Explorer can't use the LocalServer
> version.
> 
> Can anyone help with this? I've Google'd for hours, without much
> success. I couldn't even find a good example for any other language,
> only references saying it's possible for C#.

I think that for whatever reasons, explorer always tries to create
shell extensions as InProc.  CoCreateInstance, which is the usual
API to create COM instances, allows to specify which one you want.

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


Re: ANN: PyGUI 2.0.1

2009-04-17 Thread Thomas Heller
greg schrieb:
> Suraj Barkale wrote:
> 
>> I installed this and tried out the tests on Python 2.6.1 and Windows XP
>> SP3. Following are my observations.
> 
> Thanks, I'll look into these.
> 
>> Test 33-mouse-events.py:
>> 1. mouse-enter and mouse-leave events are not reported.
> 
> That's actually expected on Windows -- I couldn't find
> any way of implementing these.

Maybe this can help:

http://blogs.msdn.com/oldnewthing/archive/2003/10/13/55279.aspx

>> Test 37-image-cursor.py:
>> 1. Mouse pointer hotspot is in the middle of the image.
> 
> That's okay too, it's meant to be there.
> 


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


pyreadline, InteractiveConsole, and tab completion on Windows

2009-04-03 Thread Thomas Heller
I have installed pyreadline, and get nice tab completion in
the normal interactive interpreter:


Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> 
and   oct   UserWarning   
ArithmeticError
assertSystemExitfilter
str
break StandardError range 
property
[...]
>>> import sys
>>> sys.   
sys.__displayhook__   sys.call_tracing  sys.getfilesystemencoding 
sys.ps1
sys.__doc__   sys.callstats sys.getrecursionlimit 
sys.ps2
[...]
>>> sys.


However, in 'code.interact()', the behaviour is different.  Hitting TAB
at the top level works as before, but hitting TAB after entering 'sys.' for 
example
doesn't show any completions:


Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> import code
>>> code.interact()
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
and   oct   UserWarning   
ArithmeticError
assertSystemExitfilter
str
break StandardError range 
property
[...]
>>> import sys
>>> sys.  


How can I get the same tab-completion?

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


Re: Easier to wrap C or C++ libraries?

2009-02-15 Thread Thomas Heller
Christian Heimes schrieb:
> Hrvoje Niksic schrieb:
>> "Diez B. Roggisch"  writes:
>> 
>>> The answer is easy: if you use C, you can use ctypes to create a
>>> wrapper - with pure python, no compilation, no platform issues.
>> 
>> The last part is not true.  ctypes doesn't work on 64-bit
>> architectures, nor does it work when Python is built with non-gcc Unix
>> compilers
> 
> ctypes works on 64bit systems, too. However it's a pain in the ... back
> to write code with ctypes that works across all platforms. I used to use
> ctypes for wrapper but eventually I switched to Cython.
> 

I have plans to add the types from the ansi-c standard to ctypes, like these:

stddef.h: ptrdiff_t, size_t, wchar_t
signal.h: sig_atomic_t
stdio.h: FILE, fpos_t, stderr, stdout, stdin
stdlib.h: div_t, ldiv_t
time.h: clock_t, time_t, struct tm

Do you think that would have helped for writing cross-platform wrappers?

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


Re: py2exe and distutils

2009-02-07 Thread Thomas Heller
> Maxim Demenko schrieb:
>> Hi,
>> i have installed Python 2.5.4 on WinXP, setuptools-0.6c9 and py2exe 0.6.9
>> Now i can't list installed modules, here is the stacktrace:
[...]
>> Any suggestion, how to fix this issue?
> 
Thomas Heller schrieb:
> Looks like a setuptools problem to me.  Here's the output on my system:

Actually, I don't know where the problem is.  Maybe pydoc?

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


Re: py2exe and distutils

2009-02-07 Thread Thomas Heller
Maxim Demenko schrieb:
> Hi,
> i have installed Python 2.5.4 on WinXP, setuptools-0.6c9 and py2exe 0.6.9
> Now i can't list installed modules, here is the stacktrace:
> 
> 
> 
> help> modules
> 
> Please wait a moment while I gather a list of all available modules...
> 
> Traceback (most recent call last):
>File "", line 1, in 
>File "C:\Programme\Python25\lib\site.py", line 346, in __call__
>  return pydoc.help(*args, **kwds)
>File "C:\Programme\Python25\lib\pydoc.py", line 1649, in __call__
>  self.interact()
>File "C:\Programme\Python25\lib\pydoc.py", line 1667, in interact
>  self.help(request)
>File "C:\Programme\Python25\lib\pydoc.py", line 1683, in help
>  elif request == 'modules': self.listmodules()
>File "C:\Programme\Python25\lib\pydoc.py", line 1804, in listmodules
>  ModuleScanner().run(callback)
>File "C:\Programme\Python25\lib\pydoc.py", line 1855, in run
>  for importer, modname, ispkg in pkgutil.walk_packages():
>File "C:\Programme\Python25\lib\pkgutil.py", line 110, in walk_packages
>  __import__(name)
>File 
> "C:\Programme\Python25\Lib\site-packages\setuptools\__init__.py", line 
> 2, in 
>  from setuptools.extension import Extension, Library
>File 
> "C:\Programme\Python25\Lib\site-packages\setuptools\extension.py", line 
> 2, in 
>  from dist import _get_unpatched
>File "C:\Programme\Python25\Lib\site-packages\setuptools\dist.py", 
> line 27, in 
>  _Distribution = _get_unpatched(_Distribution)
>File "C:\Programme\Python25\Lib\site-packages\setuptools\dist.py", 
> line 23, in _get_unpatched
>  "distutils has already been patched by %r" % cls
> AssertionError: distutils has already been patched by  py2exe.Distribution at 0x011B4F90>
> 
> Any suggestion, how to fix this issue?

Looks like a setuptools problem to me.  Here's the output on my system:

>>> help("modules")

Please wait a moment while I gather a list of all available modules...

Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python25\lib\site.py", line 346, in __call__
return pydoc.help(*args, **kwds)
  File "c:\python25\lib\pydoc.py", line 1645, in __call__
self.help(request)
  File "c:\python25\lib\pydoc.py", line 1682, in help
elif request == 'modules': self.listmodules()
  File "c:\python25\lib\pydoc.py", line 1803, in listmodules
ModuleScanner().run(callback)
  File "c:\python25\lib\pydoc.py", line 1854, in run
for importer, modname, ispkg in pkgutil.walk_packages():
  File "c:\python25\lib\pkgutil.py", line 125, in walk_packages
for item in walk_packages(path, name+'.', onerror):
  File "c:\python25\lib\pkgutil.py", line 110, in walk_packages
__import__(name)
  File 
"c:\python25\lib\site-packages\pyopengl-3.0.0b1-py2.5.egg\OpenGL\Tk\__init__.py",
 line 87, in 
_default_root.tk.call('package', 'require', 'Togl')
_tkinter.TclError: can't find package Togl
>>> ^Z

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


Re: py2exe + SQLite problem

2009-01-31 Thread Thomas Heller
Armin schrieb:
> As posted before ... set's my script (python 2.3):
> 
> from distutils.core import setup
> import py2exe
> 
> setup(windows=['dpconf.py'],
>  data_files=[ "", ["proj_db","gsd_db","dachs2.xbm"]]
>  )
> 
> When I create the distribution I got the following err msg:
> 
> *** copy data files ***
> warning: install_data: setup script did not provide a directory for '' 
> -- installing right in 'C:\pyDPCONF.2.3-dev\dist'
> error: can't copy '': doesn't exist or not a regular file

>From the Python docs (chapter 'writing the setup script):

"""
data_files specifies a sequence of (directory, files) pairs in the following 
way:

setup(...,
  data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
  ('config', ['cfg/data.cfg']),
  ('/etc/init.d', ['init-script'])]
 )
"""

So, it looks like you should use

> setup(windows=['dpconf.py'],
>  data_files=[("", ["proj_db","gsd_db","dachs2.xbm"])]
   ^ ^
>  )

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


Re: Extending Python with C or C++

2009-01-08 Thread Thomas Heller
Nick Craig-Wood schrieb:
> Thomas Heller  wrote:
>>  Nick Craig-Wood schrieb:
>> > Interesting - I didn't know about h2xml and xml2py before and I've
>> > done lots of ctypes wrapping!  Something to help with the initial
>> > drudge work of converting the structures would be very helpful.
>> > 
>> > ( http://pypi.python.org/pypi/ctypeslib/ )
>> > 
>> > I gave it a quick go and it worked fine.  I had to edit the XML in one
>> > place to make it acceptable (removing a u from a hex number).
>> 
>>  If you are using a recent version of gccxml, then you should use the
>>  ctypeslib-gccxml-0.9 branch of ctypeslib instead of the trunk. (I should
>>  really merge the gccxml-0.9 branch into the trunk;-)
>> 
>>  If you are already using the branch and the XML file is not accepted,
>>  then could you please provide a short C code snippet that reproduces
>>  the problem so that I can fix it?
> 
> I'm using gccxml from debian testing 0.9.0+cvs20080525-1 which I guess
> doesn't have the code from the branch in.

I meant the branch in the repository where ctypeslib lives in.
Anyway, it doesn't matter anymore since I merged that branch
into the ctypeslib trunk.

Now the problem with the 'u' suffix that you mentioned should be fixed, and
I also made a quick change so that the sized integer types from stdint.h are 
generated
correctly. So, please update your ctypeslib installation and trey again;-)

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


Re: Extending Python with C or C++

2009-01-07 Thread Thomas Heller
Nick Craig-Wood schrieb:
> Ralf Schoenian  wrote:
>>  Ryan wrote:
>> > I've been using Python for many years now. It's a wonderful language
>> > that I enjoy using everyday. I'm now interested in getting to know
>> > more about the guts (C/C++) and extending it. But, extending python
>> > still seems like a black art to me. Is there anymore docs or info on
>> > extending it besides the standard sparse ones (http://www.python.org/
>> > doc/2.5.2/ext/intro.html) that may give me more insight? Is there a
>> > class available? How can I learn more about the guts of python? How
>> > would one go about following an interest in contributing to the
>> > development of python.
>> 
>>  It is not exactly what you are looking for but nevertheless I am 
>>  thinking the  article "Automatic C Library Wrapping -- Ctypes from the 
>>  Trenches" may be interesting for you. You can find it in the latest 
>>  Python Papers issue or simply following the link: 
>>  http://ojs.pythonpapers.org/index.php/tpp/article/view/71
> 
> Interesting - I didn't know about h2xml and xml2py before and I've
> done lots of ctypes wrapping!  Something to help with the initial
> drudge work of converting the structures would be very helpful.
> 
> ( http://pypi.python.org/pypi/ctypeslib/ )
> 
> I gave it a quick go and it worked fine.  I had to edit the XML in one
> place to make it acceptable (removing a u from a hex number).  The
> output of xml2py was an excellent place to start for the conversion,
> though I don't think I'd want to use an automated process like in the
> paper above as its output needed tweaking.
> 
> ...

If you are using a recent version of gccxml, then you should use the
ctypeslib-gccxml-0.9 branch of ctypeslib instead of the trunk. (I should
really merge the gccxml-0.9 branch into the trunk;-)

If you are already using the branch and the XML file is not accepted,
then could you please provide a short C code snippet that reproduces
the problem so that I can fix it?


> Here are my thoughts on the conversion :-
> 
> It converted an interface which looked like this (the inotify interface)
> 
> struct inotify_event {
> int  wd;   /* Watch descriptor */
> uint32_t mask; /* Mask of events */
> uint32_t cookie;   /* Unique cookie associating related
>   events (for rename(2)) */
> uint32_t len;  /* Size of name field */
> char name[];   /* Optional null-terminated name */
> };
> 
> Into this
> 
> class inotify_event(Structure):
> pass
> inotify_event._fields_ = [
> ('wd', c_int),
> ('mask', uint32_t),
> ('cookie', uint32_t),
> ('len', uint32_t),
> ('name', c_char * 0),
> ]
> 
> Which is a very good start.  However it defined these which clearly
> aren't portable
> 
> int32_t = c_int
> uint32_t = c_uint
> 
> Whereas it should have been using the ctypes inbuilt types
> 
> c_int32
> c_uint32

IMO this would be difficult to achive automatically.  There are cases
wher c_int is the correct type, in other cases c_int32 is correct.

> Also I don't think c_char * 0 does anything sensible in ctypes,
> c_byte * 0 is what is required plus a bit of casting.  This is a
> non-standard GNU extension to C though.
> 
> All that said though, it looks like a great time saver.
> 

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


Re: ANN: New Book: Programming in Python 3

2008-12-19 Thread Thomas Heller
Steve Holden schrieb:
> Thomas Heller wrote:
>> Question from a non-native english speaker: is this now valid english?
>> 
>>   "One of Python’s great strengths"
>> ^
>>   "and also teaches Python’s functional programming features"
>>   ^
>>   "The book’s approach is wholly practical"
>>^
> It always has been valid English. The apostrophe is only omitted from
> personal pronouns (hers, its, and so on).

I see, thanks.  But, is the apostrophe optional in the above fragments?

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


Re: ANN: New Book: Programming in Python 3

2008-12-19 Thread Thomas Heller
Mark Summerfield schrieb:
> Just a follow-up to say that the book has now been published in the
> U.S.
> It is now in stock at InformIT, and should reach other stores, e.g.,
> Amazon, in a week or so.
> 
> Also, the introduction, the first few pages of the first chapter, the
> whole of chapter 12 (regular expressions), and the index are now
> available for free download in a PDF from here:
> http://www.informit.com/store/product.aspx?isbn=0137129297

Question from a non-native english speaker: is this now valid english?

  "One of Python’s great strengths"
^
  "and also teaches Python’s functional programming features"
  ^
  "The book’s approach is wholly practical"
   ^

Curious,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes and misaligned doubles

2008-12-11 Thread Thomas Heller
Jan Roelens schrieb:
> Dear python experts,
> 
> How can I change the alignment of types in the ctypes package? I have
> a library that was built with gcc using the -malign-double option. I
> also have python code that can create ctypes wrapper code from the
> include files for that library. The problem is that structs that
> contain fields of type double are not correctly wrapped half of the
> time. This is because ctypes assumes an alignment of 4 (on a 32-bit
> platform) for the double type, while the library I'm wrapping uses an
> alignment of 8 for these doubles.

To force an alignment that is smaller than the native alignment, you
can use the _pack_ attribute in your Structure definitions.

http://docs.python.org/library/ctypes.html?highlight=_pack_#ctypes.Structure._pack_

To force an alignment that is larger than the native alignment, you
must use padding.

> Is there a way to change the alignment of a ctypes type without
> recompiling the whole ctypes package? If I can't change it, is there a
> way to define my own type based on c_double but with a different
> alignment?

No.

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


Re: Checking if an int fits in 32 bits?

2008-12-04 Thread Thomas Heller
Roy Smith schrieb:
> I'm working with marshaling data over a binary wire protocol.  I'm
> using struct.pack() to handle the low-level encoding of ints.  One of
> the things I need to do is make sure an int can be represented in 4
> bytes.  Is there a portable way to do that?  For now, I'm doing signed
> ints, but I'll certainly have to do unsigned 32-bit ints (and 64-bit
> ints) at some point.  Not to mention shorts and chars.
> 
> At first I thought pack() might raise an exception on a value
> overflow, that but doesn't seem to be the case:
> 
 [hex(ord(c)) for c in struct.pack('!i', 9L)]
> ['0xde', '0x9f', '0xff', '0xff']
> 
> Should I be thinking more along the lines of bit masking (and worrying
> about all the niggling 2-complement issues) in the Python code?  Or is
> there some cleaner way to do this?
> 

You could try something like this:

import ctypes
def int_fits_in_32bit(value):  
  return ctypes.c_int32(value) == value

and similar for signed/unsigned short/int/long.  Packing and unpacking with
the struct module, however, does basically the same.

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


Re: C extension type gives type error in power operator

2008-11-20 Thread Thomas Heller
Paul Moore schrieb:
> I'm trying to implement an extension type with a power operator. The
> operator is unusual in that I want to allow my objects to be raised to
> an integer power:
> 
>p = Pattern()
>p3 = p ** 3
> 
> I've implemented the code for a nb_power slot, it converts the "other"
> argument to a C long using PyInt_AsLong().
> 
> static PyObject *
> Pattern_pow (PyObject *self, PyObject *other, PyObject *modulo)
> {
> long n = PyInt_AsLong(other);
> ...
> /* Ignore modulo argument - not meaningful */
> 
> if (n == -1 && PyErr_Occurred())
> return NULL;
> ...
> }
> 
> However, when I try to use the operator, I get the following error:
> TypeError: unsupported operand type(s) for ** or pow():
> '_ppeg.Pattern' and 'int'

Try to set Py_TPFLAGS_CHECKTYPES in your extension type (in the tp_flags slot).

from object.h:
  /* PyNumberMethods do their own coercion */
  #define Py_TPFLAGS_CHECKTYPES (1L<<4)

> I'm not sure where this error is coming from, as I don't have any type
> checks in my code. Is there something else I should add to allow mixed-
> type operations? (This is Python 2.5, if it matters).
> 
> Oh, and is there a good reference for writing C extensions for Python
> anywhere? The manuals aren't bad, but I keep hitting empty sections
> (e.g., 10.5 Number Object Structures).

I normally look into the newest python reference manual, even if I'm
programming for older versions.  But sometimes you have to took into
the header files, too.

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


Re: Passing a memory address (pointer) to an extension?

2008-10-22 Thread Thomas Heller
Philip Semanchuk schrieb:
> I'm writing a Python extension in C that wraps a function which takes  
> a void * as a parameter. (The function is shmat() which attaches a  
> chunk of shared memory to the process at the address supplied by the  
> caller.) I would like to expose this function to Python, but I don't  
> know how to define the interface.
> 
> Specifically, when calling PyArg_ParseTuple(), what letter should I  
> use to represent the pointer in the format string? The best idea I can  
> come up with is to use a long and then cast it to a void *, but  
> assuming that a long is big enough to store a void * is a shaky  
> assumption. I could use a long long (technically still risky, but  
> practically probably OK) but I'm not sure how widespread long longs are.

I suggest "O!" and a converter function calling PyLong_AsVoidPtr().

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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-22 Thread Thomas Heller
Andy schrieb:
> Dear Python dev community,
> 
> [...]  Basically,
> we use embedded python and use it to wrap our high performance C++
> class set which wraps OpenGL, DirectX and our own software renderer.
> In addition to wrapping our C++ frameworks, we use python to perform
> various "worker" tasks on worker thread (e.g. image loading and
> processing).  However, we require *true* thread/interpreter
> independence so python 2 has been frustrating at time, to say the
> least.
[...]
> 
> Sadly, the only way we could get truly independent interpreters was to
> put python in a dynamic library, have our installer make a *duplicate*
> copy of it during the installation process (e.g. python.dll/.bundle ->
> python2.dll/.bundle) and load each one explicitly in our app, so we
> can get truly independent interpreters.  In other words, we load a
> fresh dynamic lib for each thread-independent interpreter (you can't
> reuse the same dynamic library because the OS will just reference the
> already-loaded one).

Interesting questions you ask.

A random note: py2exe also does something similar for executables build
with the 'bundle = 1' option.  The python.dll and .pyd extension modules
in this case are not loaded into the process in the 'normal' way (with
some kind of windows LoadLibrary() call, instead they are loaded by code
in py2exe that /emulates/ LoadLibrary - the code segments are loaded into
memory, fixups are made for imported functions, and marked executable.

The result is that separate COM objects implemented as Python modules and
converted into separate dlls by py2exe do not share their interpreters even
if they are running in the same process.  Of course this only works on windows.
In effect this is similar to using /statically/ linked python interpreters
in separate dlls.  Can't you do something like that?

> So, my question becomes: is python 3 ready for true multithreaded
> support??  Can we finally abandon our Frankenstein approach of loading
> multiple identical dynamic libs to achieve truly independent
> interpreters?? I've reviewed all the new python 3 C API module stuff,
> and all I have to say is: whew--better late then never!!  So, although
> that solves modules offering truly independent interpreter support,
> the following questions remain:
> 
> - In python 3, the C module API now supports true interpreter
> independence, but have all the modules in the python codebase been
> converted over?  Are they all now truly compliant?  It will only take
> a single static/global state variable in a module to potentially cause
> no end of pain in a multiple interpreter environment!  Yikes!

I don't think this is the case (currently).  But you could submit patches
to Python so that at least the 'official' modules (builtin and extensions)
would behave corectly in the case of multiple interpreters.  At least
this is a much lighter task than writing your own GIL-less interpreter.

My 2 cents,

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


Re: python 3: sorting with a comparison function

2008-10-10 Thread Thomas Heller
[EMAIL PROTECTED] schrieb:
> Kay Schluehr:
>> Sometimes it helps when people just make clear how they use technical
>> terms instead of invoking vague associations.
> 
> And generally Python docs can enjoy growing few thousands examples...

Well, that may not be necessary.  But I think that a clear example how to use
the 'key=' parameter in the sort() and sorted() method/function is badly needed.

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


Re: python 3: sorting with a comparison function

2008-10-10 Thread Thomas Heller
> Thomas Heller wrote:
>> Does Python 3 have no way anymore to sort with a comparison function?
>> 
>> Both [].sort() and sorted() seem to accept only 'key' and 'reverse' 
>> arguments,
>> the 'cmp' argument seems to be gone.  Can that be?

Terry Reedy schrieb:

> Yes.  When this was discussed, no one could come up with an actual use 
> case in which the compare function was not based on a key function. 
> Calling the key function n times has to be faster than calling a compare 
> function n to O(nlogn) times with 2 keys computed for each call.  The 
> main counter argument would be if there is no room in memory for the 
> shadow array of key,index pairs.  And that can be at least sometimes 
> handled by putting the original on disk and sorting an overt key,index 
> array.  Or by using a database.
> 

[EMAIL PROTECTED] schrieb:

> Yes, that's a wonderful thing, because from the code I see around
> 99.9% of people see the cmp and just use it, totally ignoring the
> presence of the 'key' argument, that allows better and shorter
> solutions of the sorting problem. So removing the cmp is the only way
> to rub the nose of programmers on the right solution, and it goes well
> with the Python "There should be one-- and preferably only one --
> obvious way to do it.".


Thanks, I got it now.

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


Re: no unbound methods in py3k

2008-10-09 Thread Thomas Heller
Christian Heimes schrieb:
> Thomas Heller wrote:
>> Ok, so one has to write an extension to access or expose it.
>> 
>> Oh, wait - there's ctypes:
> 
> I wrote the type to help the Pyrex and Cython developers to port their 
> software to 3.0. I planed to expose the type as 
> __builtin__.instancemethod but forgot it. Maybe we can convince Barry 
> together. He still considers my bug as a release blocker for 3.0.
> 

Issue 3787 is marked as release blocker, but for 3.1 IIUC.  Would it help
if I post my use case to the tracker?

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


Re: no unbound methods in py3k

2008-10-09 Thread Thomas Heller
Terry Reedy schrieb:
> Thomas Heller wrote:
>> Christian Heimes schrieb:
>>> I've written PyInstanceMethod_Type for this use case. It's not (yet) 
>>> available for Python code.>> Oh, wait - there's ctypes:
>> 
>> Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit 
>> (Intel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> from ctypes import *
>>>>> pythonapi.PyInstanceMethod_New.restype = py_object
>>>>> pythonapi.PyInstanceMethod_New.argtypes = [py_object]
>>>>> instancemethod = pythonapi.PyInstanceMethod_New
>>>>>
>>>>> class Example:
>> ... pass
>> ...
>>>>> Example.id = instancemethod(id)
>>>>>
>>>>> x = Example()
>>>>> x.id()
>> 12597296
>>>>> id(x)
>> 12597296
> 
> A pyCapi module that exposed via ctypes useful C functions not otherwise 
> accessible, with predefinition of restype and argtypes and anything else 
> needed, might make a nice addition to PyPI if not the stdlib.  You have 
> done two here and I believe others have posted others.

Well, Lenard Lindstrom has some time ago contributed a module like that
which is available in the (more or less unmaintained) ctypeslib project:
http://svn.python.org/projects/ctypes/trunk/ctypeslib/ctypeslib/contrib/pythonhdr.py

However, it was probably more meant to provide a complete Python C api,
instead of concentrating on stuff not available to Python otherwise.

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


python 3: sorting with a comparison function

2008-10-09 Thread Thomas Heller
Does Python 3 have no way anymore to sort with a comparison function?

Both [].sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
the 'cmp' argument seems to be gone.  Can that be?

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


Re: no unbound methods in py3k

2008-10-09 Thread Thomas Heller
Christian Heimes schrieb:
> Thomas Heller wrote:
>> but this is very ugly, imo.  Is there another way?
>> The raw_func instances that I have are not descriptors (they
>> do not implement a __get__() method...)
> 
> I've written PyInstanceMethod_Type for this use case. It's not (yet) 
> available for Python code. Barry hasn't decided whether he should expose 
> the type so late in the release cycle or not. See 
> http://bugs.python.org/issue3787 and 
> http://docs.python.org/dev/3.0/c-api/method.html?highlight=pyinstancemethod#PyInstanceMethod_Type
> 

Ok, so one has to write an extension to access or expose it.

Oh, wait - there's ctypes:

Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> pythonapi.PyInstanceMethod_New.restype = py_object
>>> pythonapi.PyInstanceMethod_New.argtypes = [py_object]
>>> instancemethod = pythonapi.PyInstanceMethod_New
>>>
>>> class Example:
... pass
...
>>> Example.id = instancemethod(id)
>>>
>>> x = Example()
>>> x.id()
12597296
>>> id(x)
12597296
>>>

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


no unbound methods in py3k

2008-10-08 Thread Thomas Heller
I'm currently using code like this to create unbound methods
from functions and stick them into classes:

   method = new.instancemethod(raw_func, None, cls)
   setattr(cls, name, method)

Ok, python 2.6, run with the -3 flag, gives a warning that the new
module is going away in python 3.0, so the equivalent code is:

   method = types.MethodType(raw_func, None, cls)
   setattr(cls, name, method)

However, this code will not work in Python 3.0 because there are
no unbound methods any longer.  The only way that I found so far
is this code:

   method = lambda self, *args: raw_func(self, *args)
   setattr(cls, name, method)

or the equivalent:

   def method(self, *args):
   return raw_func(self, *args)
   setattr(cls, name, method)

but this is very ugly, imo.  Is there another way?
The raw_func instances that I have are not descriptors (they
do not implement a __get__() method...)

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


Re: how to get the thighest bit position in big integers?

2008-10-07 Thread Thomas Heller
Mark Dickinson schrieb:
> On Oct 5, 11:40 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
>> Your point, that taking floor(log2(x)) is redundant, is a good catch.
>> However, you should have added 'untested' ;-).  When value has more
>> significant bits than the fp mantissa can hold, this expression can be 1
>> off (but no more, I assume).   The following correction should be
>> sufficient:
>>
>> res = math.frexp(value)[1] - EXP_OF_ONE
>> test = 1<> if test > r: res -= 1
>> elif 2*test < r: res += 1
>>
>> For value = 2**n -1, n > 53, it is always 1 too high on my Intel
>> machine, so the first correction is sometimes needed.  I do not know if
>> the second is or not.
> 
> See also http://bugs.python.org/issue3439
> where there's a proposal to expose the _PyLong_NumBits method.  This
> would give an O(1) solution.
> 
> Mark

Here is ctypes code that works (tested with Python 2.5):

from ctypes import *

_PyLong_NumBits = pythonapi._PyLong_NumBits
_PyLong_NumBits.argtypes = py_object,
_PyLong_NumBits.restype = c_int

for i in range(64):
x = 1 << i
print i, _PyLong_NumBits(long(x))


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


Re: Directshow in Python

2008-09-18 Thread Thomas Heller
Sayanan Sivaraman schrieb:
> Hey all,
> 
> I'm trying to use DirectShow to display videos [I'm kind of new to
> Python, from more of a C++ background on windows].  I found some
> sample code online, but I am having trouble with calling the I
> 
> import ctypes
> from ctypes import *
> from comtypes import client
> from ctypes.wintypes import *
> import sys
> import time
> 
> 
> filename = sys.argv[1]
> 
> 
> qedit = client.GetModule('qedit.dll') # DexterLib
> quartz= client.GetModule("quartz.dll")
> 
> 
> CLSID_FilterGraph = '{e436ebb3-524f-11ce-9f53-0020af0ba770}'
> filter_graph =
> client.CreateObject(CLSID_FilterGraph,interface=qedit.IFilterGraph)
> filter_builder = filter_graph.QueryInterface(qedit.IGraphBuilder)
> filter_builder.RenderFile(filename, None)
> 
> media_control = filter_graph.QueryInterface(quartz.IMediaControl)
> media_control.Run()
> 
> try:
> # Look at IMediaEvent interface for EOS notification
> while True:
> time.sleep(1)
> except KeyboardInterrupt:
> pass
> 
> # Need these because finalisers don't have enough context to clean up
> after
> # themselves when script exits.
> del media_control
> del filter_builder
> del filter_graph
> 
> This code works fine.  What I would like to know, is how do I declare
> filename in the Python program to be of type LPCWSTR, so that I don't
> need to parse the command line in order to call the function
> "RenderFile()"?

If I understand your question correctly (I'm no sure):  comtypes converts
Python strings or unicode strings to the required LPCWSTR type itself.
So, you can call
  filter_builder.RenderFile("c:\\windows\\clock.avi", None)
or
  filter_builder.RenderFile(u"c:\\windows\\clock.avi", None)

If you pass a string, comtypes converts it to unicode using the "mbcs"
codec; this can be changed by setting ctypes.conversion_mode.

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


Re: handling uncaught exceptions with pdb?

2008-09-12 Thread Thomas Heller
Paul Rubin schrieb:
> I think I've asked about this before, but is there a way to set up
> Python to handle uncaught exceptions with pdb?  I know about setting
> sys.except_hook to something that calls pdb, but this is normally done
> at the outer level of a program, and by the time that hook gets
> called, the exception has already unwound the stack to the outermost
> level.  My situation is I run a multi-hour or multi-day computation
> that eventually crashes due to some unexpected input and I'd like to
> break to the debugger at the innermost level, right when the exception
> is encountered, so I can fix the error with pdb commands and resume
> processing.  Of course this presumes a certain semantics for Python
> exceptions (i.e. handling one involves scanning the stack twice, once
> to look for a handler and again to actually unwind the stack) and I'm
> not sure if it's really done that way.  I do know that Lisp has a
> requirement like that, though; so maybe there is hope.

Would this work (although it probably will slow down the program)?


import sys, pdb

def tracefunc(frame, event, arg):
if event == "exception":
pdb.set_trace()
return tracefunc

sys.settrace(tracefunc)

def test(arg):
if arg > 20:
raise ValueError(arg)
return test(arg+1)

test(0)


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


Re: ctypes error on Windows

2008-09-05 Thread Thomas Heller
Fredrik Lundh schrieb:
> Fredrik Lundh wrote:
> 
>>> I do have the tidy.dll installed (if I didn't, I couldn't even import 
>>> the tidy module).
>> 
>> typing the following into the Python interpreter might give you some 
>> more clues:
>> 
>> >>> import _tidy
>> >>> _tidy.__file__
>> >>> dir(_tidy)
> 
> or not, since ctypes is involved.
> 
> have you checked for multiple copies of tidy.dll?
> 
> if you have MSVC on your machine, try using "dumpbin /exports" on the 
> DLL to check that it really exports the symbols the binding expects.
> 

Or use dependencywalker (google for it).

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


Re: logging - how to use in a library?

2008-08-29 Thread Thomas Heller
Vinay Sajip schrieb:
> On Aug 27, 11:28 am, Thomas Heller <[EMAIL PROTECTED]> wrote:
>>
>> I came up with a workaround that seems to do what I want.  I add a NULL 
>> handler
>> to my top-level logger which is not the root logger but a logger named 
>> 'comtypes',
>> other loggers are named 'comtypes.something...'.  So the application 
>> developer
>> using the library will not see any comtypes logger messages at all, unless
>> he calls 'logging.basicConfig()', for example.
>>
> 
> This is a good solution - I'll add this as a suggestion in the
> documentation.

Cool.

BTW:  Let me say that the more I use logging the more I like it.
logging is a fantastic package!

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


Re: ctypes version mismatch

2008-08-29 Thread Thomas Heller
Paul McNett schrieb:
> Anyone have anything to suggest on this error:
> 
> {{{
> Traceback (most recent call last):
>File "shutter_studio.py", line 41, in 
>File "App.pyo", line 25, in 
>File "ui\__init__.pyo", line 23, in 
>File "ui\FrmProductionOrders.pyo", line 10, in 
>File "ui\PagEditProductionOrders.pyo", line 8, in 
>File "ui\GrdProductionOrderOpenings.pyo", line 5, in 
>File "ui\DlgEditProductionOrderOpening.pyo", line 17, in 
>File "ui\ShutterCanvas.pyo", line 9, in 
>File "wx\lib\floatcanvas\FloatCanvas.pyo", line 6, in 
>File "numpy\__init__.pyo", line 46, in 
>File "numpy\ctypeslib.pyo", line 9, in 
>File "ctypes\__init__.pyo", line 20, in 
> Exception: ('Version number mismatch', '1.0.2', '1.0.3')
> }}}
> 
> I just updated from python 2.5.1 to 2.5.2, and wxPython 2.8.7.1 to
> 2.8.8.1. The issue is only in the py2exe-built app, not when running the
> script from the command line.

I guess you should remove the build and dist directories and rebuild
your app - this is always a good idea when something goes wrong, not only
after upgrading Python or Python packages.

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


Re: logging - how to use in a library?

2008-08-27 Thread Thomas Heller
Vinay Sajip schrieb:

> Suppose a user of logging configures it wrongly by mistake, so that
> there are no handlers configured. In this case, if the logging system
> were not to output anything at all, then you would have no information
> at all about why - leading to a longer time to diagnose the problem.
> That's the only reason why the message is there, and it's output when
> there are no handlers found for an event, and logging.raiseExceptions
> is 1/True (=> a non-production environment), and
> Logger.manager.emittedNoHandlerWarning is 0/False (to avoid printing
> the message multiple times). As Ben Finney has said, an application's
> logging requirements are the determining factor; and I agree that it's
> not a good idea to do logging configuration in the library, which
> could conflict with what an application developer expects. So
> documenting your logging assumpstions is a good approach.

Well, in my library logging is not used to notify the application user
of problems in the library or so, it is used to give the application
developer who is using my library a chance to find out what the library
is doing without the need to step through the code.

So, in my case a call to logger.error or logger.warning does not mean that the
application is using the library in a wrong way, instead it means that
something isn't working in a certain way.  These 'errors' or 'warnings'
are handled by the library itself.

Unexpected exceptions are not catched by the library, of course.


I came up with a workaround that seems to do what I want.  I add a NULL handler
to my top-level logger which is not the root logger but a logger named 
'comtypes',
other loggers are named 'comtypes.something...'.  So the application developer
using the library will not see any comtypes logger messages at all, unless
he calls 'logging.basicConfig()', for example.

> The default level for logging is WARNING, not ERROR - this was judged
> to be a good default level, since under most circumstances we're
> particularly interested in warnings and errors. (A level of WARNING
> catches ERROR and CRITICAL events, too, of course.)

Sure - mistake on my side.

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


logging - how to use in a library?

2008-08-26 Thread Thomas Heller
I'm using the logging module in my comtypes library to log
'interesting' things that happen.  In other words, the idea
is if the user of the library is interested in the details that
happen in the package internally, he (she?) would configure
a logging level and handlers that write the log messages where it
is convenient.

This works great, with one exception:
If the script using the library does NOT configure logging, and somewhere the 
library calls
logger.error(...) or logger.critical(...) then he gets a message on stderr 
saying:

  No handlers could be found for logger "foo"

Of course this can be avoided by configuring a NULL-Handler, or raising the
loglevel to a very high value - but why is this necessary?
I would assume that if no handlers are configured than simply the logging
package should not output anything...

Why does logging insist on a default level of ERROR even if unconfigured, and
why does it insist to output something even if no handler is defined?

I assume it would not be a good idea to configure logging in the library itself,
possibly overwriting explicit configuration that the user has done...  I don't 
get it.

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


Re: CAB files

2008-08-08 Thread Thomas Heller
Virgil Stokes schrieb:
> I would appreciate python code for creating *.cab files.
> 
> --V. Stokes

Here is some code that I have still laying around.  It has never been
used in production and I do not know what you can do with the cab files
it creates, but I have been able to create a cab and open it with winzip.

Thomas


from ctypes import *
import sys, os, tempfile, glob

BOOL = c_int
ULONG = c_ulong
UINT = c_uint
USHORT = c_ushort

class ERF(Structure):
_fields_ = [("erfOper", c_int),
("erfType", c_int),
("fError", BOOL)]

CB_MAX_CHUNK =   32768
CB_MAX_DISK =0x7ff
CB_MAX_FILENAME =   256
CB_MAX_CABINET_NAME =   256
CB_MAX_CAB_PATH =   256
CB_MAX_DISK_NAME =  256

class CCAB(Structure):
_fields_ = [
("cb", ULONG),  # size available for cabinet on this 
media
("cbFolderThresh", ULONG),  # Thresshold for forcing a new Folder
("cbReserveCFHeader", UINT),   # Space to reserve in CFHEADER
("cbReserveCFFolder", UINT),   # Space to reserve in CFFOLDER
("cbReserveCFData", UINT), # Space to reserve in CFDATA
("iCab", c_int),# sequential numbers for cabinets
("iDisk", c_int),   # Disk number
("fFailOnIncompressible", c_int), # TRUE => Fail if a block is 
incompressible
("setID", USHORT),   # Cabinet set ID
("szDisk", c_char * CB_MAX_DISK_NAME),# current disk name
("szCab", c_char * CB_MAX_CABINET_NAME),  # current cabinet name
("szCabPath", c_char * CB_MAX_CAB_PATH),  # path for creating cabinet
]

cab = cdll.cabinet

class HFCI(object):
_handle = 0
_as_parameter_ = property(lambda self: self._handle)

def __init__(self, fnm, verbose=1):
self.verbose = verbose
self.erf = ERF()
ccab = self.ccab = CCAB()
ccab.cb = 1
ccab.cbFolderThresh = 10
ccab.szCab = os.path.basename(fnm)
dirname = os.path.dirname(fnm)
if not dirname:
dirname = "."
ccab.szCabPath = dirname + "\\"
self._init_callbacks()
self._handle = cab.FCICreate(byref(self.erf),
 self.pfn_filedest,
 cdll.msvcrt.malloc,
 cdll.msvcrt.free,
 cdll.msvcrt._open,
 cdll.msvcrt._read,
 cdll.msvcrt._write,
 cdll.msvcrt._close,
 cdll.msvcrt._lseek,
 cdll.msvcrt._unlink,
 self.pfn_gettempfnm,
 byref(ccab),
 None)

def _init_callbacks(self):
self.pfn_gettempfnm = CFUNCTYPE(c_int, c_void_p, c_int, 
c_void_p)(self._gettempfnm)
self.pfn_filedest = CFUNCTYPE(c_int)(self._filedest)
self.pfn_status = CFUNCTYPE(c_int, c_int, c_uint, c_uint, 
c_void_p)(self._status)
self.pfn_getnextcab = CFUNCTYPE(c_int)(self._getnextcab)
self.pfn_getopeninfo = CFUNCTYPE(c_int, c_char_p)(self._getopeninfo)

def _getopeninfo(self, fnm):
if self.verbose:
print "File", fnm
return cdll.msvcrt._open(fnm, os.O_BINARY | os.O_RDONLY)

def _status(self, typeStatus, cb1, cb2, pv):
return 0

def _filedest(self):
return 0

def _getnextcab(self):
return 0

def _gettempfnm(self, pszTempName, cbTempName, pv):
# same as tempfile.mktemp(), but this is deprecated
fh, fnm = tempfile.mkstemp()
os.close(fh)
os.remove(fnm)
cdll.msvcrt.strcpy(pszTempName, fnm)
return 1

def AddFile(self, src, dst=None, compressed=0):
if dst is None:
dst = os.path.basename(src)
cab.FCIAddFile(self,
   src,
   dst,
   0, # fExecute
   self.pfn_getnextcab,
   self.pfn_status,
   self.pfn_getopeninfo,
   compressed)

def Close(self):
if self._handle != 0:
cab.FCIFlushCabinet(self,
0, # fGetNextCab
self.pfn_getnextcab,
self.pfn_status)
cab.FCIDestroy(self)
self._handle = 0

if __name__ == "__main__":
import os, glob

hfci = HFCI("my-first.cab", verbose=1)

files = glob.glob(r".\cab\*.*")

for fnm in files:
hfci.AddFile(fnm)

hfci.Close()

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


Re: ctypes and how to copy data passed to callback

2008-07-28 Thread Thomas Heller
waldek schrieb:
> Hi,
> 
> I'm trying  to handle data passed to Py Callback which is called from
> C dll. Callback passes data to another thread using Queue module and
> there the data are printed out.
> 
> If data is printed out in a callback itself  it's ok. If I put on
> queue and next get from queue in another thread script prints some
> trash. Looks like the data is released when callback returned. I tired
> to make d = copy.deepcopy(data), but it does not work - I got nothing.
> Any idea why it's happening ?
> 
> - main thread  
> def callback(data, size):
> myqueue.put((data, size))
> 
> mydll = cdll.MyDLL
> cbproto = CFUNCTYPE(c_int, POINTER(c_char), c_int)
> mycallback = cbproto(callback)
> 
> mydll.RegisterCallback(mycallback)
> 
> -- thread listener
> --
> 
> while True:
> data, size = myqueue.get()
> print "***", data[:size]
> 
> --

I guess your code would work if you change it in this way:

> def callback(data, size):
> myqueue.put(data[:size])

> while True:
> data = myqueue.get()
> print "***", data

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


Re: Working with ctypes and char** data type

2008-07-24 Thread Thomas Heller
Philluminati schrieb:
> I'm a bit of a python newbie and I need to wrap a C library.
> 
> I can initialise the library using CDLL('mcclient.so')
> 
> and I can call functions correctly inside the library but I need to
> invoke one function which has this function definition:
> 
> char ** CAPAPI McSearch(HMCLINK Handle,
> short nSearchType,
> short nNoSelect,
> char **pSelect,
> short nNoWhere,
> char **pWhere,
> char **pData,
> int iTimeout);
> 
> For **pSelect I want to pass in an array of char points, which in C
> would be declared as
> 
> char *pData[] = { "ADDR", "POSTCODE" };
> 
> Can someone tell me how use pointers + char pointers together in
> python with ctypes please?

# create an array that holds two pointers to 'char *', and fill it with data:
pData = (c_char_p * 2)()
pData[0] = "ADDR"
pData[1] = "POSTCODE"

# Another way:
pData = (c_char_p * 2)("ADDR", "POSTCODE")

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


Re: ctypes and reading value under pointer passed as param of a callback

2008-07-24 Thread Thomas Heller
waldek schrieb:
> Hi,
> 
> I'm using C dll with py module and wanna read value (buffer of bytes)
> returned in py callback as parameter passed to dll function.
> 

The callback receives a pointer instance.  You can dereference the pointer
to read individual bytes in this way:
   print data[0], data[5], data[42]
or use slicing to read a bunch of bytes:
   print data[0:42]

So, you probably want something like this:

> --
> def  mycallback(data, size):
> # how to read data buffer here ?
  print data[:size]
> return 0
> 
> cbfunc = CFUNCTYPE(c_int, POINTER(c_uint8), c_int)
> 
> mydll = cdll.somedll
> mdll.foo(cbfunct)
> ---
> 
> Question: How to get bytes from the buffer passed to mycallback ???
> Let's assume that the buffer consist of 4 bytes 4 bytes 8 bytes.
> 
> I tried unpack("ii8s", data[0]) and nothing. I tried different ctypes
> passed to callback and nothing.

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


Re: python.exe crash and ctypes use

2008-07-23 Thread Thomas Heller
waldek schrieb:
> Hi,
> 
> I have module A.py and B.dll which exports C functions by cdecl_
> 
> In A.py I pass callback (py callable) to dll. Next,  thread inside dll
> simply calls my callback (in a loop). After few secs I got crash of
> python.exe.
> 
> How to debug it?
> 
> I'm using winxp and py 2.5.2
> 
> 
> ===
> def mycallback(data, size)
> return 0
> 
> CBFUNC = CFUNCTYPE(c_int,POINTER(c_int), c_int)
> dll  = cdll.mydll
> 
> if dll.RegisterCallback(CBFUNC(mycallback)) != 0:
> print "Error."
> ===

You need the callback function instance - what the CBFUNC(mycallback)
call returns - alive as long as some C code is calling it.
If you don't sooner or later the Python garbage collector will
free it since it seems to be no longer used.  ctypes does NOT keep
the callback function alive itself.

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


Re: Calling pcre with ctypes

2008-06-18 Thread Thomas Heller
moreati schrieb:
> Recently I discovered the re module doesn't support POSIX character
> classes:
> 
> Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 import re
 r = re.compile('[:alnum:]+')
 print r.match('123')
> None
> 
> So I thought I'd try out pcre through ctypes, to recreate pcredemo.c
> in python. The c code is at:
> http://vcs.pcre.org/viewvc/code/trunk/pcredemo.c?view=markup
> 
> Partial Python code is below
> 
> I'm stuck, from what I can tell a c array, such as:
> int ovector[OVECCOUNT];
> 
> translates to
> ovector = ctypes.c_int * OVECOUNT
> 
> but when I pass ovector to a function I get the traceback
> $ python pcredemo.py [a-z] fred
> Traceback (most recent call last):
>   File "pcredemo.py", line 65, in 
> compiled_re, None, subject, len(subject), 0, 0, ovector, OVECOUNT
> ctypes.ArgumentError: argument 7: : Don't
> know how to convert parameter 7
> 
> What is the correct way to construct and pass ovector?

'ctypes.c_int * OVECOUNT' does not create an array *instance*, it creates an 
array *type*,
comparable to a typedef in C.  You can create an array instance by calling the 
type, with
zero or more initializers:

ovector = (ctypes.c_int * OVECOUNT)(0, 1, 2, 3, ...)

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


Re: Instructions on how to build py2exe 0.6.8 (or an installer would be nice, too!)

2008-06-10 Thread Thomas Heller
[EMAIL PROTECTED] schrieb:
> Hello,
> 
> I'm trying to build an executable with py2exe, but unfortunately the
> version I have is 0.6.6, which has a rather annoying bug that doesn't
> let you rename the executable file if you bundle everything in a
> single executable. It seems fairly unacceptable to tell our customers
> that they can't rename a file we send them.
> 
> I hear this problem is fixed in 0.6.8, but unfortunately there's no
> standalone installer for py2exe 0.6.8 - the most recent version that
> has an installer is 0.6.6 way back from 2006 (!). Is there a
> standalone installer for py2exe 0.6.8 anywhere? If not, how do I build
> it from source? (There's no instructions in the readme - it just says
> "How to install: download the standalone installer" and doesn't
> include building instructions.)

Easy - download the sources, and enter 'python setup.py bdist_wininst' in the
top-level directory.  You need the same compiler that was used to build the
Python that you use.

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


Re: [ctypes] convert pointer to string?

2008-05-21 Thread Thomas Heller
Neal Becker schrieb:
> In an earlier post, I was interested in passing a pointer to a structure to
> fcntl.ioctl.
> 
> This works:
> 
> c = create_string_buffer (...)
> args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
> err = fcntl.ioctl(eos_fd, request, args)
> 
> Now to do the same with ctypes, I have one problem.
> 
> class eos_dl_args_t (Structure):
> _fields_ = [("length", c_ulong),
> ("data", c_void_p)]
> ...
> args = eos_dl_args_t()
> args_p = cast(pointer(args), c_void_ptr).value
> fcntl.ioctl(fd, request, args_p)  <<< May fail here
> 
> That last may fail, because .value creates a long int, and ioctl needs a
> string.
> 
> How can I convert my ctypes structure to a string?  It _cannot_ be a c-style
> 0-terminate string, because the structure may contain '0' values.
> 
ctypes instances support the buffer interface, and you can convert the buffer
contents into a string:

>>> from ctypes import *
>>> c_int(42)
c_long(42)
>>> buffer(c_int(42))

>>> buffer(c_int(42))[:]
'*\x00\x00\x00'
>>>

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


Re: Access to sysctl on FreeBSD?

2008-05-20 Thread Thomas Heller
Skye schrieb:
> Great, thanks for the help (I'm fairly new to Python, didn't know
> about ctypes)
> 
> from ctypes import *
> libc = CDLL("libc.so.7")
> size = c_uint(0)
> libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
> buf = c_char_p(" " * size.value)
> libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

IIUC, sysctlbyname writes the information you requested into the buffer supplied
as second argument.  If this is true, then the above code is incorrect.
c_char_p(src) creates an object that shares the buffer with the Python string 
'src';
but Python strings are immutable and you must not write ctypes code that 
modifies them.

Instead, you should code:

 from ctypes import *
 libc = CDLL("libc.so.7")
 size = c_uint(0)
 libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
 buf = create_string_buffer(size.value)
 libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

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


Re: py2exe Icon Resources

2008-04-30 Thread Thomas Heller
[EMAIL PROTECTED] schrieb:
> I have created an app using python and then converting it to an exe
> using py2exe, and have the following code:
> 
> "icon_resources": [(1, "appFavicon.ico"), (2, "dataFavicon.ico")]
> 
> in my py2exe setup file, the appFavicon works fine and it sets that as
> the app icon thats fine, but the program creates data files (like
> documents) and i wanted them to have a different icon...
> 
> I package my apps using Inno Setup 5, and it registers the file type
> fine so that it loads on double click, what i cant do is set the icon
> for the file.
> 
> as you can see i have packaged two different icons with py2exe but
> when i set DefaultIcon in the registry to "C:\pathtoapp\myapp.exe,2"
> it doesnt work, nor does it work with a 1 or a %1 or indeed with
> passing a path to the icon itself, nothing seems to work!!
> 
> how do you access the other icons generated from py2exe, or how do you
> set the registry up so that i looks for the icon correctly,
> 

There was a problem in the icon resources code in py2exe which was
recently fixed in CVS but there is not yet a binary release of this code.
Maybe it solves your problem...

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


Re: Ignoring windows registry PythonPath subkeys

2008-04-04 Thread Thomas Heller
Floris Bruynooghe schrieb:
> Hi
> 
> We basically want the same as the OP in [1], i.e. when python starts
> up we don't want to load *any* sys.path entries from the registry,
> including subkeys of the PythonPath key.  The result of that thread
> seems to be to edit PC/getpathp.c[2] and recompile.
> 
> This isn't that much of a problem since we're compiling python anyway,
> but is that really still the only way?  Surely this isn't such an
> outlandish requirement?

If you look into PC/getpathp.c *and* PC/dl_nt.c, you'll find that the registry 
key name
if constructed from static components plus a variable component named 
PyWin_DLLVersionString.
The latter is loaded from a string resource (with resource ID 1000, IIRC) 
inside the pythonXY.dll.

This string resource can be changed (even without compiling!); so this is a way
for you to force the lookup of PythonPath to a different registry key.  You can 
choose
something that probably does not exist.

py2exe does this also for 'frozen' executables, and so has complete control
over sys.path.

Thomas

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


Re: State of ctypes Support on HP-UX?

2008-04-03 Thread Thomas Heller
Phil Thompson schrieb:
> On Thursday 03 April 2008, Thomas Heller wrote:
>> Phil Thompson schrieb:
>> > Could somebody confirm how well ctypes is supported on HP-UX (for both
>> > PA-RISC and Itanium) for both Python v2.4 and v2.5?

>> I cannot answer your question, but if you want to try it out
>> yourself there is the HP testdrive program: http://www.testdrive.hp.com/
> 
> Thanks for the pointer. Unfortunately the answer is that there is no support 
> (at least for ctypes v1.0.2).

I tried out the current SVN version of Python myself.  ctypes doesn't compile
on the PA system (the libffi assembler code fails to compile),  but I did get
it to work on the Itanium system with gcc (I had to set LDSHARED="gcc -shared"
before configuring).  Even the ctypes unittests pass on this system.

In theory, the ctypes code should be backwards-compatible with python 2.4, 
although
in practice it currently is not, but IMO it should be possible to change it 
accordingly.

Would this be useful to you?

Thomas

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


Re: State of ctypes Support on HP-UX?

2008-04-03 Thread Thomas Heller
Phil Thompson schrieb:
> Could somebody confirm how well ctypes is supported on HP-UX (for both 
> PA-RISC 
> and Itanium) for both Python v2.4 and v2.5?
> 
> I don't have access to an HP system and Google doesn't come up with a 
> definitive answer (which may just mean it works fine, but prior experience 
> with HP means I'd like more specific assurances).
> 
> Thanks,
> Phil

I cannot answer your question, but if you want to try it out
yourself there is the HP testdrive program: http://www.testdrive.hp.com/

Thomas

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


Re: py2exe socket.gaierror (10093)

2008-03-26 Thread Thomas Heller
Knut schrieb:
>> The script can't resolve the server name. Try to do it by hand using
>> nslookup or even ping (you may want to add a few print statements inside
>> the script to see the exact host name it is trying to connect to, in case
>> it isn't what you expect)
>> If you can't resolve the host name using nslookup, there is a network
>> problem, not in your script. If you can resolve it, try your script
>> without py2exe if possible.
>>
>> --
>> Gabriel Genellina
> 
> Thank you for the quick reply Gabriel.
> 
> I have made sure the script works fine before I exe it.
> It is when I compile the program I get this error.
> 
> I don't get how the compile changes server availability.
> 

Could it be a firewall issue?

Thomas

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


Re: comtypes question

2008-03-17 Thread Thomas Heller
Jorgen Bodde schrieb:
> Hi All,
> 
> I am trying to automate a 3rd party application, and all I have to
> work on is the type library and some documentation. I hope a Python /
> COM guru can answer this or put me on the right path because I don't
> know why it does not work.
> 
> First I imported the typelib with comtypes like;
> 
>>> from comtypes.client import GetModule
>>> GetModule("C:\\Program Files\\Seagull\\BarTender\\8.0\\bartend.exe")
> 
> Which blurbs out a lot of text:
> 
> # Generating comtypes.gen._D58562C1_E51B_11CF_8941_00A024A9083F_0_8_1
> # Generating comtypes.gen.BarTender
>  'C:\Python24\lib\site-packages\comtypes\gen\_D58562C1_E51B_11CF_8941_00A024A9083F_0_8_1.pyc'>
> 
> Which seems to be ok. Now, I need to call a function on the
> Application object on which I need a "Messages" instance. The
> Application object gets created properly:
> 
>>> import comtypes.gen.Bartender as bt
>>> app = comtypes.client.CreateObject(bt.Application)
> 
> Which gives me the application (the bt.Application points to a wrapper
> class containing the CLSID of the COM class to be instantiated).
> 
> Now I browse the typelibrary and I see there is a CoClass called
> Messages. I need one of those to pass as an argument, and since
> Messages is listed as a CoClass similar to Application, I assume it
> can also be instantiated. But when I try this I get an error:
> 
 msgs = cc.CreateObject('{2B52174E-AAA4-443D-945F-568F60610F55}')

[...]

> WindowsError: [Errno -2147221164] Class not registered
> 
> Both Application and Messages are listed as CoClass inside the
> typelibrary. Does anybody know  why it gives me this error and what I
> am doing wrong?

Not all coclasses can be created from scratch with CreateObject.  Sometimes
they are created by calls to methods on some other object.
I downloaded the bartender trial, and looking into the typelib it seems that,
for example, the 'XMLScript' method on the IBtApplication interface returns
Messages instances:
COMMETHOD([dispid(17), helpstring(u'Runs xml scripts')], HRESULT, 
'XMLScript',
  ( ['in'], BSTR, 'XMLScript' ),
  ( ['in'], BtXMLSourceType, 'SourceType' ),
  ( ['out'], POINTER(POINTER(Messages)), 'Messages' ),
 
  ( ['retval', 'out'], POINTER(BSTR), 'retval' )),

Here is an interactive session:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from comtypes.gen import BarTender
>>> from comtypes.client import CreateObject
>>> app = CreateObject(BarTender.Application)
>>> print app

>>> app.XMLScript("foo", 0)
(, u'')
>>> msg, retval = app.XMLScript("foo", 0)
>>> print msg

>>> msg[0]
Traceback (most recent call last):
  File "", line 1, in 
  File "comtypes\__init__.py", line 308, in __getitem__
result = self.Item(index)
_ctypes.COMError: (-2147220992, None, (u'The item at position 0 was not 
found.', u'bartend', None, 0, None))
>>> msg[1]

>>> m = msg[1]
>>> print m

>>> m.Number
3908
>>> m.Message
u"An error occurred during XML script processing:\r\n\r\nInvalid at the top 
level of the document.\r\nLine 1, Column 1:
'foo'"
>>> ^Z

Not that the above makes much sense, but I hope it will get you started.

Thomas

BTW:  The 'official' comtypes mailing list is at
https://lists.sourceforge.net/lists/listinfo/comtypes-users.  Requires that you 
subscribe before you can
post, but you could disable mail delivery and use via gmane: 
gmane.comp.python.comtypes.user

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


Re: app runs fine with interpreter, but not under py2exe

2008-03-16 Thread Thomas Heller
Doug Morse schrieb:
> Peter,
> 
> Genius!  You nailed it -- thanks!
> 
> py2exe is apparently getting confused by the fact that packages "Numeric" and
> "numpy" both have files multiarray.pyd and umath.pyd.  It copies just one of
> each -- from $PYTHONHOME/Lib/site-packages/numpy/core -- and puts both of them
> into the top-level of the created "dist" directory.  Also, there are no such
> files as multiarray.pyc and umath.pyc in my python installation, but py2exe
> seems to create these (in the dist and dist/numpy/core directories) -- they
> are small (e.g., 526 bytes) and I'm guessing that they are stubs that simply
> point python to the matching .pyd that py2exe relocated.
[...]
> 
> Just for completeness and perhaps a bit of additional clarity, here's a
> limited directory listing of what the steps in the previous paragraph produce:
> 
> morse> find dist | egrep "(multia|umath)" | xargs ls -l
> -rwxr-xr-x 1 morse None  26624 Nov 28  2006 dist/multiarray.pyd
> -rwxr-xr-x 1 morse None 348160 Nov  8 16:16 dist/numpy/core/multiarray.pyd
> -rwxr-xr-x 1 morse None 192512 Nov  8 16:16 dist/numpy/core/umath.pyd
> -rwxr-xr-x 1 morse None  54272 Nov 28  2006 dist/umath.pyd
> 
[...]
> Is this something I should pursue getting the py2exe folks to fix / work with
> them on fixing?  In investigating this issue, I noted that a lot of other
> py2exe problems seem to revolve around confusions when duplicate files exist
> in different modules.  I wouldn't thinking that getting py2exe to pay better
> attention to the containing modules when building it's dependency tree would
> be that difficult (or is it)?

I have the impression that this issue may already be fixed in py2exe CVS but
noone bothered to do a release.  Here is the corresponding changelog entry:

* build_exe.py: Patch from Grant Edwards, slightly adjusted:
py2exe now renames the pyd files that it copies into the dist
directory so that they include the package name.  This prevents
name conflicts.

I have created installers and uploaded them (for testing!) to the starship:

http://starship.python.net/crew/theller/py2exe-0.6.7.win32-py2.3.exe
http://starship.python.net/crew/theller/py2exe-0.6.7.win32-py2.4.exe
http://starship.python.net/crew/theller/py2exe-0.6.7.win32-py2.5.exe

Please try them out.

Thanks,
Thomas

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


Re: Why this ref leak?

2008-02-27 Thread Thomas Heller
Gerhard Häring schrieb:
> import sys
> 
> def foo():
>  class C(object):
>  pass
> 
> foo()
> print ">>", sys.gettotalrefcount()
> foo()
> print ">>", sys.gettotalrefcount()
> foo()
> print ">>", sys.gettotalrefcount()
> 
>  >> 21366
>  >> 21387
>  >> 21408
> [9779 refs]
> 
> Both Python 2.4 and 2.5 don't clean up properly here. Why is this? 
> Aren't classes supposed to be garbage-collected?
> 
> -- Gerhard

Replace "foo()" with "foo(); gc.collect()" and the refcounts are stable.
Tested the python 2.6 from trunk.

Thomas

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


Re: Windows AVIFile problems

2008-01-25 Thread Thomas Heller
c d saunter schrieb:
> Hi All,
> 
> I'm trying to access individual video frames of an AVI file from within 
> Python 2.4 or 2.5 under Windows XP.
> 
> I have found this example code here for that does exactly what I want,
> using the windows avifile.dll but I am unable to find the AVIFile.h 
> header...
> 
> http://mail.python.org/pipermail/image-sig/2002-February/001748.html
> 
> An alternative is to call into avifile.dll dynamically using ctypes, 
> however under both Python 2.4 and 2.5 the following error happens:
> 
 from ctypes import *
 windll.AVIFile
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> windll.AVIFile
>   File "C:\Python25\lib\ctypes\__init__.py", line 415, in __getattr__
> dll = self._dlltype(name)
>   File "C:\Python25\lib\ctypes\__init__.py", line 340, in __init__
> self._handle = _dlopen(self._name, mode)
> WindowsError: [Error 193] %1 is not a valid Win32 application
> 

The dll is not corrupt.  It is the 16-bit dll, possibly present for legacy
16-bit support.

You must use the 32-bit dll, it seems it called avifil32.dll.

Also I guess to get the AVIFILE.H header file, you need to install some
MS SDK or the platform sdk.

Thomas

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


Re: Python and binary compatibility

2008-01-24 Thread Thomas Heller
Ambush Commander schrieb:
> I'm a newbie to Python; various packages I've used in the past (Lyx,
> LilyPond and Inkscape, to name a few) have bundled Python with them
> for various scripting needs, and Cygwin also had an install lying
> around, so when I started to use Mercurial (also Python) I decided
> that I'd consolidate all of these installations into a single Windows
> installation for general use, as well as for me to properly learn the
> language.
> 
> Whoo, it's been a journey.
> 
> The primary problem involves binary extensions to the Python
> interpreter itself, which Mercurial uses. The only C compiler I have
> on my machine is Visual Studio 2005 Express, but Python's binary
> distribution was compiled with VS 2003, so the installer refuses to
> compile the package. I understand that Python 3 uses VS 2008, but
> that's no good for me as it will probably break all of the scripts.
> 
> So, I'm trying to figure out what I should do. Mercurial's binary
> distribution was built using MingW, and I do have Cygwin lying around
> but I'd like to go for the "native" solution for the most speed. If I
> use MingW, I might as well use their pre-packaged binary. I could
> recompile Python with MSVC 2005, but I expect that will be its own can
> of worms. ActiveState is closed source and appears to have the wrong
> MSVC dependencies. All my troubles could apparently be fixed if I
> could acquire a copy of VS 2003, but Microsoft has made it incredibly
> difficult to find the download for it (I don't think it exists).
> 
> Any suggestions?

Maybe this helps?

http://www.develer.com/oss/GccWinBinaries

Thomas

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


Re: ctypes CDLL - which paths are searched?

2008-01-22 Thread Thomas Heller
Helmut Jarausch schrieb:
> Thomas Heller wrote:
>> Helmut Jarausch schrieb:
>>> Hi,
>>>
>>> how can I specify the paths to be searched for a dynamic library
>>> to be loaded by ctypes' CDLL class on a Linux system.
>>>
>>> Do I have to set os.environment['LD_LIBRARY_PATH'] ?
>>>
>> 
>> ctypes passes the argument given to CDLL(path) straight to
>> the dlopen(3) call, so your system documentation should tell you.
>> 
> 
> Thanks,
> 
> but then it's difficult to use CDLL. Setting
> os.environ['LD_LIBRARY_PATH'] within the script which
> calls CDLL is too late.
> What other methods are possible rather than put an explicit
> export LD_LIBRARY_PATH=...
> before running the script, if I don't want to put the dynamic
> library into a standard system library.

I guess you can also use an absolute pathname (but the dlopen(3)
manpage should tell you more.  I'm not too familiar with linux).

Thomas

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


  1   2   3   4   5   >