473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

translating C++ exceptions to python

Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function ()
except cpp_exception_1 :
do_stuff
except cpp_exception_2 :
do_other_stuff

any ideas how can i do the translation?
Thanks, calin

Jul 19 '05 #1
2 1703
On 13 Jun 2005 04:23:03 -0700
ca************* *@gmail.com wrote:
Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function ()
except cpp_exception_1 :
do_stuff
except cpp_exception_2 :
do_other_stuff

any ideas how can i do the translation?


1. Create Python class for your exception. For simple case the code
will be:

static PyObject *YouExceptionCl ass;

# and in module initialization function:
YouExceptionCla ss = PyErr_NewExcept ion("YourModule .YourException" , 0, 0);

2. Add it to module dictionary.

3. In wrapper for my_cpp_function use something like the following
code:

try {
my_cpp_function _impl();
} catch (YouException &exc) {
PyErr_SetString (YouExceptionCl ass, exc.what());
return 0;
}

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 19 '05 #2

On 13.06.2005, at 13:23, ca************* *@gmail.com wrote:
Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function ()
except cpp_exception_1 :
do_stuff
except cpp_exception_2 :
do_other_stuff

any ideas how can i do the translation?


If you do not already use it, have a look at
http://www.boost.org/libs/python/ a C++ -- library to wrap the
Python C API, i.e. it helps you to extend Python in C++.
AFAIK it has fascilities to transform exceptions from one type
into the other.

- harold -

---
Everybody lies. but it does not matter, as no one listens.
---

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1413
by: Barry Warsaw | last post by:
Python 2.3b2 is the second beta release of Python 2.3. There have be a slew of fixes since the first beta, and a few new "features". Our goal is to have a final Python 2.3 release by early August, so we encourage lots of testing for this beta. Highlights since beta 1 include: - IDLEfork has been merged in and now replaces the old IDLE. - The Windows installer now ships with Tcl/Tk 8.4.3. - list.index() has grown optional `start'...
29
3458
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully. I'm wondering if something of such had been attempted in python, as in compiling X language into .pyc. I do not understand the schematics of .pyc files but I assume that they are the so called python bytecode files. Or is there any...
10
3694
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
0
1119
by: Simon Brunning | last post by:
QOTW: "Python is more concerned with making it easy to write good programs than difficult to write bad ones." - Steve Holden "Scientists build so that they can learn. Programmers and engineers learn so that they can build." - Magnus Lycka "It happens that old Java programmers make one module per class when they start using Python. That's more or less equivalent of never using more than 8.3 characters in filenames in modern operating...
6
2269
by: Gonzalo Monzón | last post by:
Hi all! I have been translating some Python custom C extension code into Python, as I need these modules to be portable and run on a PocketPC without the need of compile (for the purpose its a must 2.4 as it is the last PythonCE release with great improvements). But I've been stuck with a script wich does not work as expected once translated to python, 2.4
2
5296
by: pillappa | last post by:
Hi, I am hitting this error consistently and don't know why it's happening. I would like to define all exceptions for my project in one file and use them across the project. Here's a sample - exceptions.py - class MyException(StandardError): def __init__(self, error):
28
5896
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every exception message. My naive solution was this: try: ... except Exception, e: raise e.__class__, str(e) + ", sorry!"
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.