back to listing index

Python: get string representation of PyObject?

[web search]
Original source (stackoverflow.com)
Tags: python c
Clipped on: 2016-06-18

I've got a C python extension, and I would like to print out some diagnostics.

I'm receiving a string as a PyObject*.

What's the canonical way to obtain a string rep of this object, such that it usable as a const char *?

update: clarified to emphasize access as const char *.

asked Mar 18 '11 at 19:15
Image (Asset 2/4) alt=
Mark Harrison
121k87221328
up vote 24 down vote accepted

Use PyObject_Repr (to mimic Python's repr function) or PyObject_Str (to mimic str), and then call PyString_AsString to get char * (you can, and usually should, use it as const char*, for example:

PyObject* objectsRepresentation = PyObject_Repr(yourObject);
const char* s = PyString_AsString(objectsRepresentation);

This method is OK for any PyObject. If you are absolutely sure yourObject is a Python string and not something else, like for instance a number, you can skip the first line and just do:

const char* s = PyString_AsString(yourObject);
answered Nov 21 '11 at 16:39
Image (Asset 3/4) alt=
piokuc
13.7k32657
   upvote
  flag
I am trying PyBytes_AsString(yourObject) for Python 3 and I am getting TypeError: expected bytes, str found – brita_ Feb 6 '15 at 18:08
   upvote
  flag
I didn't even mention PyBytes_AsString in my answer. Have you tried what I suggested in my answer? – piokuc Feb 9 '15 at 15:27
2 upvote
  flag
I tried, in Py3.x PyString got replaced with PyBytes but with not quite the same functionality. I ended up using: PyUnicode_AsUTF8(objectsRepresentation) – brita_ Feb 9 '15 at 21:22
2 upvote
  flag
Don't forget to Py_DECREF(objectsRepresentation) since PyObject_Repr() returns a new reference! – Steve Feb 19 at 23:47

Try PyObject_Repr (to mimic Python's repr) or PyObject_Str (to mimic Python's str).

Docs:

Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression repr(o). Called by the repr() built-in function.

answered Mar 18 '11 at 19:20
Image (Asset 4/4) alt=
Alexander Gessler
32.8k258102
   upvote
  flag
this looks like what I need... Once I've got the PyObject returned by one of these functions, how do I access that in a C-friendly way (eg. to call printf, etc) – Mark Harrison Mar 18 '11 at 19:26
   upvote
  flag
PyBytes_AS_STRING,PyUnicode_AS_STRING, ... – Alexander Gessler Mar 18 '11 at 20:18

Your Answer

asked

5 years ago

viewed

7183 times

active

4 years ago

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.6.16.3680