ANN: pyfromc - Python from C++ and vice versa

G

Gerson Kurz

Imagine the following situation:

- You have a existing C++ application. You want to migrate parts of it
to python. That is, you want to be able to call python code from C++
code.
- You also want to be able to call C++ code from python.
- Your C++ code is multithreaded and there is no guarantee that any
one thread calling python has initialized the interpreter.
- You need to distribute the resulting app with minimum overhead.

Enter pyfromc. It is a sample Win32 C++ application using an embedded
python interpreter, to be used as a potential starting point.

You can download pyfromc here:

http://p-nand-q.com/python/pyfromc-1.0.exe

Note that the installer is only 692.031 bytes large, but includes the
c++ sample app, the embedded python interpreter and the full
sourcecode.

License
There is none. Use at own risk.

*** Calling Python from C++ ***

pyfromc.py is a sample python code that exposes two global functions,
test and dumpmods. The C++ class Python inside pyfromc.cpp/.h defines
wrapper methods for these calls. E.g. if you want to add a new method
to pyfromc.py, you must also define a wrapper method in pyfromc.h and
implement it in pyfromc.cpp.

The C++ wrapper method uses the helper class PythonCall, which in turn
uses techniques described in the Python manual, section Embedding
Python in Another Application.

*** Calling C++ from Python ***

Basically, cfrompy.cpp is a standard python module, as described in
the python manual, section Extending Python with C or C++ . The only
difference is that cfrompy.cpp is not a DLL, it is part of the
executable code you write, so you have easier access to your existing
C++ code.

*** Thread safety ***

The code is (or rather: should be) threadsafe. That is, you should be
able to call python code from any native Win32 thread, without any
additional precautions (provided your C++ code is already threadsafe).
The code uses the techniques described in PEP 311 - Simplified Global
Interpreter Lock Acquisition for Extensions.

*** Distribution your app ***

Starting with python 2.3, you can distribute modules in a zipfile,
called "python23.zip", which must be in the same directory as
python23.dll. So, what you need is

- Your C++ app
- python23.dll
- all modules you need encoded in python23.zip
- all .pyd extensions you need (cannot be stored in python23.zip
because loaded as a DLL)

The only problem is: how to find out what modules you need. Well,
enter dumpmods.py, a small python script that enumerates all loaded
modules and dumps them into a target directory. Usage: When your
program ends, add these two lines:

import dumpmods
dumpmods.analyze_modules(path to store files in)

(Homepage for this code: http://p-nand-q.com/python/pyfromc.html)
where "path to store files in" has obvious connotations.
 
P

Peter Hansen

Gerson said:
License
There is none. Use at own risk.

These days, one should probably interpret that as meaning that
it includes the risk that the author will someday decide to
come and sue you for using the product, or will decide to go
commercial and claim that you no longer have the right to use
the product.

Gerson, why not browse http://www.opensource.org/licenses/
and pick one of those as the official license under which you
are releasing pyfromc, to make it easier for people to understand
what you meant to say by "There is none. Use at own risk."
("No license" really suggests that you are not allowing anyone
to use your code for any purpose, which contradicts the implied
permission in the second sentence.)

May I suggest this one http://www.opensource.org/licenses/mit-license.php
as perhaps the closest match to your apparent intent?

-Peter
 
G

Gerson Kurz

Peter said:

I find "licensing" example code, at best, ridiculous. And that's
basically all it is: it is an example, a starting point - it contains
only one method "test". Please. It took some work to figure out how to
exactly put the pieces together - but now that it works its almost
trivial. Licensing example code is anal-retentive, over-protective and
paranoid. There are way too many licenses attached to way too much
unlicenseworthy stuff, just for the sake of license fetishism, and, I
must say, "open source" has been not completely innocent in this
development. I don't care if you manage to *sell* an example code that
is freely available on the internet - if you do manage to sell it,
hell, you *deserve* the money.

Some years ago I wrote a joke language, SMITH#

http://p-nand-q.com/humor/programming_languages/smith.html

It is a joke, right, it is in my humor section. Well, I got contacted
by debian-legal and you can read the exchange here:

http://lists.debian.org/debian-legal/2001/debian-legal-200110/msg00170.html

Debian actually has a list devoted to legal issues. I don't know about
you, but I find that perverted, for a distribution that prides itself
on its freedom.

Back in the 80s, when I started programming on the Atari & Amiga,
before the GPL became any public issue, we had three kinds of licenses
: None, Public Domain and Other. And, guess what, I "published"
software (say, in 1994 - be aware, I was kind of emotional back then -
http://wuarchive.wustl.edu/aminet/util/moni/snoopy20.readme) and have
never sued anybody since, nor been sued by anyone. Life is good!

So, relax.
 
P

Peter Hansen

Gerson said:
I find "licensing" example code, at best, ridiculous. And that's
basically all it is: it is an example, a starting point - it contains
only one method "test". Please. It took some work to figure out how to
exactly put the pieces together - but now that it works its almost
trivial.

Sorry, I didn't notice that you had said it was example code. Sounded
like there was a lot more to it than that...

-Peter
 
T

Terry Reedy

Gerson Kurz said:
I find "licensing" example code, at best, ridiculous. And that's
basically all it is: it is an example, a starting point - it contains
only one method "test". Please. It took some work to figure out how to
exactly put the pieces together - but now that it works its almost
trivial. Licensing example code is anal-retentive, over-protective and
paranoid. There are way too many licenses attached to way too much
unlicenseworthy stuff, just for the sake of license fetishism,

I agree with you that the legal stuff is pretty nasty. But you
perhaps underestimate the protective aspect of minimal o.s. licenses.
Your statement "Use at own risk." *is* a license term. It is, in
summary, the second of two conditions in the MIT license pointed to by
Peter:
http://www.opensource.org/licenses/mit-license.php
(The first, to acknowledge authorship, really is hardly applicable to
an example that would have to be reworked).
Back in the 80s, when I started programming on the Atari & Amiga,
before the GPL became any public issue, we had three kinds of licenses
: None, Public Domain and Other.

Public Domain is *not* a license, but a disclaimer of license with
unilateral permission to use. Such donations, being *unconditional*,
allow false claims of authorship and do nothing to prevent
'anti-good-Samaritan' lawsuits. Software authors, in the U.S. at
least, increasingly feel the need for 'good-Samaritan' no-sue clauses
for the same reason doctors want 'good-Samaritan' no-sue laws.

The current Caldera-Sco Unix v. Linux lawsuits and $$$ demands
suggests that programmer paranoia was not so paranoid afterall.

Terry J. Reedy
 
J

JanC

Terry Reedy said:
Public Domain is *not* a license, but a disclaimer of license with
unilateral permission to use.

AFAIK in Belgium (and several other European countries) you can't legally
put your own work in the public domain; an author always keeps his or her
"author rights" (which includes copyright).
 
A

Andrew Dalke

JanC:
AFAIK in Belgium (and several other European countries) you can't legally
put your own work in the public domain; an author always keeps his or her
"author rights" (which includes copyright).

Is there work for hire, so that the copyright goes to the employer?
Can the employer sell that copyright to someone else?

Are government works covered under copyright? In the US, they
are not, and are always in the public domain.

Can works enter the public domain after a period of time?

Andrew
(e-mail address removed)
 
J

JanC

Andrew Dalke said:
JanC:

Is there work for hire, so that the copyright goes to the employer?
Can the employer sell that copyright to someone else?

Yes, in case of work-for-hire, the copyright is with the employer, and they
can sell it (they aren't authors). And authors can always license the
right to make copies to someone else, they just can't give away the
ultimate right to do that. :)
Are government works covered under copyright? In the US, they
are not, and are always in the public domain.

I'm not sure about that. Public domain would that mean you can legally
make copies of driver licenses? Don't think that's allowed... :p
Can works enter the public domain after a period of time?

Yes, but that's way too long for a computer program to be useful...
(Something like 70 years I guess.)
 
P

Peter Otten

JanC said:
Yes, but that's way too long for a computer program to be useful...
(Something like 70 years I guess.)

I think it was 70 after the author's death, so *I* wouldn't be to keen to
make it that *short*...

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,416
Messages
2,571,562
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top