Last active
November 23, 2020 11:50
-
-
Save pmp-p/47fa5746d4c02c608b9a48f456deac54 to your computer and use it in GitHub Desktop.
Run python tests as typed in REPL, displaying code input + result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define PY_SSIZE_T_CLEAN | |
#include <Python.h> | |
// NEED https://github.com/python/cpython/pull/22190 applied ! | |
#define MAX 1024 | |
char buf[MAX]; | |
#define TEST_CODE "repltest.py" | |
int | |
main(int argc, char *argv[]) | |
{ | |
wchar_t *program = Py_DecodeLocale(argv[0], NULL); | |
if (program == NULL) { | |
fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); | |
exit(1); | |
} | |
Py_SetProgramName(program); /* optional but recommended */ | |
Py_Initialize(); | |
PyRun_SimpleString("import sys;sys.ps1='';sys.ps2=''"); | |
FILE *one_file = NULL; | |
one_file = fopen(TEST_CODE, "r"); | |
int line = 0; | |
int revert = 0 ; | |
if (one_file) { | |
while( fgets(&buf[0], MAX, one_file) ) { | |
int reverse = ftell( one_file ); | |
fprintf( stdout, "%d: %s",++line, buf ); | |
fseek(one_file, revert, SEEK_SET); | |
int res = PyRun_InteractiveOne( one_file, TEST_CODE); | |
revert = ftell(one_file); | |
} | |
fclose(one_file); | |
} else { | |
fprintf( stdout, "IO error\n"); | |
} | |
if (Py_FinalizeEx() < 0) { | |
exit(1); | |
} | |
PyMem_RawFree(program); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment