string to type object (C)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • castironpi

    string to type object (C)

    Hello,

    I'm looking for a one-to-one function from strings to the built-in
    data types in C. I will be keeping the string in a file. I need the
    PyTypeObject* back from it. If nothing else I'll just do a bunch of
    strcmp( "tuple" ) { return &PyTuple_Typ e; } commands, provided
    &PyTuple_Typ e; will be linking the right address.

    The project is for a hobby, no money or official credentials on the
    line. Any help is appreciated.
  • Terry Reedy

    #2
    Re: string to type object (C)



    castironpi wrote:
    Hello,
    >
    I'm looking for a one-to-one function from strings to the built-in
    data types in C. I will be keeping the string in a file. I need the
    PyTypeObject* back from it.
    Use a dict mapping py strings to the object pointers (cast to py ints).

    Comment

    • Miles

      #3
      Re: string to type object (C)

      On Tue, Aug 5, 2008 at 2:30 AM, castironpi <castironpi@gma il.comwrote:
      I'm looking for a one-to-one function from strings to the built-in
      data types in C. I will be keeping the string in a file. I need the
      PyTypeObject* back from it. If nothing else I'll just do a bunch of
      strcmp( "tuple" ) { return &PyTuple_Typ e; } commands, provided
      &PyTuple_Typ e; will be linking the right address.
      Something along the lines of this?
      b = PyImport_Import ("__builtin__") ;
      return PyObject_GetAtt rString(b, typestring);

      -Miles

      Comment

      • castironpi

        #4
        Re: string to type object (C)

        On Aug 5, 7:59 pm, Miles <semantic...@gm ail.comwrote:
        On Tue, Aug 5, 2008 at 2:30 AM, castironpi <castiro...@gma il.comwrote:
        I'm looking for a one-to-one function from strings to the built-in
        data types in C.  I will be keeping the string in a file.  I need the
        PyTypeObject* back from it.  If nothing else I'll just do a bunch of
        strcmp( "tuple" ) { return &PyTuple_Typ e; } commands, provided
        &PyTuple_Typ e; will be linking the right address.
        >
        Something along the lines of this?
        b = PyImport_Import ("__builtin__") ;
        return PyObject_GetAtt rString(b, typestring);
        >
        -Miles
        Miles,

        That's what I was looking for. I wanted to avoid the heavy Import but
        I thought I remembered that imports are free after the first time.

        Import needed a PyString.

        PyObject* s= PyString_FromSt ring( "__builtin_ _" );
        PyObject* b= PyImport_Import (s);
        return PyObject_GetAtt rString(b, "str");

        I expect I need a Py_DECREF on s and b.

        Do you know if uncooperative (not necc'ly malicious) code could
        interfere:
        >>__builtin__.i nt= None
        >>__builtin__.i nt
        >>__builtin__.i nt= type( 0 )
        >>__builtin__.i nt
        <type 'int'>

        ? Or would PyImport_Import (s) create a namespace that had the int
        type in it from scratch?

        Comment

        • Gabriel Genellina

          #5
          Re: string to type object (C)

          En Wed, 06 Aug 2008 01:55:10 -0300, castironpi <castironpi@gma il.com>
          escribi�:
          Do you know if uncooperative (not necc'ly malicious) code could
          interfere:
          >
          >>>__builtin__. int= None
          >>>__builtin__. int
          >>>__builtin__. int= type( 0 )
          >>>__builtin__. int
          <type 'int'>
          >
          ? Or would PyImport_Import (s) create a namespace that had the int
          type in it from scratch?
          Once you modify __builtin__, any later access to builtin names will see
          the changed values.
          I don't know of any way to "reset" the builtin module - other than calling
          again _PyBuiltin_Init from C code.

          --
          Gabriel Genellina

          Comment

          Working...