Embedded Python 3 porting issues when passing FILE* to PyRun_SimpleFile() in Windows mixed-compiler environment

2012-10-04 Thread Deron Meranda
Hi.  I'm trying to convert a large C application that embeds Python so it works 
with Python 3, and am running into an new API limitation I can't solve.  I have 
a report from Windows users that there is a crashing problem occurring with 
calls to PyRun_SimpleFile (and similar functions).  This appears to happen when 
people are using a Python DLL compiled with one compiler (e.g., MicroSoft's) 
and the application with another compiler (e.g., MinGW/gcc).

The basic problem is that C type (FILE*) is not defined by the Windows OS, but 
instead is specific to the compiler and its associated runtime-environment. So 
therefore it is not safe to pass a FILE* across DLL boundaries.  However many 
of the Python C API functions only accept a FILE*.

Under Python 2 a work-around was used by calling the PyFile_FromString() then 
PyFile_AsFile(), to effectively get the underlying fopen() to occur inside the 
Python DLL so that an incompatible FILE* is not passed across the DLL boundary.

However many of those PyFile functions, including PyFile_FromFile(), were 
removed from the Python 3 API.  So how can one safely call PyRun_SimpleFile() 
in Python 3 in Windows where different compilers could be used?

Thanks
-- 
Deron Meranda
http://deron.meranda.us/
-- 
http://mail.python.org/mailman/listinfo/python-list


Need an identity operator because lambda is too slow

2007-02-17 Thread Deron Meranda
This is an optimization problem, one that occurs deep inside a loop
that gets executed thousands of times.  I'm looking for something in
Python which would act like an identity operator, or I-combinator: a
do-nothing function.  The best I know of is:  (lambda x: x), but it is
not ideal.

Consider a much-simplified example of such an iteration where
sometimes you need to transform an item by some function, but most of
the time you don't:

if some_rare_condition:
func = some_transform_function
else:
func = lambda x: x
for item in some_sequence:
item2 = func(item)
. # more stuff

Now the "lambda x:x" acts suitably like an identity operator.  But it
is very slow, when compared to using more complex-looking code:

do_transform = some_rare_condition
for item in some_sequence:
if do_transform:
item2 = transform_function(item)
else:
item2 = item
. # more stuff

What python needs is something like a built-in "operator.identity"
function, which acts like "lambda x:x", but which the byte compiler
could recognize and completely optimize away so there is no function
call overhead.

Does this exist someplace in the corners of the language, or are there
any ideas about how best to do something like this without having to
push a bunch of ugly if-then-else constructs inside all the loops
rather than a more elegant function object?  (The need for this is
more obvious in my real-world code versus the simplified examples
above).

Thanks
--
Deron Meranda

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