Exemplo n.º 1
0
static int
tok_stdin_decode(struct tok_state *tok, char **inp)
{
	PyObject *enc, *sysstdin, *decoded, *utf8;
	const char *encoding;
	char *converted;

	if (PySys_GetFile((char *)"stdin", NULL) != stdin)
		return 0;
	sysstdin = PySys_GetObject("stdin");
	if (sysstdin == NULL || !PyFile_Check(sysstdin))
		return 0;

	enc = ((PyFileObject *)sysstdin)->f_encoding;
	if (enc == NULL || !PyString_Check(enc))
		return 0;
	Py_INCREF(enc);

	encoding = PyString_AsString(enc);
	decoded = PyUnicode_Decode(*inp, strlen(*inp), 0, encoding, NULL);
	if (decoded == NULL)
		goto error_clear;

	utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
	Py_DECREF(decoded);
	if (utf8 == NULL)
		goto error_clear;

	assert(PyString_Check(utf8));
	converted = new_string(PyString_AS_STRING(utf8),
			       PyString_GET_SIZE(utf8));
	Py_DECREF(utf8);
	if (converted == NULL)
		goto error_nomem;

	PyMem_FREE(*inp);
	*inp = converted;
	if (tok->encoding != NULL)
		PyMem_FREE(tok->encoding);
	tok->encoding = new_string(encoding, strlen(encoding));
	if (tok->encoding == NULL)
		goto error_nomem;

	Py_DECREF(enc);
	return 0;

error_nomem:
	Py_DECREF(enc);
	tok->done = E_NOMEM;
	return -1;

error_clear:
	/* Fallback to iso-8859-1: for backward compatibility */
	Py_DECREF(enc);
	PyErr_Clear();
	return 0;
}
Exemplo n.º 2
0
static PyObject *builder_parse(PyObject *inputSource, ParseFlags flags,
                               PyObject *entity_factory, int asEntity,
                               PyObject *namespaces, PyObject *rule_handler)
{
  ParserState *state;
  PyObject *result;
  int gc_enabled;
  ExpatStatus status;

#ifdef DEBUG_PARSER
  FILE *stream = PySys_GetFile("stderr", stderr);
  PySys_WriteStderr("builder_parse(source=");
  PyObject_Print(inputSource, stream, 0);
  PySys_WriteStderr(", flags=%d, entity_factory=", flags);
  PyObject_Print(entity_factory, stream, 0);
  PySys_WriteStderr(", asEntity=%d, namespaces=", asEntity);
  PyObject_Print(namespaces, stream, 0);
  PySys_WriteStderr("\n");
#endif
  state = ParserState_New(entity_factory);
  if (state == NULL)
    return NULL;

  state->reader = create_reader(state);
  if (state->reader == NULL) {
    ParserState_Del(state);
    return NULL;
  }

  if (rule_handler) {
    state->rule_matcher = RuleMatchObject_New(rule_handler);
  }

  /* Disable GC (if enabled) while building the DOM tree */
  result = PyObject_Call(gc_isenabled_function, empty_args_tuple, NULL);
  if (result == NULL)
    goto finally;
  gc_enabled = PyObject_IsTrue(result);
  Py_DECREF(result);
  if (gc_enabled) {
    result = PyObject_Call(gc_disable_function, empty_args_tuple, NULL);
    if (result == NULL)
      goto finally;
    Py_DECREF(result);
  }

  Expat_SetValidation(state->reader, flags == PARSE_FLAGS_VALIDATE);
  Expat_SetParamEntityParsing(state->reader, flags != PARSE_FLAGS_STANDALONE);

  if (asEntity)
    status = ExpatReader_ParseEntity(state->reader, inputSource, namespaces);
  else
    status = ExpatReader_Parse(state->reader, inputSource);

  if (gc_enabled) {
    result = PyObject_Call(gc_enable_function, empty_args_tuple, NULL);
    if (result == NULL)
      goto finally;
    Py_DECREF(result);
  }

  /* save off the created document */
  if (status == EXPAT_STATUS_OK)
    result = (PyObject *)state->owner_document;
  else
    result = NULL;

finally:
  ExpatReader_Del(state->reader);
  ParserState_Del(state);
#ifdef DEBUG_PARSER
  PySys_WriteStderr("builder_parse() => ");
  PyObject_Print(result, PySys_GetFile("stderr", stderr), 0);
  PySys_WriteStderr("\n");
#endif
  return result;
}
Exemplo n.º 3
0
int
Command_build(twopence_Command *self, twopence_command_t *cmd)
{
	twopence_buf_t *buffer = NULL;

	twopence_command_init(cmd, self->command);

	cmd->user = self->user;
	cmd->timeout = self->timeout;
	cmd->request_tty = self->useTty;
	cmd->background = self->background;

	twopence_command_ostreams_reset(cmd);
	if (self->quiet || self->stdout == Py_None) {
		/* ostream has already been reset above */
	} else {
		/* Copy remote stdout to our stdout */
		twopence_command_iostream_redirect(cmd, TWOPENCE_STDOUT, 1, false);
	}

	if (!Command_redirect_iostream(cmd, TWOPENCE_STDOUT, self->stdout, &buffer))
		return -1;

	if (self->quiet || self->stderr == Py_None) {
		/* ostream has already been reset above */
	} else {
		/* Copy remote stderr to our stderr */
		twopence_command_iostream_redirect(cmd, TWOPENCE_STDERR, 2, false);
	}

	/* If cmd.stdout and cmd.stderr are both NULL, or both refer to the same
	 * bytearray object, send the remote stdout and stderr to a shared buffer */
	if (buffer && self->stderr == self->stdout) {
		twopence_command_ostream_capture(cmd, TWOPENCE_STDERR, buffer);
	} else
	if (!Command_redirect_iostream(cmd, TWOPENCE_STDERR, self->stderr, NULL)) {
		return -1;
	}

	if (self->stdinPath != NULL) {
		int fd = open(self->stdinPath, O_RDONLY);

		if (fd < 0) {
			PyErr_SetFromErrnoWithFilename(PyExc_IOError, self->stdinPath);
			return -1;
		}
		twopence_command_iostream_redirect(cmd, TWOPENCE_STDIN, fd, true);
	} else
	if (self->stdin) {
		if (!Command_redirect_iostream(cmd, TWOPENCE_STDIN, self->stdin, NULL))
			return -1;
	} else
	if (!self->background) {
		/* Eric wants us to pipe stdin into the command by default */
		FILE *fp;
		int fd;

		fp = PySys_GetFile("stdin", NULL);
		if (fp != NULL) {
			if ((fd = fileno(fp)) < 0) {
				PyErr_SetString(PyExc_SystemError, "cannot connect command to stdin (not a regular file)");
				return -1;
			}
			/* We dup() the file descriptor so that we no longer have to worry
			 * about what python does with its File object */
			twopence_command_iostream_redirect(cmd, TWOPENCE_STDIN, dup(fd), true);
		}
	}

	/* Copy all environment variables */
	if (self->environ.count)
		twopence_env_copy(&cmd->env, &self->environ);

	return 0;
}