@@ -337,29 +337,66 @@ int PyObject_AsFileDescriptor(PyObject* obj) {
337
337
return UPCALL_CEXT_I (_jls_PyObject_AsFileDescriptor , native_to_java (obj ));
338
338
}
339
339
340
- UPCALL_ID (PyTruffle_GetBuiltin );
341
- int PyObject_Print (PyObject * object , FILE * fd , int flags ) {
342
- void * openFunc , * args , * kwargs ;
343
- void * printfunc , * printargs , * printkwargs ;
344
- void * file ;
345
-
346
- openFunc = UPCALL_CEXT_O (_jls_PyTruffle_GetBuiltin , polyglot_from_string ("open" , SRC_CS ));
347
- args = PyTuple_New (1 );
348
- int f = fileno (fd );
349
- PyTuple_SetItem (args , 0 , PyLong_FromLong (f ));
350
- kwargs = PyDict_New ();
351
- int buffering = 1 ;
352
- PyDict_SetItemString (kwargs , "buffering" , PyLong_FromLong (buffering ));
353
- PyDict_SetItemString (kwargs , "mode" , polyglot_from_string ("w" , SRC_CS ));
354
- file = PyObject_Call (openFunc , args , kwargs );
355
-
356
- printfunc = UPCALL_CEXT_O (_jls_PyTruffle_GetBuiltin , polyglot_from_string ("print" , SRC_CS ));
357
- printargs = PyTuple_New (1 );
358
- PyTuple_SetItem (printargs , 0 , object );
359
- printkwargs = PyDict_New ();
360
- PyDict_SetItemString (printkwargs , "file" , file );
361
- PyObject_Call (printfunc , printargs , printkwargs );
362
- return 0 ;
340
+ // Taken from CPython
341
+ int PyObject_Print (PyObject * op , FILE * fp , int flags )
342
+ {
343
+ int ret = 0 ;
344
+ clearerr (fp ); /* Clear any previous error condition */
345
+ if (op == NULL ) {
346
+ Py_BEGIN_ALLOW_THREADS
347
+ fprintf (fp , "< nil > ");
348
+ Py_END_ALLOW_THREADS
349
+ }
350
+ else {
351
+ if (op -> ob_refcnt <= 0 ) {
352
+ /* XXX(twouters) cast refcount to long until %zd is
353
+ universally available */
354
+ Py_BEGIN_ALLOW_THREADS
355
+ fprintf (fp , "<refcnt %ld at %p>" ,
356
+ (long )op -> ob_refcnt , (void * )op );
357
+ Py_END_ALLOW_THREADS
358
+ }
359
+ else {
360
+ PyObject * s ;
361
+ if (flags & Py_PRINT_RAW )
362
+ s = PyObject_Str (op );
363
+ else
364
+ s = PyObject_Repr (op );
365
+ if (s == NULL )
366
+ ret = -1 ;
367
+ else if (PyBytes_Check (s )) {
368
+ fwrite (PyBytes_AS_STRING (s ), 1 ,
369
+ PyBytes_GET_SIZE (s ), fp );
370
+ }
371
+ else if (PyUnicode_Check (s )) {
372
+ PyObject * t ;
373
+ t = PyUnicode_AsEncodedString (s , "utf-8" , "backslashreplace" );
374
+ if (t == NULL ) {
375
+ ret = -1 ;
376
+ }
377
+ else {
378
+ fwrite (PyBytes_AS_STRING (t ), 1 ,
379
+ PyBytes_GET_SIZE (t ), fp );
380
+ Py_DECREF (t );
381
+ }
382
+ }
383
+ else {
384
+ PyErr_Format (PyExc_TypeError ,
385
+ "str() or repr() returned '%.100s'" ,
386
+ s -> ob_type -> tp_name );
387
+ ret = -1 ;
388
+ }
389
+ Py_XDECREF (s );
390
+ }
391
+ }
392
+ if (ret == 0 ) {
393
+ if (ferror (fp )) {
394
+ PyErr_SetFromErrno (PyExc_OSError );
395
+ clearerr (fp );
396
+ ret = -1 ;
397
+ }
398
+ }
399
+ return ret ;
363
400
}
364
401
365
402
// taken from CPython "Objects/object.c"
0 commit comments