mirror of
https://github.com/boostorg/python.git
synced 2026-01-22 17:32:55 +00:00
Added to/from_python converters for [plain] char.
Reverted to original converters for un/signed char. [SVN r9046]
This commit is contained in:
@@ -62,6 +62,7 @@ $(BPL_EXA)/getting_started2.cpp \
|
||||
$(BPL_EXA)/getting_started3.cpp \
|
||||
$(BPL_EXA)/getting_started4.cpp \
|
||||
$(BPL_EXA)/getting_started5.cpp \
|
||||
$(BPL_EXA)/passing_char.cpp \
|
||||
$(BPL_EXA)/ivect.cpp \
|
||||
$(BPL_EXA)/ivect.h \
|
||||
$(BPL_EXA)/noncopyable_export.cpp \
|
||||
@@ -73,6 +74,7 @@ $(BPL_EXA)/test_getting_started2.py \
|
||||
$(BPL_EXA)/test_getting_started3.py \
|
||||
$(BPL_EXA)/test_getting_started4.py \
|
||||
$(BPL_EXA)/test_getting_started5.py \
|
||||
$(BPL_EXA)/tst_passing_char.py \
|
||||
$(BPL_EXA)/tst_dvect.py \
|
||||
$(BPL_EXA)/tst_ivect.py \
|
||||
$(BPL_EXA)/tst_noncopyable.py
|
||||
@@ -86,6 +88,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \
|
||||
all: libbpl.a test.so abstract.so \
|
||||
getting_started1.so getting_started2.so getting_started3.so \
|
||||
getting_started4.so getting_started5.so \
|
||||
passing_char.so \
|
||||
noncopyable_export.so noncopyable_import.so \
|
||||
ivect.so dvect.so
|
||||
|
||||
@@ -138,6 +141,9 @@ getting_started4.so: $(OBJ) getting_started4.o
|
||||
getting_started5.so: $(OBJ) getting_started5.o
|
||||
$(LD) $(LDOPTS) $(OBJ) getting_started5.o -o getting_started5.so
|
||||
|
||||
passing_char.so: $(OBJ) passing_char.o
|
||||
$(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so
|
||||
|
||||
noncopyable_export.so: $(OBJ) noncopyable_export.o
|
||||
$(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so
|
||||
|
||||
@@ -173,6 +179,7 @@ clean:
|
||||
rm -f getting_started3.o getting_started3.so
|
||||
rm -f getting_started4.o getting_started4.so
|
||||
rm -f getting_started5.o getting_started5.so
|
||||
rm -f passing_char.o passing_char.so
|
||||
rm -f noncopyable_export.o noncopyable_export.so
|
||||
rm -f noncopyable_import.o noncopyable_import.so
|
||||
rm -f ivect.o ivect.so
|
||||
|
||||
@@ -65,6 +65,7 @@ $(BPL_EXA)/getting_started2.cpp \
|
||||
$(BPL_EXA)/getting_started3.cpp \
|
||||
$(BPL_EXA)/getting_started4.cpp \
|
||||
$(BPL_EXA)/getting_started5.cpp \
|
||||
$(BPL_EXA)/passing_char.cpp \
|
||||
$(BPL_EXA)/ivect.cpp \
|
||||
$(BPL_EXA)/ivect.h \
|
||||
$(BPL_EXA)/noncopyable_export.cpp \
|
||||
@@ -76,6 +77,7 @@ $(BPL_EXA)/test_getting_started2.py \
|
||||
$(BPL_EXA)/test_getting_started3.py \
|
||||
$(BPL_EXA)/test_getting_started4.py \
|
||||
$(BPL_EXA)/test_getting_started5.py \
|
||||
$(BPL_EXA)/tst_passing_char.py \
|
||||
$(BPL_EXA)/tst_dvect.py \
|
||||
$(BPL_EXA)/tst_ivect.py \
|
||||
$(BPL_EXA)/tst_noncopyable.py
|
||||
@@ -89,6 +91,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \
|
||||
all: libbpl.a test.so abstract.so \
|
||||
getting_started1.so getting_started2.so getting_started3.so \
|
||||
getting_started4.so getting_started5.so \
|
||||
passing_char.so \
|
||||
noncopyable_export.so noncopyable_import.so \
|
||||
ivect.so dvect.so
|
||||
|
||||
@@ -143,6 +146,9 @@ getting_started4.so: $(OBJ) getting_started4.o
|
||||
getting_started5.so: $(OBJ) getting_started5.o
|
||||
$(LD) $(LDOPTS) $(OBJ) getting_started5.o -o getting_started5.so
|
||||
|
||||
passing_char.so: $(OBJ) passing_char.o
|
||||
$(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so
|
||||
|
||||
noncopyable_export.so: $(OBJ) noncopyable_export.o
|
||||
$(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so
|
||||
|
||||
@@ -178,6 +184,7 @@ clean:
|
||||
rm -f getting_started3.o getting_started3.so
|
||||
rm -f getting_started4.o getting_started4.so
|
||||
rm -f getting_started5.o getting_started5.so
|
||||
rm -f passing_char.o passing_char.so
|
||||
rm -f noncopyable_export.o noncopyable_export.so
|
||||
rm -f noncopyable_import.o noncopyable_import.so
|
||||
rm -f ivect.o ivect.so
|
||||
|
||||
52
example/passing_char.cpp
Normal file
52
example/passing_char.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
// In C++, char, signed char and unsigned char are three distinct types.
|
||||
// The Boost Python Library maps signed & unsigned char to
|
||||
// Python integers. Plain char is mapped to a Python string with
|
||||
// exactly one character.
|
||||
|
||||
// Plain char.
|
||||
char get_char() { return 'ÿ'; }
|
||||
void use_char(char c) {
|
||||
std::cout << c << std::endl;
|
||||
}
|
||||
|
||||
// signed char.
|
||||
signed char get_signed_char() { return -128; }
|
||||
void use_signed_char(signed char c) {
|
||||
std::cout << c << " " << static_cast<int>(c) << std::endl;
|
||||
}
|
||||
|
||||
// unsigned char.
|
||||
unsigned char get_unsigned_char() { return 128; }
|
||||
void use_unsigned_char(unsigned char c) {
|
||||
std::cout << c << " " << static_cast<unsigned int>(c) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#include <boost/python/class_builder.hpp>
|
||||
namespace python = boost::python;
|
||||
|
||||
extern "C"
|
||||
DL_EXPORT(void)
|
||||
initpassing_char()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("passing_char");
|
||||
|
||||
this_module.def(get_char, "get_char");
|
||||
this_module.def(use_char, "use_char");
|
||||
this_module.def(get_signed_char, "get_signed_char");
|
||||
this_module.def(use_signed_char, "use_signed_char");
|
||||
this_module.def(get_unsigned_char, "get_unsigned_char");
|
||||
this_module.def(use_unsigned_char, "use_unsigned_char");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
20
example/tst_passing_char.py
Normal file
20
example/tst_passing_char.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import passing_char
|
||||
print passing_char.get_char()
|
||||
print passing_char.get_signed_char()
|
||||
print passing_char.get_unsigned_char()
|
||||
for arg in (-97, 97, -140, 140, "a", "ab"):
|
||||
try:
|
||||
print 'char', arg, ':'
|
||||
passing_char.use_char(arg)
|
||||
except (TypeError, ValueError), e:
|
||||
print e.args[0]
|
||||
try:
|
||||
print 'signed char', arg, ':'
|
||||
passing_char.use_signed_char(arg)
|
||||
except (TypeError, ValueError), e:
|
||||
print e.args[0]
|
||||
try:
|
||||
print 'unsigned char', arg, ':'
|
||||
passing_char.use_unsigned_char(arg)
|
||||
except (TypeError, ValueError), e:
|
||||
print e.args[0]
|
||||
Reference in New Issue
Block a user