The following code is made for to take a python code submission and encode and compare to the expected output in the db. However, when using special characters (‘´’, ‘~’, ‘ç’) it does not convert correctly
what causes an error in the comparison with the expected output.
For example, ‘print(‘não’)’, outputs to n�o, when it should obviously be ‘não’.
Note: the Raw stout data gives the correct hexcode for the input(Raw stdout data: <Buffer 6e e3 6f 0d 0a>).
submissionCodeHadler,js file:
`const { spawn } = require('child_process');
const fs = require('fs').promises
async function invokeSubmission(filePath, inputString, err, success) {
const python = spawn('python', [filePath]);
const inputBuffer = Buffer.from(inputString, 'utf-8');
let stdout = "";
let stderr = "";
python.stdin.write(inputBuffer);
python.stdin.end();
python.stdout.on('data', (data) => {
console.log('Raw stdout data:', data);
stdout += data.toString();
});
python.stderr.on('data', (data) => {
console.log('Raw stderr data:', data);
stderr += data.toString('utf-8');
});
python.on('close', (code) => {
if (code === 0) {
// 0 significa sucesso, então envie stdout
console.log("win", stdout);
success(stdout);
} else {
stderr = stderr.split(',');
console.log("fail", stderr);
err(stderr[1] + stderr[2]);
}
});
}
module.exports = invokeSubmission;`
SubmissionFileHandler.js file:
const fs = require('fs').promises
const config = require('../config')
const BASE_DIR = config.challengesBaseDir
fs.mkdir(BASE_DIR, { recursive: true })
class ChallengesFileHandler {
async create(filename, data) {
const path = `${BASE_DIR}/${filename}`
const fileHandle = await fs.open(path, "w")
fileHandle.writeFile(data)
fileHandle.close()
return path
}
}
module.exports = new ChallengesFileHandler()
To encode correctly.
Donnavan Souza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.