Commit eb5fc339 authored by Robert Bradshaw's avatar Robert Bradshaw

Don't require typeinfo cimport for typeid.

parent 89723dc5
......@@ -27,6 +27,8 @@ Features added
* ``libc/math.pxd`` provides ``e`` and ``pi`` as alias constants to simplify
usage as a drop-in replacement for Python's math module.
* Support for the C++ ``typeid`` operator.
Bugs fixed
----------
......
......@@ -10204,16 +10204,12 @@ class TypeidNode(ExprNode):
is_temp = 1
def get_type_info_type(self, env):
if env.is_module_scope:
env_module = env
else:
env_module = env.outer_scope
for module in env_module.cimported_modules:
if module.qualified_name == 'libcpp.typeinfo':
type_info = module.lookup('type_info')
type_info = PyrexTypes.CFakeReferenceType(PyrexTypes.c_const_type(type_info.type))
return type_info
return None
env_module = env
while not env_module.is_module_scope:
env_module = env_module.outer_scope
typeinfo_module = env_module.find_module('libcpp.typeinfo', self.pos)
typeinfo_entry = typeinfo_module.lookup('type_info')
return PyrexTypes.CFakeReferenceType(PyrexTypes.c_const_type(typeinfo_entry.type))
def analyse_types(self, env):
type_info = self.get_type_info_type(env)
......@@ -10253,12 +10249,7 @@ class TypeidNode(ExprNode):
def generate_result_code(self, code):
if self.is_type:
if self.arg_type.is_extension_type:
# the size of the pointer is boring
# we want the size of the actual struct
arg_code = self.arg_type.declaration_code("", deref=1)
else:
arg_code = self.arg_type.empty_declaration_code()
arg_code = self.arg_type.empty_declaration_code()
else:
arg_code = self.arg_type.result()
translate_cpp_exception(code, self.pos,
......
......@@ -606,17 +606,16 @@ you can declare it using the Python @staticmethod decorator, i.e.::
RTTI and typeid()
=================
Cython has support for the ``typeid(...)`` operator. To use it, you need to import
it and the ``libcpp.typeinfo`` module first:
Cython has support for the ``typeid(...)`` operator.
from cython.operator cimport typeid
from libcpp.typeinfo cimport type_info
The ``typeid(...)`` operator returns an object of the type ``const type_info &``.
If you want to store a type_info value in a C variable, you will need to store it
as a pointer rather than a reference:
from libcpp.typeinfo cimport type_info
cdef const type_info* info = &typeid(MyClass)
If an invalid type is passed to ``typeid``, it will throw an ``std::bad_typeid``
......
......@@ -8,7 +8,6 @@ from cython.operator cimport typeid, dereference as deref
from libc.string cimport const_char
from libcpp cimport bool
cimport libcpp.typeinfo
cdef out(s, result_type=None):
print '%s [%s]' % (s.decode('ascii'), result_type)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment